-
Notifications
You must be signed in to change notification settings - Fork 12
/
loaders.py
233 lines (196 loc) · 10.8 KB
/
loaders.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
'''
Data loading Utilities for preparing for various different datasets.
Includes, MNIST, CIFAR10, TinyImagenet.
For MNIST and CIFAR10 there are special adversarial samples prepared
for evaluation. -> <dataset>_adversarial(). Each function returns a
train and a test DataLoader except the dedicated functions for adversarial
samples that return a single loader.
'''
import torchvision.transforms as transforms
import torch
import torchvision
from torch.utils.data import *
import random
import numpy as np
TRANSFORMS_TR = transforms.Compose([
transforms.RandomCrop(28, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TR_COLOR32 = transforms.Compose([
transforms.Resize(32),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Lambda(lambda x : x.view(1, 32, 32).expand(3, -1, -1)),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TE = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TE_COLOR32 = transforms.Compose([
transforms.Resize(32),
transforms.ToTensor(),
transforms.Lambda(lambda x : x.view(1, 32, 32).expand(3, -1, -1)),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TR_CIFAR10 = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TR_CIFAR10_GRAY28 = transforms.Compose([
transforms.Grayscale(1),
transforms.Resize(28),
transforms.RandomCrop(28, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TE_CIFAR10 = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TE_CIFAR10_GRAY28 = transforms.Compose([
transforms.Grayscale(1),
transforms.Resize(28),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TR_SVHN = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TR_SVHN_GRAY28 = transforms.Compose([
transforms.Grayscale(1),
transforms.Resize(28),
transforms.RandomCrop(28, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TE_SVHN = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TE_SVHN_GRAY28 = transforms.Compose([
transforms.Grayscale(1),
transforms.Resize(28),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
TRANSFORMS_TR_IMAGENET = transforms.Compose([
transforms.Resize(32),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])
TRANSFORMS_TE_IMAGENET = transforms.Compose([
transforms.Resize(32),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])
TRANSFORMS_MNIST_ADV = transforms.Compose([
transforms.Grayscale(1),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
TRANSFORMS_MNIST = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
def loader(data, batch_size, subset=[], sampling=-1):
''' Interface to the dataloader function '''
if data == 'mnist_train':
return dataloader('mnist', './data', train=True, transform=TRANSFORMS_MNIST, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'mnist_test':
return dataloader('mnist', './data', train=False, transform=TRANSFORMS_MNIST, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
if data == 'mnist_color32_train':
return dataloader('mnist', './data', train=True, transform=TRANSFORMS_TR_COLOR32, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'mnist_color32_test':
return dataloader('mnist', './data', train=False, transform=TRANSFORMS_TE_COLOR32, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'cifar10_train':
return dataloader('cifar10', './data', train=True, transform=TRANSFORMS_TR_CIFAR10, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'cifar10_gray28_train':
return dataloader('cifar10', './data', train=True, transform=TRANSFORMS_TR_CIFAR10_GRAY28, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'cifar10_test':
return dataloader('cifar10', './data', train=False, transform=TRANSFORMS_TE_CIFAR10, batch_size=batch_size, sampling=sampling , num_workers=2, subset=subset)
elif data == 'cifar10_gray28_test':
return dataloader('cifar10', './data', train=False, transform=TRANSFORMS_TE_CIFAR10_GRAY28, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'svhn_train':
return dataloader('svhn', './data', train='train', transform=TRANSFORMS_TR_SVHN, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'svhn_test':
return dataloader('svhn', './data', train='test', transform=TRANSFORMS_TE_SVHN, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'svhn_gray28_train':
return dataloader('svhn', './data', train='train', transform=TRANSFORMS_TR_SVHN_GRAY28, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'svhn_gray28_test':
return dataloader('svhn', './data', train='test', transform=TRANSFORMS_TE_SVHN_GRAY28, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'fashion_mnist_train':
return dataloader('fashion_mnist', './data', train=True, transform=TRANSFORMS_TR, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'fashion_mnist_test':
return dataloader('fashion_mnist', './data', train=False, transform=TRANSFORMS_TE, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'fashion_mnist_color32_train':
return dataloader('fashion_mnist', './data', train=True, transform=TRANSFORMS_TR_COLOR32, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'fashion_mnist_color32_test':
return dataloader('fashion_mnist', './data', train=False, transform=TRANSFORMS_TE_COLOR32, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'lenet_mnist_adversarial_test':
return dataloader('/data/data1/datasets/lenet_mnist_adversarial/', train=False,
transform=TRANSFORMS_TE_CIFAR10, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'lenet_cifar10_adversarial_test':
return dataloader('/data/data1/datasets/lenet_cifar_adversarial/', train=False,
transform=TRANSFORMS_TE_CIFAR10, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'vgg_cifar10_adversarial_test':
return dataloader('vgg_cifar10_adversarial', '/data/data1/datasets/vgg_cifar_adversarial/', train=False, transform=TRANSFORMS_TE_CIFAR10, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'imagenet_train':
return dataloader('tinyimagenet', '/data/data1/datasets/tiny-imagenet-200/train/',
train=True, transform=TRANSFORMS_TR_IMAGENET, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
elif data == 'imagenet_test':
return dataloader('tinyimagenet', '/data/data1/datasets/tiny-imagenet-200/val/images/',
train=False, transform=TRANSFORMS_TE_IMAGENET, batch_size=batch_size, sampling=sampling, num_workers=2, subset=subset)
def get_dataset(data, path, train, transform):
''' Return loader for torchvision data. If data in [mnist, cifar] torchvision.datasets has built-in loaders else load from ImageFolder '''
if data == 'mnist':
dataset = torchvision.datasets.MNIST(path, train=train, download=True, transform=transform)
elif data == 'cifar10':
dataset = torchvision.datasets.CIFAR10(path, train=train, download=True, transform=transform)
elif data == 'svhn':
dataset = torchvision.datasets.SVHN(path, split=train, download=True, transform=transform)
elif data == 'fashion_mnist':
dataset = torchvision.datasets.FashionMNIST(path, train=train, download=True, transform=transform)
else:
dataset = torchvision.datasets.ImageFolder(path, transform=transform)
return dataset
def dataloader(data, path, train, transform, batch_size, num_workers, subset=[], sampling=-1):
dataset = get_dataset(data, path, train, transform)
if subset:
dataset = torch.utils.data.Subset(dataset, subset)
if sampling == -1:
sampler = SequentialSampler(dataset)
elif sampling == -2:
sampler = RandomSampler(dataset)
else:
sampler = BinarySampler(dataset, sampling)
return torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=sampler, num_workers=num_workers, drop_last=True)
class BinarySampler(Sampler):
"""One-vs-rest sampling where pivot indicates the target class """
def __init__(self, dataset, pivot):
self.dataset = dataset
self.pivot_indices = self._get_pivot_indices(pivot)
self.nonpivot_indices = self._get_nonpivot_indices()
self.indices = self._get_indices()
def _get_targets(self):
return [x for (_, x) in self.dataset]
def _get_pivot_indices(self, pivot):
return [i for i, x in enumerate(self._get_targets()) if x==pivot]
def _get_nonpivot_indices(self):
return random.sample(list(set(np.arange(len(self.dataset)))-set(self.pivot_indices)), len(self.pivot_indices))
def _get_indices(self):
return self.pivot_indices + self.nonpivot_indices
def __iter__(self):
return (self.indices[i] for i in torch.randperm(len(self.indices)))
def __len__(self):
return len(self.indices)