PyTorch metric learning, decomposed into interchangeable parts
It gives PyTorch developers a modular set of losses, miners, and testers to assemble deep metric learning pipelines without boilerplate.

What it does
pytorch-metric-learning is a PyTorch toolkit for deep metric learning. It bundles nine modules—losses, miners, distances, reducers, regularizers, trainers, testers, accuracy calculators, and dataset loaders—that work independently or snap together into full train/test workflows. You pass model embeddings and labels to a loss function like TripletMarginLoss, optionally filter pairs through a miner such as MultiSimilarityMiner, and let the library handle the triplet and pair conversions internally.
The interesting bit
The library treats loss functions as composable pipelines. You can swap the distance metric (e.g., CosineSimilarity), add an LpRegularizer, or attach a ThresholdReducer without subclassing anything, and the library automatically reconciles pairs and triplets behind the scenes. It also wraps standard tuple losses for self-supervised learning via SelfSupervisedLoss and implements momentum-encoder queues for MoCo-style training through CrossBatchMemory.
Key highlights
- Nine independent modules that combine into full workflows or drop into existing code.
- Automatic conversion between pair-based and triplet-based losses and miners.
- Built-in self-supervised wrappers and cross-batch memory for contrastive pre-training.
- Ready-made dataset helpers for CUB200, Cars196, iNaturalist 2018, and Stanford Online Products.
AccuracyCalculatorandtestersfor evaluating embedding spaces against query and reference splits.
Verdict
Reach for this if you are building retrieval, embedding, or self-supervised vision systems in PyTorch and want to experiment with metric learning components without maintaining your own boilerplate. If you are not working with embeddings or already have a fully custom training stack, it is likely overkill.
Frequently asked
- What is KevinMusgrave/pytorch-metric-learning?
- It gives PyTorch developers a modular set of losses, miners, and testers to assemble deep metric learning pipelines without boilerplate.
- Is pytorch-metric-learning open source?
- Yes — KevinMusgrave/pytorch-metric-learning is open source, released under the MIT license.
- What language is pytorch-metric-learning written in?
- KevinMusgrave/pytorch-metric-learning is primarily written in Python.
- How popular is pytorch-metric-learning?
- KevinMusgrave/pytorch-metric-learning has 6.3k stars on GitHub.
- Where can I find pytorch-metric-learning?
- KevinMusgrave/pytorch-metric-learning is on GitHub at https://github.com/KevinMusgrave/pytorch-metric-learning.