OML is a PyTorch-based framework to train and validate the models producing high-quality embeddings.
Project description
OML is a PyTorch-based framework to train and validate the models producing high-quality embeddings.
FAQ
Why do I need OML?
You may think "If I need image embeddings I can simply train a vanilla classifier and take its penultimate layer". Well, it makes sense as a starting point. But there are several possible drawbacks:
-
If you want to use embeddings to perform searching you need to calculate some distance among them (for example, cosine or L2). Usually, you don't directly optimize these distances during the training in the classification setup. So, you can only hope that final embeddings will have the desired properties.
-
The second problem is the validation process. In the searching setup, you usually care how related your top-N outputs are to the query. The natural way to evaluate the model is to simulate searching requests to the reference set and apply one of the retrieval metrics. So, there is no guarantee that classification accuracy will correlate with these metrics.
-
Finally, you may want to implement a metric learning pipeline by yourself. There is a lot of work: to use triplet loss you need to form batches in a specific way, implement different kinds of triplets mining, tracking distances, etc. For the validation, you also need to implement retrieval metrics, which include effective embeddings accumulation during the epoch, covering corner cases, etc. It's even harder if you have several gpus and use DDP. You may also want to visualize your search requests by highlighting good and bad search results. Instead of doing it by yourself, you can simply use OML for your purposes.
What is the difference between Open Metric Learning and PyTorch Metric Learning?
PML is the popular library for Metric Learning, and it includes a rich collection of losses, miners, distances, and reducers; that is why we provide straightforward examples of using them with OML. Initially, we tried to use PML, but in the end, we came up with our library, which is more pipeline / recipes oriented. That is how OML differs from PML:
-
OML has Config API which allows training models by preparing a config and your data in the required format (it's like converting data into COCO format to train a detector from mmdetection).
-
OML focuses on end-to-end pipelines and practical use cases. It has config based examples on popular benchmarks close to real life (like photos of products of thousands ids). We found some good combinations of hyperparameters on these datasets, trained and published models and their configs. Thus, it makes OML more recipes oriented than PML, and its author confirms this saying that his library is a set of tools rather the recipes, moreover, the examples in PML are mostly for CIFAR and MNIST datasets.
-
OML has the Zoo of pretrained models that can be easily accessed from the code in the same way as in
torchvision
(when you typeresnet50(pretrained=True)
). -
OML is integrated with PyTorch Lightning, so, we can use the power of its Trainer. This is especially helpful when we work with DDP, so, you compare our DDP example and the PMLs one. By the way, PML also has Trainers, but it's not in the examples and custom
train
/test
functions are used instead.
We believe that having Config API, laconic examples, and Zoo of pretrained models sets the entry threshold to a really low value.
What is Metric Learning?
Metric Learning problem (also known as extreme classification problem) means a situation in which we have thousands of ids of some entities, but only a few samples for every entity. Often we assume that during the test stage (or production) we will deal with unseen entities which makes it impossible to apply the vanilla classification pipeline directly. In many cases obtained embeddings are used to perform search or matching procedures over them.
Here are a few examples of such tasks from the computer vision sphere:
- Person/Animal Re-Identification
- Face Recognition
- Landmark Recognition
- Searching engines for online shops and many others.
Glossary (Naming convention)
embedding
- model's output (also known asfeatures vector
ordescriptor
).query
- a sample which is used as a request in the retrieval procedure.gallery set
- the set of entities to search items similar toquery
(also known asreference
orindex
).Sampler
- an argument forDataLoader
which is used to form batchesMiner
- the object to form pairs or triplets after the batch was formed bySampler
. It's not necessary to form the combinations of samples only inside the current batch, thus, the memory bank may be a part ofMiner
.Samples
/Labels
/Instances
- as an example let's consider DeepFashion dataset. It includes thousands of fashion item ids (we name themlabels
) and several photos for each item id (we name the individual photo asinstance
orsample
). All of the fashion item ids have their groups like "skirts", "jackets", "shorts" and so on (we name themcategories
). Note, we avoid using the termclass
to avoid misunderstanding.training epoch
- batch samplers which we use for combination-based losses usually have a length equal to[number of labels in training dataset] / [numbers of labels in one batch]
. It means that we don't observe all of the available training samples in one epoch (as opposed to vanilla classification), instead, we observe all of the available labels.
How good may be a model trained with OML?
It may be comparable with the current (2022 year) SotA methods, for example, Hyp-ViT. (Few words about this approach: it's a ViT architecture trained with contrastive loss, but the embeddings were projected into some hyperbolic space. As the authors claimed, such a space is able to describe the nested structure of real-world data. So, the paper requires some heavy math to adapt the usual operations for the hyperbolical space.)
We trained the same architecture with triplet loss, fixing the rest of the parameters: training and test transformations, image size, and optimizer. See configs in Models Zoo. The trick was in heuristics in our miner and sampler:
-
Category Balance Sampler forms the batches limiting the number of categories C in it. For instance, when C = 1 it puts only jackets in one batch and only jeans into another one (just an example). It automatically makes the negative pairs harder: it's more meaningful for a model to realise why two jackets are different than to understand the same about a jacket and a t-shirt.
-
Hard Triplets Miner makes the task even harder keeping only the hardest triplets (with maximal positive and minimal negative distances).
Here are CMC@1 scores for 2 popular benchmarks. SOP dataset: Hyp-ViT — 85.9, ours — 86.6. DeepFashion dataset: Hyp-ViT — 92.5, ours — 92.1. Thus, utilising simple heuristics and avoiding heavy math we are able to perform on SotA level.
How does OML work under the hood?
Training part implies using losses, well-established for metric learning, such as the angular losses (like ArcFace) or the combinations based losses (like TripletLoss or ContrastiveLoss). The latter benefits from effective mining schemas of triplets/pairs, so we pay great attention to it. Thus, during the training we:
- Use
DataLoader
+Sampler
to form batches (for exampleBalanceSampler
) - [Only for losses based on combinations] Use
Miner
to form effective pairs or triplets, including those which utilize a memory bank. - Compute loss.
Validation part consists of several steps:
- Accumulating all of the embeddings (
EmbeddingMetrics
). - Calculating distances between them with respect to query/gallery split.
- Applying some specific retrieval techniques like query reranking or score normalisation.
- Calculating retrieval metrics like CMC@k, Precision@k or MeanAveragePrecision@k.
What about Self-Supervised Learning?
Recent research in SSL definitely obtained great results. The problem is that these approaches required an enormous amount of computing to train the model. But in our framework, we consider the most common case when the average user has no more than a few GPUs.
At the same time, it would be unwise to ignore success in this sphere, so we still exploit it in two ways:
- As a source of checkpoints that would be great to start training with. From publications and our experience, they are much better as initialisation than the default supervised model trained on ImageNet. Thus, we added the possibility to initialise your models using these pretrained checkpoints only by passing an argument in the config or the constructor.
- As a source of inspiration. For example, we adapted the idea of a memory bank from MoCo for the TripletLoss.
Do I need to know other frameworks to use OML?
No, you don't. OML is a framework-agnostic. Despite we use PyTorch Lightning as a loop
runner for the experiments, we also keep the possibility to run everything on pure PyTorch.
Thus, only the tiny part of OML is Lightning-specific and we keep this logic separately from
other code (see oml.lightning
). Even when you use Lightning, you don't need to know it, since
we provide ready to use Config API.
The possibility of using pure PyTorch and modular structure of the code leaves a room for utilizing OML with your favourite framework after the implementation of the necessary wrappers.
Can I use OML without any knowledge in DataScience?
Yes. To run the experiment with Config API
you only need to write a converter
to our format (it means preparing the
.csv
table with 5 predefined columns).
That's it!
Probably we already have a suitable pre-trained model for your domain in our Models Zoo. In this case, you don't even need to train it.
Documentation
Documentation is available via the link.
Installation
OML is available in PyPI:
pip install -U open-metric-learning
You can also pull the prepared image from DockerHub...
docker pull omlteam/oml:gpu
docker pull omlteam/oml:cpu
...or build one by your own
make docker_build RUNTIME=cpu
make docker_build RUNTIME=gpu
Get started using Config API
Using configs is the best option if your dataset and pipeline are standard enough or if you are not experienced in Machine Learning or Python. You can find more details in the examples.
Get started using Python
The most flexible, but knowledge-requiring approach. You are not limited by our project structure and you can use only that part of the functionality which you need. You can start with fully working code snippets below that train and validate the model on a tiny dataset of figures. ㅤ
Feature extractor
Training
import torch
from tqdm import tqdm
from oml.datasets.base import DatasetWithLabels
from oml.losses.triplet import TripletLossWithMiner
from oml.miners.inbatch_all_tri import AllTripletsMiner
from oml.models.vit.vit import ViTExtractor
from oml.samplers.balance import BalanceSampler
from oml.utils.download_mock_dataset import download_mock_dataset
dataset_root = "mock_dataset/"
df_train, _ = download_mock_dataset(dataset_root)
model = ViTExtractor("vits16_dino", arch="vits16", normalise_features=False).train()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-6)
train_dataset = DatasetWithLabels(df_train, dataset_root=dataset_root)
criterion = TripletLossWithMiner(margin=0.1, miner=AllTripletsMiner())
sampler = BalanceSampler(train_dataset.get_labels(), n_labels=2, n_instances=2)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_sampler=sampler)
for batch in tqdm(train_loader):
embeddings = model(batch["input_tensors"])
loss = criterion(embeddings, batch["labels"])
loss.backward()
optimizer.step()
optimizer.zero_grad()
Validation
import torch
from tqdm import tqdm
from oml.datasets.base import DatasetQueryGallery
from oml.metrics.embeddings import EmbeddingMetrics
from oml.models.vit.vit import ViTExtractor
from oml.utils.download_mock_dataset import download_mock_dataset
dataset_root = "mock_dataset/"
_, df_val = download_mock_dataset(dataset_root)
model = ViTExtractor("vits16_dino", arch="vits16", normalise_features=False).eval()
val_dataset = DatasetQueryGallery(df_val, dataset_root=dataset_root)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=4)
calculator = EmbeddingMetrics()
calculator.setup(num_samples=len(val_dataset))
with torch.no_grad():
for batch in tqdm(val_loader):
batch["embeddings"] = model(batch["input_tensors"])
calculator.update_data(batch)
metrics = calculator.compute_metrics()
Training + Validation [Lightning]
import pytorch_lightning as pl
import torch
from oml.datasets.base import DatasetQueryGallery, DatasetWithLabels
from oml.lightning.modules.retrieval import RetrievalModule
from oml.lightning.callbacks.metric import MetricValCallback
from oml.losses.triplet import TripletLossWithMiner
from oml.metrics.embeddings import EmbeddingMetrics
from oml.miners.inbatch_all_tri import AllTripletsMiner
from oml.models.vit.vit import ViTExtractor
from oml.samplers.balance import BalanceSampler
from oml.utils.download_mock_dataset import download_mock_dataset
dataset_root = "mock_dataset/"
df_train, df_val = download_mock_dataset(dataset_root)
# model
model = ViTExtractor("vits16_dino", arch="vits16", normalise_features=False)
# train
optimizer = torch.optim.SGD(model.parameters(), lr=1e-6)
train_dataset = DatasetWithLabels(df_train, dataset_root=dataset_root)
criterion = TripletLossWithMiner(margin=0.1, miner=AllTripletsMiner())
batch_sampler = BalanceSampler(train_dataset.get_labels(), n_labels=2, n_instances=3)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_sampler=batch_sampler)
# val
val_dataset = DatasetQueryGallery(df_val, dataset_root=dataset_root)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=4)
metric_callback = MetricValCallback(metric=EmbeddingMetrics())
# run
pl_model = RetrievalModule(model, criterion, optimizer)
trainer = pl.Trainer(max_epochs=1, callbacks=[metric_callback], num_sanity_val_steps=0)
trainer.fit(pl_model, train_dataloaders=train_loader, val_dataloaders=val_loader)
Training + Validation [Lightning Distributed]
import pytorch_lightning as pl
import torch
from oml.datasets.base import DatasetQueryGallery, DatasetWithLabels
from oml.lightning.modules.retrieval import RetrievalModuleDDP
from oml.lightning.callbacks.metric import MetricValCallbackDDP
from oml.losses.triplet import TripletLossWithMiner
from oml.metrics.embeddings import EmbeddingMetricsDDP
from oml.miners.inbatch_all_tri import AllTripletsMiner
from oml.models.vit.vit import ViTExtractor
from oml.samplers.balance import BalanceSampler
from oml.utils.download_mock_dataset import download_mock_dataset
dataset_root = "mock_dataset/"
df_train, df_val = download_mock_dataset(dataset_root)
# model
model = ViTExtractor("vits16_dino", arch="vits16", normalise_features=False)
# train
optimizer = torch.optim.SGD(model.parameters(), lr=1e-6)
train_dataset = DatasetWithLabels(df_train, dataset_root=dataset_root)
criterion = TripletLossWithMiner(margin=0.1, miner=AllTripletsMiner())
batch_sampler = BalanceSampler(train_dataset.get_labels(), n_labels=2, n_instances=3)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_sampler=batch_sampler)
# val
val_dataset = DatasetQueryGallery(df_val, dataset_root=dataset_root)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=4)
metric_callback = MetricValCallbackDDP(metric=EmbeddingMetricsDDP()) # DDP specific
# run
pl_model = RetrievalModuleDDP(model=model, criterion=criterion, optimizer=optimizer,
loaders_train=train_loader, loaders_val=val_loader # DDP specific
)
ddp_args = {"accelerator": "auto", "devices": 2, "strategy": pl.plugins.DDPPlugin(), "replace_sampler_ddp": False} # DDP specific
trainer = pl.Trainer(max_epochs=1, callbacks=[metric_callback], num_sanity_val_steps=0, **ddp_args)
trainer.fit(pl_model) # we don't pass loaders to .fit() in DDP
Colab: there is no Colab link since it provides only single-GPU machines. ㅤ
Using a trained model for retrieval
import torch
from oml.const import MOCK_DATASET_PATH
from oml.inference.flat import inference_on_images
from oml.models import ViTExtractor
from oml.registry.transforms import get_transforms_for_pretrained
from oml.utils.download_mock_dataset import download_mock_dataset
from oml.utils.misc_torch import pairwise_dist
_, df_val = download_mock_dataset(MOCK_DATASET_PATH)
df_val["path"] = df_val["path"].apply(lambda x: MOCK_DATASET_PATH / x)
queries = df_val[df_val["is_query"]]["path"].tolist()
galleries = df_val[df_val["is_gallery"]]["path"].tolist()
model = ViTExtractor.from_pretrained("vits16_dino")
transform, _ = get_transforms_for_pretrained("vits16_dino")
args = {"num_workers": 0, "batch_size": 8}
features_queries = inference_on_images(model, paths=queries, transform=transform, **args)
features_galleries = inference_on_images(model, paths=galleries, transform=transform, **args)
dist_mat = pairwise_dist(x1=features_queries, x2=features_galleries)
ii_closest = torch.argmin(dist_mat, dim=1)
print(f"Indices of the items closest to queries: {ii_closest}")
Postprocessing
You can also boost retrieval accuracy of your features extractor by adding a postprocessor (we recommend
to check the examples above first).
In the example below we train a siamese model to re-rank top retrieval outputs of the original model
by performing inference on pairs (query, output_i)
where i=1..top_n
.
For the Config-API analogue of the pipeline below, please, check the config. The documentation for related classes is available via the link. Note, this functionality is new and a work still in progress.
Postprocessor: Training + Validation
from pprint import pprint
import torch
from torch.nn import BCEWithLogitsLoss
from torch.utils.data import DataLoader
from oml.datasets.base import DatasetWithLabels, DatasetQueryGallery
from oml.inference.flat import inference_on_dataframe
from oml.metrics.embeddings import EmbeddingMetrics
from oml.miners.pairs import PairsMiner
from oml.models.siamese import ConcatSiamese
from oml.models.vit.vit import ViTExtractor
from oml.retrieval.postprocessors.pairwise import PairwiseImagesPostprocessor
from oml.samplers.balance import BalanceSampler
from oml.transforms.images.torchvision.transforms import get_normalisation_resize_torch
from oml.utils.download_mock_dataset import download_mock_dataset
# Let's start with saving embeddings of a pretrained extractor for which we want to build a postprocessor
dataset_root = "mock_dataset/"
download_mock_dataset(dataset_root)
extractor = ViTExtractor("vits16_dino", arch="vits16", normalise_features=False)
transform = get_normalisation_resize_torch(im_size=64)
embeddings_train, embeddings_val, df_train, df_val = \
inference_on_dataframe(dataset_root, "df.csv", extractor=extractor, transforms_extraction=transform)
# We are building Siamese model on top of existing weights and train it to recognize positive/negative pairs
siamese = ConcatSiamese(extractor=extractor, mlp_hidden_dims=[100])
optimizer = torch.optim.SGD(siamese.parameters(), lr=1e-6)
miner = PairsMiner(hard_mining=True)
criterion = BCEWithLogitsLoss()
train_dataset = DatasetWithLabels(df=df_train, transform=transform, extra_data={"embeddings": embeddings_train})
batch_sampler = BalanceSampler(train_dataset.get_labels(), n_labels=2, n_instances=2)
train_loader = DataLoader(train_dataset, batch_sampler=batch_sampler)
for batch in train_loader:
# We sample pairs on which the original model struggled most
ids1, ids2, is_negative_pair = miner.sample(features=batch["embeddings"], labels=batch["labels"])
probs = siamese(x1=batch["input_tensors"][ids1], x2=batch["input_tensors"][ids2])
loss = criterion(probs, is_negative_pair.float())
loss.backward()
optimizer.step()
optimizer.zero_grad()
# Siamese re-ranks top-n retrieval outputs of the original model performing inference on pairs (query, output_i)
val_dataset = DatasetQueryGallery(df=df_val, extra_data={"embeddings": embeddings_val}, transform=transform)
valid_loader = DataLoader(val_dataset, batch_size=4, shuffle=False)
postprocessor = PairwiseImagesPostprocessor(top_n=3, pairwise_model=siamese, transforms=transform)
calculator = EmbeddingMetrics(postprocessor=postprocessor)
calculator.setup(num_samples=len(val_dataset))
for batch in valid_loader:
calculator.update_data(data_dict=batch)
pprint(calculator.compute_metrics()) # Pairwise inference happens here
Usage with PyTorch Metric Learning
You can easily access a lot of content from PyTorch Metric Learning with our library. You can see that the examples below are different from the basic ones only in a few lines of code:
Training with loss from PML
import torch
from tqdm import tqdm
from oml.datasets.base import DatasetWithLabels
from oml.losses.triplet import TripletLossWithMiner
from oml.miners.inbatch_all_tri import AllTripletsMiner
from oml.models.vit.vit import ViTExtractor
from oml.samplers.balance import BalanceSampler
from oml.utils.download_mock_dataset import download_mock_dataset
from pytorch_metric_learning import losses, distances, reducers, miners
dataset_root = "mock_dataset/"
df_train, _ = download_mock_dataset(dataset_root)
model = ViTExtractor("vits16_dino", arch="vits16", normalise_features=False).train()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-6)
train_dataset = DatasetWithLabels(df_train, dataset_root=dataset_root)
# PML specific
# criterion = losses.TripletMarginLoss(margin=0.2, triplets_per_anchor="all")
criterion = losses.ArcFaceLoss(num_classes=df_train["label"].nunique(), embedding_size=model.feat_dim) # for classification-like losses
sampler = BalanceSampler(train_dataset.get_labels(), n_labels=2, n_instances=2)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_sampler=sampler)
for batch in tqdm(train_loader):
embeddings = model(batch["input_tensors"])
loss = criterion(embeddings, batch["labels"])
loss.backward()
optimizer.step()
optimizer.zero_grad()
Training with distance, reducer, miner and loss from PML
import torch
from tqdm import tqdm
from oml.datasets.base import DatasetWithLabels
from oml.losses.triplet import TripletLossWithMiner
from oml.miners.inbatch_all_tri import AllTripletsMiner
from oml.models.vit.vit import ViTExtractor
from oml.samplers.balance import BalanceSampler
from oml.utils.download_mock_dataset import download_mock_dataset
from pytorch_metric_learning import losses, distances, reducers, miners
dataset_root = "mock_dataset/"
df_train, _ = download_mock_dataset(dataset_root)
model = ViTExtractor("vits16_dino", arch="vits16", normalise_features=False).train()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-6)
train_dataset = DatasetWithLabels(df_train, dataset_root=dataset_root)
# PML specific
distance = distances.LpDistance(p=2)
reducer = reducers.ThresholdReducer(low=0)
criterion = losses.TripletMarginLoss()
miner = miners.TripletMarginMiner(margin=0.2, distance=distance, type_of_triplets="all")
sampler = BalanceSampler(train_dataset.get_labels(), n_labels=2, n_instances=2)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_sampler=sampler)
for batch in tqdm(train_loader):
embeddings = model(batch["input_tensors"])
loss = criterion(embeddings, batch["labels"], miner(embeddings, batch["labels"])) # PML specific
loss.backward()
optimizer.step()
optimizer.zero_grad()
Note, during the validation process OpenMetricLearning computes L2 distances. Thus, when choosing a distance from PML,
we recommend you to pick distances.LpDistance(p=2)
.
To use content from PyTorch Metric Learning with our Config API just follow the standard tutorial of adding custom loss.
Zoo
Below are the models trained with OML on 4 public datasets. For more details about the training process and configs, please, visit examples submodule and it's Readme. All metrics below were obtained on the images with the sizes of 224 x 224:
model | cmc1 | dataset | weights | experiment |
---|---|---|---|---|
ViTExtractor.from_pretrained("vits16_inshop") |
0.921 | DeepFashion Inshop | link | link |
ViTExtractor.from_pretrained("vits16_sop") |
0.866 | Stanford Online Products | link | link |
ViTExtractor.from_pretrained("vits16_cars") |
0.907 | CARS 196 | link | link |
ViTExtractor.from_pretrained("vits16_cub") |
0.837 | CUB 200 2011 | link | link |
We also provide an integration with the models pretrained by other researchers. All metrics below were obtained on the images with the sizes of 224 x 224:
model | Stanford Online Products | DeepFashion InShop | CUB 200 2011 | CARS 196 |
---|---|---|---|---|
ViTCLIPExtractor.from_pretrained("sber_vitb32_224") |
0.547 | 0.514 | 0.448 | 0.618 |
ViTCLIPExtractor.from_pretrained("sber_vitb16_224") |
0.565 | 0.565 | 0.524 | 0.648 |
ViTCLIPExtractor.from_pretrained("sber_vitl14_224") |
0.512 | 0.555 | 0.606 | 0.707 |
ViTCLIPExtractor.from_pretrained("openai_vitb32_224") |
0.612 | 0.491 | 0.560 | 0.693 |
ViTCLIPExtractor.from_pretrained("openai_vitb16_224") |
0.648 | 0.606 | 0.665 | 0.767 |
ViTCLIPExtractor.from_pretrained("openai_vitl14_224") |
0.670 | 0.675 | 0.745 | 0.844 |
ViTExtractor.from_pretrained("vits16_dino") |
0.648 | 0.509 | 0.627 | 0.265 |
ViTExtractor.from_pretrained("vits8_dino") |
0.651 | 0.524 | 0.661 | 0.315 |
ViTExtractor.from_pretrained("vitb16_dino") |
0.658 | 0.514 | 0.541 | 0.288 |
ViTExtractor.from_pretrained("vitb8_dino") |
0.689 | 0.599 | 0.506 | 0.313 |
ResnetExtractor.from_pretrained("resnet50_moco_v2") |
0.493 | 0.267 | 0.264 | 0.149 |
ResnetExtractor.from_pretrained("resnet50_imagenet1k_v1") |
0.515 | 0.284 | 0.455 | 0.247 |
How to use models from Zoo?
from oml.const import CKPT_SAVE_ROOT as CKPT_DIR, MOCK_DATASET_PATH as DATA_DIR
from oml.models import ViTExtractor
from oml.registry.transforms import get_transforms_for_pretrained
model = ViTExtractor.from_pretrained("vits16_dino")
transforms, im_reader = get_transforms_for_pretrained("vits16_dino")
img = im_reader(DATA_DIR / "images" / "circle_1.jpg") # put path to your image here
img_tensor = transforms(img)
# img_tensor = transforms(image=img)["image"] # for transforms from Albumentations
features = model(img_tensor.unsqueeze(0))
# Check other available models:
print(list(ViTExtractor.pretrained_models.keys()))
# Load checkpoint saved on a disk:
model_ = ViTExtractor(weights=CKPT_DIR / "vits16_dino.ckpt", arch="vits16", normalise_features=False)
Contributing guide
We welcome new contributors! Please, see our:
Extra materials
You can also read some extra materials related to OML:
-
Theory and practice of metric learning with the usage of OML. Post in English on Medium | Post in Russian on Habr | Post in Chinese on CSDN, translated by Chia-Chen Chang.
-
The report for Berlin-based meetup: "Computer Vision in production". November, 2022. Link
Acknowledgments
The project was started in 2020 as a module for Catalyst library. I want to thank people who worked with me on that module: Julia Shenshina, Nikita Balagansky, Sergey Kolesnikov and others.
I would like to thank people who continue working on this pipeline when it became a separe project: Julia Shenshina, Misha Kindulov, Aleksei Tarasov and Verkhovtsev Leonid.
I also want to thank NewYorker, since the part of functionality was developed (and used) by its computer vision team led by me.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file open-metric-learning-0.3.13.tar.gz
.
File metadata
- Download URL: open-metric-learning-0.3.13.tar.gz
- Upload date:
- Size: 133.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 531dcd8eb159fbfda0d02d01b11279b04e95a6ac1520b5e9cd6a4f5ff1a9be3a |
|
MD5 | 2f74d418a83bde986581238e035caa7b |
|
BLAKE2b-256 | e8f729d0c7ec0cf8b106a1db761bd6f1194965241dc00b55a299821e464ea795 |
File details
Details for the file open_metric_learning-0.3.13-py3-none-any.whl
.
File metadata
- Download URL: open_metric_learning-0.3.13-py3-none-any.whl
- Upload date:
- Size: 156.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ce370c0f8922003d63a75e036120245959381a1be8c2b5c64f9ca4738d54426 |
|
MD5 | b744052fe3923fbf937f3633d34446c4 |
|
BLAKE2b-256 | 49f71f1a9f05cb4e01fb6f687efdca70eeb4ad3a60ce48b7c2703c2d838ddd4f |