TensorFlow
CIFAR-10
CNN
Model Testing
Deep Learning

How to test tensorflow cifar10 cnn tutorial model?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The CIFAR-10 dataset contains 60,000 32x32 color images in 10 classes (airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck). TensorFlow's CNN tutorial trains a model on the 50,000 training images. Testing involves loading the trained model, running inference on the 10,000 test images, and evaluating accuracy, per-class precision, and confusion matrices. The trained model should achieve roughly 70-75% accuracy with the basic tutorial architecture.

Loading CIFAR-10 and Preprocessing

python
1import tensorflow as tf
2import numpy as np
3
4# Load CIFAR-10 — automatically splits into train/test
5(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
6
7# Normalize pixel values to [0, 1]
8x_test = x_test.astype("float32") / 255.0
9
10# Class names for readable output
11class_names = [
12    "airplane", "automobile", "bird", "cat", "deer",
13    "dog", "frog", "horse", "ship", "truck"
14]
15
16print(f"Test set: {x_test.shape[0]} images, shape {x_test.shape[1:]}")
17# Test set: 10000 images, shape (32, 32, 3)

Building the Tutorial CNN Model

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Conv2D(32, (3, 3), activation="relu", input_shape=(32, 32, 3)),
3    tf.keras.layers.MaxPooling2D((2, 2)),
4    tf.keras.layers.Conv2D(64, (3, 3), activation="relu"),
5    tf.keras.layers.MaxPooling2D((2, 2)),
6    tf.keras.layers.Conv2D(64, (3, 3), activation="relu"),
7    tf.keras.layers.Flatten(),
8    tf.keras.layers.Dense(64, activation="relu"),
9    tf.keras.layers.Dense(10),  # 10 classes, logits output
10])
11
12model.compile(
13    optimizer="adam",
14    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
15    metrics=["accuracy"],
16)

Evaluating on the Test Set

python
1# Option 1: Evaluate using model.evaluate()
2test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=2)
3print(f"Test accuracy: {test_accuracy:.4f}")
4print(f"Test loss: {test_loss:.4f}")
5# Test accuracy: ~0.7100
6# Test loss: ~0.8800

Loading a Saved Model for Testing

python
1# Save after training
2model.save("cifar10_cnn_model.keras")
3
4# Load for testing — no need to retrain
5loaded_model = tf.keras.models.load_model("cifar10_cnn_model.keras")
6
7# Evaluate the loaded model
8test_loss, test_accuracy = loaded_model.evaluate(x_test, y_test, verbose=2)
9print(f"Loaded model accuracy: {test_accuracy:.4f}")

Predicting Individual Images

python
1# Get predictions for all test images
2predictions = model.predict(x_test)
3
4# predictions shape: (10000, 10) — logits for each class
5# Convert logits to class indices
6predicted_classes = np.argmax(predictions, axis=1)
7true_classes = y_test.flatten()
8
9# Predict a single image
10single_image = x_test[0:1]  # Keep batch dimension
11pred = model.predict(single_image)
12predicted_class = class_names[np.argmax(pred)]
13true_class = class_names[y_test[0][0]]
14print(f"Predicted: {predicted_class}, True: {true_class}")

Per-Class Accuracy

python
1from sklearn.metrics import classification_report
2
3predicted_classes = np.argmax(model.predict(x_test), axis=1)
4true_classes = y_test.flatten()
5
6print(classification_report(true_classes, predicted_classes,
7                            target_names=class_names))

Output:

 
1              precision    recall  f1-score   support
2    airplane       0.76      0.73      0.74      1000
3  automobile       0.82      0.82      0.82      1000
4        bird       0.58      0.57      0.58      1000
5         cat       0.51      0.52      0.52      1000
6        deer       0.65      0.63      0.64      1000
7         dog       0.59      0.60      0.60      1000
8        frog       0.76      0.78      0.77      1000
9       horse       0.76      0.76      0.76      1000
10        ship       0.80      0.82      0.81      1000
11       truck       0.78      0.78      0.78      1000
12    accuracy                           0.70     10000

Confusion Matrix

python
1from sklearn.metrics import confusion_matrix
2import matplotlib.pyplot as plt
3import seaborn as sns
4
5cm = confusion_matrix(true_classes, predicted_classes)
6
7plt.figure(figsize=(10, 8))
8sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
9            xticklabels=class_names, yticklabels=class_names)
10plt.xlabel("Predicted")
11plt.ylabel("True")
12plt.title("CIFAR-10 Confusion Matrix")
13plt.tight_layout()
14plt.savefig("confusion_matrix.png")
15plt.show()

Visualizing Predictions

python
1import matplotlib.pyplot as plt
2
3def show_predictions(images, true_labels, pred_labels, n=16):
4    plt.figure(figsize=(12, 12))
5    for i in range(n):
6        plt.subplot(4, 4, i + 1)
7        plt.imshow(images[i])
8        color = "green" if true_labels[i] == pred_labels[i] else "red"
9        plt.title(f"True: {class_names[true_labels[i]]}\n"
10                  f"Pred: {class_names[pred_labels[i]]}", color=color)
11        plt.axis("off")
12    plt.tight_layout()
13    plt.show()
14
15show_predictions(x_test, true_classes, predicted_classes)

Testing with Data Augmentation

To check model robustness, apply transformations to test images:

python
1# Test with horizontal flips
2x_test_flipped = np.flip(x_test, axis=2)
3_, flip_acc = model.evaluate(x_test_flipped, y_test, verbose=0)
4print(f"Flipped accuracy: {flip_acc:.4f}")
5
6# Test with brightness changes
7x_test_bright = np.clip(x_test * 1.3, 0, 1)
8_, bright_acc = model.evaluate(x_test_bright, y_test, verbose=0)
9print(f"Brightened accuracy: {bright_acc:.4f}")

Testing a Custom Image

python
1from PIL import Image
2
3# Load and preprocess a custom image
4img = Image.open("my_cat.jpg").resize((32, 32))
5img_array = np.array(img).astype("float32") / 255.0
6img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension
7
8pred = model.predict(img_array)
9predicted_class = class_names[np.argmax(pred)]
10confidence = tf.nn.softmax(pred[0]).numpy()
11print(f"Predicted: {predicted_class} ({confidence[np.argmax(pred)] * 100:.1f}%)")

Common Pitfalls

  • Not normalizing test data the same way as training data: If training uses x / 255.0, testing must too. Mismatched preprocessing causes dramatically lower accuracy. Always apply identical transforms to train and test sets.
  • Forgetting from_logits=True: The tutorial model outputs raw logits (no softmax layer). When calling model.predict(), apply tf.nn.softmax() to get probabilities. Using np.argmax on logits works for class selection but gives incorrect confidence values.
  • Evaluating on training data instead of test data: Using model.evaluate(x_train, y_train) reports training accuracy, not generalization performance. Always use the held-out x_test, y_test split.
  • Not keeping the batch dimension for single images: model.predict() expects shape (batch, 32, 32, 3). A single image has shape (32, 32, 3). Use np.expand_dims(img, 0) or img[np.newaxis] to add the batch dimension.
  • Expecting high accuracy from the basic tutorial model: The tutorial CNN achieves about 70-75% accuracy on CIFAR-10. State-of-the-art models reach 96%+ using deeper architectures (ResNet, EfficientNet), data augmentation, and longer training. The tutorial model is intentionally simple for learning purposes.

Summary

  • Use model.evaluate(x_test, y_test) for overall test accuracy and loss
  • Use model.predict() with np.argmax to get predicted class labels
  • Normalize test data identically to training data (/ 255.0)
  • Use classification_report from scikit-learn for per-class precision, recall, and F1
  • Visualize errors with confusion matrices and side-by-side prediction plots
  • The tutorial model achieves ~70-75% accuracy — deeper architectures reach 96%+

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.