Lab 10: Fashion MNIST

Scenario: No scenario for this lab!

You are not required to writeup a “complete” report for this lab! It’s been a long semester and you’ve already written many reports! Simply submit the (blank) template to Canvas for full points.

Goal

The goal of this lab is to develop a model to classify images of clothing, a classification task.

Data

The data (and task) for this lab originally comes from Zalando Research.

We will access this data through tools available in pytorch.

Data in Python

The following code can be used to download or re-access the data.

import torch
from torchvision import datasets
from torchvision.transforms import ToTensor

# Download training data from open datasets.
training_data = datasets.FashionMNIST(
    root="data",
    train=True,
    download=True,
    transform=ToTensor(),
)

# Download test data from open datasets.
test_data = datasets.FashionMNIST(
    root="data",
    train=False,
    download=True,
    transform=ToTensor(),
)

The first time this code runs, it will create a directory named data (in the same directory of whatever notebook you are working in), place the data there, and load it. After the first time, running this code again will simply read in the downloaded data.

Like MNIST, there are ten classes for the y data, also represented by integers. The following maps, in order, to the integers 0 to 9.

classes = [
    "T-shirt/top",
    "Trouser",
    "Pullover",
    "Dress",
    "Coat",
    "Sandal",
    "Shirt",
    "Sneaker",
    "Bag",
    "Ankle boot",
]

The X data are 28x28 greyscale images of articles of clothing.

Sample Statistics

Before modeling, be sure to look at the data. Calculate the summary statistics requested on PrairieLearn and create a visualization for your report.

Models

For this lab you will select one model to submit to the autograder. You may use any modeling techniques you’d like. The only rules are:

  • Models must be built in PyTorch using nn.Module class.
  • Your model should be created with torch version 2.3.0 or newer.
  • Your model should be created with torchvision version 0.18.0 or newer.
  • Your model should be serialized to TorchScript.
    • Your serialized model must be less than 5MB.

While you can use any modeling technique, each lab is designed such that a model using only techniques seen so far in the course can pass the checks in the autograder.

We will use accuracy to assess your submitted model.

To obtain the maximum points via the autograder, your model performance must meet or exceed:

Test Accuracy: 0.90

Model Persistence

To submit your model to the autograder, you will first need to serialize your model. With a pytorch model named model_name, convert it to TorchScript. Then, write this object to disk.

# Convert to TorchScript
model_scripted = torch.jit.script(model_name)
# Write to disk
model_scripted.save("fashion-mnist.pt")

Template Notebook

Compute

Completing this lab necessitates access to powerful computational resources.

if torch.cuda.is_available():
    device = "cuda"
elif torch.backends.mps.is_available():
    device = "mps"
else:
    device = "cpu"

print(f"Using {device} device!")

If your machine does not allow for mps or cuda consider using:

Submission

Before submission, especially of your report, you should be sure to review the Lab Policy page!

On Canvas, be sure to submit both your source .ipynb file and a rendered .html version of the report.