Course materials · AI Applications

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
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

  1. Run the data preparation cell. Print the image shape and Label from train_data[0] and describe the image's three axes.
  2. 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.
  3. 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

  1. 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.
  2. 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

  1. 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.
  2. 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

  1. 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()
  2. 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.