Pytorch-metric-learning

Latest version: v2.5.0

Safety actively analyzes 630450 Python packages for vulnerabilities to keep your Python projects secure.

Scan your dependencies

Page 7 of 9

0.9.94

Various bug fixes and improvements

- A list or dictionary of miners can be passed into MultipleLosses. 212
- Fixed bug where MultipleLosses failed in list mode. 213
- Fixed bug where IntraPairVarianceLoss and MarginLoss were overriding sub_loss_names instead of _sub_loss_names. This likely caused embedding regularizers to have no effect for these two losses. 215
- ModuleWithRecordsAndReducer now creates copies of the input reducer when necessary. 216
- Moved cos.clone() inside torch.no_grad() in RegularFaceRegularizer. Should be more efficient? 219
- In utils.inference, moved faiss import inside of FaissIndexer since that is the only class that requires it. 222
- Added a copy_weights init argument to LogitGetter, to make copying optional 223

0.9.93

Small update

- Optimized get_random_triplet_indices, so if you were using DistanceWeightedMiner, or if you ever set the triplets_per_anchor argument to something other than "all" anywhere in your code, it should run a lot faster now. Thanks AlexSchuy

0.9.92

New Features

DistributedLossWrapper and DistributedMinerWrapper
Added [DistributedLossWrapper and DistributedMinerWrapper](https://kevinmusgrave.github.io/pytorch-metric-learning/distributed). Wrap a loss or miner with these when using PyTorch's DistributedDataParallel (i.e. multiprocessing). Most of the code is by JohnGiorgi (https://github.com/JohnGiorgi/DeCLUTR).

python
from pytorch_metric_learning import losses, miners
from pytorch_metric_learning.utils import distributed as pml_dist
loss_func = pml_dist.DistributedLossWrapper(loss = losses.ContrastiveLoss())
miner = pml_dist.DistributedMinerWrapper(miner = miners.MultiSimilarityMiner())


For a working example, see the ["Multiprocessing with DistributedDataParallel"](https://github.com/KevinMusgrave/pytorch-metric-learning/tree/master/examples#simple-examples) notebook.

Added enqueue_idx to CrossBatchMemory
Now you can make CrossBatchMemory work with [MoCo](https://arxiv.org/pdf/1911.05722.pdf). This adds a great deal of flexibility to the MoCo framework, because you can use any tuple loss and tuple miner in CrossBatchMemory.

Previously this wasn't possible because all embeddings passed into CrossBatchMemory would go into the memory queue. In contrast, MoCo only queues the momentum encoder's embeddings.

The new enqueue_idx argument lets you do this, by specifying which embeddings should be added to memory. Here's a modified snippet from the [MoCo on CIFAR10](https://github.com/KevinMusgrave/pytorch-metric-learning/tree/master/examples#simple-examples) notebook:
python
from pytorch_metric_learning.losses import CrossBatchMemory, NTXentLoss

loss_fn = CrossBatchMemory(loss = NTXentLoss(), embedding_size = 64, memory_size = 16384)

snippet from the training loop
for images, _ in train_loader:
...
previous_max_label = torch.max(loss_fn.label_memory)
num_pos_pairs = encQ_out.size(0)
labels = torch.arange(0, num_pos_pairs)
labels = torch.cat((labels , labels)).to(device)

add an offset so that the labels do not overlap with any labels in the memory queue
labels += previous_max_label + 1

we want to enqueue the output of encK, which is the 2nd half of the batch
enqueue_idx = torch.arange(num_pos_pairs, num_pos_pairs*2)

all_enc = torch.cat([encQ_out, encK_out], dim=0)

now only encK_out will be added to the memory queue
loss = loss_fn(all_enc, labels, enqueue_idx = enqueue_idx)
...


Check out the [MoCo on CIFAR10](https://github.com/KevinMusgrave/pytorch-metric-learning/tree/master/examples#simple-examples) notebook to see the entire script.


TuplesToWeightsSampler
This is a [simple offline miner](https://kevinmusgrave.github.io/pytorch-metric-learning/samplers/#tuplestoweightssampler). It does the following:
1. Take a random subset of your dataset, if you provide subset_size
2. Use a specified miner to mine tuples from the subset dataset.
3. Compute weights based on how often an element appears in the mined tuples.
4. Randomly sample, using the weights as probabilities.

python
from pytorch_metric_learning.samplers import TuplesToWeightsSampler
from pytorch_metric_learning.miners import MultiSimilarityMiner

miner = MultiSimilarityMiner(epsilon=-0.2)
sampler = TuplesToWeightsSampler(model, miner, dataset, subset_size = 5000)
then pass the sampler into your Dataloader


LogitGetter
Added [utils.inference.LogitGetter](https://kevinmusgrave.github.io/pytorch-metric-learning/inference_models/#logitgetter) to make it easier to compute logits of classifier loss functions.
python
from pytorch_metric_learning.losses import ArcFaceLoss
from pytorch_metric_learning.utils.inference import LogitGetter

loss_fn = ArcFaceLoss(num_classes = 100, embedding_size = 512)
LG = LogitGetter(loss_fn)
logits = LG(embeddings)


Other
- Added optional batch_size argument to MPerClassSampler. If you pass in this argument, then each batch is guaranteed to have m samples per class. Otherwise, most batches will have m samples per class, but it's not guaranteed for every batch. Note there restrictions on the values of m and batch_size. For example, batch_size must be a multiple of m. For all the restrictions, see [the documentation](https://kevinmusgrave.github.io/pytorch-metric-learning/samplers/#mperclasssampler).

- Added trainable_attributes to BaseTrainer and to standardize the set_to_train and set_to_eval functions.

- Added save_models init argument to HookContainer. If set to False then models will not be saved.

- Added losses_sizes as a stat for BaseReducer

- Added a type check and conversion in common_functions.labels_to_indices to go from torch tensor to numpy

0.9.91

Bug Fixes and Improvements
- Fixed CircleLoss bug, by improving the logsumexp keep_mask implementation. See https://github.com/KevinMusgrave/pytorch-metric-learning/issues/173
- Fixed convert_to_weights bug, which caused a runtime error when an empty indices_tuple was passed in. See https://github.com/KevinMusgrave/pytorch-metric-learning/issues/174
- ProxyAnchorLoss now adds miner weights to the exponents which are fed to logsumexp. This is equivalent to scaling each loss component by e^(miner_weight). The previous behavior was to scale each loss component by just miner_weight.

Other updates
- Added an [example notebook](https://github.com/KevinMusgrave/pytorch-metric-learning/tree/master/examples#simple-examples) which shows how to use a customized loss + miner in a simple training loop.
- A new [arxiv paper](https://arxiv.org/abs/2008.09164) and an improved Readme give a better high level explanation of the library.
- Added better explanations for the [testers](https://kevinmusgrave.github.io/pytorch-metric-learning/testers/) and the [default accuracy metrics](https://kevinmusgrave.github.io/pytorch-metric-learning/accuracy_calculation/#explanations-of-the-default-accuracy-metrics)

0.9.90

********** Summary **********
The main update is the new distances module, which adds an extra level of modularity to loss functions. It is a pretty big design change, which is why so many arguments have become obsolete. See [the documentation](https://kevinmusgrave.github.io/pytorch-metric-learning/distances/) for a description of the new module.

Other updates include support for half-precision, new regularizers and mixins, improved documentation, and default values for most initialization parameters.


********** Breaking Changes **********

Dependencies
This library now requires PyTorch >= 1.6.0. Previously there was no explicit version requirement.

Losses and Miners
All loss functions
normalize_embeddings has been removed
- If you never used this argument, nothing needs to be done.
- normalize_embeddings = True: just remove the argument.
- normalize_embeddings = False: remove the argument and instead pass it into a distance object. For example:
python
from pytorch_metric_learning.distances import LpDistance
loss_func = TripletMarginLoss(distance=LpDistance(normalize_embeddings=False))


ContrastiveLoss, GenericPairLoss, BatchHardMiner, HDCMiner, PairMarginMiner
use_similarity has been removed
- If you never used this argument, nothing needs to be done.
- use_similarity = True: remove the argument and:
python
if you had set normalize_embeddings = False
from pytorch_metric_learning.distances import DotProductSimilarity
loss_func = ContrastiveLoss(distance=DotProductSimilarity(normalize_embeddings=False))

otherwise
from pytorch_metric_learning.distances import CosineSimilarity
loss_func = ContrastiveLoss(distance=CosineSimilarity())


squared_distances has been removed
- If you never used this argument, nothing needs to be done.
- squared_distances = True: remove the argument and instead pass power=2 into a distance object. For example:
python
from pytorch_metric_learning.distances import LpDistance
loss_func = ContrastiveLoss(distance=LpDistance(power=2))

- squared_distances = False: just remove the argument.

ContrastiveLoss, TripletMarginLoss
power has been removed
- If you never used this argument, nothing needs to be done.
- power = 1: just remove the argument
- power = X, where X != 1: remove the argument and instead pass it into a distance object. For example:
python
from pytorch_metric_learning.distances import LpDistance
loss_func = TripletMarginLoss(distance=LpDistance(power=2))


TripletMarginLoss
distance_norm has been removed
- If you never used this argument, nothing needs to be done.
- distance_norm = 2: just remove the argument
- distance_norm = X, where X != 2: remove the argument and instead pass it as p into a distance object. For example:
python
from pytorch_metric_learning.distances import LpDistance
loss_func = TripletMarginLoss(distance=LpDistance(p=1))


NPairsLoss
l2_reg_weight has been removed
- If you never used this argument, nothing needs to be done.
- l2_reg_weight = 0: just remove the argument
- l2_reg_weight = X, where X > 0: remove the argument and instead pass in an LpRegularizer and weight:
python
from pytorch_metric_learning.regularizers import LpRegularizer
loss_func = NPairsLoss(embedding_regularizer=LpRegularizer(), embedding_reg_weight=0.123)


SignalToNoiseRatioContrastiveLoss
regularizer_weight has been removed
- If you never used this argument, nothing needs to be done.
- regularizer_weight = 0: just remove the argument
- regularizer_weight = X, where X > 0: remove the argument and instead pass in a ZeroMeanRegularizer and weight:
python
from pytorch_metric_learning.regularizers import LpRegularizer
loss_func = SignalToNoiseRatioContrastiveLoss(embedding_regularizer=ZeroMeanRegularizer(), embedding_reg_weight=0.123)


SoftTripleLoss
reg_weight has been removed
- If you never used this argument, do the following to obtain the same default behavior:
python
from pytorch_metric_learning.regularizers import SparseCentersRegularizer
weight_regularizer = SparseCentersRegularizer(num_classes, centers_per_class)
SoftTripleLoss(..., weight_regularizer=weight_regularizer, weight_reg_weight=0.2)

- reg_weight = X: remove the argument, and use the SparseCenterRegularizer as shown above.

WeightRegularizerMixin and all classification loss functions
- If you never specified regularizer or reg_weight, nothing needs to be done.
- regularizer = X: replace with weight_regularizer = X
- reg_weight = X: replace with weight_reg_weight = X

Classification losses
- For all losses and miners, default values have been set for as many arguments as possible. This has caused a change in ordering in positional arguments for several of the classification losses. The typical form is now:
python
loss_func = SomeClassificatinLoss(num_classes, embedding_loss, <keyword arguments>)

See the documentation for specifics

Reducers
ThresholdReducer
threshold has been replaced by low and high
- Replace threshold = X with low = X

Regularizers
All regularizers
normalize_weights has been removed
- If you never used this argument, nothing needs to be done.
- normalize_weights = True: just remove the argument.
- normalize_weights = False: remove the argument and instead pass normalize_embeddings = False into a distance object. For example:
python
from pytorch_metric_learning.distances import DotProductSimilarity
loss_func = RegularFaceRegularizer(distance=DotProductSimilarity(normalize_embeddings=False))


Inference

MatchFinder
mode has been removed
- Replace mode="sim" with either distance=CosineSimilarity() or distance=DotProductSimilarity()
- Replace mode="dist" with distance=LpDistance()
- Replace mode="squared_dist" with distance=LpDistance(power=2)



********** New Features **********
Distances
Distances bring an additional level of modularity to building loss functions. Here's an example of how they work.

Consider the TripletMarginLoss in its default form:
python
from pytorch_metric_learning.losses import TripletMarginLoss
loss_func = TripletMarginLoss(margin=0.2)

This loss function attempts to minimize [d<sub>ap</sub> - d<sub>an</sub> + margin]<sub>+</sub>.

In other words, it tries to make the anchor-positive distances (d<sub>ap</sub>) smaller than the anchor-negative distances (d<sub>an</sub>).

Typically, d<sub>ap</sub> and d<sub>an</sub> represent Euclidean or L2 distances. But what if we want to use a squared L2 distance, or an unnormalized L1 distance, or completely different distance measure like signal-to-noise ratio? With the distances module, you can try out these ideas easily:
python
TripletMarginLoss with squared L2 distance
from pytorch_metric_learning.distances import LpDistance
loss_func = TripletMarginLoss(margin=0.2, distance=LpDistance(power=2))

TripletMarginLoss with unnormalized L1 distance
loss_func = TripletMarginLoss(margin=0.2, distance=LpDistance(normalize_embeddings=False, p=1))

TripletMarginLoss with signal-to-noise ratio
from pytorch_metric_learning.distances import SNRDistance
loss_func = TripletMarginLoss(margin=0.2, distance=SNRDistance())


You can also use similarity measures rather than distances, and the loss function will make the necessary adjustments:
python
TripletMarginLoss with cosine similarity
from pytorch_metric_learning.distances import CosineSimilarity
loss_func = TripletMarginLoss(margin=0.2, distance=CosineSimilarity())

With a similarity measure, the TripletMarginLoss internally swaps the anchor-positive and anchor-negative terms: [s<sub>an</sub> - s<sub>ap</sub> + margin]<sub>+</sub>. In other words, it will try to make the anchor-negative similarities smaller than the anchor-positive similarities.

All **losses, miners, and regularizers** accept a distance argument. So you can try out the MultiSimilarityMiner using SNRDistance, or the NTXentLoss using LpDistance(p=1) and so on. Note that some losses/miners/regularizers have restrictions on the type of distances they can accept. For example, some classification losses only allow CosineSimilarity or DotProductSimilarity as their distance measure between embeddings and weights. To view restrictions for specific loss functions, see [the documentation](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/)

There are four distances implemented (LpDistance, SNRDistance, CosineSimilarity, DotProductSimilarity), but of course you can extend the BaseDistance class and write a custom distance measure if you want. See [the documentation](https://kevinmusgrave.github.io/pytorch-metric-learning/distances/) for more.

EmbeddingRegularizerMixin
All loss functions now extend EmbeddingRegularizerMixin, which means you can optionally pass in (to any loss function) an embedding regularizer and its weight. The embedding regularizer will compute some loss based on the embeddings alone, ignoring labels and tuples. For example:
python
from pytorch_metric_learning.regularizers import LpRegularizer
loss_func = MultiSimilarityLoss(embedding_regularizer=LpRegularizer(), embedding_reg_weight=0.123)


WeightRegularizerMixin is now a subclass of WeightMixin
As in previous versions, classification losses extend WeightRegularizerMixin, which which means you can optionally pass in a weight matrix regularizer. Now that WeightRegularizerMixin extends WeightMixin, you can also specify the weight initialization function [in object form](https://kevinmusgrave.github.io/pytorch-metric-learning/common_functions/#torchinitwrapper):
python
from ..utils import common_functions as c_f
import torch

use kaiming_uniform, with a=1 and mode='fan_out'
weight_init_func = c_f.TorchInitWrapper(torch.nn.kaiming_uniform_, a=1, mode='fan_out')
loss_func = SomeClassificationLoss(..., weight_init_func=weight_init_func)


New Regularizers
For increased modularity, the regularizers hard-coded in several loss functions were separated into their own classes. The new regularizers are:
- LpRegularizer
- SparseCentersRegularizer
- ZeroMeanRegularizer

Support for half-precision
In previous versions, various functions would break in half-precision (float16) mode. Now all distances, losses, miners, regularizers, and reducers work with half-precision, float32, and double (float64).

New collect_stats argument
All distances, losses, miners, regularizers, and reducers now have a collect_stats argument, which is True by default. This means that various statistics are collected in each forward pass, and these statistics can be useful to look at during experiments. However, if you don't care about collecting stats, you can set collect_stats=False, and the stat computations will be skipped.

Other updates

- You no longer have to explicitly call .to(device) on classification losses, because their weight matrices will be moved to the correct device during the forward pass if necessary. See issue https://github.com/KevinMusgrave/pytorch-metric-learning/issues/139

- Reasonable default values have been set for all losses and miners, to make these classes easier to try out. In addition, equations have been added to many of the class descriptions in the documentation. See issue https://github.com/KevinMusgrave/pytorch-metric-learning/issues/140

- Calls to torch.nonzero have been replaced by torch.where.

- The documentation for ArcFaceLoss and CosFaceLoss have been fixed to reflect the actual usage. (The documentation previously indicated that some arguments are positional, when they are actually keyword arguments.)

- The tensorboard_folder argument for utils.logging_presets.get_record_keeper is now optional. If you don't specify it, then there will be no tensorboard logs, which can be useful if speed is a concern.

- The loss dictionary in BaseTrainer is now cleared at the end of each epoch, to free up GPU memory. See issue https://github.com/KevinMusgrave/pytorch-metric-learning/issues/171

0.9.89

CrossBatchMemory
- Fixed bug where CrossBatchMemory would use self-comparisons as positive pairs. This was uniquely a CrossBatchMemory problem because of the nature of adding each current batch to the queue.
- Fixed bug where DistanceWeightedMiner would not work with CrossBatchMemory due to missing ref_label
- Changed 3rd keyword argument of forward() from input_indices_tuple to indices_tuple to be consistent with all other losses.

AccuracyCalculator
- Fixed bug in AccuracyCalculator where it would return NaN if the reference set contained none of query set labels. Now it will log a warning and return 0.

BaseTester
- Fixed bug where "compared_to_training_set" mode of BaseTester fails due to list(None) bug.

InferenceModel
- New get_nearest_neighbors function will return nearest neighbors of a query. By btseytlin

Loss and miner utils
- Switched to fill_diagonal_ in the get_all_pairs_indices and get_all_triplets_indices code, instead of creating torch.eye.

Page 7 of 9

© 2024 Safety CLI Cybersecurity Inc. All Rights Reserved.