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.

Approach:

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.

pexels-photo-257540
pexels-photo-257540

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

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

prediction
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.

 

References:

Share Button

Renuka Joshi

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.

4 thoughts on “Image Classification Using CNN

  • July 20, 2018 at 4:23 pm
    Permalink

    Interesting 🙂

    Need your suggestion:

    classifier.fit_generator(training_set,
    steps_per_epoch = (8000/32),
    epochs = 25,
    validation_data = test_set,
    validation_steps = (2000/32))

    error:

    if not self.built:
    -> 1262 raise RuntimeError(‘The model needs to be compiled ‘
    1263 ‘before being used.’)
    1264 return self.model.fit_generator(generator,

    RuntimeError: The model needs to be compiled before being used.

    Reply
    • July 24, 2018 at 9:18 am
      Permalink

      Hi Vikash,
      Thank you for your comment. To answer your question , have you tried to compile the CNN classifier using clasaifier.compile as given in the article itself. Check very first section of Creating CNN model topic in the article. Hope this helps.

      Reply
  • October 3, 2018 at 9:24 pm
    Permalink

    Hello Renuka,

    I am having problems because I followed the exact same steps, but it’s saying that I’m only passing in 3 arguments into fit_generator. Do you have an idea what the problem might be? The program is running with the Tensorflow backend.

    Thanks,
    Anika

    Reply
    • October 6, 2018 at 8:07 pm
      Permalink

      Hi Anika,

      To answer your question could you please let me know what arguments you are passing to fit_generator?

      Thank you.

      Reply

Leave a Reply

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