Day 33: Project: train a tiny classifier end-to-end on Kaggle T4
First real GPU training run
Everything from Day 26 assembles today into a complete, GPU-trained classifier — on Kaggle's free T4 (or Colab). Start small: a handful of classes on a modest dataset (FashionMNIST is a perfect warmup before your own garment photos). The goal isn't accuracy yet; it's proving your end-to-end pipeline — Dataset, DataLoader, model, training loop, validation, checkpoint — actually runs on real hardware.
import torch, torch.nn as nn
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
device = "cuda" if torch.cuda.is_available() else "cpu"
tf = transforms.Compose([transforms.ToTensor()])
train = datasets.FashionMNIST(".", train=True, download=True, transform=tf)
val = datasets.FashionMNIST(".", train=False, download=True, transform=tf)
train_loader = DataLoader(train, batch_size=64, shuffle=True, num_workers=2)
val_loader = DataLoader(val, batch_size=256)
model = nn.Sequential(
nn.Flatten(), nn.Linear(28*28, 128), nn.ReLU(), nn.Linear(128, 10)
).to(device)
criterion, optimizer = nn.CrossEntropyLoss(), torch.optim.Adam(model.parameters(), 1e-3)
for epoch in range(5):
model.train()
for x, y in train_loader:
x, y = x.to(device), y.to(device)
optimizer.zero_grad()
loss = criterion(model(x), y)
loss.backward()
optimizer.step()Kaggle/Colab realities
Free GPU time is capped (Kaggle: ~30 h/week) and sessions die if idle. Save checkpoints to persistent storage, not just the session, or you'll lose a trained model to a disconnect. Set num_workers modestly (2) on these platforms — too many can actually slow things down there.
Prove the pipeline on a GPU
Train the FashionMNIST classifier above to >85% validation accuracy on a free T4, add a validation loop, and save a checkpoint you can reload. This is a rehearsal: once this pipeline is solid, swapping in your own garment dataset (Day 38) is a small change, not a new build.
On a free Kaggle/Colab GPU session, why is it critical to checkpoint your model to persistent storage during a long run?