MSDS458 Research Assignment 02 - Part 1¶

More Technical: Throughout the notebook. This types of boxes provide more technical details and extra references about what you are seeing. They contain helpful tips, but you can safely skip them the first time you run through the code.

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.

The CIFAR-10 dataset
https://www.cs.toronto.edu/~kriz/cifar.html

Imports¶

In [43]:
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
In [44]:
%matplotlib inline
np.set_printoptions(precision=3, suppress=True)

Verify TensorFlow Version and Keras Version¶

In [45]:
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
In [46]:
print("Keras version: ", keras.__version__)
Keras version:  2.10.0

Mount Google Drive to Colab Environment¶

In [47]:
# from google.colab import drive
# drive.mount('/content/gdrive')

EDA Functions¶

In [48]:
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
In [49]:
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()

Research Assignment Reporting Functions¶

In [50]:
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()
In [51]:
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)))) 
In [52]:
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')

Loading cifar10 Dataset¶

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.

In [53]:
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()
  • Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test).
  • x_train, x_test: uint8 arrays of color image data with shapes (num_samples, 32, 32).
  • y_train, y_test: uint8 arrays of digit labels (integers in range 0-9)

EDA Training and Test Datasets¶

  • Imported 50000 examples for training and 10000 examples for test
  • Imported 50000 labels for training and 10000 labels for test
In [54]:
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)

Review Labels¶

In [55]:
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

Plot Subset of Examples¶

In [56]:
(train_images, train_labels),(test_images, test_labels)= tf.keras.datasets.cifar10.load_data()
In [57]:
x_preview, y_preview = get_three_classes(train_images, train_labels)
x_preview, y_preview = get_three_classes(test_images, test_labels)
In [58]:
class_names_preview = ['aeroplane', 'car', 'bird']

show_random_examples(x_preview, y_preview, y_preview)

Preprocessing Data for Model Development¶

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
In [59]:
class_names = ['airplane'
,'automobile'
,'bird'
,'cat'
,'deer'
,'dog'
,'frog' 
,'horse'
,'ship'
,'truck']

Create Validation Data Set¶

In [60]:
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)

Confirm Datasets {Train, Validation, Test}¶

In [61]:
print(x_train_split.shape, x_valid_split.shape, x_test.shape)
(45000, 32, 32, 3) (5000, 32, 32, 3) (10000, 32, 32, 3)

Rescale Examples {Train, Validation, Test}¶

The images are 28x28 NumPy arrays, with pixel values ranging from 0 to 255

  1. Each element in each example is a pixel value
  2. Pixel values range from 0 to 255
  3. 0 = black
  4. 255 = white
In [62]:
x_train_norm = x_train_split/255
x_valid_norm = x_valid_split/255
x_test_norm = x_test/255

Create the Model¶

Build CNN Model¶

We use a Sequential class defined in Keras to create our model. The first 9 layers Conv2D MaxPooling handle feature learning. The last 3 layers, handle classification

In [63]:
model = Sequential([
  Flatten(input_shape=x_train_norm.shape[1:]),
  Dense(units=2000,activation=tf.nn.softmax),
  Dropout(0.3),
  Dense(units=2000,activation=tf.nn.softmax),
  Dropout(0.3),
  Dense(units=10, activation=tf.nn.softmax)       
])
In [64]:
model.summary()
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 flatten_1 (Flatten)         (None, 3072)              0         
                                                                 
 dense_3 (Dense)             (None, 2000)              6146000   
                                                                 
 dropout (Dropout)           (None, 2000)              0         
                                                                 
 dense_4 (Dense)             (None, 2000)              4002000   
                                                                 
 dropout_1 (Dropout)         (None, 2000)              0         
                                                                 
 dense_5 (Dense)             (None, 10)                20010     
                                                                 
=================================================================
Total params: 10,168,010
Trainable params: 10,168,010
Non-trainable params: 0
_________________________________________________________________
In [65]:
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.

Compiling the model¶

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.

tf.keras.losses.SparseCategoricalCrossentropy
https://www.tensorflow.org/api_docs/python/tf/keras/losses/SparseCategoricalCrossentropy
In [66]:
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['accuracy'])

Training the model¶

Module: tf.keras.callbacks
tf.keras.callbacks.EarlyStopping
https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/EarlyStopping
tf.keras.callbacks.ModelCheckpoint
https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/ModelCheckpoint
In [85]:
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 [==============================] - 139s 196ms/step - loss: 0.7440 - accuracy: 0.7428 - val_loss: 0.8154 - val_accuracy: 0.7152
Epoch 2/200
704/704 [==============================] - 135s 192ms/step - loss: 0.7170 - accuracy: 0.7510 - val_loss: 0.8042 - val_accuracy: 0.7278
Epoch 3/200
704/704 [==============================] - 136s 193ms/step - loss: 0.6900 - accuracy: 0.7610 - val_loss: 0.7930 - val_accuracy: 0.7250
Epoch 4/200
704/704 [==============================] - 137s 194ms/step - loss: 0.6659 - accuracy: 0.7687 - val_loss: 0.7645 - val_accuracy: 0.7368
Epoch 5/200
704/704 [==============================] - 137s 194ms/step - loss: 0.6529 - accuracy: 0.7727 - val_loss: 0.7673 - val_accuracy: 0.7418
Epoch 6/200
704/704 [==============================] - 133s 189ms/step - loss: 0.6322 - accuracy: 0.7798 - val_loss: 0.7811 - val_accuracy: 0.7318
Epoch 7/200
704/704 [==============================] - 141s 200ms/step - loss: 0.6154 - accuracy: 0.7852 - val_loss: 0.7865 - val_accuracy: 0.7336
Epoch 8/200
704/704 [==============================] - 234s 332ms/step - loss: 0.5954 - accuracy: 0.7934 - val_loss: 0.7545 - val_accuracy: 0.7360

Evaluate the model¶

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.

In [86]:
model = tf.keras.models.load_model("CNN_model.h5")
print(f"Test acc: {model.evaluate(x_test_norm, y_test)[1]:.3f}")
313/313 [==============================] - 14s 42ms/step - loss: 0.7693 - accuracy: 0.7432
Test acc: 0.743

Predictions¶

In [87]:
preds = model.predict(x_test_norm)
print('shape of preds: ', preds.shape)
313/313 [==============================] - 14s 42ms/step
shape of preds:  (10000, 10)

Plotting Performance Metrics¶

We use Matplotlib to create 2 plots--displaying the training and validation loss (resp. accuracy) for each (training) epoch side by side.

In [88]:
history_dict = history.history
history_dict.keys()
Out[88]:
dict_keys(['loss', 'accuracy', 'val_loss', 'val_accuracy'])
In [89]:
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
Out[89]:
loss accuracy val_loss val_accuracy
3 0.666 0.769 0.765 0.737
4 0.653 0.773 0.767 0.742
5 0.632 0.780 0.781 0.732
6 0.615 0.785 0.786 0.734
7 0.595 0.793 0.754 0.736

Plot Training Metrics (Loss and Accuracy)¶

In [90]:
plot_history(history)

Confusion matrices¶

Using both sklearn.metrics. Then we visualize the confusion matrix and see what that tells us.

In [91]:
pred1= model.predict(x_test_norm)
pred1=np.argmax(pred1, axis=1)
313/313 [==============================] - 15s 46ms/step
In [92]:
print_validation_report(y_test, pred1)
Classification Report
              precision    recall  f1-score   support

           0       0.77      0.78      0.78      1000
           1       0.87      0.86      0.86      1000
           2       0.68      0.62      0.65      1000
           3       0.56      0.58      0.57      1000
           4       0.73      0.68      0.70      1000
           5       0.63      0.67      0.65      1000
           6       0.74      0.84      0.78      1000
           7       0.82      0.78      0.80      1000
           8       0.84      0.83      0.83      1000
           9       0.82      0.80      0.81      1000

    accuracy                           0.74     10000
   macro avg       0.74      0.74      0.74     10000
weighted avg       0.74      0.74      0.74     10000

Accuracy Score: 0.7432
Root Mean Square Error: 2.1067985190805505
In [93]:
plot_confusion_matrix(y_test,pred1)

Load HDF5 Model Format¶

tf.keras.models.load_model
https://www.tensorflow.org/api_docs/python/tf/keras/models/load_model
In [94]:
model = tf.keras.models.load_model('CNN_model.h5')
In [95]:
preds = model.predict(x_test_norm)
313/313 [==============================] - 14s 42ms/step
In [96]:
preds.shape
Out[96]:
(10000, 10)

Predictions¶

In [97]:
cm = sns.light_palette((260, 75, 60), input="husl", as_cmap=True)
In [98]:
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)
Out[98]:
  airplane automobile bird cat deer dog frog horse ship truck
0 0.07% 0.00% 0.14% 90.68% 0.09% 6.29% 2.18% 0.02% 0.51% 0.02%
1 0.19% 68.41% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 31.32% 0.08%
2 14.15% 6.17% 0.25% 0.68% 0.06% 0.05% 0.03% 0.36% 75.80% 2.46%
3 69.21% 1.61% 0.36% 1.27% 0.04% 0.00% 0.00% 0.01% 27.48% 0.01%
4 0.00% 0.00% 0.65% 5.31% 8.84% 0.12% 85.08% 0.00% 0.00% 0.00%
5 0.01% 0.02% 0.09% 0.90% 0.62% 0.65% 97.23% 0.43% 0.01% 0.03%
6 0.95% 60.11% 0.03% 1.86% 0.00% 10.45% 15.15% 1.14% 0.01% 10.30%
7 0.32% 0.00% 18.37% 7.52% 26.52% 11.35% 34.28% 1.62% 0.00% 0.03%
8 0.04% 0.00% 0.53% 95.70% 1.02% 1.11% 1.10% 0.46% 0.03% 0.01%
9 2.12% 73.87% 0.11% 0.06% 0.13% 0.01% 0.06% 0.03% 0.21% 23.41%
10 70.61% 0.21% 3.73% 5.32% 7.09% 1.70% 0.12% 1.97% 9.02% 0.25%
11 0.00% 0.41% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 99.59%
12 0.00% 0.02% 7.60% 25.75% 3.07% 61.98% 0.44% 1.08% 0.04% 0.01%
13 0.00% 0.04% 0.01% 0.14% 0.11% 0.14% 0.01% 99.55% 0.00% 0.01%
14 0.00% 0.05% 0.00% 0.00% 0.00% 0.00% 0.00% 0.01% 0.00% 99.92%
15 1.37% 0.46% 0.46% 1.64% 1.39% 0.59% 66.84% 0.00% 27.22% 0.02%
16 0.00% 0.03% 0.05% 5.64% 0.01% 94.04% 0.16% 0.03% 0.00% 0.04%
17 0.23% 0.01% 1.20% 53.60% 0.69% 5.78% 0.51% 37.32% 0.07% 0.57%
18 0.50% 7.69% 0.01% 0.02% 0.00% 0.01% 0.00% 0.01% 88.61% 3.14%
19 0.00% 0.00% 0.02% 0.07% 0.03% 0.02% 99.86% 0.00% 0.00% 0.00%
In [99]:
(_,_), (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()
In [100]:
# 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)
In [101]:
activations = activation_model.predict(img_tensor)
len(activations)
1/1 [==============================] - 1s 669ms/step
Out[101]:
8
In [102]:
layer_names = []
for layer in model.layers:
    layer_names.append(layer.name)
    
layer_names
Out[102]:
['conv2d_2',
 'max_pooling2d_2',
 'dropout_2',
 'conv2d_3',
 'max_pooling2d_3',
 'dropout_3',
 'flatten_1',
 'dense_1']
In [ ]: