-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata.py
More file actions
52 lines (40 loc) · 1.47 KB
/
data.py
File metadata and controls
52 lines (40 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""FashionMNIST data loading for classification.
Downloads FashionMNIST via torchvision and splits into train/val sets.
The images are converted to tensors (1x28x28) with values in [0, 1].
"""
import torch
from torch.utils.data import random_split
def load_data(split_ratio=0.8, seed=42):
"""Load FashionMNIST and split into train/val datasets.
Args:
split_ratio: Fraction of training data used for training (rest is validation).
seed: Random seed for reproducibility.
Returns:
(train_dataset, val_dataset) as random subsets of the FashionMNIST training set.
Raises:
ImportError: If torchvision is not installed.
"""
try:
from torchvision import datasets, transforms
except ImportError:
raise ImportError(
"torchvision is required for the classification recipe.\n"
"Install it with: uv pip install torchvision\n"
"Or: pip install torchvision"
)
transform = transforms.Compose([
transforms.ToTensor(),
])
full_dataset = datasets.FashionMNIST(
root="./data",
train=True,
download=True,
transform=transform,
)
train_size = int(len(full_dataset) * split_ratio)
val_size = len(full_dataset) - train_size
generator = torch.Generator().manual_seed(seed)
train_dataset, val_dataset = random_split(
full_dataset, [train_size, val_size], generator=generator
)
return train_dataset, val_dataset