Week 10 · Separate training and evaluation in image classification
English translation
Before practice
01 Performance on new data
Example scenario How can you check whether a model that correctly classifies familiar photos will also classify unseen photos well?
Description
Distinguish fitting training data from Generalization
Evaluate performance on data not used for training
02 Information from incorrect predictions
Example scenario What do knowing how many predictions are correct and inspecting which photos were misclassified each tell you?
Description
Errors concentrated in a particular Class or input condition
Data and Label issues and model limitations
Run Code Cells: ▶ or Shift+Enter, in order from the top.
Edit a Text Cell: double-click → write your response → Shift+Enter.
Response cell: enter your own code.
After changing values: rerun that cell and the relevant cells below it.
Local setup
PyTorch and torchvision: install compatible versions together. Verified with Python 3.11 · PyTorch 2.12.1 · torchvision 0.27.1.
MNIST: internet access is required on the first run. Download location: mnist_data.
If CERTIFICATE_VERIFY_FAILED occurs with python.org Python on a Mac, run Install Certificates.command in the corresponding Python folder under Applications and try again.
Practice
1. MNIST and execution scale
Run the data preparation cell. Print the image shape and Label from train_data[0] and describe the image's three axes.
Iterate through all of train_loader and collect image counts per Batch in a list. Record the first Batch size, last Batch size, and total Batch count.
Calculate the expected last Batch size from len(train_used) and Batch Size 64 and compare it with the printed result.
2. Train and evaluate the CNN
Run the data cell with QUICK_RUN=True, followed by the CNN training and evaluation cells. Record the training epochs, training image count, and evaluation image count.
Find model.train() in the training code and model.eval() and torch.no_grad() in the evaluation code. Explain in one sentence each whether it controls Dropout behavior or Gradient recording.
To compare with the full dataset, change to QUICK_RUN=False and rerun from the data cell.
3. Plots and misclassifications
Compare the actual and predicted Labels in the visualizations of examples and mistakes against each image. For correct and incorrect examples, record which digit was predicted as which, and describe noticeable features in misclassified images.
Alongside the recorded Test Accuracy, write the number of evaluation images. Distinguish evaluation on a subset under QUICK_RUN=True from evaluation on the entire Test Set.
Submission and saving
Include code, actual outputs and plots, and your own interpretation. Submit through KLAS; check the semester schedule and latest announcements for the deadline.
Filename: Name_StudentID_Week10.ipynb.
Restart the runtime, run all code from the top, then save.
Optional practice
Evaluating the same image twice with Dropout enabled
After training and evaluating the CNN, add a Code Cell at the bottom of the notebook and run the following code.
debug_image = test_used[0][0].unsqueeze(0)
for training in [True, False]:
model.train(training)
with torch.no_grad():
first = model(debug_image)
second = model(debug_image)
print("train" if training else "eval",
"maximum difference:", (first - second).abs().max().item())
model.eval()
In a Text Cell, record the maximum absolute difference for each mode and explain why the same image produces different outputs in terms of Dropout. This code evaluates without computing Gradients or updating weights and ends in eval mode.