The CIFAR-10 dataset (Canadian Institute For Advanced Research) is a collection of images that are commonly used to train machine learning and computer vision algorithms. It is one of the most widely used datasets for machine learning research. The CIFAR-10 dataset contains 60,000 32x32 color images in 10 different classes. The 10 different classes represent airplanes, cars, birds, cats, deer, dogs, frogs, horses, ships, and trucks. There are 6,000 images of each class.
import numpy as np
import pandas as pd
from packaging import version
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_squared_error as MSE
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import models, layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D, BatchNormalization, Dropout, Flatten, Dense
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras.preprocessing import image
from tensorflow.keras.utils import to_categorical
%matplotlib inline
np.set_printoptions(precision=3, suppress=True)
print("This notebook requires TensorFlow 2.0 or above")
print("TensorFlow version: ", tf.__version__)
assert version.parse(tf.__version__).release[0] >=2
This notebook requires TensorFlow 2.0 or above TensorFlow version: 2.10.0
print("Keras version: ", keras.__version__)
Keras version: 2.10.0
# from google.colab import drive
# drive.mount('/content/gdrive')
def get_three_classes(x, y):
def indices_of(class_id):
indices, _ = np.where(y == float(class_id))
return indices
indices = np.concatenate([indices_of(0), indices_of(1), indices_of(2)], axis=0)
x = x[indices]
y = y[indices]
count = x.shape[0]
indices = np.random.choice(range(count), count, replace=False)
x = x[indices]
y = y[indices]
y = tf.keras.utils.to_categorical(y)
return x, y
def show_random_examples(x, y, p):
indices = np.random.choice(range(x.shape[0]), 10, replace=False)
x = x[indices]
y = y[indices]
p = p[indices]
plt.figure(figsize=(10, 5))
for i in range(10):
plt.subplot(2, 5, i + 1)
plt.imshow(x[i])
plt.xticks([])
plt.yticks([])
col = 'green' if np.argmax(y[i]) == np.argmax(p[i]) else 'red'
plt.xlabel(class_names_preview[np.argmax(p[i])], color=col)
plt.show()
def plot_history(history):
losses = history.history['loss']
accs = history.history['accuracy']
val_losses = history.history['val_loss']
val_accs = history.history['val_accuracy']
epochs = len(losses)
plt.figure(figsize=(16, 4))
for i, metrics in enumerate(zip([losses, accs], [val_losses, val_accs], ['Loss', 'Accuracy'])):
plt.subplot(1, 2, i + 1)
plt.plot(range(epochs), metrics[0], label='Training {}'.format(metrics[2]))
plt.plot(range(epochs), metrics[1], label='Validation {}'.format(metrics[2]))
plt.legend()
plt.show()
def print_validation_report(y_test, predictions):
print("Classification Report")
print(classification_report(y_test, predictions))
print('Accuracy Score: {}'.format(accuracy_score(y_test, predictions)))
print('Root Mean Square Error: {}'.format(np.sqrt(MSE(y_test, predictions))))
def plot_confusion_matrix(y_true, y_pred):
mtx = confusion_matrix(y_true, y_pred)
fig, ax = plt.subplots(figsize=(8,8))
sns.heatmap(mtx, annot=True, fmt='d', linewidths=.75, cbar=False, ax=ax,cmap='Blues',linecolor='white')
# square=True,
plt.ylabel('true label')
plt.xlabel('predicted label')
The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images.
The dataset is divided into five training batches and one test batch, each with 10000 images. The test batch contains exactly 1000 randomly-selected images from each class. The training batches contain the remaining images in random order, but some training batches may contain more images from one class than another. Between them, the training batches contain exactly 5000 images from each class.
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()
print('train_images:\t{}'.format(x_train.shape))
print('train_labels:\t{}'.format(y_train.shape))
print('test_images:\t\t{}'.format(x_test.shape))
print('test_labels:\t\t{}'.format(y_test.shape))
train_images: (50000, 32, 32, 3) train_labels: (50000, 1) test_images: (10000, 32, 32, 3) test_labels: (10000, 1)
print("First ten labels training dataset:\n {}\n".format(y_train[0:10]))
print("This output the numeric label, need to convert to item description")
First ten labels training dataset: [[6] [9] [9] [4] [1] [1] [2] [7] [8] [3]] This output the numeric label, need to convert to item description
(train_images, train_labels),(test_images, test_labels)= tf.keras.datasets.cifar10.load_data()
x_preview, y_preview = get_three_classes(train_images, train_labels)
x_preview, y_preview = get_three_classes(test_images, test_labels)
class_names_preview = ['aeroplane', 'car', 'bird']
show_random_examples(x_preview, y_preview, y_preview)
The labels are an array of integers, ranging from 0 to 9. These correspond to the class of clothing the image represents:
Label | Class_ |
---|---|
0 | airplane |
1 | automobile |
2 | bird |
3 | cat |
4 | deer |
5 | dog |
6 | frog |
7 | horse |
8 | ship |
9 | truck |
class_names = ['airplane'
,'automobile'
,'bird'
,'cat'
,'deer'
,'dog'
,'frog'
,'horse'
,'ship'
,'truck']
x_train_split, x_valid_split, y_train_split, y_valid_split = train_test_split(x_train
,y_train
,test_size=.1
,random_state=42
,shuffle=True)
print(x_train_split.shape, x_valid_split.shape, x_test.shape)
(45000, 32, 32, 3) (5000, 32, 32, 3) (10000, 32, 32, 3)
The images are 28x28 NumPy arrays, with pixel values ranging from 0 to 255
x_train_norm = x_train_split/255
x_valid_norm = x_valid_split/255
x_test_norm = x_test/255
We use a Sequential class defined in Keras to create our model. The first 2 layers Dense handle feature learning. The last layer, handles classification into the 10 classes.
model = Sequential([
Flatten(input_shape=x_train_norm.shape[1:]),
Dense(units=2000,activation=tf.nn.softmax),
Dense(units=2000,activation=tf.nn.softmax),
Dense(units=10, activation=tf.nn.softmax)
])
model.summary()
Model: "sequential_2" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= flatten_2 (Flatten) (None, 3072) 0 dense_6 (Dense) (None, 2000) 6146000 dense_7 (Dense) (None, 2000) 4002000 dense_8 (Dense) (None, 10) 20010 ================================================================= Total params: 10,168,010 Trainable params: 10,168,010 Non-trainable params: 0 _________________________________________________________________
keras.utils.plot_model(model, "CIFAR10.png", show_shapes=True)
You must install pydot (`pip install pydot`) and install graphviz (see instructions at https://graphviz.gitlab.io/download/) for plot_model to work.
In addition to setting up our model architecture, we also need to define which algorithm should the model use in order to optimize the weights and biases as per the given data. We will use stochastic gradient descent.
We also need to define a loss function. Think of this function as the difference between the predicted outputs and the actual outputs given in the dataset. This loss needs to be minimised in order to have a higher model accuracy. That's what the optimization algorithm essentially does - it minimises the loss during model training. For our multi-class classification problem, categorical cross entropy is commonly used.
Finally, we will use the accuracy during training as a metric to keep track of as the model trains.
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("CNN_model.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
Epoch 1/200 704/704 [==============================] - 63s 87ms/step - loss: 2.2930 - accuracy: 0.1301 - val_loss: 2.2398 - val_accuracy: 0.1760 Epoch 2/200 704/704 [==============================] - 75s 106ms/step - loss: 2.1581 - accuracy: 0.1773 - val_loss: 2.1204 - val_accuracy: 0.1734 Epoch 3/200 704/704 [==============================] - 75s 107ms/step - loss: 2.1078 - accuracy: 0.1790 - val_loss: 2.1011 - val_accuracy: 0.1752 Epoch 4/200 704/704 [==============================] - 85s 121ms/step - loss: 2.1034 - accuracy: 0.1802 - val_loss: 2.0886 - val_accuracy: 0.1774 Epoch 5/200 704/704 [==============================] - 115s 164ms/step - loss: 2.0971 - accuracy: 0.1802 - val_loss: 2.0928 - val_accuracy: 0.1738 Epoch 7/200 704/704 [==============================] - 116s 165ms/step - loss: 2.0944 - accuracy: 0.1810 - val_loss: 2.0921 - val_accuracy: 0.1758 Epoch 8/200 704/704 [==============================] - 77s 110ms/step - loss: 2.0319 - accuracy: 0.2244 - val_loss: 2.0515 - val_accuracy: 0.2148 Epoch 13/200 704/704 [==============================] - 77s 109ms/step - loss: 2.0111 - accuracy: 0.2331 - val_loss: 2.0123 - val_accuracy: 0.2296 Epoch 14/200 704/704 [==============================] - 63s 89ms/step - loss: 1.9884 - accuracy: 0.2417 - val_loss: 1.9932 - val_accuracy: 0.2338 Epoch 16/200 704/704 [==============================] - 57s 82ms/step - loss: 1.9817 - accuracy: 0.2411 - val_loss: 1.9961 - val_accuracy: 0.2342 Epoch 17/200 704/704 [==============================] - 54s 76ms/step - loss: 1.9751 - accuracy: 0.2438 - val_loss: 1.9910 - val_accuracy: 0.2336
In order to ensure that this is not a simple "memorization" by the machine, we should evaluate the performance on the test set. This is easy to do, we simply use the evaluate
method on our model.
model = tf.keras.models.load_model("CNN_model.h5")
print(f"Test acc: {model.evaluate(x_test_norm, y_test)[1]:.3f}")
313/313 [==============================] - 4s 13ms/step - loss: 1.9805 - accuracy: 0.2417 Test acc: 0.242
preds = model.predict(x_test_norm)
print('shape of preds: ', preds.shape)
313/313 [==============================] - 4s 12ms/step shape of preds: (10000, 10)
We use Matplotlib to create 2 plots--displaying the training and validation loss (resp. accuracy) for each (training) epoch side by side.
history_dict = history.history
history_dict.keys()
dict_keys(['loss', 'accuracy', 'val_loss', 'val_accuracy'])
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
loss | accuracy | val_loss | val_accuracy | |
---|---|---|---|---|
12 | 2.011 | 0.233 | 2.012 | 0.230 |
13 | 1.995 | 0.238 | 1.996 | 0.240 |
14 | 1.988 | 0.242 | 1.993 | 0.234 |
15 | 1.982 | 0.241 | 1.996 | 0.234 |
16 | 1.975 | 0.244 | 1.991 | 0.234 |
plot_history(history)
Using both sklearn.metrics
. Then we visualize the confusion matrix and see what that tells us.
pred1= model.predict(x_test_norm)
pred1=np.argmax(pred1, axis=1)
313/313 [==============================] - 4s 12ms/step
print_validation_report(y_test, pred1)
Classification Report precision recall f1-score support 0 0.35 0.02 0.03 1000 1 0.30 0.60 0.40 1000 2 0.00 0.00 0.00 1000 3 0.14 0.02 0.03 1000 4 0.00 0.00 0.00 1000 5 0.30 0.39 0.34 1000 6 0.18 0.80 0.29 1000 7 0.20 0.02 0.04 1000 8 0.30 0.56 0.39 1000 9 0.26 0.01 0.03 1000 accuracy 0.24 10000 macro avg 0.20 0.24 0.16 10000 weighted avg 0.20 0.24 0.16 10000 Accuracy Score: 0.2417 Root Mean Square Error: 3.8913750782981587
/Users/apoorvsara/opt/anaconda3/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1318: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /Users/apoorvsara/opt/anaconda3/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1318: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /Users/apoorvsara/opt/anaconda3/lib/python3.9/site-packages/sklearn/metrics/_classification.py:1318: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))
plot_confusion_matrix(y_test,pred1)
model = tf.keras.models.load_model('CNN_model.h5')
preds = model.predict(x_test_norm)
313/313 [==============================] - 4s 13ms/step
preds.shape
(10000, 10)
cm = sns.light_palette((260, 75, 60), input="husl", as_cmap=True)
df = pd.DataFrame(preds[0:20], columns = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'])
df.style.format("{:.2%}").background_gradient(cmap=cm)
airplane | automobile | bird | cat | deer | dog | frog | horse | ship | truck | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 3.27% | 4.21% | 8.64% | 21.83% | 7.57% | 27.24% | 11.09% | 8.62% | 4.10% | 3.44% |
1 | 4.25% | 35.04% | 2.33% | 5.66% | 2.51% | 3.47% | 3.42% | 3.63% | 8.47% | 31.22% |
2 | 27.10% | 10.36% | 6.20% | 3.18% | 3.77% | 2.16% | 1.09% | 4.58% | 28.44% | 13.11% |
3 | 27.10% | 10.36% | 6.20% | 3.18% | 3.77% | 2.16% | 1.09% | 4.58% | 28.44% | 13.11% |
4 | 4.46% | 2.76% | 15.39% | 11.03% | 17.51% | 10.85% | 17.78% | 16.38% | 1.37% | 2.47% |
5 | 4.47% | 2.77% | 15.38% | 11.04% | 17.51% | 10.85% | 17.77% | 16.38% | 1.37% | 2.47% |
6 | 2.51% | 3.61% | 7.01% | 24.24% | 5.71% | 33.50% | 9.30% | 6.71% | 4.46% | 2.94% |
7 | 4.46% | 2.76% | 15.39% | 11.03% | 17.52% | 10.85% | 17.78% | 16.38% | 1.37% | 2.47% |
8 | 4.46% | 2.76% | 15.39% | 11.04% | 17.51% | 10.85% | 17.77% | 16.38% | 1.37% | 2.47% |
9 | 12.15% | 21.54% | 4.93% | 6.00% | 4.04% | 3.96% | 2.80% | 5.47% | 16.68% | 22.42% |
10 | 27.10% | 10.36% | 6.20% | 3.18% | 3.77% | 2.16% | 1.09% | 4.58% | 28.44% | 13.11% |
11 | 4.24% | 35.07% | 2.32% | 5.65% | 2.50% | 3.47% | 3.42% | 3.62% | 8.46% | 31.23% |
12 | 3.23% | 4.21% | 8.53% | 21.97% | 7.45% | 27.53% | 10.98% | 8.51% | 4.15% | 3.44% |
13 | 5.21% | 30.60% | 3.25% | 7.36% | 3.35% | 4.87% | 4.33% | 4.74% | 9.19% | 27.10% |
14 | 25.71% | 11.06% | 6.31% | 3.50% | 3.93% | 2.38% | 1.24% | 4.83% | 27.30% | 13.74% |
15 | 4.47% | 2.77% | 15.38% | 11.05% | 17.49% | 10.87% | 17.76% | 16.37% | 1.37% | 2.48% |
16 | 4.46% | 4.56% | 11.33% | 17.32% | 11.01% | 18.77% | 13.59% | 11.86% | 3.25% | 3.84% |
17 | 4.49% | 2.83% | 15.28% | 11.19% | 17.32% | 11.02% | 17.67% | 16.28% | 1.41% | 2.52% |
18 | 27.02% | 10.40% | 6.21% | 3.19% | 3.78% | 2.17% | 1.10% | 4.60% | 28.39% | 13.14% |
19 | 4.46% | 2.76% | 15.39% | 11.03% | 17.52% | 10.85% | 17.78% | 16.38% | 1.37% | 2.47% |
(_,_), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
img = test_images[2000]
img_tensor = image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
class_names = ['airplane'
,'automobile'
,'bird'
,'cat'
,'deer'
,'dog'
,'frog'
,'horse'
,'ship'
,'truck']
plt.imshow(img, cmap='viridis')
plt.axis('off')
plt.show()
# Extracts the outputs of the top 8 layers:
layer_outputs = [layer.output for layer in model.layers[:8]]
# Creates a model that will return these outputs, given the model input:
activation_model = models.Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(img_tensor)
len(activations)
1/1 [==============================] - 0s 416ms/step
4
layer_names = []
for layer in model.layers:
layer_names.append(layer.name)
layer_names
['flatten_2', 'dense_6', 'dense_7', 'dense_8']