finetuner.tuner.miner.mining_strategies module

class finetuner.tuner.miner.mining_strategies.TorchStrategicMiningHelper(pos_strategy, neg_strategy)[source]

Bases: object

This helper implements easy-hard mining for tuples or triplets in siamese and triplet respectively. The following strategies are available.

Pos. Strategy: - ‘hard’: Returns hardest positive (furthest) sample per anchor - ‘semihard’: Returns the hardest positive sample per anchor, such

that it is closer than the selected negative

  • ‘easy’: Returns the easiest positive sample per anchor

  • ‘all’: Returns all positive samples

Neg. Strategy: - ‘hard’: Returns hardest negative (closest) sample per anchor - ‘semihard’: Returns the hardest negative sample per anchor, such

that it is further than the selected negative

  • ‘easy’: Returns the easiest negative sample per anchor

  • ‘all’: Returns all negative samples

Not allowed: - pos. and neg. strategy cannot be set to ‘semihard’ simultaneously - When pos. or neg. strategy is set to ‘semihard’ the other cannot be

set to ‘all’

Parameters
  • pos_strategy (str) – Strategy for selecting positive samples

  • neg_strategy (str) – Strategy for selecting negative samples

apply_strategy(match_mat, diff_mat, dist_mat, to_numpy=False)[source]

Wraps the application of mining strategies to update the matrices with positive and negative matches depending, using the distance matrix for filtering

Parameters
  • match_mat (Union[Tensor, ndarray]) – Matrix indicating matches between positive embeddings

  • diff_mat (Union[Tensor, ndarray]) – Matrix indicating matches between negative embeddings

  • dist_mat (Union[Tensor, ndarray]) – Matrix with pair-wise embedding distances

  • to_numpy (bool) – Boolean switch determining whether to return numpy or torch tensors

Return match_mat

Updated matrix of positve matches after applying strategy

Return diff_mat

Updated matrix of negative matches after applying strategy