NA / 5
We will cover the following tasks in 48 minutes:
Introduction & Importing Libraries
Before we get started, let’s quickly understand the pre-requisites: You will need to know a bit of Python. You should know the basics of Keras and Tensorflow. If you are new to them, please check out my Tensorflow beginner series on Rhyme to get up to speed. You will also need a google account to sign up for Google Cloud Platform but I assume almost everyone has one so that won’t be a problem. Please note that while you don’t need to use GPUs for this project, we will use them in the future projects and for that GCP or Google Cloud Platform may ask you to provide credit card info. They will give you some free credit when you get started though so you shouldn’t actually incur any actual charges even by the end of the whole series. So, if you don’t have a GCP account, then please sign up for it and if you do, then log into it.
So far, we know that we are going to use custom layers in Keras but that’s about it. More specifically, we are going to create a model which will be used to verify if any two given images from the MNIST dataset are of the same digit class or not. There can be many ways to do this, but the approach that we want to focus on does not use any classifier training like what we’ve done before in the TensorFlow beginner series.
Instead, we will create a model which will compare compressed feature representations of any two MNIST images and if the two feature representations, also called embeddings, are similar to each other then the model will classify the image pair as a positive pair or a pair of images of the same class. If the two embeddings are different enough from each other, the model will classify the pair as a negative pair.
Get Dataset Functions
Because we want to generate pairs of examples for both the training set and the test set, it makes sense to define a function to help us do that. Here, we want to send two images at a time to our model. However, the label will still remain just a single value of either 0 for a negative pair or 1 for a positive pair. Basically we have two images as inputs to our model instead of just one.
Creating the Model
e want to create a model that will take a pair of images as input and give us a binary 0 or 1 output. 1 if the pair of images are both of the digit 0 and output 0 if they are not of the same digit. We are going to use Keras’ functional API to create our model. This is pretty straight-forward to use. Once the model architecture is implemented, all we have to do now is train it on the training data and then evaluate it on the test data. Let’s do that in the next task.
Model Training and Evaluation
Our test set accuracy is almost the same as the validation accuracy, which is good. So, congratulations, we have successfully created and trained a model which uses a custom layer and can tell us if any two images are both of the digit 0 or not. Now, this application may seem pretty useless in the real world but the underlying idea can be quite useful. This technique is actually similar to what is used in face verification systems and we created a simple version of it using VGG Face in our Tensorflow advanced series available on Rhyme. Later in this Google Cloud AI Platform series, we will create a more complex, more practical and production ready face verification system completely from scratch.
We will get embeddings for these images with the help of the trained model. Then, we will reduce the dimensionality of the resulting data to fit just 1 value per example. We are using TSNE to do this dimensionality reduction though we could use other ideas or even our networks final layers linear output before activation. But the idea here is to represent our 16 dimensional vectors on just a single axis. This way, we can visually see if the images of 0s are easily separable from the images of other digits as far as their learned embeddings are concerned.
About the Host (Amit Yadav)
I am a machine learning engineer with focus in computer vision and sequence modelling for automated signal processing using deep learning techniques. My previous experiences include leading chatbot development for a large corporation.