-
Notifications
You must be signed in to change notification settings - Fork 5
/
data.py
285 lines (237 loc) · 9.21 KB
/
data.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import torch
from torchvision import datasets, transforms
import torch.utils.data as data
import numpy as np
import os
def get_loaders(opt):
if opt.dataset == 'mnist':
return get_mnist_loaders(opt)
elif opt.dataset == 'cifar10':
return get_cifar10_loaders(opt)
elif opt.dataset == 'cifar100':
return get_cifar100_loaders(opt)
elif opt.dataset == 'svhn':
return get_svhn_loaders(opt)
elif opt.dataset.startswith('imagenet'):
return get_imagenet_loaders(opt)
elif opt.dataset == 'logreg':
return get_logreg_loaders(opt)
elif 'class' in opt.dataset:
return get_logreg_loaders(opt)
def dataset_to_loaders(train_dataset, test_dataset, opt):
kwargs = {'num_workers': opt.workers,
'pin_memory': True} if opt.cuda else {}
idxdataset = IndexedDataset(train_dataset, opt, train=True)
train_sampler = None
train_loader = torch.utils.data.DataLoader(
idxdataset,
batch_size=opt.batch_size,
sampler=train_sampler,
shuffle=(train_sampler is None),
drop_last=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
IndexedDataset(test_dataset, opt),
batch_size=opt.test_batch_size, shuffle=False,
**kwargs)
train_test_loader = torch.utils.data.DataLoader(
IndexedDataset(train_dataset, opt, train=True),
batch_size=opt.test_batch_size, shuffle=False,
**kwargs)
return train_loader, test_loader, train_test_loader
def get_minvar_loader(train_loader, opt):
kwargs = {'num_workers': opt.workers,
'pin_memory': True} if opt.cuda else {}
idxdataset = train_loader.dataset
train_loader = torch.utils.data.DataLoader(
idxdataset,
batch_size=opt.g_batch_size,
shuffle=True,
drop_last=False, **kwargs)
return train_loader
class IndexedDataset(data.Dataset):
def __init__(self, dataset, opt, train=False):
np.random.seed(2222)
self.ds = dataset
self.opt = opt
def __getitem__(self, index):
subindex = index
img, target = self.ds[subindex]
return img, target, index
def __len__(self):
return len(self.ds)
def get_mnist_loaders(opt, **kwargs):
transform = transforms.ToTensor()
if not opt.no_transform:
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
train_dataset = datasets.MNIST(
opt.data, train=True, download=True, transform=transform)
test_dataset = datasets.MNIST(opt.data, train=False, transform=transform)
return dataset_to_loaders(train_dataset, test_dataset, opt, **kwargs)
def get_cifar10_100_transform(opt):
normalize = transforms.Normalize(mean=(0.4914, 0.4822, 0.4465),
std=(0.2023, 0.1994, 0.2010))
if opt.data_aug:
transform = [
transforms.RandomAffine(10, (.1, .1), (0.7, 1.2), 10),
transforms.ColorJitter(.2, .2, .2),
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32),
transforms.ToTensor(),
normalize,
]
else:
transform = [
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]
return normalize, transform
def get_cifar10_loaders(opt):
normalize, transform = get_cifar10_100_transform(opt)
train_dataset = datasets.CIFAR10(root=opt.data, train=True,
transform=transforms.Compose(transform),
download=True)
test_dataset = datasets.CIFAR10(
root=opt.data, train=False, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
normalize,
]))
return dataset_to_loaders(train_dataset, test_dataset, opt)
def get_cifar100_loaders(opt):
normalize, transform = get_cifar10_100_transform(opt)
train_dataset = datasets.CIFAR100(root=opt.data, train=True,
transform=transforms.Compose(transform),
download=True)
test_dataset = datasets.CIFAR100(
root=opt.data, train=False, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
normalize,
]))
return dataset_to_loaders(train_dataset, test_dataset, opt)
def get_svhn_loaders(opt, **kwargs):
normalize = transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))
if opt.data_aug:
transform = [
transforms.RandomAffine(10, (.1, .1), (0.7, 1.), 10),
transforms.ColorJitter(.2, .2, .2),
transforms.RandomCrop(32),
transforms.ToTensor(),
normalize,
]
else:
transform = [
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))
]
train_dataset = torch.utils.data.ConcatDataset(
(datasets.SVHN(
opt.data, split='train', download=True,
transform=transforms.Compose(transform)),
datasets.SVHN(
opt.data, split='extra', download=True,
transform=transforms.Compose(transform))))
test_dataset = datasets.SVHN(opt.data, split='test', download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))
]))
return dataset_to_loaders(train_dataset, test_dataset, opt)
def get_imagenet_loaders(opt):
# Data loading code
traindir = os.path.join(opt.data, 'train')
valdir = os.path.join(opt.data, 'val')
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
train_dataset = datasets.ImageFolder(
traindir,
transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]))
test_dataset = datasets.ImageFolder(valdir, transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
]))
return dataset_to_loaders(train_dataset, test_dataset, opt)
class InfiniteLoader(object):
def __init__(self, data_loader):
self.data_loader = data_loader
def __iter__(self):
self.data_iter = iter([])
return self
def __next__(self):
try:
data = next(self.data_iter)
except StopIteration:
if isinstance(self.data_loader, list):
II = self.data_loader
self.data_iter = (II[i] for i in torch.randperm(len(II)))
else:
self.data_iter = iter(self.data_loader)
data = next(self.data_iter)
return data
def next(self):
# for python2
return self.__next__()
def __len__(self):
return len(self.data_loader)
def random_orthogonal_matrix(gain, shape):
if len(shape) < 2:
raise RuntimeError("Only shapes of length 2 or more are "
"supported.")
flat_shape = (shape[0], np.prod(shape[1:]))
a = np.random.normal(0.0, 1.0, flat_shape)
u, _, v = np.linalg.svd(a, full_matrices=False)
# pick the one with the correct shape
q = u if u.shape == flat_shape else v
q = q.reshape(shape)
return np.asarray(gain * q, dtype=np.float)
class LinearDataset(data.Dataset):
def __init__(self, C, D, num, dim, num_class, train=True):
X = np.zeros((C.shape[0], num))
Y = np.zeros((num,))
for i in range(num_class):
n = num // num_class
e = np.random.normal(0.0, 1.0, (dim, n))
X[:, i * n:(i + 1) * n] = np.dot(D[:, :, i], e) + C[:, i:i + 1]
Y[i * n:(i + 1) * n] = i
self.X = X
self.Y = Y
self.classes = range(num_class)
def __getitem__(self, index):
X = torch.Tensor(self.X[:, index]).float()
Y = int(self.Y[index])
return X, Y
def __len__(self):
return self.X.shape[1]
def get_logreg_loaders(opt, **kwargs):
# np.random.seed(1234)
np.random.seed(2222)
# print("Create W")
C = opt.c_const * random_orthogonal_matrix(1.0, (opt.dim, opt.num_class))
D = opt.d_const * random_orthogonal_matrix(
1.0, (opt.dim, opt.dim, opt.num_class))
# print("Create train")
train_dataset = LinearDataset(C, D, opt.num_train_data, opt.dim,
opt.num_class, train=True)
# print("Create test")
test_dataset = LinearDataset(C, D,
opt.num_test_data, opt.dim, opt.num_class,
train=False)
torch.save((train_dataset.X, train_dataset.Y,
test_dataset.X, test_dataset.Y,
C), opt.logger_name + '/data.pth.tar')
return dataset_to_loaders(train_dataset, test_dataset, opt)