This is a self-correcting activity generated by nbgrader. Fill in any place that says YOUR CODE HERE or YOUR ANSWER HERE. Run subsequent cells to check your code.


CIFAR10

In this activity, you’ll train models to associate images representing common objects with their class.

The CIFAR10 dataset consists of 60,000 32x32 colour images in 10 classes, with 6,000 images per class. The classes are completely mutually exclusive. There are 50,000 training images and 10,000 test images.

CIFAR10 images

Package setup

# Import base packages
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
# Setup plots
%matplotlib inline
plt.rcParams["figure.figsize"] = 10, 8
%config InlineBackend.figure_format = 'retina'
sns.set()
# Import ML packages (edit this list if needed)
import tensorflow as tf

print(f"TensorFlow version: {tf.__version__}")
print(f"Keras version: {tf.keras.__version__}")
print("GPU found :)" if tf.config.list_physical_devices("GPU") else "No GPU :(")

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical

Utility functions

def plot_loss_acc(history):
    """Plot training and (optionally) validation loss and accuracy"""

    loss = history.history["loss"]
    epochs = range(1, len(loss) + 1)

    plt.figure(figsize=(10, 10))

    plt.subplot(2, 1, 1)
    plt.plot(epochs, loss, ".--", label="Training loss")
    final_loss = loss[-1]
    title = "Training loss: {:.4f}".format(final_loss)
    plt.ylabel("Loss")
    if "val_loss" in history.history:
        val_loss = history.history["val_loss"]
        plt.plot(epochs, val_loss, "o-", label="Validation loss")
        final_val_loss = val_loss[-1]
        title += ", Validation loss: {:.4f}".format(final_val_loss)
    plt.title(title)
    plt.legend()

    acc = history.history["accuracy"]

    plt.subplot(2, 1, 2)
    plt.plot(epochs, acc, ".--", label="Training acc")
    final_acc = acc[-1]
    title = "Training accuracy: {:.2f}%".format(final_acc * 100)
    plt.xlabel("Epochs")
    plt.ylabel("Accuracy")
    if "val_accuracy" in history.history:
        val_acc = history.history["val_accuracy"]
        plt.plot(epochs, val_acc, "o-", label="Validation acc")
        final_val_acc = val_acc[-1]
        title += ", Validation accuracy: {:.2f}%".format(final_val_acc * 100)
    plt.title(title)
    plt.legend()

Step 1: Loading the data

Question

  • Load the CIFAR10 dataset included with Keras.

  • Display the first 30 test images.

# Load the data

# YOUR CODE HERE
# Plot the first 30 images

# YOUR CODE HERE

Step 2: Training a dense neural network

Question

  • Prepare data for training with a dense network.

  • Train a model on the data to obtain the expected validation accuracy. Use 20% of the training set for validation. Store the training history in a variable named history.

# Prepare data for training

# YOUR CODE HERE
# Create a model and print its summary

# YOUR CODE HERE
# Train the model and show results

# YOUR CODE HERE
# Plot training history
plot_loss_acc(history)
# Retrieve final validation accuracy
val_acc = history.history["val_accuracy"][-1]

# Assert final accuracy
assert val_acc > 0.44
# Evaluate model performance on test data
_, test_acc = model.evaluate(x_test, y_test, verbose=0)

print(f'Test accuracy: {test_acc * 100:.2f}%')

Step 3: Training a convnet

Question

Train a model on the data to obtain the expected validation accuracy. Store the training history in a variable named history.

# Create a convnet

# YOUR CODE HERE
# Train the convnet

# YOUR CODE HERE
# Plot training history
plot_loss_acc(history)
# Retrieve final validation accuracy
val_acc = history.history["val_accuracy"][-1]

# Assert final validation accuracy
assert val_acc > 0.66
# Evaluate model performance on test data
_, test_acc = model.evaluate(x_test, y_test, verbose=0)

print(f"Test accuracy: {test_acc * 100:.2f}%")