-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
41 lines (31 loc) · 1.3 KB
/
datasets.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import torch
import torchvision
from torch.utils.data import DataLoader
import transforms
class MNIST(object):
def __init__(self, batch_size, use_gpu, num_workers):
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
pin_memory = True if use_gpu else False
trainset = torchvision.datasets.MNIST(root='./data/mnist', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=batch_size, shuffle=True,
num_workers=num_workers, pin_memory=pin_memory,
)
testset = torchvision.datasets.MNIST(root='./data/mnist', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(
testset, batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=pin_memory,
)
self.trainloader = trainloader
self.testloader = testloader
self.num_classes = 10
__factory = {
'mnist': MNIST,
}
def create(name, batch_size, use_gpu, num_workers):
if name not in __factory.keys():
raise KeyError("Unknown dataset: {}".format(name))
return __factory[name](batch_size, use_gpu, num_workers)