<jupyter_start><jupyter_text>Small Image Classification Using Convolutional Neural Network (CNN) In this notebook, we will classify small images cifar10 dataset from tensorflow keras datasets. There are total 10 classes as shown below. We will use CNN for classification<jupyter_code>import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np<jupyter_output><empty_output><jupyter_text>Load the dataset<jupyter_code>(X_train, y_train), (X_test,y_test) = datasets.cifar10.load_data()
X_train.shape
X_test.shape<jupyter_output><empty_output><jupyter_text>Here we see there are 50000 training images and 1000 test images<jupyter_code>y_train.shape
y_train[:5]<jupyter_output><empty_output><jupyter_text>y_train is a 2D array, for our classification having 1D array is good enough. so we will convert this to now 1D array<jupyter_code>y_train = y_train.reshape(-1,)
y_train[:5]
y_test = y_test.reshape(-1,)
classes = ["airplane","automobile","bird","cat","deer","dog","frog","horse","ship","truck"]<jupyter_output><empty_output><jupyter_text>Let's plot some images to see what they are<jupyter_code>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)
plot_sample(X_train, y_train, 1)<jupyter_output><empty_output><jupyter_text>Normalize the images to a number from 0 to 1. Image has 3 channels (R,G,B) and each value in the channel can range from 0 to 255. Hence to normalize in 0-->1 range, we need to divide it by 255 Normalizing the training data<jupyter_code>X_train = X_train / 255.0
X_test = X_test / 255.0<jupyter_output><empty_output><jupyter_text>Build simple artificial neural network for image classification<jupyter_code>ann = models.Sequential([
        layers.Flatten(input_shape=(32,32,3)),
        layers.Dense(3000, activation='relu'),
        layers.Dense(1000, activation='relu'),
        layers.Dense(10, activation='sigmoid')    
    ])

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

ann.fit(X_train, y_train, epochs=5)<jupyter_output>Epoch 1/5
1563/1563 [==============================] - 7s 5ms/step - loss: 1.8603 - accuracy: 0.3354
Epoch 2/5
1563/1563 [==============================] - 8s 5ms/step - loss: 1.6588 - accuracy: 0.4120
Epoch 3/5
1563/1563 [==============================] - 8s 5ms/step - loss: 1.5720 - accuracy: 0.4455
Epoch 4/5
1563/1563 [==============================] - 8s 5ms/step - loss: 1.5097 - accuracy: 0.4680
Epoch 5/5
1563/1563 [==============================] - 8s 5ms/step - loss: 1.4598 - accuracy: 0.4859<jupyter_text>**You can see that at the end of 5 epochs, accuracy is at around 48.48%**<jupyter_code>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))<jupyter_output>Classification Report: 
               precision    recall  f1-score   support

           0       0.58      0.54      0.56      1000
           1       0.51      0.74      0.60      1000
           2       0.39      0.34      0.36      1000
           3       0.34      0.33      0.33      1000
           4       0.65      0.15      0.24      1000
           5       0.39      0.35      0.37      1000
           6       0.60      0.41      0.49      1000
           7       0.33      0.80      0.46      1000
           8       0.71      0.49      0.58      1000
           9       0.57      0.47      0.52      1000

    accuracy                           0.46     10000
   macro avg       0.51      0.46      0.45     10000
weighted avg       0.51      0.46      0.45     10000<jupyter_text>Now let us build a convolutional neural network to train our images<jupyter_code>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)<jupyter_output>Epoch 1/10
1563/1563 [==============================] - 6s 4ms/step - loss: 1.5192 - accuracy: 0.4548
Epoch 2/10
1563/1563 [==============================] - 6s 4ms/step - loss: 1.1484 - accuracy: 0.6002
Epoch 3/10
1563/1563 [==============================] - 6s 4ms/step - loss: 1.0044 - accuracy: 0.6497
Epoch 4/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.9205 - accuracy: 0.6823
Epoch 5/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.8628 - accuracy: 0.7020
Epoch 6/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.8106 - accuracy: 0.7186
Epoch 7/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.7674 - accuracy: 0.7340
Epoch 8/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.7278 - accuracy: 0.7475
Epoch 9/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.6919 - accuracy: 0.7611
Epoch 10/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.658[...]<jupyter_text>**With CNN, at the end 5 epochs, accuracy was at around 70.28% which is a significant improvement over ANN. CNN's are best for image classification and gives superb accuracy. Also computation is much less compared to simple ANN as maxpooling reduces the image dimensions while still preserving the features**<jupyter_code>cnn.evaluate(X_test,y_test)
y_pred = cnn.predict(X_test)
y_pred[:5]
y_classes = [np.argmax(element) for element in y_pred]
y_classes[:5]
y_test[:5]
plot_sample(X_test, y_test,3)
classes[y_classes[3]]
classes[y_classes[3]]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1807742782152231, 'fraction_numerical': 0.10137795275590551, 'mean_word_length': 3.398989898989899, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '161111', 'license': 'MIT', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2039, 'n_words': 527, 'path': "Program's_Contributed_By_Contributors/AI-Summer-Course/py-master/DeepLearningML/16_cnn_cifar10_small_image_classification/cnn_cifar10_dataset.ipynb", 'repo_name': 'siddharthdeo99/Hacktoberfest2k21', 'size': '6096'}