-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist.py
24 lines (17 loc) · 840 Bytes
/
mnist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import torchvision
from torchvision import transforms
def get_mnist(split, path, normalize=True, as_image=True):
transform_list = [torchvision.transforms.ToTensor()]
if normalize:
# from Pytorch examples: https://github.com/pytorch/examples/blob/main/mnist/main.py
norm = transforms.Normalize((0.1307,), (0.3081,))
transform_list.append(norm)
# input shape (28,28) or (784,)
if not as_image:
view = torchvision.transforms.Lambda(lambda x: x.view(-1).view(784))
transform_list.append(view)
ds = torchvision.datasets.MNIST(root=path,
train= (split=='train'),
download=True,
transform=torchvision.transforms.Compose(transform_list))
return ds