Custom datasets
Load your own data into PyTorch with Dataset, DataLoader, ImageFolder, samplers and a transforms pipeline.
Goal of the lesson
By the end of this 3-hour session you should be able to:
- explain the contract of
torch.utils.data.Dataset, - use
ImageFolderfor the common image-classification layout, - write your own
Datasetfor any data you have on disk, - compose a transforms pipeline with deterministic preprocessing and stochastic augmentation,
- balance unbalanced batches with
WeightedRandomSampler, - build a small image classifier from a folder of images you assembled yourself.
Suggested timing
| Block | Topic |
|---|---|
| 15 min | Why custom datasets, the Dataset contract |
| 25 min | Get the data, ImageFolder |
| 30 min | Transforms — deterministic vs. augmentation |
| 30 min | Write a Dataset from scratch |
| 25 min | DataLoader knobs and WeightedRandomSampler |
| 55 min | Capstone — your own image classifier |
Why custom datasets
Built-in datasets like FashionMNIST and MNIST are training wheels. The moment you have a real project you’ll be loading your own files: photos in folders, audio in WAV files, sensor logs in CSV, MRI scans in DICOM, etc.
PyTorch has a small, composable API for that:
| Building block | Purpose |
|---|---|
torch.utils.data.Dataset | Your data, indexed by integer. |
torch.utils.data.DataLoader | Wraps a Dataset to deliver batches, shuffling, parallel loading. |
torchvision.datasets.ImageFolder | A ready-made Dataset for images organized by folder. |
torchvision.transforms | Image preprocessing and augmentation. |
torch.utils.data.Sampler | Decides which indices to draw on each epoch. |
We’ll use a small subset of Food-101 (pizza, steak, sushi) as a running example. The same code patterns work for medical images, satellite images, audio spectrograms, or anything else you load from disk.
Setup
uv init --python 3.12 datasets
cd datasets
uv add torch torchvision matplotlib pillow requests
=
Get the data
The dataset ships as a zip on the mrdbourke/pytorch-deep-learning repo. Download it once and unpack it into data/.
=
= /
=
= /
= /
= /
The folder structure is the standard convention for image classification:
pizza_steak_sushi/
├── train/
│ ├── pizza/
│ ├── steak/
│ └── sushi/
└── test/
├── pizza/
├── steak/
└── sushi/Estàs llegint una vista prèvia.
Inicia sessió amb Google per llegir la pàgina completa.
Inicia sessió amb GoogleAmb qualsevol compte de Google. Només et demanarem que acceptis les condicions del servei.