One Shot Learning with Siamese Networks

Mamadou K. KEITA
ILLUMINATION
Published in
4 min readMar 14, 2023

--

Image by Gerd Altmann from Pixabay

Do you ever wonder how machines can learn from a limited number of examples? How can they predicate with great precision?

I have spent a lot of time exploring various aspects and technics of these fields, to understand how machines learn efficiently. During my research, I came across One-Shot Learning (OSL) — a novel approach to machine learning that allows models to recognize new objects or concepts from just a single example or a few examples. In this article, I will provide you with a brief overview of what OSL is, how it works, and how you can implement it using Siamese networks.

Whether you are a student, researcher, or someone interested in the latest developments in technology, you will find this article interesting and will gain some understanding of OSL and its implementation. Let us quickly dive into the world of One-Shot Learning!

What is One-Shot Learning?

One Shot learning is a machine learning algorithm that requires extraordinarily little database to identify or access the similarities between the objects. In other words, it is a type of learning where the algorithm can generalize from very few examples.

Conventional machine learning algorithms require enormous amounts of data to achieve high accuracy in classification and prediction tasks. This might be a severe problem where data is scarce and difficult to obtain. This is when the One-Shot Learning enters the equation because it is designed to work with less data, thus making it effective in a variety of applications such as facial recognition, disease diagnosis and object detection.

However, finding a technique for describing the input data that captures the crucial aspects of the object is a significant challenge for one-shot learning. This is because the model must recognize the object from a single sample, even if it has undergone changes in scale, rotation, or lighting.

So, how can we address this challenge?

Well, Siamese networks are a type of neural network architecture that has been used extensively for One Shot Learning tasks. The key idea behind Siamese networks is to learn a similarity metric between two inputs. In the case of OSL, the inputs are two images, and the output of the network is a measure of how similar the two images are.

A basic siamese network architecture implementation accepts two input images (left), has identical CNN subnetworks for each input with each subnetwork ending in a fully-connected layer (middle), computes the Euclidean distance between the fully-connected layer outputs, and then passes the distance through a sigmoid activation function to determine similarity (right). Image edited by the author in Canva.

The Siamese network consists of two or more identical subnetworks that share the same weights and architecture. The subnetworks are fed with two different images, and the outputs are compared using a distance metric, such as Euclidean distance or cosine similarity. The distance metric is then used to classify whether the two images belong to the same class or different classes.

Enough explanation, let’s now implement an example of a siamese network using Keras and Tensorflow.

import tensorflow as tf
from tensorflow import keras

# Define the Siamese Network
def get_siamese_model(input_shape):
left_input = keras.layers.Input(input_shape)
right_input = keras.layers.Input(input_shape)

model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64, (10,10), activation='relu', input_shape=input_shape))
model.add(keras.layers.MaxPooling2D())
model.add(keras.layers.Conv2D(128, (7,7), activation='relu'))
model.add(keras.layers.MaxPooling2D())
model.add(keras.layers.Conv2D(128, (4,4), activation='relu'))
model.add(keras.layers.MaxPooling2D())
model.add(keras.layers.Conv2D(256, (4,4), activation='relu'))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(4096, activation='sigmoid'))

# Generate the two encoded outputs
encoded_l = model(left_input)
encoded_r = model(right_input)

# Add a customized layer to compute the absolute difference between the encodings
L1_distance = lambda x: tf.keras.backend.abs(x[0]-x[1])
both = keras.layers.Lambda(L1_distance)([encoded_l, encoded_r])

# Add a dense layer with a sigmoid unit to generate the similarity score
prediction = keras.layers.Dense(1, activation='sigmoid')(both)

# Connect the inputs with the outputs
siamese_net = keras.models.Model(inputs=[left_input,right_input],outputs=prediction)

# Return the model
return siamese_net

This Siamese network model takes two input images and outputs a prediction of whether they belong to the same class or not. The input shape of the images is defined in the input_shape parameter of the get_siamese_model function.

The first part of the model defines the architecture of the convolutional neural network (CNN) used to encode the input images. CNN consists of several convolutional layers followed by max-pooling layers to down sample the feature maps. The output of the CNN is then flattened and passed through a dense layer with a sigmoid activation function to generate an encoded output.

The encoded outputs of the two input images are then passed through a customized layer that computes the absolute difference between them. This is done using a lambda layer that takes in two inputs and computes their absolute difference using the tf.keras.backend.abs function. The absolute difference is then passed through a dense layer with a sigmoid activation function to generate a similarity score, which is the output of the Siamese network.

Conclusion

One Shot Learning is a powerful machine learning paradigm that can recognize new objects from a single example or a few examples. Siamese networks are a type of neural network architecture that can learn similarity from two inputs. This makes them an ideal choice for one-shot learning tasks that involve limited data.

This brings us to the end of this article where we learned about The One Shot Leaning and Siamese networks. Are you interested in Machine Learning, Deep learning, Robotics, etc… Follow for more contents.

--

--

Mamadou K. KEITA
ILLUMINATION

Machine Learning Engineer & Researcher, Entrepreneur, Impact driven. "Technology and Innovation to serve People"