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.
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
Building the Tutorial CNN Model
Evaluating on the Test Set
Loading a Saved Model for Testing
Predicting Individual Images
Per-Class Accuracy
Output:
Confusion Matrix
Visualizing Predictions
Testing with Data Augmentation
To check model robustness, apply transformations to test images:
Testing a Custom Image
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 callingmodel.predict(), applytf.nn.softmax()to get probabilities. Usingnp.argmaxon 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-outx_test, y_testsplit. - Not keeping the batch dimension for single images:
model.predict()expects shape(batch, 32, 32, 3). A single image has shape(32, 32, 3). Usenp.expand_dims(img, 0)orimg[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()withnp.argmaxto get predicted class labels - Normalize test data identically to training data (
/ 255.0) - Use
classification_reportfrom 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
- How to train a customized transformer model with custom dataset formatting
- How to train a model with only an Embedding layer in Keras and no labels
- How to train a `RNN` with LSTM cells for time series prediction
- How to train Keras model with multiple inputs in Tensorflow 2.2?
- How to tie word embedding and softmax weights in keras?
- How to train a model in nodejs tensorflow.js?
- How to test that no exception is thrown?
- How to test the connection to RabbitMQ Server?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.