Main navigation

Menu
Add To Bookmark

How to Classify an image using CNN?


By NIIT Editorial

Published on 07/07/2021

8 minutes

Convolutional Neural Network (CNN) is the cornerstone of image classification. Basically, taking an image and assigning it as a class and a unique label as a deep learning phenomenon is included in CNN. An imperative part of machine learning experiments is formed because of Image classification via CNN.

From the picture tagging feature of Facebook to Product recommendations of Amazon, from functioning of automatic cars to healthcare imagery – CNN is extensively used for a number of applications. The requirement of minimal preprocessing is what makes CNN widely popular. In simpler words, the 2D images are readable due to the application of filters. Apparently, the other conventional algorithms are not able to do so.

Functioning of CNN

Also known as ConvNets, the Convolutional neural networks were initially introduced by Yann LeCun ( a postdoctoral computer science researcher) in the 1980s. The early version of CNNs, also known as LeNet, has the ability to recognise handwritten digits. A niche market of CNNs can be seen in postal services and banking.

A CNN is usually equipped with input, output and hidden layers, enabling the process to move further and helping in classifying the image. Some of the imperative layers namely ReLU layers, convolutional layers, fully connected layers and pooling layers are a part of the hidden layers.

Classifying an image using CNN programming

Keep-on reading to know about how you can classify an image using CNN:

Firstly, let’s write the description about what the program will include so that it is clear what this program actually does.

# Description: This program classifies images

Secondly, make sure that the installation of the dependencies and packages has been done. If the packages aren’t installed already then you need to run a few things like command in your terminal, Google Colab website or command prompt. Notice that you should know the location of the installation of the python programming language.

pip install tensorflow keras numpy skimage matplotlib

Importing the libraries should be done as follows:

import tensorflow as tf

from tensorflow import keras

from keras.models import Sequential

from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D

Dropout

from tensorflow.keras import layers

from keras.utils import to categorical

import numpy as np

import matplotlib.pyplot as plt

plt.style.use (‘fivethirtyeight’)

The next step is to load the data set into the variables x.train (the variable containing the images to train on), y.train (the variable containing the images’ labels in the training set), x.test (the variable containing the images to train on) and the y.test (the variable containing the images’ labels in the training set).

#Load the data

from keras.datasets import cifar10

(a.bus, b.bus), (a.test, b.test) = cifar10.load_data()

Explore the Data

Following is the required coding:

print ( (type (x.train), (“,”), (type (y.train) (type (x.test), (“,”), (type (y.test) )

Furthermore, you need to fetch the shape of the x.train, y.train, x.test and y.test data. Once you are done with this, you may observe a 4-dimensional array in the data set of x.train. The rows are 50,000 along with 32*32 pixels of image along with a depth of 3 (RGB). Here, R denotes Red, G denotes Green, and B stands for Blue.

You will find a 2-Dimensional array of the y.train data shape having 50,000 rows and 1 column. Similarly, the x.test data set has a 4-dimensional array along with 10,000 rows and 32*32 pixel image with a depth of 3 (RGB). The data shape of y.test is a 2-Dimensional array with 10,000 rows and 1 column.

The code is as follows:

#Get the shape of x.train

print (‘x.train shape: ‘, x.train.shape)

#Get the shape of y.train

print (‘y.train shape: ‘, x.test.shape)

#Get the shape of x.train

print (‘x.test shape : ‘, x.test.shape)

#Get the shape of y.train

print (‘y test shape : ‘, y.test.shape)

The image that will be generated would be in the training data set like a numpy array. An image would be generated as a series of pixel values.

Index = 0

x.train [index]

Image = plt.imshow (x train [index])

The image label is: [6]

The label classification in relation to the number is as follows:

Classify = [ ‘bird’, ‘airplane’, ‘cat’, ‘frog’, ‘automobile’, ‘deer’, ‘ship’, ’dog’, ‘truck’ ]

#Print the image class print (‘The image class is: ‘, classify [y.train [index]  [0] ] )

You can use One-Hot Encoding for converting the given labels into a set of 10 numbers to an input and finally into the neural network. The course’s number corresponds with the number of labels for classifying the images.

y.train_one_hot = to_categorical (y_train)

y.test_one_hot = to_categorical (y_test)

Once this code is written, you can print the new labels into the training data set.

Print (y.train_one_hot)

Now, normalizing the pixels is required for the images to be in the value between 0 and 1, while the normal values lie between 0 and 255, this will help in forming the neural network.

x.train = x.train / 255

x.test = x.test / 255

Building the Convolution Neural Network Model

For creating the model we need to create the architecture with the help of Sequential ()

modl = Sequential ()

After this, add the first convolution layer in order to extract the input image’s features while creating 32 5*5 ReLu convoluted features which are also called feature maps. As it is the first layer we should know the input’s dimension shape is 32*32 pixel image with a depth of 3 (RGB).

modl.add (Conv2D (32, (5,5), activation = ‘relu’,

input_shape = (32,32,3) ) )

The next layer will be a pooling layer along with a 2*2 pixel filter for fetching that max element from the feature map. This leads to a reduction in the dimension of the feature maps by half, also known as sub sampling.

 

modl.add (MaxPooling2D (pool_size = (2, 2) ) )

Now you have to create another convolution layer along with pooling layer without including the input_shape.

modl.add (Conv2D (64,  (5, 5),  activation = ‘relu’ ) )

modl.add (MaxPooling2D (pool size = (2,  2) ) )

Now adding the flattening layer will help in reducing the image into a linear array which is commonly known as a one 1-Dimension vector to feed into along with connecting with the neural network.

Modl.add (Flatten () )

Create a neural network now. Here the first layer has 1000 neurons and the activation function ReLu.

modl.add ( Dense (1000, activation = ‘relu’ ) )

Adding a drop out layer is required with a 50% drop out

modl.add ( Dropout (0.5) ) 

Creating a neural network with the first layer having 500 neurons and the activation function ReLu.

modl.add ( Dense ( 500, activation = ‘relu’ ) )

Now adding a drop out layer with 50% drop out.

modl.add (Dropout ( 0.5 ) )

Creating a neural network wherein the first layer has 250 neurons and those neurons in the first layer have 250 neurons and the activation function ReLu.

modl.add ( Dense ( 250, activation = ‘relu’ ) )

Now creating the last layer of this neural network with only 10 neurons which will be one for each label. Use the softmax function.

modl.add ( Dense (10, activation = ‘softmax’ ) )

This is how the CNN model should be looking like:

modl = Sequential ( ) modl.add ( Conv2D ( 32,  (5, 5 ) , activation = ‘relu’ , input_shape = ( 32, 32, 3 ) ) )

 modl.add (MaxPooling2D (pool_size = (2, 2 ) ) )

modl.add ( Conv2D ( 64,   (5,  5) , activation = ‘relu’ ) ) modl.add (MaxPooling2D (pool_size = (2, 2 ) ) )

modl.add (Flatten () )

modl.add (Dense (1000, activation = ‘relu’ ) )

modl. Add ( Dropout (0.5) )

modl.add (Dense (500, activation = ‘relu’ ) )

modl.add (Dropout ( 0.5) )

modl.add (Dense (250, activation = ‘relu’) )

modl.add (Dense (10, activation = ‘softmax’ ) )

Above mentioned is how one can classify an image using CNN. If you are more keen to learn the intricacies, then have a look at the Advanced Post Graduate Program in Data Science and Machine Learning offered by NIIT. It is a customised course especially designed for students having a flair in coding languages, and an interest in data science.

 



Advanced PGP in Data Science and Machine Learning (Full Time)

Become an industry-ready StackRoute Certified Data Science professional through immersive learning of Data Analysis and Visualization, ML models, Forecasting & Predicting Models, NLP, Deep Learning and more with this Job-Assured Program with a minimum CTC of ₹5LPA*.

Job Assured Program*

Practitioner Designed

Top