Open In Colab

(tutorial6=)

Tutorial 6: CIFAR-10 CNN Assignment#

In this assignment, you will build a Convolutional Neural Network (CNN) using TensorFlow/Keras to classify images from the CIFAR-10 dataset. This dataset contains 10 classes: airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck.

Outline:

  1. Load and explore CIFAR-10.

  2. Split the training data into training and validation sets (40,000 / 10,000).

  3. Visualize sample images with their class names.

  4. Build your CNN model.

  5. Compile and train the model (for 3 epochs to keep training time short).

  6. Evaluate the model on the test set (10,000 images).

1. Load CIFAR-10 Dataset#

I’ll load the CIFAR-10 dataset using TensorFlow’s tf.keras.datasets.cifar10.load_data(). This dataset automatically comes split into a training set (50,000 images) and a test set (10,000 images).

# TODO: Load the CIFAR-10 dataset
import tensorflow as tf
import numpy as np

# Load dataset
(x_train_full, y_train_full), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()

# Normalize the images to [0, 1]
x_train_full = x_train_full.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

print('Full training set shape:', x_train_full.shape)
print('Test set shape:', x_test.shape)

# The CIFAR-10 classes
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
               'dog', 'frog', 'horse', 'ship', 'truck']
print('Classes:', class_names)
2025-05-08 15:08:23.693218: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2025-05-08 15:08:23.696412: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2025-05-08 15:08:23.704970: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:467] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1746716903.718587   75335 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1746716903.722778   75335 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
W0000 00:00:1746716903.734021   75335 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1746716903.734031   75335 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1746716903.734033   75335 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1746716903.734035   75335 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
2025-05-08 15:08:23.738164: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
        0/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0s/step

   212992/170498071 ━━━━━━━━━━━━━━━━━━━━ 43s 0us/step

  2719744/170498071 ━━━━━━━━━━━━━━━━━━━━ 6s 0us/step 

  8536064/170498071 ━━━━━━━━━━━━━━━━━━━ 2s 0us/step

 14221312/170498071 ━━━━━━━━━━━━━━━━━━━ 2s 0us/step

 19873792/170498071 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step

 25550848/170498071 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step

 31383552/170498071 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step

 37109760/170498071 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step

 42737664/170498071 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step

 48406528/170498071 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step

 54026240/170498071 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step

 59711488/170498071 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step

 65478656/170498071 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step

 71180288/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

 76832768/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

 82534400/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

 88358912/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

 93962240/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

 99557376/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

105267200/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

110788608/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

116563968/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

122257408/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

127844352/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

133554176/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

139395072/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

145055744/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

150708224/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

156377088/170498071 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

162168832/170498071 ━━━━━━━━━━━━━━━━━━━ 0s 0us/step

167813120/170498071 ━━━━━━━━━━━━━━━━━━━ 0s 0us/step

170498071/170498071 ━━━━━━━━━━━━━━━━━━━━ 2s 0us/step
Full training set shape: (50000, 32, 32, 3)
Test set shape: (10000, 32, 32, 3)
Classes: ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

2. Split Data into Training and Validation Sets#

Using the full training set (50,000 images), split the data into:

  • Training set: 40,000 images

  • Validation set: 10,000 images

You can use scikit-learn’s train_test_split function to perform this split.

# TODO: Split x_train_full and y_train_full into training and validation sets
from sklearn.model_selection import train_test_split

# YOUR CODE HERE
raise NotImplementedError()

print('Training set shape:', x_train.shape)
print('Validation set shape:', x_val.shape)
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Cell In[2], line 5
      2 from sklearn.model_selection import train_test_split
      4 # YOUR CODE HERE
----> 5 raise NotImplementedError()
      7 print('Training set shape:', x_train.shape)
      8 print('Validation set shape:', x_val.shape)

NotImplementedError: 

3. Visualize Sample Images#

To get a sense of the data, here’s a grid of 9 sample images from the training set along with their corresponding class names (using the index of the label to look up the class name from the list provided).

import matplotlib.pyplot as plt

plt.figure(figsize=(10,10))
for i in range(9):
    ax = plt.subplot(3, 3, i+1)
    plt.imshow(x_train[i])
    # y_train[i] is in an array shape; extract the integer label
    plt.title(class_names[int(y_train[i])])
    plt.axis('off')
plt.show()

4. Build Your CNN Model#

Construct a simple CNN using TensorFlow/Keras with the following guidelines:

  • Input Layer: Your model will accept images of shape (32, 32, 3).

  • Convolutional Layers: Include at least two convolutional layers (each followed by a pooling layer).

  • Flattening: Use a Flatten layer to convert the 2D feature maps to a 1D vector.

  • Dense Layers: Add one or more dense layers to learn non-linear combinations of the features.

  • Output Layer: An output dense layer with 10 units (one for each class) using softmax activation, so that the output is a probability distribution over classes.

Write your model architecture below.

# TODO: Build your CNN model using tf.keras
from tensorflow import keras
from tensorflow.keras import layers

# YOUR CODE HERE
raise NotImplementedError()

model.summary()

5. Compile the Model#

Compile your model using the sparse_categorical_crossentropy loss function.

# YOUR CODE HERE
raise NotImplementedError()

6. Train the Model#

Train your model for 3 epochs. Note that the full CIFAR-10 training set (40,000 images) may take a while, so you only need to train for 3 epochs. But if you’d like to train more to see how much accuracy you can achieve, feel free to.

Do you end up overfitting the data? Underfitting the data?

# TODO: Train your model

epochs = 3

# YOUR CODE HERE
raise NotImplementedError()
      

7. Evaluate on Test Data#

Evaluate the performance of your trained model on the CIFAR-10 test set (10,000 images). This will give you a clear picture of how well your model generalizes to unseen data.

# YOUR CODE HERE
raise NotImplementedError()