5.0 / 5
We will cover the following tasks in 51 minutes:
We will understand the Rhyme interface and our learning environment. You will get a virtual machine, you will need Jupyter Notebook and TensorFlow for this course and both of these are already installed on your virtual machine. Jupyter Notebooks are very popular with Data Science and Machine Learning Engineers as one can write code in cells and use other cells for documentation. In this project, we will use the concept of transfer learning and apply it to a binary image classification problem. We will create a model that will be able to distinguish between Glasses and Tables. This dataset is available on GitHub and was published by Turkish Engineer Muhammed Buyukkinaci.
Start by importing the ResNet50 model. We will also need the Sequential class from Keras. We will also import the Dense layer from Keras. We will use this to create a new output layer. And as always, we will import NumPy - the fundamental package for scientific computing in Python.
Create and Compile a New Model
The first layer is going to be our ResNet50 model WITH weights set to the pre-trained weights from ImageNet training. We will set include top to false. This means that the final dense layer with the 1000 outputs is not going to be used. Instead, we will apply global average pooling to the output of the last convolutional layer in the ResNet model and that will make our output of the ResNet layer a 2D tensor. Then, we will add the output layer, which is a dense layer, which has just 2 nodes. Technically, we don’t really need the 2 nodes either in binary classification and just 1 node will do. But, for this example, we will use 2 nodes and you can apply the same concept to multi-class classification problems as well.
We will use the
categorical cross entropy for our loss function and we will use the common
stochastic gradient descent algorithm for the optimizer. We will use only one training metric and that will be accuracy.
Image Data Generator
We will use the
ImageDataGenerator class from the image preprocessing library of Keras. We will create two data generators with the help of this class - one for our training set and another one for our validation set. We will set the preprocessing function of the
ImageDataGenerator class to the Resnet50
preprocess_input method that we imported earlier.
We will create a data generator for the training dataset and another one for the validation dataset. We can use
flow_from_directory method and specify the data directory, the target size, and the class mode for each data generator.
Train the Model
The first layer, which is the ResNet50 layer, is already trained in the ImageNet dataset and we are not training that layer. We are only asking the model to learn the 4098 parameters it needs to learn for all the connections between our output layer and the last layer of the ResNet50 model.
Since we are using the image data generator, we need to use a variant of the fit method called
Specify the train generator for the training data, validation generator for validation data, validation steps, steps per epoch. And of course, specify epochs - which is an iteration over the entire data provided.
Prepare Test Images
Let’s define a function called
prepare images. This will take paths to test images as input. This function will first create an array of images. We will use Keras’ image preprocessing and load two helper functions from there. One function to help us load images and other one to help us convert those images to NumPy arrays. Such a NumPy array can then be fed into our preprocess input method that we imported from Keras’ ResNet module earlier. This will create a suitable format to be fed into our model.
Make Predictions on Test Images
We will create a function which will show the prediction for every test image that we pass into it along with a prediction on that image. Since we have only two classes, the prediction will simply be if the given image is a glass or a table.
We will use the
prepare images function which will return test data in an appropriate format. Then we make the predictions by using the predict method on our model. Next, we just have to print out the prediction. But remember, we will need to get the index of the highest confidence score and we will print out the corresponding class name.
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.