diff --git a/CONTENTS.md b/CONTENTS.md index 6c4bfef3..b0ab5758 100644 --- a/CONTENTS.md +++ b/CONTENTS.md @@ -21,12 +21,14 @@ | [**IntraPairVarianceLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#intrapairvarianceloss) | [Deep Metric Learning with Tuplet Margin Loss](http://openaccess.thecvf.com/content_ICCV_2019/papers/Yu_Deep_Metric_Learning_With_Tuplet_Margin_Loss_ICCV_2019_paper.pdf) | [**LargeMarginSoftmaxLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#largemarginsoftmaxloss) | [Large-Margin Softmax Loss for Convolutional Neural Networks](https://arxiv.org/pdf/1612.02295.pdf) | [**LiftedStructreLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#liftedstructureloss) | [Deep Metric Learning via Lifted Structured Feature Embedding](https://arxiv.org/pdf/1511.06452.pdf) +| [**ManifoldLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#manifoldloss) | [Ensemble Deep Manifold Similarity Learning using Hard Proxies](https://openaccess.thecvf.com/content_CVPR_2019/papers/Aziere_Ensemble_Deep_Manifold_Similarity_Learning_Using_Hard_Proxies_CVPR_2019_paper.pdf) | [**MarginLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#marginloss) | [Sampling Matters in Deep Embedding Learning](https://arxiv.org/pdf/1706.07567.pdf) | [**MultiSimilarityLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#multisimilarityloss) | [Multi-Similarity Loss with General Pair Weighting for Deep Metric Learning](http://openaccess.thecvf.com/content_CVPR_2019/papers/Wang_Multi-Similarity_Loss_With_General_Pair_Weighting_for_Deep_Metric_Learning_CVPR_2019_paper.pdf) | [**NCALoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#ncaloss) | [Neighbourhood Components Analysis](https://www.cs.toronto.edu/~hinton/absps/nca.pdf) | [**NormalizedSoftmaxLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#normalizedsoftmaxloss) | - [NormFace: L2 Hypersphere Embedding for Face Verification](https://arxiv.org/pdf/1704.06369.pdf)
- [Classification is a Strong Baseline for DeepMetric Learning](https://arxiv.org/pdf/1811.12649.pdf) | [**NPairsLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#npairsloss) | [Improved Deep Metric Learning with Multi-class N-pair Loss Objective](http://www.nec-labs.com/uploads/images/Department-Images/MediaAnalytics/papers/nips16_npairmetriclearning.pdf) | [**NTXentLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#ntxentloss) | - [Representation Learning with Contrastive Predictive Coding](https://arxiv.org/pdf/1807.03748.pdf)
- [Momentum Contrast for Unsupervised Visual Representation Learning](https://arxiv.org/pdf/1911.05722.pdf)
- [A Simple Framework for Contrastive Learning of Visual Representations](https://arxiv.org/abs/2002.05709) +| [**P2SGradLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#p2sgradloss) | [P2SGrad: Refined Gradients for Optimizing Deep Face Models](https://arxiv.org/abs/1905.02479) | [**PNPLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#pnploss) | [Rethinking the Optimization of Average Precision: Only Penalizing Negative Instances before Positive Ones is Enough](https://arxiv.org/pdf/2102.04640.pdf) | [**ProxyAnchorLoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#proxyanchorloss) | [Proxy Anchor Loss for Deep Metric Learning](https://arxiv.org/pdf/2003.13911.pdf) | [**ProxyNCALoss**](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#proxyncaloss) | [No Fuss Distance Metric Learning using Proxies](https://arxiv.org/pdf/1703.07464.pdf) diff --git a/README.md b/README.md index 26d568ae..f8eb0c7b 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,15 @@ ## News +**June 18**: v2.2.0 +- Added [ManifoldLoss](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#manifoldloss) and [P2SGradLoss](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#p2sgradloss). +- Added a `symmetric` flag to [SelfSupervisedLoss](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#selfsupervisedloss). +- See the [release notes](https://github.com/KevinMusgrave/pytorch-metric-learning/releases/tag/v2.2.0). +- Thank you [domenicoMuscill0](https://github.com/domenicoMuscill0). + **April 5**: v2.1.0 - Added [PNPLoss](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#pnploss) -- Thanks to contributor [interestingzhuo](https://github.com/interestingzhuo). - -**January 29**: v2.0.0 -- Added SelfSupervisedLoss, plus various API improvements. See the [release notes](https://github.com/KevinMusgrave/pytorch-metric-learning/releases/tag/v2.0.0). -- Thanks to contributor [cwkeam](https://github.com/cwkeam). +- Thanks you [interestingzhuo](https://github.com/interestingzhuo). ## Documentation @@ -225,6 +227,7 @@ Thanks to the contributors who made pull requests! | Contributor | Highlights | | -- | -- | +|[domenicoMuscill0](https://github.com/domenicoMuscill0)| - [ManifoldLoss](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#manifoldloss)
- [P2SGradLoss](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#p2sgradloss) |[mlopezantequera](https://github.com/mlopezantequera) | - Made the [testers](https://kevinmusgrave.github.io/pytorch-metric-learning/testers) work on any combination of query and reference sets
- Made [AccuracyCalculator](https://kevinmusgrave.github.io/pytorch-metric-learning/accuracy_calculation/) work with arbitrary label comparisons | |[cwkeam](https://github.com/cwkeam) | - [SelfSupervisedLoss](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#selfsupervisedloss)
- [VICRegLoss](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#vicregloss)
- Added mean reciprocal rank accuracy to [AccuracyCalculator](https://kevinmusgrave.github.io/pytorch-metric-learning/accuracy_calculation/)
- BaseLossWrapper| |[marijnl](https://github.com/marijnl)| - [BatchEasyHardMiner](https://kevinmusgrave.github.io/pytorch-metric-learning/miners/#batcheasyhardminer)
- [TwoStreamMetricLoss](https://kevinmusgrave.github.io/pytorch-metric-learning/trainers/#twostreammetricloss)
- [GlobalTwoStreamEmbeddingSpaceTester](https://kevinmusgrave.github.io/pytorch-metric-learning/testers/#globaltwostreamembeddingspacetester)
- [Example using trainers.TwoStreamMetricLoss](https://github.com/KevinMusgrave/pytorch-metric-learning/blob/master/examples/notebooks/TwoStreamMetricLoss.ipynb) | @@ -273,6 +276,7 @@ This library contains code that has been adapted and modified from the following - https://github.com/ronekko/deep_metric_learning - https://github.com/tjddus9597/Proxy-Anchor-CVPR2020 - http://kaizhao.net/regularface +- https://github.com/nii-yamagishilab/project-NN-Pytorch-scripts ### Logo Thanks to [Jeff Musgrave](https://www.designgenius.ca/) for designing the logo. diff --git a/docs/losses.md b/docs/losses.md index b0719258..2af1f905 100644 --- a/docs/losses.md +++ b/docs/losses.md @@ -545,6 +545,57 @@ losses.LiftedStructureLoss(neg_margin=1, pos_margin=0, **kwargs): * **loss**: The loss per positive pair in the batch. Reduction type is ```"pos_pair"```. +## ManifoldLoss + +[Ensemble Deep Manifold Similarity Learning using Hard Proxies](https://openaccess.thecvf.com/content_CVPR_2019/papers/Aziere_Ensemble_Deep_Manifold_Similarity_Learning_Using_Hard_Proxies_CVPR_2019_paper.pdf) + +```python +losses.ManifoldLoss( + l: int, + K: int = 50, + lambdaC: float = 1.0, + alpha: float = 0.8, + margin: float = 5e-4, + **kwargs + ) +``` + +**Parameters** + +- **l**: embedding size. + +- **K**: number of proxies. + +- **lambdaC**: regularization weight. Used in the formula `loss = intrinsic_loss + lambdaC*context_loss`. + If `lambdaC=0`, then it uses only the intrinsic loss. If `lambdaC=np.inf`, then it uses only the context loss. + +- **alpha**: parameter of the Random Walk. Must be in the range `(0,1)`. It specifies the amount of similarity between neighboring nodes. + +- **margin**: margin used in the calculation of the loss. + + +Example usage: +```python +loss_fn = ManifoldLoss(128) + +# use random cluster centers +loss = loss_fn(embeddings) +# or specify indices of embeddings to use as cluster centers +loss = loss_fn(embeddings, indices_tuple=indices) +``` + +**Important notes** + +`labels`, `ref_emb`, and `ref_labels` are not supported for this loss function. + +In addition, `indices_tuple` is **not** for the output of miners. Instead, it is for a list of indices of embeddings to be used as cluster centers. + + +**Default reducer**: + + - This loss returns an **already reduced** loss. + + ## MarginLoss [Sampling Matters in Deep Embedding Learning](https://arxiv.org/pdf/1706.07567.pdf){target=_blank} ```python @@ -761,6 +812,37 @@ losses.NTXentLoss(temperature=0.07, **kwargs) * **loss**: The loss per positive pair in the batch. Reduction type is ```"pos_pair"```. + +## P2SGradLoss +[P2SGrad: Refined Gradients for Optimizing Deep Face Models](https://arxiv.org/abs/1905.02479) +```python +losses.P2SGradLoss(descriptors_dim, num_classes, **kwargs) +``` + +**Parameters** + +- **descriptors_dim**: The embedding size. + +- **num_classes**: The number of classes in your training dataset. + + +Example usage: +```python +loss_fn = P2SGradLoss(128, 10) +loss = loss_fn(embeddings, labels) +``` + +**Important notes** + +`indices_tuple`, `ref_emb`, and `ref_labels` are not supported for this loss function. + + +**Default reducer**: + + - This loss returns an **already reduced** loss. + + + ## PNPLoss [Rethinking the Optimization of Average Precision: Only Penalizing Negative Instances before Positive Ones is Enough](https://arxiv.org/pdf/2102.04640.pdf){target=_blank} ```python @@ -849,14 +931,31 @@ loss_optimizer.step() ## SelfSupervisedLoss -A common use case is to have `embeddings` and `ref_emb` be augmented versions of each other. For most losses, you have to create labels to indicate which `embeddings` correspond with which `ref_emb`. `SelfSupervisedLoss` automates this. +A common use case is to have `embeddings` and `ref_emb` be augmented versions of each other. For most losses, you have to create labels to indicate which `embeddings` correspond with which `ref_emb`. + +`SelfSupervisedLoss` is a wrapper that takes care of this by creating labels internally. It assumes that: + +- `ref_emb[i]` is an augmented version of `embeddings[i]`. +- `ref_emb[i]` is the only augmented version of `embeddings[i]` in the batch. ```python +losses.SelfSupervisedLoss(loss, symmetric=True, **kwargs) +``` + +**Parameters**: + +* **loss**: The loss function to be wrapped. +* **symmetric**: If `True`, then the embeddings in both `embeddings` and `ref_emb` are used as anchors. If `False`, then only the embeddings in `embeddings` are used as anchors. + +Example usage: + +``` loss_fn = losses.TripletMarginLoss() loss_fn = SelfSupervisedLoss(loss_fn) loss = loss_fn(embeddings, ref_emb) ``` + ??? "Supported Loss Functions" - [AngularLoss](losses.md#angularloss) - [CircleLoss](losses.md#circleloss) diff --git a/src/pytorch_metric_learning/utils/accuracy_calculator.py b/src/pytorch_metric_learning/utils/accuracy_calculator.py index 77c792ff..8356deb7 100644 --- a/src/pytorch_metric_learning/utils/accuracy_calculator.py +++ b/src/pytorch_metric_learning/utils/accuracy_calculator.py @@ -440,7 +440,7 @@ def get_accuracy( ): raise ValueError( "When ref_includes_query is True, the first len(query) elements of reference must be equal to query.\n" - "Likewise, the first len(query_labels) elements of reference_lbels must be equal to query_labels.\n" + "Likewise, the first len(query_labels) elements of reference_labels must be equal to query_labels.\n" ) self.curr_function_dict = self.get_function_dict(include, exclude)