Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Comparative study on image classification using convolution neural

network(CNN)

Name:-M.Dharani

Reg No:-322010405002

Department of Electronics and Communication Engineering,

GITAM School of Technology, GITAM deemed to be University – Bengaluru

Email:dmandli@gitam.in

Abstract:-
Image classification is a critical task in computer vision that involves categorizing images into
different classes or categories. Convolutional Neural Networks (CNN) have proven to be highly
effective in solving this task. CNNs are a type of deep learning algorithm that utilizes layers of
interconnected filters to learn and extract hierarchical representations of features from the input
images.

The process of image classification using CNN involves two main stages: training and testing.
During the training stage, the CNN model is trained on a large dataset of labeled images to learn
the features and patterns that distinguish between different image classes. This is done by
adjusting the weights of the filters in the CNN through backpropagation of errors.

In the testing stage, the trained model is used to classify new, unlabeled images. The model takes
the image as input and generates a probability distribution over the different classes. The class
with the highest probability is considered to be the predicted class for that image.

The performance of CNN-based image classification models is evaluated using metrics such as
accuracy, precision, recall, and F1 score. These models have been successfully applied to a wide
range of image classification tasks, including object recognition, face recognition, and medical
image analysis.
INTRODUCTION:-
Image classification is an important task in computer vision, which involves identifying the
content of an image and categorizing it into predefined classes or categories. It has numerous
applications in areas such as autonomous vehicles, medical diagnosis, surveillance, and more.
Convolutional Neural Networks (CNNs) have become a popular choice for image classification
due to their ability to learn and extract features automatically from raw image data.

CNNs are a type of deep learning algorithm that is designed to process images as input.
They consist of multiple layers of interconnected filters that can learn and extract hierarchical
features from the input images. The early layers of the CNN learn low-level features such as edges
and lines, while the later layers learn high-level features such as shapes and objects.

The process of image classification using CNNs involves two main stages: training and
testing. During the training stage, the CNN model is trained on a large dataset of labeled images
to learn the features and patterns that distinguish between different image classes. This is done
by adjusting the weights of the filters in the CNN through backpropagation of errors.

In the testing stage, the trained CNN model is used to classify new, unlabeled images.
The model takes the image as input and generates a probability distribution over the different
classes. The class with the highest probability is considered to be the predicted class for that
image.

In recent years, CNNs have achieved state-of-the-art performance in various image classification
tasks, including object recognition, facial recognition, and medical image analysis. The ability of
CNNs to automatically learn and extract features from raw image data has made them a powerful
tool in computer vision, and their potential for future developments is vast.

METHODOLOGY:-

The methodology for image classification using CNN involves the following steps:
1.Data Collection: Collect a large dataset of labeled images for training the CNN model. The
dataset should contain images from each of the target classes, and each image should be labeled
with its corresponding class.

2.Data Preprocessing: Preprocess the collected dataset by resizing the images to a common size,
converting them to grayscale or RGB, and normalizing their pixel values.

3.Model Architecture: Choose a suitable CNN architecture for the image classification task, such
as VGG, ResNet, or Inception. The architecture should have appropriate layers for learning and
extracting features from the images.

4.Model Training: Train the CNN model on the preprocessed dataset using a suitable
optimization algorithm such as Stochastic Gradient Descent (SGD). During the training process,
the model learns to extract features from the input images and minimize the classification loss.

5.Model Evaluation: Evaluate the trained model on a separate test dataset to measure its
performance. Metrics such as accuracy, precision, recall, and F1 score can be used to evaluate
the model's performance.

6.Model Fine-Tuning: Fine-tune the trained model by adjusting its hyperparameters such as
learning rate, batch size, and number of epochs. This process can help to further improve the
model's performance.

7.Model Deployment: Deploy the trained and fine-tuned model for use in real-world
applications. The model can be used to classify new, unlabeled images and generate predictions
for their corresponding classes.

Overall, image classification using CNNs involves collecting and preprocessing data,
choosing a suitable model architecture, training and evaluating the model, fine-tuning its
hyperparameters, and deploying it for real-world applications. The methodology requires a
combination of technical expertise in deep learning and computer vision, as well as domain-
specific knowledge of the image classification task at hand.
FLOW CHART:-
Start

Take the dataset

Training dataset

CNN training

Testing data

CNN generated model

Evaluation of images
classified

End
ALGORITHM:-

Here is a step-by-step algorithm for image classification using CNN:

1.Collect and preprocess the data: Gather images of the objects or patterns you want to classify
and preprocess them. Preprocessing may involve resizing, normalization, and augmentation.
2.Split the data into training and testing sets: Divide the data into a training set (usually around
80% of the data) and a testing set (usually around 20% of the data).
3.Define the CNN model architecture: Create a CNN model with one or more convolutional
layers, pooling layers, and fully connected layers. The number and size of these layers will depend
on the complexity of the task.
4.Compile the model: Set the loss function, optimization algorithm, and evaluation metric for the
model. The loss function measures how well the model performs, the optimization algorithm
adjusts the model parameters to minimize the loss, and the evaluation metric is used to monitor
the model's performance during training.
5.Train the model: Train the model using the training data. During training, the model's
parameters are adjusted to minimize the loss.
6.Evaluate the model: Test the trained model using the testing data to evaluate its accuracy. The
evaluation metric chosen in step 4 is used to measure the model's performance.
7.Fine-tune the model: If the model's performance is not satisfactory, fine-tune the model by
adjusting its architecture or hyperparameters, such as the learning rate, number of epochs, or
number of layers.
8.Use the model for prediction: Once the model is trained and fine-tuned, use it to predict the
labels of new images. The input image is passed through the model, and the output is the
predicted label.
9.Save and deploy the model: Save the trained model to disk so that it can be used later. Deploy
the model in a production environment where it can be used to classify new images.
RESULT:-

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np
(X_train, y_train), (X_test,y_test) = datasets.cifar10.load_data()
X_train.shape
OUTPUT:-

(50000, 32, 32, 3)

X_test.shape
OUTPUT:-

(10000, 32, 32, 3)

y_train.shape
OUTPUT:-

(50000, 1)

y_train[:5]
OUTPUT:-

array([[6],

[9],

[9],

[4],

[1]], dtype=uint8)

y_train = y_train.reshape(-1,)
y_train[:5]
OUTPUT:-

array([6, 9, 9, 4, 1], dtype=uint8)

y_test = y_test.reshape(-1,)
classes = ["airplane","automobile","bird","cat","deer","dog","frog","horse","ship","truck"]
def plot_sample(X, y, index):
plt.figure(figsize = (15,2))
plt.imshow(X[index])
plt.xlabel(classes[y[index]])
plot_sample(X_train, y_train, 0)
OUTPUT:-

plot_sample(X_train, y_train, 1)
OUTPUT:-
X_train = X_train / 255.0
X_test = X_test / 255.0
ann = models.Sequential([
layers.Flatten(input_shape=(32,32,3)),
layers.Dense(3000, activation='relu'),
layers.Dense(1000, activation='relu'),
layers.Dense(10, activation='softmax')
])

ann.compile(optimizer='SGD',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

ann.fit(X_train, y_train, epochs=5)


OUTPUT:-

Epoch 1/5
1563/1563 [==============================] - 194s 124ms/step - loss: 1.8125 - accuracy:
0.3543
Epoch 2/5
1563/1563 [==============================] - 160s 102ms/step - loss: 1.6233 - accuracy:
0.4271
Epoch 3/5
1563/1563 [==============================] - 157s 100ms/step - loss: 1.5418 - accuracy:
0.4558
Epoch 4/5
1563/1563 [==============================] - 156s 100ms/step - loss: 1.4822 - accuracy:
0.4793
Epoch 5/5
1563/1563 [==============================] - 157s 100ms/step - loss: 1.4322 - accuracy:
0.4950
<keras.callbacks.History at 0x7f0ee95aeb80>
from sklearn.metrics import confusion_matrix , classification_report
import numpy as np
y_pred = ann.predict(X_test)
y_pred_classes = [np.argmax(element) for element in y_pred]

print("Classification Report: \n", classification_report(y_test, y_pred_classes))


OUTPUT:-

313/313 [==============================] - 10s 33ms/step


Classification Report:
precision recall f1-score support

0 0.47 0.65 0.54 1000


1 0.65 0.55 0.60 1000
2 0.40 0.30 0.34 1000
3 0.35 0.37 0.36 1000
4 0.39 0.45 0.41 1000
5 0.49 0.25 0.33 1000
6 0.45 0.68 0.54 1000
7 0.67 0.40 0.50 1000
8 0.52 0.69 0.59 1000
9 0.60 0.51 0.55 1000

accuracy 0.48 10000


macro avg 0.50 0.48 0.48 10000
weighted avg 0.50 0.48 0.48 10000
cnn = models.Sequential([
layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),

layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),


layers.MaxPooling2D((2, 2)),

layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
cnn.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

cnn.fit(X_train, y_train, epochs=10)


OUTPUT:-

Epoch 1/10
1563/1563 [==============================] - 81s 51ms/step - loss: 1.4501 - accuracy:
0.4792
Epoch 2/10
1563/1563 [==============================] - 77s 49ms/step - loss: 1.0954 - accuracy:
0.6169
Epoch 3/10
1563/1563 [==============================] - 77s 49ms/step - loss: 0.9766 - accuracy:
0.6610
Epoch 4/10
1563/1563 [==============================] - 77s 49ms/step - loss: 0.8946 - accuracy:
0.6886
Epoch 5/10
1563/1563 [==============================] - 77s 49ms/step - loss: 0.8269 - accuracy:
0.7128
Epoch 6/10
1563/1563 [==============================] - 76s 49ms/step - loss: 0.7738 - accuracy:
0.7307
Epoch 7/10
1563/1563 [==============================] - 77s 49ms/step - loss: 0.7249 - accuracy:
0.7464
Epoch 8/10
1563/1563 [==============================] - 80s 51ms/step - loss: 0.6800 - accuracy:
0.7619
Epoch 9/10
1563/1563 [==============================] - 77s 49ms/step - loss: 0.6398 - accuracy:
0.7766
Epoch 10/10
1563/1563 [==============================] - 72s 46ms/step - loss: 0.6029 - accuracy:
0.7891
<keras.callbacks.History at 0x7f7eba373070>
cnn.evaluate(X_test,y_test)
OUTPUT:-
313/313 [==============================] - 5s 17ms/step - loss: 1.2186 - accuracy: 0.6829
[1.2186319828033447, 0.6829000115394592]
y_pred = cnn.predict(X_test)
y_pred[:5]
313/313 [==============================] - 5s 16ms/step
array([[3.0084918e-04, 5.4282134e-07, 4.5759452e-06, 9.2590648e-01,
3.7519060e-06, 3.5717201e-03, 2.9293802e-05, 3.1066138e-05,
7.0123188e-02, 2.8547529e-05],
[5.5280078e-04, 2.2816366e-06, 8.4219167e-08, 1.6693980e-09,
5.0112609e-12, 7.5212045e-11, 5.7475760e-12, 6.1798087e-13,
9.9944437e-01, 4.4862796e-07],
[3.7822016e-02, 9.4965214e-01, 1.1749586e-04, 3.7938603e-03,
3.8406753e-05, 7.8410245e-05, 4.9003976e-04, 5.9558655e-04,
6.0945824e-03, 1.3172893e-03],
[9.9811763e-01, 1.2394710e-06, 2.9982888e-04, 9.5578245e-05,
2.2156551e-05, 1.0044848e-08, 1.1284504e-07, 1.6290231e-07,
1.4630613e-03, 1.8505996e-07],
[3.5731187e-09, 3.4730049e-11, 1.8573288e-02, 4.3932453e-04,
6.8429786e-01, 4.2895877e-04, 2.9625887e-01, 8.5408373e-12,
1.6812537e-06, 2.4682961e-08]], dtype=float32)
y_classes = [np.argmax(element) for element in y_pred]
y_classes[:5]
OUTPUT:-
[3, 8, 1, 0, 4]
y_test[:5]
OUTPUT:-
array([3, 8, 8, 0, 6], dtype=uint8)
plot_sample(X_test, y_test,3)

classes[y_classes[3]]
OUTPUT:-
‘airplane’

CONCLUSION:-
In conclusion, image classification using Convolutional Neural Networks (CNNs) is a
powerful technique for identifying objects or patterns within an image. It involves collecting and
preprocessing data, creating a CNN model, training and evaluating the model, fine-tuning it if
necessary, and using it to predict the labels of new images.

CNNs are particularly effective for image classification because they can automatically
learn features from raw data, such as edges, corners, and textures, which are then used to
identify objects. This eliminates the need for manual feature extraction, which can be time-
consuming and error-prone.

Furthermore, CNNs can be trained with large datasets, resulting in high accuracy and
robustness. They are also scalable, meaning they can be used for a wide range of image
classification tasks, from simple binary classification to more complex multi-class classification.

Overall, image classification using CNNs has become a critical technology in areas such
as computer vision, image processing, and machine learning, with applications ranging from
medical diagnosis and object recognition to self-driving cars and facial recognition systems.

REFERENCES:-

1.Krizhevsky, A., Sutskever, I., & Hinton, G. E. (2012). ImageNet Classification with Deep
Convolutional Neural Networks. Advances in neural information processing systems, 1097-1105.

2.Simonyan, K., & Zisserman, A. (2014). Very Deep Convolutional Networks for Large-Scale Image
Recognition. arXiv preprint arXiv:1409.1556.

3.LeCun, Y., Bengio, Y., & Hinton, G. (2015). Deep learning. Nature, 521(7553), 436-444.
4.Deng, J., Dong, W., Socher, R., Li, L. J., Li, K., & Fei-Fei, L. (2009). ImageNet: A Large-Scale
Hierarchical Image Database. 2009 IEEE Conference on Computer Vision and Pattern Recognition,
248-255.

5.Geron, A. (2019). Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow:
Concepts, Tools, and Techniques to Build Intelligent Systems. O'Reilly Media, Inc.

6.Chollet, F. (2018). Deep Learning with Python. Manning Publications.

You might also like