Examples on Google Colab

March 1, 2022 ยท View on GitHub

Before running the notebooks, make sure that the runtime type is set to "GPU", by going to the Runtime menu, and clicking on "Change runtime type".

Click "Open in playground" on the Colab header to interact with the notebook.

Simple examples

NotebookDescriptionColab Link
MNIST using TripletMarginLossTrain with TripletMarginLoss, evaluate with AccuracyCalculator.Open In Colab
MNIST using SubCenterArcFaceLossTrain with SubCenterArcFaceLoss, view outlier samples.Open In Colab
MoCo on CIFAR10Self-supervision using MoCo with CrossBatchMemoryOpen In Colab
Multiprocessing with DistributedDataParallelAn example using pytorch_metric_learning.utils.distributedOpen In Colab

Training/testing workflows with logging and model saving

The following notebooks are meant to show entire training/testing workflows. (If you want to use just a loss or miner in your own code, see the notebooks above.) They generally go through the following steps:

  • initialize models, optimizers, and transforms
  • creates train/validation splits
  • initialize a loss, miner, sampler, trainer, and tester
  • train the model, record accuracy, and plot the embedding space
NotebookDescriptionColab Link
MetricLossOnlyUse just a metric loss.Open In Colab
A scRNAseq Metric EmbeddingAn example using canonical single-cell RNAseq cell types.Open In Colab
TrainWithClassifierUse a metric loss + classification loss and network.Open In Colab
CascadedEmbeddingsUse multiple sub-networks and mine their outputs.Open In Colab
DeepAdversarialMetricLearningUse a generator to create hard negatives during training.Open In Colab
TwoStreamMetricLossFor use with two-stream datasets, where anchors and positives/negatives come from different sources.Open In Colab
InferenceUse the inference module after you're done training.Open In Colab