forked from juefeix/pnn.pytorch.update
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.py
99 lines (85 loc) · 6.77 KB
/
dataloader.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import torch
import torchvision.datasets as datasets
import torchvision.transforms as transforms
class Dataloader:
"""
Interface for load train and test data
"""
def __init__(self, args, input_size):
self.args = args
self.dataset_train_name = args.dataset_train
self.dataset_test_name = args.dataset_test
self.input_size = input_size
### Train preparation ###
if self.dataset_train_name == 'CIFAR10' or self.dataset_train_name == 'CIFAR100':
self.dataset_train = getattr(datasets, self.dataset_train_name)(root=self.args.dataroot,
train=True,
download=True,
transform=transforms.Compose([
transforms.RandomCrop(self.input_size, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
]))
elif self.dataset_train_name == 'MNIST':
self.dataset_train = getattr(datasets, self.dataset_train_name)(root=self.args.dataroot,
train=True,
download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
elif self.dataset_train_name == 'EMNIST':
self.dataset_train = getattr(datasets, self.dataset_train_name)(root=self.args.dataroot,
split='balanced',
train=True,
download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,),(0.3081,))
]))
else:
raise(Exception("Unknown Dataset"))
### Test preparation ###
if self.dataset_test_name == 'CIFAR10' or self.dataset_test_name == 'CIFAR100':
self.dataset_test = getattr(datasets, self.dataset_test_name)(root=self.args.dataroot,
train=False,
download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
]))
elif self.dataset_test_name == 'MNIST':
self.dataset_test = getattr(datasets, self.dataset_test_name)(root=self.args.dataroot,
train=False,
download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
elif self.dataset_train_name == 'EMNIST':
self.dataset_test = getattr(datasets, self.dataset_train_name)(root=self.args.dataroot,
split='balanced',
train=False,
download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
else:
raise(Exception("Unknown Dataset"))
def create(self, flag=None):
if flag == "Train":
dataloader_train = torch.utils.data.DataLoader(self.dataset_train, batch_size=self.args.batch_size,
shuffle=True, num_workers=int(self.args.nthreads), pin_memory=True)
return dataloader_train
if flag == "Test":
dataloader_test = torch.utils.data.DataLoader(self.dataset_test, batch_size=self.args.batch_size,
shuffle=False, num_workers=int(self.args.nthreads), pin_memory=True)
return dataloader_test
if flag is None:
dataloader_train = torch.utils.data.DataLoader(self.dataset_train, batch_size=self.args.batch_size,
shuffle=True, num_workers=int(self.args.nthreads), pin_memory=True)
dataloader_test = torch.utils.data.DataLoader(self.dataset_test, batch_size=self.args.batch_size,
shuffle=False, num_workers=int(self.args.nthreads), pin_memory=True)
return dataloader_train, dataloader_test