Lab 10: Fashion Image Classifier

Author

Your Name Here

Published

December 14, 2023

Introduction and Data

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

This lab is a part of the final project.

Unlike previous labs, there will be no template provided, and you are not required to calculate any summary statistics. Although you certainly can and should calculate summary statistics on the training data. But for your benefit, not for grading. Additionally, you will only submit a model, not a notebook.

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

We will access this data through tools available in pytorch.

After training a model, you will submit to the autograder found on PrairieLearn.

Additional submission details below.

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.

You are free to import packages and modules as you see fit.

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.

Model Training (Graded Work)

In the autograder, we will use accuracy to evaluate your submitted model.

To submit your models to the autograder, you will need to serialize them. With a pytorch model, we will first take your model, named _______, and convert it to TorchScript. Then, when will write it to disk.

Be sure to change _______ to the name of the model you defined, which should be a class built off of nn.Module then fit to data.

# Convert to TorchScript
model_scripted = torch.jit.script("_______")
# Write to disk
model_scripted.save("fashion_model.pt")
TypeError: <class 'str'> is a built-in class

As always, your submitted model must be less than 5MB.

Notes

It will be important that you use adequate compute resources for this lab. Run the following:

# Get cpu, gpu or mps device for training
device = "cpu"
if torch.cuda.is_available():
    device = "cuda"
if torch.backends.mps.is_available():
    device = "mps"
if torch.cuda.is_available():
    device = "cuda"
print(f"Using {device} device")

If this code returns mps or cuda, you’re all set!

If it returns cpu, your machine will likely be inadequate, that is, it will take far to long to train models.

Thankfully, there is a solution! Use Google Colab!

Be sure to sign-in with your University credentials!