-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasets.py
More file actions
24 lines (19 loc) · 898 Bytes
/
Copy pathdatasets.py
File metadata and controls
24 lines (19 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import torch
import torchvision.datasets as datasets
import torchvision.transforms as transforms
def onehot_transform(label, num_class=10):
target = torch.zeros(num_class)
target[label] = 1
return target
def get_shift_MNIST(root="data", shift=2):
rand_shift_transform = transforms.RandomAffine(degrees=0,
translate=(shift/28, shift/28))
pil_to_tensor = transforms.ToTensor()
train = datasets.MNIST(root=root, train=True, download=True,
transform=transforms.Compose([rand_shift_transform,
pil_to_tensor]),
target_transform=onehot_transform)
test = datasets.MNIST(root=root, train=False, download=True,
transform=pil_to_tensor,
target_transform=onehot_transform)
return train, test