Separate training and evaluation in image classification
English translation
AI Applications · Week 10
This week's goal · Load MNIST, train a model, and inspect predictions and misclassifications on new images.
Run: Save a copy to your Drive in Colab, then run Code Cells from the top using ▶ or Shift+Enter. Double-click Text Cells marked ⟦Enter your response⟧ to edit them.
Before feeding data to the model
Feed actual MNIST images to the Week 9 CNN. Dataset[i] provides an image and Label, and DataLoader groups multiple images into a Batch. A grayscale image (1,28,28) after ToTensor() becomes (N,1,28,28) in a Batch. Use this transformation for both training and evaluation; do not feed the original integer Pixel array directly to the evaluation model.
1. Inspect one data sample first
MNIST consists of 28×28 grayscale handwritten digits and labels 0~9. Before training on the entire dataset, display one image and print its Tensor shape and label.
Inspect one data sample first
ToTensor converts image values into Tensors. If applying additional Normalization, use the same transformation for training and evaluation. If an image appears flipped, check the shape and display axes.
2. DataLoader creates Batches
A Dataset defines how to retrieve one sample, while a DataLoader groups samples and iterates over them. Shuffle order during training; for evaluation, process every sample exactly once without omissions.
DataLoader creates Batches
The last Batch may be smaller than earlier Batches. To compute Accuracy reliably, divide the total number of correct predictions by the actual sample count rather than taking an unweighted mean of Batch Accuracies.
3. Different modes for training and evaluation
Training computes Gradients and updates Parameters in model.train(). Evaluation uses model.eval() and no_grad() to compute results without updates. eval changes Layer behaviors such as Dropout for evaluation, while no_grad stops recording Gradient computations. Use both for evaluation.
Different modes for training and evaluation
eval changes behaviors such as Dropout, while no_grad disables Gradient tracking. Calling optimizer.step on evaluation data invalidates the evaluation.
4. Inspect numbers and images together
Compute test accuracy, then display predictions and targets for 10 images together. Inspect up to five misclassified images as well. Selecting images in dataset order instead of showing only correct examples makes the record less biased.
Inspect numbers and images together
Quick-run mode uses a subset of the data to check that training and evaluation code runs correctly even on a CPU. When comparing results with the full-data exercise, always record the sample count, epochs, and execution mode. Do not report quick-run Accuracy as the full experiment.
Check your understanding
- What values should go into the code, and what does each row and column of the resulting array represent? If calculating physical quantities, have you also checked the units?
- Can you identify which parts to rerun when you change one condition?
- Do the results match your expectations? If they differ, can you explain why using the output values or plots?
Practice materials
- AI_Applications_Week10_ImageClassification.ipynbPreview Colab notebook · Download Colab notebook
- L17_Image_Classification_MNIST.ipynbPreview Colab notebook · Download Colab notebook