-
Notifications
You must be signed in to change notification settings - Fork 4
/
datasets.py
316 lines (248 loc) · 11 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
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
from PIL import Image
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, datasets
from torchvision.datasets.utils import check_integrity
from typing import *
import numpy as np
import os
import pickle
import torch
# for MRI Dataset
from pathlib import Path
from scipy.io import loadmat
import copy
import json
from torchvision.transforms import ToTensor
# set this environment variable to the location of your imagenet directory if you want to read ImageNet data.
# make sure your val directory is preprocessed to look like the train directory, e.g. by running this script
# https://raw.githubusercontent.com/soumith/imagenetloader.torch/master/valprep.sh
IMAGENET_LOC_ENV = "IMAGENET_DIR"
# list of all datasets
DATASETS = ["imagenet", "imagenet32", "cifar10", "mnist", "stl10", "restricted_imagenet"]
img_to_tensor = ToTensor()
def get_dataset(dataset: str, split: str) -> Dataset:
"""Return the dataset as a PyTorch Dataset object"""
if dataset == "imagenet":
return _imagenet(split)
elif dataset == "imagenet32":
return _imagenet32(split)
elif dataset == "cifar10":
return _cifar10(split)
elif dataset == "stl10":
return _stl10(split)
def get_num_classes(dataset: str):
"""Return the number of classes in the dataset. """
if dataset == "imagenet":
return 1000
elif dataset == "stl10":
return 10
elif dataset == "cifar10":
return 10
def get_normalize_layer(dataset: str) -> torch.nn.Module:
"""Return the dataset's normalization layer"""
if dataset == "imagenet":
return NormalizeLayer(_IMAGENET_MEAN, _IMAGENET_STDDEV)
elif dataset == "cifar10":
return NormalizeLayer(_CIFAR10_MEAN, _CIFAR10_STDDEV)
elif dataset == "imagenet32":
return NormalizeLayer(_IMAGENET_MEAN, _IMAGENET_STDDEV)
elif dataset == "tinyimagenet":
return NormalizeLayer(_IMAGENET_MEAN, _IMAGENET_STDDEV)
elif dataset == "mnist":
return NormalizeLayer(_MNIST_MEAN, _MNIST_STDDEV)
elif dataset == "stl10":
return NormalizeLayer(_STL10_MEAN, _STL10_STDDEV)
def get_input_center_layer(dataset: str) -> torch.nn.Module:
"""Return the dataset's Input Centering layer"""
if dataset == "imagenet":
return InputCenterLayer(_IMAGENET_MEAN)
elif dataset == "cifar10":
return InputCenterLayer(_CIFAR10_MEAN)
_IMAGENET_MEAN = [0.485, 0.456, 0.406]
_IMAGENET_STDDEV = [0.229, 0.224, 0.225]
_CIFAR10_MEAN = [0.4914, 0.4822, 0.4465]
_CIFAR10_STDDEV = [0.2023, 0.1994, 0.2010]
_MNIST_MEAN = [0.12486005]
_MNIST_STDDEV = [0.4898408]
_STL10_MEAN = [1.7776489e-07, -3.6621095e-08, -9.346008e-09]
_STL10_STDDEV = [1.0, 1.0, 1.0]
def _stl10(split: str) -> Dataset:
dataset_path = os.path.join(os.getenv('PT_DATA_DIR', 'datasets'), 'stl10')
if split == "train":
return datasets.STL10(dataset_path, split='train', download=True, transform=transforms.Compose([transforms.RandomHorizontalFlip(p=0.5),transforms.RandomVerticalFlip(p=0.5),transforms.ToTensor()]))
if split == "train+unlabeled":
return datasets.STL10(dataset_path, split='train+unlabeled', download=True, transform=transforms.Compose(
[transforms.RandomHorizontalFlip(p=0.5), transforms.RandomVerticalFlip(p=0.5), transforms.ToTensor()]))
elif split == "test":
return datasets.STL10(dataset_path, split='test', download=True, transform=transforms.ToTensor())
else:
raise Exception("Unknown split name.")
def _cifar10(split: str) -> Dataset:
dataset_path = os.path.join(os.getenv('PT_DATA_DIR', 'datasets'), 'dataset_cache')
if split == "train":
return datasets.CIFAR10(dataset_path, train=True, download=True, transform=transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()
]))
elif split == "test":
return datasets.CIFAR10(dataset_path, train=False, download=True, transform=transforms.ToTensor())
else:
raise Exception("Unknown split name.")
def _mnist(split: str) -> Dataset:
dataset_path = os.path.join(os.getenv('PT_DATA_DIR', 'datasets'), 'dataset_cache')
if split == "train":
return datasets.MNIST(dataset_path, train=True, download=True, transform=transforms.ToTensor())
elif split == "test":
return datasets.MNIST(dataset_path, train=False, download=True, transform=transforms.ToTensor())
else:
raise Exception("Unknown split name.")
def _imagenet(split: str) -> Dataset:
if not IMAGENET_LOC_ENV in os.environ:
raise RuntimeError("environment variable for ImageNet directory not set")
dir = os.environ[IMAGENET_LOC_ENV]
if split == "train":
subdir = os.path.join(dir, "train")
transform = transforms.Compose([
transforms.RandomSizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()
])
elif split == "test":
subdir = os.path.join(dir, "val")
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor()
])
return datasets.ImageFolder(subdir, transform)
def _imagenet32(split: str) -> Dataset:
dataset_path = os.path.join(os.getenv('PT_DATA_DIR', 'datasets'), 'Imagenet32')
if split == "train":
return ImageNetDS(dataset_path, 32, train=True, transform=transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()]))
elif split == "test":
return ImageNetDS(dataset_path, 32, train=False, transform=transforms.ToTensor())
class NormalizeLayer(torch.nn.Module):
"""Standardize the channels of a batch of images by subtracting the dataset mean
and dividing by the dataset standard deviation.
In order to certify radii in original coordinates rather than standardized coordinates, we
add the Gaussian noise _before_ standardizing, which is why we have standardization be the first
layer of the classifier rather than as a part of preprocessing as is typical.
"""
def __init__(self, means: List[float], sds: List[float]):
"""
:param means: the channel means
:param sds: the channel standard deviations
"""
super(NormalizeLayer, self).__init__()
self.means = torch.tensor(means).cuda()
self.sds = torch.tensor(sds).cuda()
def forward(self, input: torch.tensor):
(batch_size, num_channels, height, width) = input.shape
means = self.means.repeat((batch_size, height, width, 1)).permute(0, 3, 1, 2)
sds = self.sds.repeat((batch_size, height, width, 1)).permute(0, 3, 1, 2)
return (input - means)/sds
# from https://github.com/hendrycks/pre-training
class ImageNetDS(Dataset):
"""`Downsampled ImageNet <https://patrykchrabaszcz.github.io/Imagenet32/>`_ Datasets.
Args:
root (string): Root directory of dataset where directory
``ImagenetXX_train`` exists.
img_size (int): Dimensions of the images: 64,32,16,8
train (bool, optional): If True, creates dataset from training set, otherwise
creates from test set.
transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
target_transform (callable, optional): A function/transform that takes in the
target and transforms it.
"""
base_folder = 'Imagenet{}_train'
train_list = [
['train_data_batch_1', ''],
['train_data_batch_2', ''],
['train_data_batch_3', ''],
['train_data_batch_4', ''],
['train_data_batch_5', ''],
['train_data_batch_6', ''],
['train_data_batch_7', ''],
['train_data_batch_8', ''],
['train_data_batch_9', ''],
['train_data_batch_10', '']
]
test_list = [
['val_data', ''],
]
def __init__(self, root, img_size, train=True, transform=None, target_transform=None):
self.root = os.path.expanduser(root)
self.transform = transform
self.target_transform = target_transform
self.train = train # training set or test set
self.img_size = img_size
self.base_folder = self.base_folder.format(img_size)
# if not self._check_integrity():
# raise RuntimeError('Dataset not found or corrupted.') # TODO
# now load the picked numpy arrays
if self.train:
self.train_data = []
self.train_labels = []
for fentry in self.train_list:
f = fentry[0]
file = os.path.join(self.root, self.base_folder, f)
with open(file, 'rb') as fo:
entry = pickle.load(fo)
self.train_data.append(entry['data'])
self.train_labels += [label - 1 for label in entry['labels']]
self.mean = entry['mean']
self.train_data = np.concatenate(self.train_data)
self.train_data = self.train_data.reshape((self.train_data.shape[0], 3, 32, 32))
self.train_data = self.train_data.transpose((0, 2, 3, 1)) # convert to HWC
else:
f = self.test_list[0][0]
file = os.path.join(self.root, f)
fo = open(file, 'rb')
entry = pickle.load(fo)
self.test_data = entry['data']
self.test_labels = [label - 1 for label in entry['labels']]
fo.close()
self.test_data = self.test_data.reshape((self.test_data.shape[0], 3, 32, 32))
self.test_data = self.test_data.transpose((0, 2, 3, 1)) # convert to HWC
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
if self.train:
img, target = self.train_data[index], self.train_labels[index]
else:
img, target = self.test_data[index], self.test_labels[index]
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
if self.train:
return len(self.train_data)
else:
return len(self.test_data)
def _check_integrity(self):
root = self.root
for fentry in (self.train_list + self.test_list):
filename, md5 = fentry[0], fentry[1]
fpath = os.path.join(root, self.base_folder, filename)
if not check_integrity(fpath, md5):
return False
return True
if __name__ == "__main__":
dataset = get_dataset('imagenet32', 'train')
embed()