Top AI Repos — open-source AI, indexed and scored
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Siamese and triplet networks with online pair/triplet mining in PyTorch
| Date | Stars |
|---|---|
| 2026-07-24 | 3174 |
| 2026-07-25 | 3174 |
| 2026-07-28 | 3174 |
| 2026-07-30 | 3174 |
| 2026-08-06 | 3174 |
Today
— stars today
This week
— stars this week
This month
— stars this month
Momentum
0.0
growth rate 0.00%/day
# Siamese and triplet learning with online pair/triplet mining
PyTorch implementation of siamese and triplet networks for learning embeddings.
Siamese and triplet networks are useful to learn mappings from image to a compact Euclidean space where distances correspond to a measure of similarity [2]. Embeddings trained in such way can be used as features vectors for classification or few-shot learning tasks.
# Installation
Requires [pytorch](http://pytorch.org/) 0.4 with torchvision 0.2.1
For pytorch 0.3 compatibility checkout tag torch-0.3.1
# Code structure
- **datasets.py**
- *SiameseMNIST* class - wrapper for a MNIST-like dataset, returning random positive and negative pairs
- *TripletMNIST* class - wrapper for a MNIST-like dataset, returning random triplets (anchor, positive and negative)
- *BalancedBatchSampler* class - BatchSampler for data loader, randomly chooses *n_classes* and *n_samples* from each class based on labels
- **networks.py**
- *EmbeddingNet* - base network for encoding images into embedding vector
- *ClassificationNet* - wrapper for an embedding network, adds a fully connected layer and log softmax for classification
- *SiameseNet* - wrapper for an embedding network, processes pairs of inputs
- *TripletNet* - wrapper for an embedding network, processes triplets of inputs
- **losses.py**
- *ContrastiveLoss* - contrastive loss for pairs of embeddings and pair target (same/different)
- *TripletLoss* - triplet loss for triplets of embeddings
- *OnlineContrastiveLoss* - contrastive loss for a mini-batch of embeddings. Uses a *PairSelector* object to find positive and negative pairs within a mini-batch using ground truth class labels and computes contrastive loss for these pairs
- *OnlineTripletLoss* - triplet loss for a mini-batch of embeddings. Uses a *TripletSelector* object to find triplets within a mini-batch using ground truth class labels and computes triplet loss
- **trainer.py**
- *fit* - unified function for training a network with different number of inputs and different types of loss functions
- **metrics.py**
- Sample metrics that can be used with *fit* function from *trainer.py*
- **utils.py**
- *PairSelector* - abstract class defining objects generating pairs based on embeddings and ground truth class labels. Can be used with *OnlineContrastiveLoss*.
- *AllPositivePairSelector, HardNegativePairSelector* - PairSelector implementations
- *TripletSelector* - abstract class defining objects generating triplets based on embeddings and ground truth class labels. Can be used with *OnlineTripletLoss*.
- *AllTripletSelector*, *HardestNegativeTripletSelector*, *RandomNegativeTripletSelector*, *SemihardNegativeTripletSelector* - TripletSelector implementations
# Examples - MNIST
We'll train embeddings on MNIST dataset. Experiments were run in [jupyter notebook](Experiments_MNIST.ipynb).
We'll go through learning supervised feature embeddings using different loss functions on MNIST dataset. This is just for visualization purposes, thus we'll be using 2-dimensional embeddings which isn't the best choice in practice.
For every experiment the same embedding network is used (32 conv 5x5 -> PReLU -> MaxPool 2x2 -> 64 conv 5x5 -> PReLU -> MaxPool 2x2 -> Dense 256 -> PReLU -> Dense 256 -> PReLU -> Dense 2) and we don't perform any hyperparameter search.
## Baseline - classification with softmax
We add a fully-connected layer with the number of classes and train the network for classification with softmax and cross-entropy. The network trains to ~99% accuracy. We extract 2 dimensional embeddings from penultimate layer:
Train set:

Test set:

While the embeddings look separable (which is what we trained them for), they don't have good metric properties. They might not be the best choice as a descriptor for new classes.
## Siamese network
Now we'll train a siamese network that takes a pair of Excerpt of 10,817 characters
Read on GitHubAdam Bielski · University of Bern · Switzerland
22
Aleksey
1
Would you bet a product on this? Bounded 0–100 and slow moving.
matched fp:677b82f3356d6e77, topic:deep-learning, topic:pytorch