Image Classification Using Convolutional Neural Networks: A step by step guide
This article was published as a part of the Data Science Blogathon.
Convolutional Neural Networks come under the subdomain of Machine Learning which is Deep Learning. Algorithms under Deep Learning process information the same way the human brain does, but obviously on a very small scale, since our brain is too complex (our brain has around 86 billion neurons).
Why CNN for Image Classification?
Image classification involves the extraction of features from the image to observe some patterns in the dataset. Using an ANN for the purpose of image classification would end up being very costly in terms of computation since the trainable parameters become extremely large.
For example, if we have a 50 X 50 image of a cat, and we want to train our traditional ANN on that image to classify it into a dog or a cat the trainable parameters become –
(50*50) * 100 image pixels multiplied by hidden layer + 100 bias + 2 * 100 output neurons + 2 bias = 2,50,302
We use filters when using CNNs. Filters exist of many different types according to their purpose.
Filters help us exploit the spatial locality of a particular image by enforcing a local connectivity pattern between neurons.
Convolution basically means a pointwise multiplication of two functions to produce
a third function. Here one function is our image pixels matrix and another is our filter. We slide the filter over the image and get the dot product of the two matrices. The resulting matrix is called an “Activation Map” or “Feature Map”.
There are multiple convolutional layers extracting features from the image and finally the output layer.
Now there are a lot of other things such as channels, pooling, etc which go into the depth of the theory. Here we will concentrate on the practical.
PRACTICAL: Step by Step Guide
I will be working on Google Colab and I have connected the dataset through Google Drive, so the code provided by me should work if the same setup is being used. Remember to make appropriate changes according to your setup.
Step 1: Choose a Dataset
Choose a dataset of your interest or you can also create your own image dataset for solving your own image classification problem. An easy place to choose a dataset is on kaggle.com.
The dataset I’m going with can be found here.
This dataset contains 12,500 augmented images of blood cells (JPEG) with accompanying cell type labels (CSV). There are approximately 3,000 images for each of 4 different cell types grouped into 4 different folders (according to cell type). The cell types are Eosinophil, Lymphocyte, Monocyte, and Neutrophil.
Here are all the libraries that we would require and the code for importing them.
from keras.models import Sequential
import tensorflow as tf import tensorflow_datasets as tfds tf.enable_eager_execution()
from keras.layers.core import Dense, Activation, Dropout, Flatten from keras.layers.convolutional import Convolution2D, MaxPooling2D from keras.optimizers import SGD, RMSprop, adam from keras.utils import np_utils from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier from sklearn import metricsfrom sklearn.utils import shuffle from sklearn.model_selection import train_test_splitimport matplotlib.image as mpimg import matplotlib.pyplot as plt import numpy as np import os import cv2 import randomfrom numpy import * from PIL import Image import theano
Step 2: Prepare Dataset for Training
Preparing our dataset for training will involve assigning paths and creating categories(labels), resizing our images.
Resizing images into 200 X 200
path_test = "/content/drive/My Drive/semester 5 - ai ml/datasetHomeAssign/TRAIN"
CATEGORIES = ["EOSINOPHIL", "LYMPHOCYTE", "MONOCYTE", "NEUTROPHIL"] print(img_array.shape)IMG_SIZE =200 new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
Step 3: Create Training Data
Training is an array that will contain image pixel values and the index at which the image in the CATEGORIES list.
training = def createTrainingData(): for category in CATEGORIES: path = os.path.join(path_test, category) class_num = CATEGORIES.index(category) for img in os.listdir(path): img_array = cv2.imread(os.path.join(path,img)) new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) training.append([new_array, class_num])createTrainingData()
Step 4: Shuffle the Dataset
Step 5: Assigning Labels and Features
This shape of both the lists will be used in Classification using the NEURAL NETWORKS.
X = y =for features, label in training: X.append(features) y.append(label) X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 3)
Step 6: Normalising X and converting labels to categorical data
X = X.astype('float32') X /= 255 from keras.utils import np_utils Y = np_utils.to_categorical(y, 4) print(Y) print(shape(Y))
Step 7: Split X and Y for use in CNN
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 4)
Step 8: Define, compile and train the CNN Model
batch_size = 16 nb_classes =4 nb_epochs = 5 img_rows, img_columns = 200, 200 img_channel = 3 nb_filters = 32 nb_pool = 2 nb_conv = 3
model = tf.keras.Sequential([ tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu, input_shape=(200, 200, 3)), tf.keras.layers.MaxPooling2D((2, 2), strides=2), tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu), tf.keras.layers.MaxPooling2D((2, 2), strides=2), tf.keras.layers.Dropout(0.5), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation=tf.nn.relu), tf.keras.layers.Dense(4, activation=tf.nn.softmax) ])
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy']) model.fit(X_train, y_train, batch_size = batch_size, epochs = nb_epochs, verbose = 1, validation_data = (X_test, y_test))
Step 9: Accuracy and Score of model
score = model.evaluate(X_test, y_test, verbose = 0 ) print("Test Score: ", score) print("Test accuracy: ", score)
In these 9 simple steps, you would be ready to train your own Convolutional Neural Networks model and solve real-world problems using these skills. You can practice these skills on platforms like Analytics Vidhya and Kaggle. You can also play around by changing different parameters and discovering how you would get the best accuracy and score. Try changing the batch_size, the number of epochs or even adding/removing layers in the CNN model, and have fun!
The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.
Leave a Reply Your email address will not be published. Required fields are marked *