Published 13 October 2022

👋 Hi!! Have you ever wondered how all people in a group photo are automatically labelled in your Phone Gallary? Behind every interactive user interface there are complex algorithm that is used to recognize and label each picture on any given platform. With every picture of ours, we only aid in improving the algorithm’s efficiency.

Image Classification is one of the most widely used algorithms where we see the application of Artificial Intelligence. In this article, we will learn how to use a convolutional neural network to build an ImageClassifiers that helps recognize Images. We will use Keras with TensorFlow to train our ML models to identify Airplanes, Motorbikes & Schooners from Kaggle datasets.


Consider the above image example of what the human and the machine sees. As we see the entire picture, the computer sees an array of pixels. For example, if the image size if 128Ă—128, then the size of the array will be 128x128x3. Here, 128 stands for each height and width, 3 stands for the RGB channel where each colour channel is represented by a separate array. The pixel intensity varies from 0 to 255.
By using multiple Convolutional and Pooling layers, computer extracts the base level features from the images.








Source: Basic CNN Architecture:

Explaination of Basic Algorithm
Neural networks calculate the weighted sums. The calculated sum of weights is passed as input to the activation function in the hidden layers. The activation function is a function to map the inputs to the desired output. It takes the “weighted sum of input” as the input to the function, adds a bias, and decides whether the neuron should be fired or not. The output layer gives the predicted output, and the model output is compared with the actual output. After training the neural network, the model uses the backpropagation method to improve the performance of the network. The cost function helps to reduce the error rate.

Importing libraries

					
#framework that help you build deep neural network
> !pip install keras  
> !pip install tensorflow
> import tensorflow as tf 
						
#tensorflow version
> print(tf.__version__)
>import numpy as np 
>import pandas as pd 
>import os
									
#data virtulisation 
>import matplotlib.image as mpimg 
>import matplotlib.pyplot as plt
>import seaborn as sns
>import cv2 	
>%matplotlib inline

#generate batches of tensor image data with real time data augumentation
>from keras.preprocessing.image import ImageDataGenerator
>from glob import glob	

#mount Drive on Colab
>import cv2
>from google.colab.patches import cv2_imshow

#getiing kaggle dataset directly to Colab
>!pip install -q kaggle

#download datasets that are not listed in the kaggle competitions
>!kaggle datasets download -d maricinnamon/'competition name' -p /content/drive/MyDrive/ --unzip
					
				

Splitting kaggle Datasets into Dev/Valid/Test ratio


>pip install split-folders
>splitfolders.ratio("/content/drive/MyDrive/", output="/content/drive/MyDrive/SplitData",seed=1337, ratio=(.8, .1, .1), group_prefix=None, move=False)

Model Evaluation on differnt CNN models


Model-1 (no pre-processing)






































Model-2 (with pre-processing parameters)






































Model-3 (increase layers(4))















































Model-4 (increase epoch limit(30))
















































Model-5 (increase layers(5))






















































Model-6 (number of neuron=100,200,100(3 layers))










































Model-7 (number of neuron=200,300,100(3 layers))











































Model-8 (shape of conv2d=(32,32,3))












































Model-9: no. of neuron=200,300,100,200,300 layer(5) shape of conv2d=(128,128,1)


















































Time-Differnce between Models











Performance Measures






















Prediction on New Data
				
	
#Making_single_predictions
from tensorflow.keras.utils import load_img,img_to_array

testImage="/content/drive/MyDrive/caltech101-airplanes-motorbikes-schooners/SplitData/test/airplanes/image_0013.jpg"
test_image=load_img(testImage,target_size=(128,128))
test_image=img_to_array(test_image)
test_image=np.expand_dims(test_image,axis=0)
result=classifier.predict(test_image,verbose=0)
img=cv2.imread(testImage)

plt.imshow(img)
plt.title('picture')
plt.show()
print("_"*50)
print('Prediction is: ',Resultmap[np.argmax(result)])
	
	










Further Improvisation

Pruning
Pruning can be used to optimize the number of neurons in the hidden layer and increases computational and resolution performances. It trims the neurons during training, by identifying those which have no impact on the performance of the network. It can also be identified by checking the weights of the neurons, weights that are close to zero have relatively less importance.
Moreover, the number of neurons and number layers required for the hidden layer also depends upon training cases, amount of outliers, the complexity of data that is to be learned, and the type of activation functions used.


Challenges faced

Challenge 1:In Model8 Input Size and input shape was leading to alot of matrix incompatibility.
Solution: Used same pixels for both inputSize and inputShape

Challenge 2: Loading dataset from kaggle datset
Solution: Used kaggle.json to create session between google Colab and kaggle website

Challenge 3: Number of Neurons In Input and Output Layers
Solution:If less number of neurons is chosen it will lead to underfitting and high statistical bias. Whereas if we choose too many neurons it may lead to overfitting, high variance, and increases the time it takes to train the network.


References

[1] Filemon.Warsaw-Poland. Ways to download kaggle data in Google Colab DOI:https://www.kaggle.com/general/74235
[2] Johannes Filter.Release 0.5.1Split folders with files (e.g. images) into train, validation and test (dataset) folders.DOI:https://libraries.io/pypi/split-folders
[3] Jannik ZĂĽrn PhD student in robotics DOI https://jannik-zuern.medium.com/using-a-tpu-in-google-colab-54257328d7da


Contribution over References

-directly mount google drive instead of storing split datasets (default) sample_data folder provided by google colab.
-one epoch takes approximately 25 secs compared to the approx. 7 minutes on the Tesla K80 GPU, resulting in a speedup of almost 17.Thus,Enabling TPU support in notebook


Avatar of Author

Sakshi

Learning never Stops