if torch.cuda.is_available():
= "cuda"
device elif torch.backends.mps.is_available():
= "mps"
device else:
= "cpu"
device
print(f"Using {device} device!")
Lab 11: Fashion
For Lab 11, you will develop a model to classify images of clothing (fashion).
Background
No background for this lab!
Scenario and Goal
No scenario for this lab!
PyTorch
Unlike other labs, instead of sklearn
, you will be required to use PyTorch.
To install PyTorch and a necessary data package, use:
uv pip install torch
uv pip install torchvision
This lab will mostly follow the PyTorch Quickstart guide.
Compute
Completing this lab necessitates access to powerful computational resources.
If your machine does not allow for mps
or cuda
consider using:
- Google Colaboratory
- Be sure to change the runtime type to a GPU.
- Illinois Computes Research Notebooks
- Be sure to select the PyTorch option to utilize a GPU.
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
= datasets.FashionMNIST(
training_data ="data",
root=True,
train=True,
download=ToTensor(),
transform
)
# download test data from open datasets
= datasets.FashionMNIST(
test_data ="data",
root=False,
train=True,
download=ToTensor(),
transform )
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.
Models
For this lab you will select one model to submit to the autograder. You may use any modeling techniques you’d like, so long as it meets these requirements:
- Models must be built in PyTorch using
nn.Module
class. - Your model should be created with
torch
version2.6.0
or newer. - Your model should be created with
torchvision
version0.21.0
or newer. - Your model should be serialized to TorchScript.
- Your serialized model must be less than 5MB.
To obtain the maximum points via the autograder, your model must outperform the following metrics:
Test Accuracy: 0.9
Model Persistence
To submit your model to the autograder, you will first need to serialize your model. With a pytorch
model named model
, convert it to TorchScript. Then, write this object to disk.
# convert to TorchScript
= torch.jit.script(model)
model_scripted
# write to disk
"fashion.pt") model_scripted.save(
Submission
On Canvas, be sure to submit both your source .ipynb
file and a rendered .html
version of the report.