Image Classification with Convolutional Neural Network(CNN)

#import libraries
import tensorflow as tf
from tensorflow.keras import datasets,layers,models
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)

    2.8.2

#loading cifar10 dataset
(X_train,y_train),(X_test,y_test)=datasets.cifar10.load_data()

    Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
    170500096/170498071 [==============================] - 2s 0us/step
    170508288/170498071 [==============================] - 2s 0us/step

X_train.shape

    (50000, 32, 32, 3)

X_test.shape

    (10000, 32, 32, 3)

X_train[1]
    array([[[154, 177, 187],
        [126, 137, 136],
        [105, 104, 95],
        ...,
        [ 91, 95, 71],
        [ 87, 90, 71],
        [ 79, 81, 70]],

        [[140, 160, 169],
        [145, 153, 154],
        [125, 125, 118],
        ...,
        [ 96, 99, 78],
        [ 77, 80, 62],
        [ 71, 73, 61]],
        [[140, 155, 164],
        [139, 146, 149],
        [115, 115, 112],
        ...,
        [ 79, 82, 64],
        [ 68, 70, 55],
        [ 67, 69, 55]],
        ...,
        [[175, 167, 166],
        [156, 154, 160],
        [154, 160, 170],
        ...,
        [ 42, 34, 36],
        [ 61, 53, 57],
        [ 93, 83, 91]],
        [[165, 154, 128],
        [156, 152, 130],
        [159, 161, 142],
        ...,
        [103, 93, 96],
        [123, 114, 120],
        [131, 121, 131]],
        [[163, 148, 120],
        [158, 148, 122],
        [163, 156, 133],
        ...,
        [143, 133, 139],
        [143, 134, 142],
        [143, 133, 144]]], dtype=uint8)


plt.figure(figsize=(15,2))
plt.imshow(X_train[1])

        <matplotlib.image.AxesImage at 0x7f6ba0a39090>
        img
        

y_train.shape (50000, 1) y_test.shape (10000, 1) X_test.shape (10000, 32, 32, 3) y_train[:5] array([[6], [9], [9], [4], [1]], dtype=uint8) y_train=y_train.reshape(-1, ) y_train[:5] array([6, 9, 9, 4, 1], dtype=uint8) classes=['airplane', 'automobile', 'bird', 'cat', 'deer','dog', 'frog', 'horse', 'ship', 'truck'] classes[9] 'truck' classes[3] 'cat' def plot_sample(X,y,index): #function definition plt.figure(figsize=(15,2)) plt.imshow(X[index]) plt.xlabel(classes[y[index]]) plot_sample(X_train, y_train,15) #function call img

X_train[0] array([[[ 59, 62, 63], [ 43, 46, 45], [ 50, 48, 43], ..., [158, 132, 108], [152, 125, 102], [148, 124, 103]], [[ 16, 20, 20], [ 0, 0, 0], [ 18, 8, 0], ..., [123, 88, 55], [119, 83, 50], [122, 87, 57]], [[ 25, 24, 21], [ 16, 7, 0], [ 49, 27, 8], ..., [118, 84, 50], [120, 84, 50], [109, 73, 42]], ..., [[208, 170, 96], [201, 153, 34], [198, 161, 26], ..., [160, 133, 70], [ 56, 31, 7], [ 53, 34, 20]], [[180, 139, 96], [173, 123, 42], [186, 144, 30], ..., [184, 148, 94], [ 97, 62, 34], [ 83, 53, 34]], [[177, 144, 116], [168, 129, 94], [179, 142, 87], ..., [216, 184, 140], [151, 118, 84], [123, 92, 72]]], dtype=uint8)

# Normalize pixel values to be between 0 and 1
X_train[0]/255

    array([[[0.23137255, 0.24313725, 0.24705882],
        [0.16862745, 0.18039216, 0.17647059],
        [0.19607843, 0.18823529, 0.16862745],
        ...,
        [0.61960784, 0.51764706, 0.42352941],
        [0.59607843, 0.49019608, 0.4 ],
        [0.58039216, 0.48627451, 0.40392157]],
        [[0.0627451 , 0.07843137, 0.07843137],
        [0. , 0. , 0. ],
        [0.07058824, 0.03137255, 0. ],
        ...,
        [0.48235294, 0.34509804, 0.21568627],
        [0.46666667, 0.3254902 , 0.19607843],
        [0.47843137, 0.34117647, 0.22352941]],
        [[0.09803922, 0.09411765, 0.08235294],
        [0.0627451 , 0.02745098, 0. ],
        [0.19215686, 0.10588235, 0.03137255],
        ...,
        [0.4627451 , 0.32941176, 0.19607843],
        [0.47058824, 0.32941176, 0.19607843],
        [0.42745098, 0.28627451, 0.16470588]],
        ...,
        [[0.81568627, 0.66666667, 0.37647059],
        [0.78823529, 0.6 , 0.13333333],
        [0.77647059, 0.63137255, 0.10196078],
        ...,
        [0.62745098, 0.52156863, 0.2745098 ],
        [0.21960784, 0.12156863, 0.02745098],
        [0.20784314, 0.13333333, 0.07843137]],
        [[0.70588235, 0.54509804, 0.37647059],
        [0.67843137, 0.48235294, 0.16470588],
        [0.72941176, 0.56470588, 0.11764706],
        ...,
        [0.72156863, 0.58039216, 0.36862745],
        [0.38039216, 0.24313725, 0.13333333],
        [0.3254902 , 0.20784314, 0.13333333]],
        [[0.69411765, 0.56470588, 0.45490196],
        [0.65882353, 0.50588235, 0.36862745],
        [0.70196078, 0.55686275, 0.34117647],
        ...,
        [0.84705882, 0.72156863, 0.54901961],
        [0.59215686, 0.4627451 , 0.32941176],
        [0.48235294, 0.36078431, 0.28235294]]])

#Normalize X values(X_train and X_test)
X_train=X_train/255
X_test=X_test/255

cnn=models.Sequential([
                      #cnn
                      layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(32,32,3)),
                      layers.MaxPooling2D((2,2)),

                      layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(32,32,3)),
                      layers.MaxPooling2D((2,2)),
                      
                      #dense

                      layers.Flatten(), #i/p layer
                      layers.Dense(64, activation='relu'), #hidden layer
                      layers.Dense(10, activation='softmax') #o/p layer

])

#Compile the CNN model
cnn.compile(optimizer='adam',
                loss='sparse_categorical_crossentropy',
                metrics=['accuracy'])

#Trained CNN model by taking X_train, y_train
cnn.fit(X_train,y_train,epochs=10)


    Epoch 1/10
    1563/1563 [==============================] - 32s 20ms/step - loss: 1.5050 - accuracy: 0.4618
    Epoch 2/10
    1563/1563 [==============================] - 31s 20ms/step - loss: 1.1793 - accuracy: 0.5829
    Epoch 3/10
    1563/1563 [==============================] - 31s 20ms/step - loss: 1.0507 - accuracy: 0.6306
    Epoch 4/10
    1563/1563 [==============================] - 31s 20ms/step - loss: 0.9667 - accuracy: 0.6610
    Epoch 5/10
    1563/1563 [==============================] - 31s 20ms/step - loss: 0.9094 - accuracy: 0.6828
    Epoch 6/10
    1563/1563 [==============================] - 31s 20ms/step - loss: 0.8623 - accuracy: 0.6986
    Epoch 7/10
    1563/1563 [==============================] - 31s 20ms/step - loss: 0.8238 - accuracy: 0.7140
    Epoch 8/10
    1563/1563 [==============================] - 31s 20ms/step - loss: 0.7873 - accuracy: 0.7254
    Epoch 9/10
    1563/1563 [==============================] - 31s 20ms/step - loss: 0.7547 - accuracy: 0.7350
    Epoch 10/10
    1563/1563 [==============================] - 31s 20ms/step - loss: 0.7238 - accuracy: 0.7462
    <keras.callbacks.History at 0x7f6b9c63afd0>

cnn.evaluate(X_test,y_test)

    313/313 [==============================] - 2s 6ms/step - loss: 0.9332 - accuracy: 0.6815
    [0.9331670999526978, 0.6815000176429749]

y_test[:5]
    array([[3],
        [8],
        [8],
        [0],
        [6]], dtype=uint8)


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

    array([3, 8, 8, 0, 6], dtype=uint8)


plot_sample(X_test,y_test,8)

img


y_pred=cnn.predict(X_test) y_pred[:5] array([[3.1555672e-03, 1.8381045e-04, 6.1340368e-04, 8.1873548e-01, 1.5266554e-04, 9.2642970e-02, 3.7672713e-02, 2.2223765e-04, 4.0264670e-02, 6.3564554e-03], [8.1968911e-02, 2.8955036e-01, 2.1545849e-05, 1.1302979e-05, 9.3894369e-06, 8.7148493e-09, 5.5376180e-08, 5.2514201e-07, 6.2506562e-01, 3.3723507e-03], [2.1281379e-01, 1.6645499e-01, 3.2400803e-03, 1.0689645e-02, 2.2049958e-03, 1.7857052e-04, 1.6819979e-03, 7.1567751e-04, 5.6291568e-01, 3.9104566e-02], [8.5628039e-01, 8.8171713e-04, 4.5855913e-02, 2.2154953e-03, 6.1223619e-03, 2.4696726e-06, 7.7176141e-04, 1.0081311e-04, 8.7526225e-02, 2.4288418e-04], [2.7409758e-05, 7.5265685e-05, 6.3377554e-03, 5.9270053e-03, 9.5748788e-01, 6.4907270e-04, 2.9365823e-02, 6.6335131e-05, 4.9083297e-05, 1.4386724e-05]], dtype=float32) np.argmax(y_pred[0]) 3 y_classes=[np.argmax(k) for k in y_pred] #list comprhension y_classes[:5] [3, 8, 8, 0, 4] y_test[:5] array([3, 8, 8, 0, 6], dtype=uint8) plot_sample(X_test,y_test,3) img
classes[y_classes[3]] 'airplane'


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext