-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataloader.py
277 lines (206 loc) · 8.46 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
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
import os
import warnings
import numpy as np
import torch as th
import torchnet as tnt
import torchvision
import torchvision.transforms as transforms
from torchvision.datasets import CIFAR10, CIFAR100, FashionMNIST, MNIST
import torch.utils.data.sampler as sampler
from math import floor
def cutout(mask_size,channels=3):
if channels>1:
mask_color=tuple([0]*channels)
else:
mask_color=0
mask_size_half = mask_size // 2
offset = 1 if mask_size % 2 == 0 else 0
if mask_size >0:
def _cutout(image):
image = np.asarray(image).copy()
if channels >1:
h, w = image.shape[:2]
else:
h, w = image.shape
cxmin, cxmax = 0, w + offset
cymin, cymax = 0, h + offset
cx = np.random.randint(cxmin, cxmax)
cy = np.random.randint(cymin, cymax)
xmin = cx - mask_size_half
ymin = cy - mask_size_half
xmax = xmin + mask_size
ymax = ymin + mask_size
xmin = max(0, xmin)
ymin = max(0, ymin)
xmax = min(w, xmax)
ymax = min(h, ymax)
square = image[ymin:ymax, xmin:xmax]
avg = np.mean(square, axis=(0,1))
image[ymin:ymax, xmin:xmax] = avg
if channels==1:
image = image[:,:,None]
return image
else:
def _cutout(image):
return image
return _cutout
def mnist(datadir, training_transforms=[], mode='train', transform=False, greyscale=False, **kwargs):
assert mode in ['train', 'test']
if greyscale==True:
warnings.warn('mnist is already greyscale')
if len(training_transforms)>0 or transform is True:
warnings.warn('mnist dataloader does not transform images')
train=mode=='train'
ds = MNIST(root=os.path.join(datadir,'mnist'), download=True, train=train)
data = getattr(ds, 'train_data' if train else 'test_data')
labels = getattr(ds, 'train_labels' if train else 'test_labels')
augment = tnt.transform.compose([lambda x: x[None,:,:].float() /255.0])
tds = tnt.dataset.TensorDataset([data, labels])
tds = tds.transform({0:augment})
tds = tds.parallel(**kwargs)
tds.Nsamples= 60000 if mode=='train' else 10000
tds.classes = 10
tds.image_shape = (1, 28, 28)
return tds
def TinyImageNet(datadir, greyscale=False,
training_transforms = [], mode='train', transform=True, **kwargs):
assert mode in ['train', 'test', 'val']
dataset_dir = os.path.join(datadir,'TinyImageNet')
gtransf = [transforms.Grayscale()] if greyscale else []
pathexist = os.path.isdir(dataset_dir)
if not pathexist:
raise ValueError('download TinyImageNet from "http://cs231n.stanford.edu/tiny-imagenet-200.zip" '
' and unzip to %s'%sdatset_dir)
train=mode=='train'
if train and transform:
d = os.path.join(dataset_dir,mode)
train_dataset = torchvision.datasets.ImageFolder(d, transforms.Compose([
*gtransf,
transforms.RandomCrop(64,padding=8),
transforms.RandomHorizontalFlip(),
*training_transforms,
transforms.ToTensor()]))
ds = th.utils.data.DataLoader(train_dataset, **kwargs)
else:
d = os.path.join(dataset_dir,mode)
test_dataset = torchvision.datasets.ImageFolder(d,
transforms.Compose([*gtransf,
transforms.ToTensor()]))
ds = th.utils.data.DataLoader(test_dataset, **kwargs)
if mode=='train':
ds.Nsamples = 100000
else:
ds.Nsamples = 10000 # both test and validation sets have 10000 images
ds.classes = 200
ds.image_shape = (3, 64, 64)
return ds
def Fashion(datadir, training_transforms = [], mode='train', transform=True, **kwargs):
assert mode in ['train', 'test']
train=mode=='train'
if train and transform:
tlist = [transforms.RandomCrop(28,padding=4),
transforms.RandomHorizontalFlip(),
*training_transforms,
transforms.ToTensor()]
else:
tlist = [transforms.ToTensor()]
transform = transforms.Compose(tlist)
root = os.path.join(datadir,'FashionMNIST')
ds = FashionMNIST(root, download=True, train=train, transform=transform)
dataloader = th.utils.data.DataLoader(ds, **kwargs)
dataloader.Nsamples = 60000 if train else 10000
dataloader.classes = 10
dataloader.image_shape = (1, 28, 28)
return dataloader
def cifar10(datadir, greyscale=False, training_transforms=[], mode='train', transform=True, subset=None, **kwargs):
assert mode in ['train', 'test', 'val']
train=mode=='train'
root = os.path.join(datadir,'cifar10')
gtransf = [transforms.Grayscale()] if greyscale else []
if mode in ['train','test']:
if train and transform:
tlist = [*gtransf,
transforms.RandomCrop(32,padding=4),
transforms.RandomHorizontalFlip(),
*training_transforms,
transforms.ToTensor()]
else:
tlist = [*gtransf,
transforms.ToTensor()]
transform = transforms.Compose(tlist)
ds = CIFAR10(root, download=True, train=train, transform=transform)
sample_size = 50000 if train else 10000
else:
filename = 'cifar10.1'
label_filename = 'cifar10.1_v6_labels.npy'
imagedata_filename = 'cifar10.1_v6_data.npy'
label_filepath = os.path.join(root, label_filename)
imagedata_filepath = os.path.join(root, imagedata_filename)
try:
labels = np.load(label_filepath)
data = np.load(imagedata_filepath)
except FileNotFoundError as e:
raise type(e)('Download CIFAR10.1 .npy files from https://github.com/modestyachts/CIFAR-10.1 '
'and place in %s'%root)
ds = tnt.dataset.TensorDataset([data, labels])
augment = transforms.ToTensor()
ltrans = lambda x: np.array(x, dtype=np.int_)
ds = ds.transform({0:augment, 1:ltrans})
sample_size=2000
# check if we're looking at a range
if isinstance(subset, range):
indices = np.arange(subset.start, subset.stop)
elif isinstance(subset, tuple) and len(subset)==2:
indices = np.arange(subset[0], subset[1])
elif isinstance(subset, np.ndarray):
indices = subset
elif isinstance(subset, float):
if (subset > 0. and subset < 1.):
num_samples = floor(subset * sample_size)
assert num_samples >0
indices = np.random.choice(sample_size, num_samples)
else:
raise ValueError('subset fraction must be between 0 and 1')
elif subset is not None:
raise ValueError('Invalid subset parameter.')
if subset:
# according to Pytorch docs shuffle cannot be true if we are using a sampler
# so we're going to turn it off in case that it's on
kwargs['shuffle'] = False
dataloader = th.utils.data.DataLoader(ds,
sampler=sampler.SubsetRandomSampler(indices), **kwargs)
dataloader.Nsamples = indices.size
else:
dataloader = th.utils.data.DataLoader(ds, **kwargs)
if mode=='train':
dataloader.Nsamples = 50000
elif mode=='test':
dataloader.Nsamples = 10000
else:
dataloader.Nsamples = 2000
dataloader.classes = 10
dataloader.image_shape = (3, 32, 32)
return dataloader
def cifar100(datadir, greyscale=False,
training_transforms = [], mode='train', transform=True, **kwargs):
assert mode in ['train', 'test']
train=mode=='train'
gtransf = [transforms.Grayscale()] if greyscale else []
if train and transform:
tlist = [*gtransf,
transforms.RandomCrop(32,padding=4),
transforms.RandomHorizontalFlip(),
*training_transforms,
transforms.ToTensor()]
else:
tlist = [*gtransf,
transforms.ToTensor()]
transform = transforms.Compose(tlist)
root = os.path.join(datadir,'cifar100')
ds = CIFAR100(root, download=True, train=train,transform=transform)
dataloader = th.utils.data.DataLoader(ds,
**kwargs)
dataloader.Nsamples = 50000 if train else 10000
dataloader.classes = 100
dataloader.image_shape = (3, 32, 32)
return dataloader