Deep Learning

Build an image classifier under 5 minutes

By April 12, 2019 No Comments

In this blog, I will explain how you can build your own Image Classifier. I will be doing this with the help of a powerful neural network architecture called the Convolutional Neural Network (CNN).

If you guys have no idea about what I am talking about, CHILL OUT, neither do I… just joking.

You can learn all the concepts of CNN from scratch in this blog.

I definitely encourage you to read the above blog, because it will help you understand what CNN is and other stuff related to it. I have also explained the math for you guys to understand the core of CNN.

Without further delay, let’s build an Image Classifier. This will not take more than 5 minutes.
YEAH, WHAT YOU READ IS CORRECT… LESS THAN 5 MINUTES.

OUR MISSION:

Our mission should we choose to accept it, is to build an Image Classifier to distinguish between a dog and a cat.

Mip

TECHNOLOGY STACK:

I will be using Python-3 for programming. Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales.

I will be using Keras for building the neural network. Keras is an open-source neural-network library written in Python. It is capable of running on top of TensorFlow, Microsoft Cognitive Toolkit, or Theano. Designed to enable fast experimentation with deep neural networks, it focuses on being user-friendly, modular, and extensible.

TRAINING IMAGE:

We will need a set of images to teach the neural network about the new classes you want to recognize. We will use dog and cat photos to initially train. Download the photos using Google image search and a Chrome extension called Fatkun Batch Download Image. This allows you to download all the images in a particular tab.

Save the image in a separate folder named ‘dog and cat’. It’s recommended that we use at least 1000 images in each folder for training.

If you guys have any problem doing this, please don’t hesitate to ping me. I will put all the pictures of dogs and cats that I have downloaded on the cloud so you can download and use them.

pictures of dogs and cats

CODE:

STEP 1: Importing the Keras libraries and packages.
Obviously, the first step of any coding is the importing the libraries.

The Keras library helps us build our convolutional neural network. We import a sequential model, which is a pre-built Keras model, where you can just add the layers. We import the convolution and pooling layers. We also import dense layers, as they are used to predict the labels.

The dropout layer reduces over fitting and the flattening layer expands a three-dimensional vector into a one-dimensional vector. Finally, we import numpy for matrix operations.

If you find it hard to understand any of this, I would firmly request you to go read the blog that I have mentioned above. I have explained all these concepts in that blog, both conceptually and mathematically.

STEP 2: Initializing the CNN.

This is where the architecture of CNN is built. The first step of initializing the CNN is done by creating a convolution layer, and Keras makes it so simple to build a Convolution layer.
All it takes is just a line of code and there you go, you will have a convolutional layer. It is in this layer that we specify:

  • – The number of filters to be applied (e.g. 32)
  • – The dimension of each filter (e.g. 3×3)
  • – The input dimension of the image (e.g. 64,64,3)
  • – The activation function (e.g. “relu”)

image classifier

The second step is specifying the pooling layer. The pooling operation is generally used to reduce the size of the inputs, hence speeding up the computation. Again, Keras makes it so simple to do a Max Pooling. In this, all we have to specify is the pooling size.

Pooling layer

The third step is called Flattening. In this step, we expand a three-dimensional vector into a one-dimensional vector. Again, in Keras it just a line of code and there you have it.

Flattening

The final step is to create a fully connected layer from the previously flattened layer. The fully connected layers are usually the last few layers of a CNN. These would act identically to the layers in a traditional deep neural network.

Fully connected layers

COMPILING THE NEURAL NETWORK:

In this step, we specify the other parameters that will be used to train the Neural Network. Some of the vital parameters are Optimizer, Loss Function, and Metric. I will not be explaining these things here, because the concepts need a separate blog to be understood.

Fitting the CNN to the images:

This step is where we actually split the image dataset to train and test the dataset, and give it as an input to the model. So the CNN can train itself on the training dataset, and test it on the test dataset.

Keras, again, makes it easy for us to do this. Instead of manually splitting the dataset into train and test, all we have to do is call a function which automatically does it for us.

Once this is executed, you can actually see the model training.

Model training

Testing our Classifier:

This is the fun part, because this is going to tell us how good our model is at distinguishing the dog and cat.

Congratulations, you’ve taken your first steps into a larger world of deep learning!

Deep learning

Joish Bosco

Joish Bosco

Joish J Basco is a Software Engineer and Experienced in Python and Javascript with a touch of Sarcasm. He also works in Machine Learning, Deep Learning, and Computer Vision. Apart from the technical stuff, he is also an Excellent Listener, Good Speaker, and a practicing Mentalist.