Open Metric Learning (OML)
repository·main·Indexed 21 days ago
https://github.com/oml-team/open-metric-learningA PyTorch-based framework for training and validating models that produce high-quality embeddings for metric learning tasks. OML provides modular pipelines for feature extraction, validation, and inference, supporting contrastive losses (e.g., TripletLoss) and classification losses (e.g., ArcFace). It includes tools for preparing benchmark datasets such as CARS 196, CUB 200 2011, InShop, and SOP, as well as a postprocessing pipeline for Siamese Transformer for Image Retrieval (STIR).
What's inside Open Metric Learning
- OML (Open Metric Learning) is a PyTorch-based framework designed for training and validating models that produce high-quality embeddings. It provides tools for metric learning tasks, enabling developers to build models capable of learning meaningful representations in a shared embedding space.
What is Open Metric Learning (OML)?
mainOpen Metric Learning (OML) is a Python-based framework designed for training and validating deep learning models that produce high-quality embeddings for metric learning tasks (also known as extreme classification).
Unlike vanilla classifiers, OML is optimized for scenarios where you have many entities (thousands of IDs) but few samples per entity, and where the goal is often retrieval or searching (e.g., Face Recognition, Re-Identification, or Product Search).
Key differentiators from other libraries like PyTorch Metric Learning (PML) include:
- Pipelines: Config-based training workflows that handle data preparation and model training end-to-end.
- Zoo: A collection of pretrained models accessible similarly to
torchvision. - PyTorch Lightning Integration: Built-in support for high-performance training loops and Distributed Data Parallel (DDP).
- Recipe-Oriented: Focuses on practical, real-world benchmarks and hyperparameter combinations rather than just providing a collection of low-level tools.
Use postprocessing pipelines for retrieval re-ranking
mainPostprocessing pipelines in OML allow you to train and validate auxiliary models designed to improve the retrieval performance of a primary feature extractor model. This process is commonly referred to as retrieval re-ranking.What is STIR (Siamese Transformer for Image Retrieval)?
mainSTIR is a pairwise postprocessing (re-ranking) approach designed for image retrieval. Unlike traditional re-ranking transformers that rely on global or local feature extraction, STIR uses an attention mechanism to directly compare a query image and a retrieved candidate at the pixel level in a single forward pass. This method is designed to re-rank the top outputs of a baseline model (typically trained with triplet loss and hard negative mining) to achieve state-of-the-art performance on datasets like Stanford Online Products and DeepFashion In-shop.What is logged during OML Pipeline execution
mainWhen using integrated loggers in Pipelines, the following information is automatically captured:
- Metrics: Performance metrics defined in your config via
metric_args(e.g.,CMC@1,Precision@5,MAP@5).- Tip: Set
metrics_args.return_only_overall_category: Falseto log metrics independently for each category in multi-category datasets.
- Tip: Set
- Loss Values: Averaged over batches and epochs. Some built-in OML losses (like
TripletLossWithMargin) log additional statistics such as positive/negative distances and the fraction of active triplets. - Visualizations: Error analysis visualizations (e.g., showing queries, closest irrelevant items, and ground truths).
- Reproducibility Artifacts: Source code, configuration files, dataframes, and tags.
- Metrics: Performance metrics defined in your config via
Understand Feature Extraction Pipelines
mainFeature extraction pipelines in OML are designed to train, validate, and perform inference on models that represent images as feature vectors. There are three primary pipeline types:
extractor_training_pipeline: Handles both training and validation.extractor_validation_pipeline: Performs validation only.extractor_prediction_pipeline: Runs inference on a trained model and saves the extracted features to disk.
These pipelines can be used via configuration files or as plain Python implementations.
Understand the core concepts and terminology of OML
mainTo use OML effectively, it is important to understand its specific naming conventions and the metric learning problem it solves:
Metric Learning Problem
Metric learning (or extreme classification) involves training models where you have thousands of unique IDs (entities) but only a few samples per ID. The goal is to produce embeddings that allow for efficient searching or matching, often involving unseen entities at test time.
Glossary
- embedding: The model's output (also called
features vectorordescriptor). - query: A sample used as a request in a retrieval procedure.
- gallery set: The set of entities to search through (also called
referenceorindex). - Sampler: A
DataLoaderargument used to form batches. - Miner: An object that forms pairs or triplets after the
Samplerhas formed a batch. Miners may use a memory bank to find combinations outside the current batch. - Samples/Labels/Instances: In a dataset,
labelsare the unique IDs (e.g., fashion item IDs), andinstancesorsamplesare the individual photos/data points for those labels. - categories: Groups of labels (e.g., "jackets").
- training epoch: In OML, an epoch typically refers to observing all available labels rather than all available samples, because batch samplers for combination-based losses are often defined by the number of labels.
- embedding: The model's output (also called
Access pretrained models from the OML Zoo
mainOML provides a Zoo of pretrained models. These can be accessed in your code in a manner similar totorchvision. This is useful for both direct use and as a starting point for further training (e.g., initializing a model with a self-supervised checkpoint instead of standard ImageNet weights).How Pipelines are structured
mainEvery OML Pipeline is composed of three fundamental building blocks:
- Config file (
.yaml): Describes the entire experiment run, including model parameters, device settings, and component selections. - Registry of classes & functions: A mapping system that connects configuration keys to Python constructors or functions.
- Entrypoint function & script: The logic that orchestrates the pipeline execution, typically invoked via a script using Hydra for configuration management.
Because Pipelines use Hydra, you can override configuration values directly from the command line (e.g.,
python script.py model.args.weights=null).python validate.py model.args.weights=null- Config file (
Boost retrieval accuracy using a pairwise model as a re-ranker
mainYou can improve the accuracy of vector search results by using a Siamese (pairwise) model as a re-ranker. This process involves taking the top-$N$ outputs from an initial retrieval model and performing inference on pairs consisting of the query and each retrieved output(query_i, output_j)wherej=1..top_n. The pairwise model then re-ranks these outputs based on their similarity scores.Use bounding boxes and audio offsets in datasets
mainOML supports additional metadata in the CSV format for specialized tasks:
- Bounding Boxes (Images): Use
x_1,x_2,y_1, andy_2columns. The format isleft,right,top,bot. Ensure thatx_1 < x_2andy_1 < y_2. If only some images have boxes, leave the other rows empty. - Audio Offsets (Audios): Use the
start_timecolumn (float) to specify the offset from which the audio should start being read. - Re-identification (Re-id): Use the
sequencecolumn (string or integer) to group sequences of photos.
- Bounding Boxes (Images): Use
Configure Training with Contrastive vs Classification Losses
mainWhen setting up a training pipeline, the choice of loss function dictates your sampling and mining requirements:
Contrastive Losses (e.g.,
TripletLoss)- Requirement: Requires a
Miner(to produce triplets using strategies likehard mining) and aBatches Sampler(to ensure batches contain enough different labels to form triplets). - Mechanism: Mining occurs inside the
forward(features, labels)pass.
Classification Losses (e.g.,
ArcFace)- Requirement: No mining step is required by design. Batch sampling is optional.
- Mechanism: The feature vector is considered to be the output of the layer immediately preceding the classification head. Mining is not used, and the
forward(features, labels)signature is still maintained.
- Requirement: Requires a