Complete guide to Similarity-based learning for Counterfeit detection Part -2

(Training Siamese network for Counterfeit detection)

Yash Vardhan Singh
5 min readJun 7, 2022

Harsh Parmar Khushboo Tak Trapti Kalra Yash Vardhan Singh
(IBM Consulting- AI and Advanced Analytics, CIC India)

In this article, we will talk about how to train the Siamese network to perform similarity-based learning using contrastive loss.

To get a basic idea about the Siamese network check Part1:

If you want to know about the inference pipeline for counterfeit detection, check Part 3:

The code and approach mentioned in this article were used for similarity-based learning on some confidential data. We applied this method to detect if a given product is authentic or counterfeit. However, this method can be applied to various use cases like finding anomalies, face recognition, etc.

You must have heard of this topic (We know!) but we try to simplify the end-to-end process to help understand it better and also provide a code walk-through for our training pipeline.

Before we train the model, we will need to prepare the data. As you know that our target is to make the model learn in a way that brings similar objects together and keeps dissimilar objects away. There are different loss functions available that help in similarity-based training but we keep our scope limited to contrastive loss.

Let’s get started and take you through the journey of training the Siamese model . . .

Data Preparation
The input to the twin network(Siamese network with shared weights and biases) will be ‘pairs’ of images.
We will generate two pairs: positive-positive and positive-negative.

First thing is to import packages we need ( Good coders code, Great coders reuse! 😅)

After importing the required packages we define path for our authentic and counterfeit data. In our case ‘authentic’ is a ‘positive’ class and ‘counterfeit’ is a ‘negative’ class.

Before going any further with implementation. Let us understand the way we want to make the pairs.
We have two directories with authentic and counterfeit images respectively. ‘Authentic-Authentic’ pair is formed by pairing a randomly chosen authentic image from ‘auth-dir’ with another(not the one we selected) randomly chosen image from ‘auth-dir’. ‘Authentic-Counterfeit’ pair is formed by pairing a randomly chosen authentic image from ‘auth-dir’ with another randomly chosen image from ‘counterfeit-dir’

Fabric dataset courtesy Olivia Walton from IBM

Let’s get back to the code . . .

We label ‘auth-auth’ pair as ‘1’ and ‘auth-counterfeit’ as 0

One advantage of the Siamese network is that it requires less training data and is also robust to the class imbalance problem.

We randomly choose the number of images we need to form pairs from each of the directory. Here, ‘modeling_set_ ’ defines number of images we want from the respective directories for modeling.

Next, we read the images from the directories and randomly choose authentic and counterfeit images for modeling.

Now we have all the required parameters to call a function that will generate pairs for us. We know that both positive-positive pairs and positive-negative pairs will have the same set of first images(positive ones), called anchor images.

‘extract_pairs’ function take parameters as defined above. As needed we can take half/one-third or any number of images as anchor ones. We then pair anchor images with remaining authentic to form ‘auth-auth’ pair and anchor images with counterfeit to form ‘auth-counterfeit’ pair.

We have the ‘datasets’ that contains both the pairs (auth-auth and auth-counterfeit).
Last step is to divide it into Train/Test/Val and save it in the CSV file for use in model building.

The critical work of data prep is done and now we are excited to start laying down the Siamese network.

Import required packages

Read train/test/val data from the saved csvs

What is Transfer learning?

Human learners appear to have inherent ways to transfer knowledge between tasks. That is, we recognize and apply relevant knowledge from previous learning experiences when we encounter new tasks. The more related a new task is to our previous experience, the more easily we can master it.

Transfer learning is the improvement of learning in a new task through the transfer of knowledge from a related task that has already been learned.

Transfer learning attempts to develop methods to transfer knowledge learned in one or more source tasks and use it to improve learning in a related target task.

We quote Andrew Ng: Transfer learning will be the next driver of machine learning’s commercial success after supervised learning.

Backbone Network: Convolutional Neural Networks usually take an image as an input and compute its features and encode it in a feature map. These CNNs are called the backbones of the network.

In our case we can use any one of the following as the backbone of our twin network:

In this article we have discussed the hands on implementation using VGG16 as the backbone of our twin neural network.

‘build_model(backbone)’ functions defines the network architecture by taking backbone as a parameter. The backbone can be VGG16/ResNet50/Inception etc.

We define ‘input shape’ for the model. This parameter will change depending on the pre-trained model we choose.
The layers of the backbone model are non-trainable. The ‘base_network’ will be same for both the input images ( as weights and biases are shared in Twin network).

Next, re-use the same instance `base_network`, the weights of the network will be shared across the two branches

Below are the few functions we used during training the model.The code comments are self explanatory.

The ‘loss’ function is used to apply contrastive loss while training the model. Further information is available on :

‘processed_a’ and ‘processed_b’ are the embeddings that we get after passing each of them through the base_network.
We define a ‘merge layer’ to calculate the euclidean distance between two embeddings followed by batch normalisation and output layer.

Apply optimizer and compile the model

We read most of the values from the config file.
Tip: You can define a config file to have values for configurable parameters of code.

Below function is defined to generate batches of images that will be used to train the model.

Finally we have all the required data and parameters to train the model. Hyperparameters and some other parameters are configurable but for the sake of understanding the process, we haven’t mentioned the code structure we had followed.

Once the model is trained we can save the Keras model and load it at the time of inference.
This article focused on preparing the data and training the Siamese network having a backbone as the pre-trained model using the contrastive loss function.

To know how to use this trained twin network at the inference time, please check our article:

Thanks for reading!!
If you liked the article do let us know by hitting the like button!

--

--

Yash Vardhan Singh

Data Scientist | NLP | Computer Vision | Deep Learning | LLMs |