The siamese approach allows to learn a meaningful embedding by training a CNN with pairs of genuine and impostor images. Weights are shared during training so that pairs of images will be represented according to both their features and the label that identifies the pair as of two images representing the same object or two different ones.
Here are collected a series of scripts in which the contrastive loss approach has been explored while developing the algorithm for idtracker.ai.
The code, written for tensorflow0.6 uses only basic functions and it is heavily commented. It includes a study on the contrastive loss and its parameters.
The function plotter allows to test and plot in a 3d scatter the contrastive loss implemented in
plotter.contrastive_loss1
In this implementation we used dataset of images generated in matlab, however it should be easy to rearrange the code in order to load images or binary files.
Test images can be classified by using KNN or hierarchical clustering in siamese_model_HD_KNN and siamese_model_HD_Clustering, respectively. Caveat: The two scripts are as self-contained as reduntant.