Python Data Foundations Documentation

A plain documentation-style guide for Python, data handling, visualization, and machine learning basics.

Convolutional Neural Networks with CIFAR-10

This page builds a convolutional neural network for CIFAR-10 image classification, including one-hot labels, convolution and pooling blocks, dropout, early stopping, evaluation, and prediction on a custom image.

What you should be able to do
  • Load CIFAR-10 and understand its image and label shapes.
  • Convert labels to one-hot format for categorical crossentropy.
  • Build CNN blocks with Conv2D, MaxPooling2D, Dropout, Flatten, and Dense layers.
  • Evaluate predictions with class reports and a confusion matrix.
  • Resize an external image before passing it to the trained model.
Reusable patterns
  • Conv2D layers learn local image features; pooling reduces spatial size.
  • Dropout reduces overfitting by randomly disabling activations during training.
  • EarlyStopping can stop training when validation loss stops improving.

Setup and CIFAR-10 Data

CIFAR-100 instead of CIFAR-10.

The CNN workflow is the same for CIFAR-10 and CIFAR-100, but the number of classes changes. CIFAR-10 has 10 classes; CIFAR-100 has 100 fine-grained classes.

from tensorflow import keras

(x_train, y_train), (x_test, y_test) = keras.datasets.cifar100.load_data()

x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0

y_train_one_hot = keras.utils.to_categorical(y_train, num_classes=100)
y_test_one_hot = keras.utils.to_categorical(y_test, num_classes=100)

If the task says to use fine-grained labels, use the normal CIFAR-100 labels from cifar100.load_data(). Do not collapse them into broader superclasses unless the task explicitly asks for coarse labels.

In case the number of classes changes.

The final dense layer and one-hot encoding must agree. For CIFAR-100, use 100 output neurons.

num_classes = 100

y_train_final = keras.utils.to_categorical(y_train_final, num_classes=num_classes)
y_val_final = keras.utils.to_categorical(y_val_final, num_classes=num_classes)

model.add(keras.layers.Dense(num_classes, activation="softmax"))

If the model ends with Dense(10, activation="softmax") but the labels have 100 classes, training will fail or the model will learn the wrong output shape.

In case the CNN receives autoencoder output.

Some tasks first denoise or reconstruct images with an autoencoder, then train a CNN on the reconstructed images. The important part is to reshape predictions back to image format and keep labels aligned with the selected images.

reconstructed = autoencoder.predict(x_train_noisy_flat)
reconstructed_images = reconstructed[:6000].reshape(-1, 32, 32, 3)
reconstructed_labels = y_train[:6000]

After this, split the reconstructed images and labels together.

from sklearn.model_selection import train_test_split

x_train_final, x_val_final, y_train_final, y_val_final = train_test_split(
    reconstructed_images,
    reconstructed_labels,
    test_size=0.3,
    random_state=42,
    stratify=reconstructed_labels
)

Use stratify before one-hot encoding. Stratification expects class labels, not one-hot vectors.

In case the task mentions padding or edge filling.

If the task says the convolution should use edge padding, add padding="same". Without it, Keras uses padding="valid" by default and the image shrinks after convolution.

model.add(keras.layers.Conv2D(
    32,
    (3, 3),
    activation="relu",
    padding="same",
    input_shape=(32, 32, 3)
))

Listing 1. Import TensorFlow and check its version

This step supports the workflow shown in the surrounding listings.

import tensorflow as tf
tf.__version__

Listing 2. Import Keras

This step supports the workflow shown in the surrounding listings.

from tensorflow import keras

Listing 3. Load CIFAR-10 images and labels

This prepares the data or pretrained resource used by the following examples.

from keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

Listing 4. Check the training image tensor shape

Training updates model weights and stores the learning history for later inspection.

x_train.shape

Listing 5. Check the training label tensor shape

Training updates model weights and stores the learning history for later inspection.

y_train.shape

Listing 6. Inspect raw pixel values for one image

This step supports the workflow shown in the surrounding listings.

x_train[0]

Listing 7. Display the first CIFAR-10 image

This visualization step helps verify what the data or model output looks like.

import matplotlib.pyplot as plt
plt.imshow(x_train[0])

Listing 8. Print the first image label

This step supports the workflow shown in the surrounding listings.

print('First image label:', y_train[0])

Listing 9. Convert labels to one-hot vectors

This step supports the workflow shown in the surrounding listings.

y_train_one_hot=keras.utils.to_categorical(y_train,10)
y_test_one_hot=keras.utils.to_categorical(y_test,10)

Listing 10. Inspect one one-hot encoded label

This step supports the workflow shown in the surrounding listings.

y_train_one_hot[0]

Listing 11. Normalize CIFAR-10 pixel values

This transforms the raw input into the numeric scale or shape expected by the model.

x_train=x_train/255
x_test=x_test/255

CNN Architecture

Listing 12. Create an empty Sequential CNN model

This defines or inspects part of the neural network architecture.

model=keras.models.Sequential()

Listing 13. Add the first convolution layer

This defines or inspects part of the neural network architecture.

model.add(keras.layers.Conv2D(32,(3,3), activation='relu',padding='same',input_shape=(32,32,3)))

Listing 14. Add the second convolution layer

This defines or inspects part of the neural network architecture.

model.add(keras.layers.Conv2D(32,(3,3), activation='relu',padding='same'))

Listing 15. Add max pooling to reduce image size

This step supports the workflow shown in the surrounding listings.

model.add(keras.layers.MaxPooling2D(pool_size=(2,2)))

Listing 16. Add dropout after the first convolution block

This step supports the workflow shown in the surrounding listings.

model.add(keras.layers.Dropout(0.25))

Listing 17. Add the second convolution block

This step supports the workflow shown in the surrounding listings.

# layers 5 to 8
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(keras.layers.MaxPooling2D(pool_size=(2,2)))
model.add(keras.layers.Dropout(0.25))

Listing 18. Flatten feature maps before dense layers

This defines or inspects part of the neural network architecture.

model.add(keras.layers.Flatten())

Listing 19. Add a dense classification layer

This defines or inspects part of the neural network architecture.

model.add(keras.layers.Dense(512, activation='relu'))

Listing 20. Add dropout before the output layer

This defines or inspects part of the neural network architecture.

model.add(keras.layers.Dropout(0.5))

Listing 21. Add the softmax output layer

This defines or inspects part of the neural network architecture.

model.add(keras.layers.Dense(10, activation='softmax'))

Listing 22. Print the CNN architecture summary

This step supports the workflow shown in the surrounding listings.

model.summary()

Training

Listing 23. Compile the CNN with categorical crossentropy

Compilation chooses the loss function, optimizer, and metrics used during training.

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

Listing 24. Create an early-stopping callback

This step supports the workflow shown in the surrounding listings.

from tensorflow.keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(monitor='val_loss', patience=3,
                               restore_best_weights=True)

Listing 25. Train the CNN with validation monitoring

Training updates model weights and stores the learning history for later inspection.

# train the model with EarlyStopping
model_history=model.fit(x_train,y_train_one_hot, batch_size=32, epochs=20,validation_split=0.2, callbacks=[early_stopping])

Listing 26. Plot CNN training history

Training updates model weights and stores the learning history for later inspection.

import pandas as pd
pd.DataFrame(model_history.history).plot(figsize=(8,5))
plt.grid(True)
plt.xlabel('Epochs')
plt.show

Listing 27. Save the trained CIFAR-10 model

Training updates model weights and stores the learning history for later inspection.

model.save('my_cifar_model.h5')

Evaluation

Listing 28. Evaluate the CNN on the test set

This step supports the workflow shown in the surrounding listings.

model.evaluate(x_test,y_test_one_hot)

Listing 29. Predict one-hot class probabilities

Prediction applies the trained model to new data and returns probabilities or labels.

prediction_probabilities=model.predict(x_test)  # prediction result in one-hot/probability form
prediction_probabilities # this can be displayed as an activity

Listing 30. Convert probabilities to class indexes

This step supports the workflow shown in the surrounding listings.

import numpy as np
y_pred=np.argmax(prediction_probabilities,axis=-1)

Listing 31. Define readable CIFAR-10 class names

This step supports the workflow shown in the surrounding listings.

class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

Listing 32. Print the CIFAR-10 classification report

This evaluation step compares predicted classes with the true labels.

from sklearn.metrics import classification_report
print(classification_report(y_test,y_pred, target_names=class_names))

Listing 33. Build and display the confusion matrix

This evaluation step compares predicted classes with the true labels.

from sklearn import metrics
confusion_matrix_values = metrics.confusion_matrix(y_test, y_pred)

cm_display = metrics.ConfusionMatrixDisplay(confusion_matrix = confusion_matrix_values, display_labels = class_names)
fig, ax = plt.subplots(figsize=(10,10))
cm_display.plot(ax=ax)
plt.show()

Custom Image Prediction

Listing 34. Download an example cat image in Colab

This prepares the data or pretrained resource used by the following examples.

!curl -o cat.jpg https://www.site.com/cat.jpg

Listing 35. Read the downloaded image

This prepares the data or pretrained resource used by the following examples.

image = plt.imread("/content/cat.jpg")

Listing 36. Check the downloaded image shape

This prepares the data or pretrained resource used by the following examples.

image.shape

Listing 37. Resize the image to CIFAR-10 input size

This step supports the workflow shown in the surrounding listings.

from skimage.transform import resize
resized_image = resize(image, (32,32))

Listing 38. Display the resized image

This visualization step helps verify what the data or model output looks like.

plt.imshow(resized_image)

Listing 39. Predict class probabilities for the custom image

Prediction applies the trained model to new data and returns probabilities or labels.

import numpy as np
probabilities = model.predict(np.array( [resized_image,] ))

Listing 40. Inspect custom-image probabilities

This step supports the workflow shown in the surrounding listings.

probabilities

Listing 41. Sort predicted probabilities by confidence

Prediction applies the trained model to new data and returns probabilities or labels.

index = np.argsort(probabilities[0,:])

Listing 42. Print the four most likely custom-image classes

This step supports the workflow shown in the surrounding listings.

for i in range (9,5,-1):  # top probabilities
  print(class_names[index[i]], ":", probabilities[0,index[i]])

Back to overview