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 AlgorithmNeural 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)])
PruningFurther Improvisation
Challenge 1:In Model8 Input Size and input shape was leading to alot of matrix incompatibility.Challenges faced
[1] Filemon.Warsaw-Poland. Ways to download kaggle data in Google Colab DOI:https://www.kaggle.com/general/74235References
-directly mount google drive instead of storing split datasets (default) sample_data folder provided by google colab.Contribution over References
Sakshi
Learning never Stops