dl-workshop-2022

Build and train a basic convolution neural network

The goal here is to build a convolution neural network and play with its parameters to achieve a high accuracy on the dataset of your choice. Please re-order the blocks of code below, copy-paste the code blocks in appropriate order in a new Colab notebook, run them, and analyze the outputs.

After having an initail version that runs, please experiment to answer the following questions: How will the a) prediction accuracy, and b) training speed change if you:

Block A: Train the model

history = model.fit(train_ds, epochs=10, validation_data=valid_ds)

Block B: Compile the model to check for errors

import tensorflow as tf
model.compile(optimizer='rmsprop', loss = 'categorical_crossentropy', metrics=['accuracy'])

Block C: Add Dense layers on top

Dense layers take vectors as input (which are 1D), while the current output is a 3D tensor. The last number of neurons should match your number of classes.

model.add(layers.Flatten())
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(6))
model.add(layers.Softmax())
model.summary()

Block D: Create the convolutional base

Define the convolutional base using a common pattern: a stack of Conv2D and MaxPooling2D layers. As input, a CNN takes tensors of shape (image_height, image_width, color_channels), ignoring the batch size.

from tensorflow.keras import datasets, layers, models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(48, 48, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.summary()

Block E: Peek into what the model is predicting

import matplotlib.pyplot as plt
import numpy as np

images, labels = valid_ds[0]
for i in range(len(images)):
    print('')
    print('Checking image', i)
    x = images[i]
    plt.figure()
    plt.imshow(x)
    plt.show()
    print('Ground truth:', labels[i])
    x = np.expand_dims(x, axis=0)
    p = model.predict(x, verbose=0)
    print('Prediction:', p.argmax(), p)
    if i == 5: break

Block F: Investigate learning curves

Look back and investigate the training by comparing the accuracy on the training dataset and validation dataset

plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 1])
plt.legend(loc='lower right')

Block G: Evaluate the model on the validation dataset

valid_loss, valid_acc = model.evaluate(valid_ds, verbose=2)

Block H: Inspect the model

Inspect the number of paramaters, number of layers, input shape, and number of output classes using the two approaches below:

print(model.summary())
tf.keras.utils.plot_model(model, show_layer_names=True, show_shapes=True)

Block I: Load the data

! unzip face-expression-kaggle.zip
from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
    rescale=1./255)

train_ds = datagen.flow_from_directory(
  './face-expression-kaggle/images/train/',
  target_size=(48, 48),
  shuffle=True,
  batch_size=32)

valid_ds = datagen.flow_from_directory(
  './face-expression-kaggle/images/validation/',
  target_size=(48, 48),
  shuffle=True,
  batch_size=32)

Display one image and it’s label

import matplotlib.pyplot as plt
# Take one batch full of images
images, labels = train_ds[0]
print(images.shape)
plt.imshow(images[0])
plt.title(labels[0])
plt.axis("off")