Image Classification Using CNN

The following two tabs change content below.
I am a technology enthusiast and always up for challenges. Recently I have started getting hands dirty in machine learning using python and aspiring to gather everything I can.

Latest posts by Renuka Joshi (see all)

Hello all! welcome to another tutorial of deep learning. In previous tutorial we learnt the process of convolutional neural networks. In this tutorial we will perform image classification using CNN. So , let’s get started!

Image Classification Using CNN

Convolutional neural network(CNN) deals with the image processing. Machine learns from the data provided and based on that machine recognizes the images. For example, if we provide a certain set of different images of dogs and cats to convolutional neural network model, it correctly recognizes the image of cat and dog.

Problem Statement:

Our objective is to predict whether an image is of a cat or dog.


The problem statement makes it clear that its a classification problem. Our model should be able to classify image into a cat or dog. Download dataset for CNN from superdatascience  We have learned about convolutional neural networks. We will create our model using following steps:

  • Creating CNN using convolution, max pooling, flattening and full connection
  • Compiling CNN
  • Training model using Keras
  • Predictions

Creating CNN model

We need theano, tensorflow and keras libraries to create convolutional neural networks. Installation steps are given in ANN article. We are now ready to perform image classification using CNN.

Let us understand what is going on here.

  • We’ve initialized a Sequential neural network
  • Then we have added a convolutional layer with Convolution2D object of 32 feature maps of 3 rows and 3 columns. input_shape parameter specifies, we are providing coloured images of 64 X 64 size. Rectifier activation function is applied on first convolutional layer.
  • Then we performed max pooling with 2 X 2 matrix.
  • We added another convolution layer and performed max pooling for more accuracy.
  • Then we performed simple flattening operation.
  • With full connection operation we have initialized input and output layers of neural networks.
  • Then we compiled whole neural network.

Fitting Images to CNN

The above operation is performed so that machine can recognize sheared, zoomed or horizontal versions of same image from training dataset.

Note that we have 10000 images of cats and dogs classified into folders structure as below.

  • dataset
    • training_set(8000)
      • cats
      • dogs
    • test_set(2000)
      • cats
      • dogs

We are going to read images of cats and dogs from training_set and test_set directories. Above code will convert images in 64 X 64 format (because our neural networks accepts 64 X 64 images) in batches of 32.

Here we are fitting training set and test set to neural network to validate the accuracy per epoch. steps_per_epoch are typically number of samples of training set divided by batch size and epochs is the number of iterations to be performed on model. validation_steps are number samples of test set divided by batch size.

Execute the program with ctrl+enter. Let the machine complete all the 25 epochs. While epochs are running you will be able to see the accuracy of training and test set is increasing.

Epoch 23/25

250/250 [==============================] – 113s 451ms/step – loss: 0.3654 – acc: 0.8385 – val_loss: 0.4391 – val_acc: 0.8115

Epoch 24/25

250/250 [==============================] – 114s 458ms/step – loss: 0.3591 – acc: 0.8389 – val_loss: 0.4090 – val_acc: 0.8265

Epoch 25/25

250/250 [==============================] – 112s 450ms/step – loss: 0.3471 – acc: 0.8402 – val_loss: 0.4621 – val_acc: 0.8050

Predicting the Image

After running above code our model is now ready to predict the image provided.

We retrieved class_labels from training set.class_indices.items(). 

{0: ‘cats’, 1: ‘dogs’}

Now, we have downloaded this cute dog from google search.



Copied the file into the same folder of program. Let us see whether our program can do image classification using CNN.

We’ve read image from folder, resized it to 64 X 64 array, expanded dimensions of image to be compatible with neural network and predicted result, let us see the prediction

Out[66]: {0: ‘cats’, 1: ‘dogs’}

Out[67]: array([[1]])

From above we can see that our machine has predicted the image of the cute little puppy and classified it as a dog. I hope this article helped understand image classification using CNN.



Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *