-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader_cifar10.py
460 lines (384 loc) · 14.5 KB
/
data_loader_cifar10.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import torch
import numpy as np
from sklearn import model_selection
from torchvision import datasets
from torchvision import transforms
from torch.utils.data.sampler import SubsetRandomSampler, SequentialSampler
from autoaugment import CIFAR10Policy
from cutout import Cutout
import pickle
def get_train_valid_loader(data_dir,
batch_size,
augment=True,
shuffle=False,
show_sample=False,
num_workers=4,
pin_memory=False,
cutout=False,
cutout_length=16,
auto_augment=False,
resize=False,
datasetType='Full',
resizelength=300):
"""
Utility function for loading and returning train and valid
multi-process iterators over the CIFAR-10 dataset. A sample
9x9 grid of the images can be optionally displayed.
If using CUDA, num_workers should be set to 1 and pin_memory to True.
Params
------
- data_dir: path directory to the dataset.
- batch_size: how many samples per batch to load.
- augment: whether to apply the data augmentation scheme
mentioned in the paper. Only applied on the train split.
- random_seed: fix seed for reproducibility.
- valid_size: percentage split of the training set used for
the validation set. Should be a float in the range [0, 1].
- shuffle: whether to shuffle the train/validation indices.
- show_sample: plot 9x9 sample grid of the dataset.
- num_workers: number of subprocesses to use when loading the dataset.
- pin_memory: whether to copy tensors into CUDA pinned memory. Set it to
True if using GPU.
Returns
-------
- train_loader: training set iterator.
- valid_loader: validation set iterator.
"""
CIFAR_MEAN = [0.49139968, 0.48215827, 0.44653124]
CIFAR_STD = [0.24703233, 0.24348505, 0.26158768]
normalize = transforms.Normalize(
mean=CIFAR_MEAN,
std=CIFAR_STD,
)
valid_transform = []
# define transforms
if resize:
valid_transform = [
transforms.Resize(resizelength),
]
valid_transform.extend([
transforms.ToTensor(),
normalize,
])
valid_transform = transforms.Compose(valid_transform)
if resize:
train_transform = [
transforms.Resize(resizelength),
transforms.RandomCrop(resizelength, padding=4),
]
else:
train_transform = [
transforms.RandomCrop(32, padding=4),
]
train_transform.extend([
transforms.RandomHorizontalFlip(),
])
if auto_augment:
train_transform.extend([
CIFAR10Policy(),
])
train_transform.extend([
transforms.ToTensor(),
normalize,
])
if cutout:
train_transform.extend([
Cutout(cutout_length),
])
train_transform = transforms.Compose(train_transform)
'''
if resize:
train_transform = transforms.Compose([
transforms.Resize(resizelength),
transforms.RandomCrop(resizelength, padding=4),
transforms.RandomHorizontalFlip(), CIFAR10Policy(),
transforms.ToTensor(),
normalize,
])
else:
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(), CIFAR10Policy(),
transforms.ToTensor(),
normalize,
])
if cutout:
train_transform.transforms.append(Cutout(cutout_length)) #can be changed
'''
#if auto_augment:
# train_transform.transforms.insert(2, AutoAugment())
'''
else:
train_transform = transforms.Compose([
transforms.ToTensor(),
normalize,
])
'''
# load the dataset
train_dataset = datasets.CIFAR10(
root=data_dir, train=True,
download=True, transform=train_transform,
)
valid_dataset = datasets.CIFAR10(
root=data_dir, train=True,
download=True, transform=valid_transform,
)
# Generate stratified splits, and store indexes
'''
targets = train_dataset.targets
train_idx, valid_idx = model_selection.train_test_split(
np.arange(len(targets)), test_size=0.02, train_size=0.08, random_state=42, shuffle=True, stratify=targets)
# Check stratification
print(np.unique(np.array(targets)[train_idx], return_counts=True))
print(np.unique(np.array(targets)[valid_idx], return_counts=True))
with open('./data/trainPartial10Cifar10Indexes', 'wb') as f:
pickle.dump(train_idx, f)
with open('./data/valPartial10Cifar10Indexes', 'wb') as f:
pickle.dump(valid_idx, f)
'''
if datasetType.lower() == 'full':
with open(data_dir+'trainFullCifar10Indexes', 'rb') as f:
train_idx = pickle.load(f)
with open(data_dir+'valFullCifar10Indexes', 'rb') as f:
valid_idx = pickle.load(f)
elif "trainentire" in datasetType.lower():
#with open(data_dir+'valFullCifar10Indexes', 'rb') as f:
# valid_idx = pickle.load(f)
return (get_entire_train(train_dataset, valid_dataset, batch_size, shuffle, num_workers, pin_memory, data_dir, resize, resizelength))
elif "partial" in datasetType.lower() and "fly" in datasetType.lower():
targets = train_dataset.targets
train_idx, valid_idx = model_selection.train_test_split(
np.arange(len(targets)), test_size=0.02, train_size=0.08, random_state=42, shuffle=True, stratify=targets)
else: #Partial
with open(data_dir+'trainPartial10Cifar10Indexes', 'rb') as f:
train_idx = pickle.load(f)
with open(data_dir+'valPartial10Cifar10Indexes', 'rb') as f:
valid_idx = pickle.load(f)
# Datasets are already shuffled using scikit to create the indexes
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=shuffle,
sampler=SequentialSampler(train_idx),
num_workers=num_workers, pin_memory=pin_memory,
)
valid_loader = torch.utils.data.DataLoader(
valid_dataset, batch_size=batch_size, shuffle=shuffle,
sampler=SequentialSampler(valid_idx),
num_workers=num_workers, pin_memory=pin_memory,
)
'''
# Full Dataset, normal
num_train = len(train_dataset)
indices = list(range(num_train))
split = int(np.floor(valid_size * num_train))
if shuffle:
np.random.seed(random_seed)
np.random.shuffle(indices)
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, sampler=train_sampler,
num_workers=num_workers, pin_memory=pin_memory,
)
valid_loader = torch.utils.data.DataLoader(
valid_dataset, batch_size=batch_size, sampler=valid_sampler,
num_workers=num_workers, pin_memory=pin_memory,
)
'''
n_classes = 10
return (train_loader, valid_loader, n_classes)
def get_entire_train(train_dataset, valid_dataset, batch_size, shuffle, num_workers, pin_memory, data_dir, resize=False, resizelength=300):
CIFAR_MEAN = [0.49139968, 0.48215827, 0.44653124]
CIFAR_STD = [0.24703233, 0.24348505, 0.26158768]
normalize = transforms.Normalize(
mean=CIFAR_MEAN,
std=CIFAR_STD,
)
valid_transform = []
# define transforms
if resize:
valid_transform = [
transforms.Resize(resizelength),
]
valid_transform.extend([
transforms.ToTensor(),
normalize,
])
valid_transform = transforms.Compose(valid_transform)
valid_dataset = datasets.CIFAR10(
root=data_dir, train=False,
download=True, transform=valid_transform,
)
valid_loader = torch.utils.data.DataLoader(
valid_dataset, batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=pin_memory,
)
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=shuffle,
num_workers=num_workers, pin_memory=pin_memory,
)
n_classes = 10
return (train_loader, valid_loader, n_classes)
def get_test_loader(data_dir,
batch_size,
shuffle=False,
num_workers=4,
pin_memory=False,
resize=False,
resizelength=300):
"""
Utility function for loading and returning a multi-process
test iterator over the CIFAR-10 dataset.
If using CUDA, num_workers should be set to 1 and pin_memory to True.
Params
------
- data_dir: path directory to the dataset.
- batch_size: how many samples per batch to load.
- shuffle: whether to shuffle the dataset after every epoch.
- num_workers: number of subprocesses to use when loading the dataset.
- pin_memory: whether to copy tensors into CUDA pinned memory. Set it to
True if using GPU.
Returns
-------
- data_loader: test set iterator.
"""
CIFAR_MEAN = [0.49139968, 0.48215827, 0.44653124]
CIFAR_STD = [0.24703233, 0.24348505, 0.26158768]
normalize = transforms.Normalize(
mean=CIFAR_MEAN,
std=CIFAR_STD,
)
# define transform
if resize:
transform = transforms.Compose([
transforms.Resize(resizelength),
transforms.ToTensor(),
normalize,])
else:
transform = transforms.Compose([
transforms.ToTensor(),
normalize,])
dataset = datasets.CIFAR10(
root=data_dir, train=False,
download=True, transform=transform,
)
'''
# Generate partial test dataset
targets = dataset.targets
_, test_idx = model_selection.train_test_split(
np.arange(len(targets)), test_size=0.1, random_state=42, shuffle=True, stratify=targets)
with open('./data/testPartial10Cifar10Indexes', 'wb') as f:
pickle.dump(test_idx, f)
print(np.unique(np.array(targets)[test_idx], return_counts=True))
'''
#if datasetType == "Full":
# Full test dataset
data_loader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, shuffle=shuffle,
num_workers=num_workers, pin_memory=pin_memory,
)
'''
else: #Partial
with open(data_dir+'testPartial10Cifar10Indexes', 'rb') as f:
test_idx = pickle.load(f)
data_loader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, shuffle=shuffle, sampler=SequentialSampler(test_idx),
num_workers=num_workers, pin_memory=pin_memory,
)
'''
return data_loader
'''
def get_train_valid_loader(data_dir,
batch_size,
augment,
random_seed,
valid_size=0.1,
shuffle=True,
show_sample=False,
num_workers=4,
pin_memory=False):
"""
Utility function for loading and returning train and valid
multi-process iterators over the CIFAR-10 dataset. A sample
9x9 grid of the images can be optionally displayed.
If using CUDA, num_workers should be set to 1 and pin_memory to True.
Params
------
- data_dir: path directory to the dataset.
- batch_size: how many samples per batch to load.
- augment: whether to apply the data augmentation scheme
mentioned in the paper. Only applied on the train split.
- random_seed: fix seed for reproducibility.
- valid_size: percentage split of the training set used for
the validation set. Should be a float in the range [0, 1].
- shuffle: whether to shuffle the train/validation indices.
- show_sample: plot 9x9 sample grid of the dataset.
- num_workers: number of subprocesses to use when loading the dataset.
- pin_memory: whether to copy tensors into CUDA pinned memory. Set it to
True if using GPU.
Returns
-------
- train_loader: training set iterator.
- valid_loader: validation set iterator.
"""
error_msg = "[!] valid_size should be in the range [0, 1]."
assert ((valid_size >= 0) and (valid_size <= 1)), error_msg
normalize = transforms.Normalize(
mean=[0.4914, 0.4822, 0.4465],
std=[0.2023, 0.1994, 0.2010],
)
# define transforms
valid_transform = transforms.Compose([
transforms.ToTensor(),
normalize,
])
if augment:
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
else:
train_transform = transforms.Compose([
transforms.ToTensor(),
normalize,
])
# load the dataset
train_dataset = datasets.CIFAR10(
root=data_dir, train=True,
download=True, transform=train_transform,
)
valid_dataset = datasets.CIFAR10(
root=data_dir, train=True,
download=True, transform=valid_transform,
)
num_train = len(train_dataset)
indices = list(range(num_train))
split = int(np.floor(valid_size * num_train))
if shuffle:
np.random.seed(random_seed)
np.random.shuffle(indices)
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, sampler=train_sampler,
num_workers=num_workers, pin_memory=pin_memory,
)
valid_loader = torch.utils.data.DataLoader(
valid_dataset, batch_size=batch_size, sampler=valid_sampler,
num_workers=num_workers, pin_memory=pin_memory,
)
# visualize some images
if show_sample:
sample_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=9, shuffle=shuffle,
num_workers=num_workers, pin_memory=pin_memory,
)
data_iter = iter(sample_loader)
images, labels = data_iter.next()
X = images.numpy().transpose([0, 2, 3, 1])
plot_images(X, labels)
return (train_loader, valid_loader)
'''