CNN

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

6/8/23, 3:24 PM CNN

Image classification on cifar 10 dataset

The CIFAR-10 dataset contains 60,000 color images of 32 x 32 pixels in 3 channels divided
into 10 classes. Each class contains 6,000 images. The training set contains 50,000 images,
while the test sets provides 10,000 images.

CNN works on three stages

1. first stage a convoluation layer extracts the features of images or data.

2. in second stage a pooling layer reduces the dimensionality of image

3. third stage a flattening layer transforms our model in one dimension and feed it to fully
connected dense layer.dense layer then performs prediction of image.

In neural network, there are two generally used APIs.

1. Sequential API
2. Functional API

sequential API allows us to create a model layer wise and add it to sequential class.

the draw back of sequential api is we cannot use it to create a model where we want to use
multiple sources and get output at different location

to overcome this drawback we use functional api.

functional api we can create multiple input and output model

we are using cnn, so we will be using a convolution layer. the most common used and layer
we are using conv2D means convolution takes place on 2 axis.and another type is Conv1D.
conv1d is generally for "texts" and conv2d gerally used for "images"

file:///C:/Users/Admin/Downloads/CNN.html 1/6
6/8/23, 3:24 PM CNN

1. first parameter is "filter". value of the filters show the number of filters from which the
CNN model and the convoluation model learn from.

2. "kernel-size". kernel means a filter which will move through the image and extract
features of the part using a dot product. kernel size means dimension of filter. the value
of kernel size generally an odd number 3,5,7. we have used kernel size 3. which means
the filter size is of 3*3

3. "padding". there are two types of padding same and valid.

in valid padding, there is no padding of zeros on the boundary of images

in same padding, there is alyers of zeros padded on all the boundary of image.so there is no
loss of data.

There are 4 famous activation functions:

1. Sigmoid function: The value range is between 0 to 1. The graph is a steep graph, so
even a small change can bring a big difference. It is mainly used for binary classification,
as demarcation can be easily done as value above or below 0.5.

2. TanH function: It is abbreviation of Tangent Hyperbolic function. It is a derived function


of Sigmoid function. The mathematics behind these activation function is out of the
scope of this article, so I would not jump there. The range of the value is between -1 to
1.

3. ReLu function: It is the abbreviation of Rectified Linear Unit. It is the most famous
activation of deep learning. It is famous because it is easier to compute since the
mathematical function is easier and simple than other activation functions.

4. SoftMax function: SoftMax function is more elucidated form of Sigmoid function. It is


used for multi-class classification. The function calculates the probabilities of a particular
class in a function. Thus the output value range of the function is between 0 to 1. The
primary difference between Sigmoid function and SoftMax function is, Sigmoid function
can be used for binary classification while the SoftMax function can be used for Multi-
Class Classification also.

Pooling layer is used to reduce the size of the image along with keeping the important
parameters in role. Thus it helps to reduce the computation in the model.

Flattening Layer is added after the stack of convolutional layers and pooling layers.
Flattening layer converts the 3d image vector into 1d. Because after the stack of layers,
mentioned before, a final fully connected Dense layer is added. Now the Dense layer
requires the data to be passed in 1dimension, so flattening layer is quintessential.

file:///C:/Users/Admin/Downloads/CNN.html 2/6
6/8/23, 3:24 PM CNN

Import All the required Library

In [1]: import keras


from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
import os

import numpy as np

import seaborn as sns


import matplotlib
import matplotlib.pyplot as plt

from sklearn.metrics import confusion_matrix, classification_report


import itertools
import tensorflow as tf
%matplotlib inline

In [2]: # setting class names


class_names=['airplane','automobile','bird','cat','deer','dog','frog','horse','ship
#loading the dataset
(x_train,y_train),(x_test,y_test)=cifar10.load_data()

Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz


170498071/170498071 [==============================] - 6s 0us/step

Normalizing the Images

In [3]: x_train=x_train/255.0
x_train.shape

Out[3]: (50000, 32, 32, 3)

In [4]: x_test=x_test/255.0
x_test.shape

Out[4]: (10000, 32, 32, 3)

In [5]: plt.imshow(x_test[220])

Out[5]: <matplotlib.image.AxesImage at 0x7fb34f6fb190>

file:///C:/Users/Admin/Downloads/CNN.html 3/6
6/8/23, 3:24 PM CNN

In [6]: cifar10_model=tf.keras.models.Sequential()

In [7]: # First Layer


cifar10_model.add(tf.keras.layers.Conv2D(filters=32,kernel_size=3,padding="same", a

# Second Layer
cifar10_model.add(tf.keras.layers.Conv2D(filters=32,kernel_size=3,padding="same", a

# Max Pooling Layer


cifar10_model.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2,padding='valid'))

# Third Layer
cifar10_model.add(tf.keras.layers.Conv2D(filters=64,kernel_size=3,padding="same", a

# Fourth Layer
cifar10_model.add(tf.keras.layers.Conv2D(filters=64,kernel_size=3,padding="same", a

# Max Pooling Layer


cifar10_model.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2,padding='valid'))

# Flattening Layer
cifar10_model.add(tf.keras.layers.Flatten())

file:///C:/Users/Admin/Downloads/CNN.html 4/6
6/8/23, 3:24 PM CNN

# Droput Layer
cifar10_model.add(tf.keras.layers.Dropout(0.5,noise_shape=None,seed=None))

# Adding the first fully connected layer


cifar10_model.add(tf.keras.layers.Dense(units=128,activation='relu'))

# Output Layer
cifar10_model.add(tf.keras.layers.Dense(units=10,activation='softmax'))

cifar10_model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 32, 32, 32) 896

conv2d_1 (Conv2D) (None, 32, 32, 32) 9248

max_pooling2d (MaxPooling2D (None, 16, 16, 32) 0


)

conv2d_2 (Conv2D) (None, 16, 16, 64) 18496

conv2d_3 (Conv2D) (None, 16, 16, 64) 36928

max_pooling2d_1 (MaxPooling (None, 8, 8, 64) 0


2D)

flatten (Flatten) (None, 4096) 0

dropout (Dropout) (None, 4096) 0

dense (Dense) (None, 128) 524416

dense_1 (Dense) (None, 10) 1290

=================================================================
Total params: 591,274
Trainable params: 591,274
Non-trainable params: 0
_________________________________________________________________

In [8]: cifar10_model.compile(loss="sparse_categorical_crossentropy", optimizer="Adam", met

In [9]: cifar10_model.fit(x_train,y_train,epochs=15)

file:///C:/Users/Admin/Downloads/CNN.html 5/6
6/8/23, 3:24 PM CNN

Epoch 1/15
1563/1563 [==============================] - 21s 6ms/step - loss: 1.4049 - sparse_
categorical_accuracy: 0.4930
Epoch 2/15
1563/1563 [==============================] - 8s 5ms/step - loss: 1.0030 - sparse_c
ategorical_accuracy: 0.6453
Epoch 3/15
1563/1563 [==============================] - 8s 5ms/step - loss: 0.8437 - sparse_c
ategorical_accuracy: 0.7018
Epoch 4/15
1563/1563 [==============================] - 9s 6ms/step - loss: 0.7508 - sparse_c
ategorical_accuracy: 0.7353
Epoch 5/15
1563/1563 [==============================] - 9s 6ms/step - loss: 0.6877 - sparse_c
ategorical_accuracy: 0.7550
Epoch 6/15
1563/1563 [==============================] - 8s 5ms/step - loss: 0.6306 - sparse_c
ategorical_accuracy: 0.7756
Epoch 7/15
1563/1563 [==============================] - 10s 6ms/step - loss: 0.5807 - sparse_
categorical_accuracy: 0.7948
Epoch 8/15
1563/1563 [==============================] - 9s 6ms/step - loss: 0.5484 - sparse_c
ategorical_accuracy: 0.8052
Epoch 9/15
1563/1563 [==============================] - 8s 5ms/step - loss: 0.5129 - sparse_c
ategorical_accuracy: 0.8183
Epoch 10/15
1563/1563 [==============================] - 9s 6ms/step - loss: 0.4854 - sparse_c
ategorical_accuracy: 0.8264
Epoch 11/15
1563/1563 [==============================] - 9s 6ms/step - loss: 0.4634 - sparse_c
ategorical_accuracy: 0.8343
Epoch 12/15
1563/1563 [==============================] - 8s 5ms/step - loss: 0.4398 - sparse_c
ategorical_accuracy: 0.8431
Epoch 13/15
1563/1563 [==============================] - 9s 6ms/step - loss: 0.4228 - sparse_c
ategorical_accuracy: 0.8500
Epoch 14/15
1563/1563 [==============================] - 9s 6ms/step - loss: 0.4038 - sparse_c
ategorical_accuracy: 0.8549
Epoch 15/15
1563/1563 [==============================] - 8s 5ms/step - loss: 0.3837 - sparse_c
ategorical_accuracy: 0.8619
Out[9]: <keras.callbacks.History at 0x7fb34614de70>

In [10]: test_loss, test_accuracy = cifar10_model.evaluate(x_test, y_test)

313/313 [==============================] - 1s 3ms/step - loss: 0.6674 - sparse_cat


egorical_accuracy: 0.7880

In [11]: print("Test accuracy: {}".format(test_accuracy))

Test accuracy: 0.7879999876022339

file:///C:/Users/Admin/Downloads/CNN.html 6/6

You might also like