Spaces:
Sleeping
Sleeping
Download src/handwriting/data.py from lzpiece/IntelligentHandwrittenCGS: direct link, hf CLI and curl.
- Browser
- Download file 2.52 kB
-
https://huggingface.co/spaces/lzpiece/IntelligentHandwrittenCGS/resolve/main/src/handwriting/data.py
- Command line
-
hf download hf://spaces/lzpiece/IntelligentHandwrittenCGS/src/handwriting/data.py
-
curl -L -o data.py https://huggingface.co/spaces/lzpiece/IntelligentHandwrittenCGS/resolve/main/src/handwriting/data.py
2.52 kB
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from pathlib import Path | |
| from typing import Tuple | |
| import torch | |
| from torch.utils.data import DataLoader, Dataset, random_split | |
| from torchvision import datasets, transforms | |
| from .preprocess import MNIST_MEAN, MNIST_STD | |
| class DataLoaders: | |
| train: DataLoader | |
| val: DataLoader | |
| test: DataLoader | |
| def _build_transforms(use_augmentation: bool) -> Tuple[transforms.Compose, transforms.Compose]: | |
| base = [transforms.ToTensor()] | |
| aug = [] | |
| if use_augmentation: | |
| aug = [transforms.RandomAffine(degrees=15, translate=(0.1, 0.1), scale=(0.9, 1.1))] | |
| train_tf = transforms.Compose(aug + base) | |
| eval_tf = transforms.Compose(base) | |
| return train_tf, eval_tf | |
| def _maybe_normalize(tf: transforms.Compose, normalize: bool) -> transforms.Compose: | |
| if not normalize: | |
| return tf | |
| return transforms.Compose(list(tf.transforms) + [transforms.Normalize((MNIST_MEAN,), (MNIST_STD,))]) | |
| def make_mnist_loaders( | |
| data_root: Path, | |
| batch_size: int, | |
| seed: int, | |
| num_workers: int, | |
| use_augmentation: bool, | |
| normalize: bool = True, | |
| val_ratio: float = 0.1, | |
| ) -> DataLoaders: | |
| data_root.mkdir(parents=True, exist_ok=True) | |
| train_tf, eval_tf = _build_transforms(use_augmentation) | |
| train_tf = _maybe_normalize(train_tf, normalize=normalize) | |
| eval_tf = _maybe_normalize(eval_tf, normalize=normalize) | |
| full_train = datasets.MNIST(root=str(data_root), train=True, download=True, transform=train_tf) | |
| test_ds = datasets.MNIST(root=str(data_root), train=False, download=True, transform=eval_tf) | |
| n_total = len(full_train) | |
| n_val = int(n_total * val_ratio) | |
| n_train = n_total - n_val | |
| generator = torch.Generator().manual_seed(seed) | |
| train_ds, val_ds = random_split(full_train, [n_train, n_val], generator=generator) | |
| train_loader = DataLoader( | |
| train_ds, | |
| batch_size=batch_size, | |
| shuffle=True, | |
| num_workers=num_workers, | |
| pin_memory=torch.cuda.is_available(), | |
| ) | |
| val_loader = DataLoader( | |
| val_ds, | |
| batch_size=batch_size, | |
| shuffle=False, | |
| num_workers=num_workers, | |
| pin_memory=torch.cuda.is_available(), | |
| ) | |
| test_loader = DataLoader( | |
| test_ds, | |
| batch_size=batch_size, | |
| shuffle=False, | |
| num_workers=num_workers, | |
| pin_memory=torch.cuda.is_available(), | |
| ) | |
| return DataLoaders(train=train_loader, val=val_loader, test=test_loader) | |