5.0 / 5
We will cover the following tasks in 48 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.
Cats vs Dogs Dataset
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.
Get Random Batch
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.
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.
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.
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.
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.