-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader.py
304 lines (248 loc) · 12.1 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
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
import torch
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, random_split
from torchvision.models import Inception_V3_Weights
from dataset import CelebaDataset, ImagenetDataset
def my_collate_fn(data):
# TODO: Implement your function
# But I guess in your case it should be:
return tuple(data)
# return data
def lambda_function(x):
return torch.flatten(x)
def load_mnist(batch_size: int=64, root: str=None):
"""
Load MNIST data
"""
root = root + "datasets/MNIST/"
t = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda_function)]
)
train = datasets.MNIST(root=root, train=True, download=True, transform=t)
train_data, valid_data = random_split(train, [55000, 5000])
test_data = datasets.MNIST(root=root, train=False, download=True, transform=t)
train_dataloader = DataLoader(train_data, batch_size=batch_size, pin_memory=True, shuffle=True, num_workers=4)
valid_dataloader = DataLoader(valid_data, batch_size=batch_size, pin_memory=True, num_workers=4)
test_dataloader = DataLoader(test_data, batch_size=batch_size, pin_memory=True, num_workers=4)
return train_dataloader, valid_dataloader, test_dataloader
def load_mnist_x(batch_size: int=64, root: str=None, dataset_len=1000):
"""
Load MNIST 1000 data
"""
torch.manual_seed(43)
root = root + "datasets/MNIST/"
t = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda_function)]
)
train = datasets.MNIST(root=root, train=True, download=True, transform=t)
train_data, _ = random_split(train, [dataset_len, len(train)-dataset_len])
train_dataloader = DataLoader(train_data, batch_size=batch_size, pin_memory=True, num_workers=4)
return train_dataloader
def load_cifar(batch_size: int=64, root: str=None):
"""
Load CIFAR-10 data
"""
root = root + "datasets/CIFAR10/"
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
train = datasets.CIFAR10(root=root, train=True, download=True, transform=transform)
train_data, valid_data = random_split(train, [45000, 5000])
test_data = datasets.CIFAR10(root=root, train=False, download=True, transform=transform)
train_dataloader = DataLoader(train_data, batch_size=batch_size, pin_memory=True, shuffle=True, num_workers=4)
valid_dataloader = DataLoader(valid_data, batch_size=batch_size, pin_memory=True, num_workers=4)
test_dataloader = DataLoader(test_data, batch_size=batch_size, pin_memory=True, num_workers=4)
return train_dataloader, valid_dataloader, test_dataloader
def load_cifar_x(batch_size: int=64, root: str=None, dataset_len=1000):
"""
Load CIFAR-10 1000 data
"""
torch.manual_seed(43)
root = root + "datasets/CIFAR10/"
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
train = datasets.CIFAR10(root=root, train=True, download=True, transform=transform)
train_data, _ = random_split(train, [dataset_len, len(train)-dataset_len])
train_dataloader = DataLoader(train_data, batch_size=batch_size, pin_memory=True, num_workers=4)
return train_dataloader
def load_cifar100(batch_size: int=64, root: str=None):
"""
Load CIFAR-100 data
"""
root = root + "datasets/CIFAR100/"
train_transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, padding=4,padding_mode='reflect'),
transforms.RandomRotation(15),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
train = datasets.CIFAR100(root=root, train=True, download=True, transform=transform)
train_data, valid_data = random_split(train, [45000, 5000])
test_data = datasets.CIFAR100(root=root, train=False, download=True, transform=transform)
train_dataloader = DataLoader(train_data, batch_size=batch_size, pin_memory=True, shuffle=True, num_workers=4)
valid_dataloader = DataLoader(valid_data, batch_size=batch_size, pin_memory=True, num_workers=4)
test_dataloader = DataLoader(test_data, batch_size=batch_size, pin_memory=True, num_workers=4)
return train_dataloader, valid_dataloader, test_dataloader
def load_cifar100_x(batch_size: int=64, root: str=None, dataset_len=1000):
"""
Load CIFAR-100 data of x length
"""
torch.manual_seed(43)
root = root + "datasets/CIFAR100/"
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
train = datasets.CIFAR10(root=root, train=True, download=True, transform=transform)
train_data, _ = random_split(train, [dataset_len, len(train)-dataset_len])
train_dataloader = DataLoader(train_data, batch_size=batch_size, pin_memory=True, num_workers=4)
return train_dataloader
def load_celeba(batch_size: int=64, root: str=None):
"""
Load CelebA dataset
"""
root = root + "datasets/CelebA/"
transform_train = transforms.Compose([
transforms.CenterCrop(178),
transforms.Resize(128),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
transform_test = transforms.Compose([
transforms.CenterCrop(178),
transforms.Resize(128),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
train_data = CelebaDataset(txt_path=root+'celeba_gender_attr_train.txt',
img_dir=root+'img_align_celeba/',
transform=transform_train)
train_data, valid_data = random_split(train_data, [150000, 12079])
test_data = CelebaDataset(txt_path=root+'celeba_gender_attr_test.txt',
img_dir=root+'img_align_celeba/',
transform=transform_test)
train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True, pin_memory=True, num_workers=4)
valid_dataloader = DataLoader(valid_data, batch_size=batch_size, shuffle=True, pin_memory=True, num_workers=4)
test_dataloader = DataLoader(test_data, batch_size=batch_size, pin_memory=True, num_workers=4)
return train_dataloader, valid_dataloader, test_dataloader
def load_celeba_x(batch_size: int=64, root: str=None, dataset_len=1000):
"""
Load CelebA dataset
"""
torch.manual_seed(43)
root = root + "datasets/CelebA/"
transform_train = transforms.Compose([
transforms.CenterCrop(178),
transforms.Resize(128),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
train = CelebaDataset(txt_path=root+'celeba_gender_attr_train.txt',
img_dir=root+'img_align_celeba/',
transform=transform_train)
train_data, _ = random_split(train, [dataset_len, len(train)-dataset_len])
train_dataloader = DataLoader(train_data, batch_size=batch_size, pin_memory=True, num_workers=4)
return train_dataloader
def load_gtsrb(batch_size: int=64, root: str=None):
"""
Load GTSRB data
"""
root = root + "datasets/GTSRB/"
transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.3403, 0.3121, 0.3214),
(0.2724, 0.2608, 0.2669))
])
train = datasets.GTSRB(root=root, split="train", download=True, transform=transform)
train_data, valid_data = random_split(train, [len(train)-5000, 5000])
test_data = datasets.GTSRB(root=root, split="test", download=True, transform=transform)
train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True, pin_memory=True, num_workers=4)
valid_dataloader = DataLoader(valid_data, batch_size=batch_size, pin_memory=True, num_workers=4)
test_dataloader = DataLoader(test_data, batch_size=batch_size, pin_memory=True, num_workers=4)
return train_dataloader, valid_dataloader, test_dataloader
def load_gtsrb_x(batch_size: int=64, root: str=None, dataset_len=1000):
"""
Load GTSRB data
"""
torch.manual_seed(43)
root = root + "datasets/GTSRB/"
transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.3403, 0.3121, 0.3214),
(0.2724, 0.2608, 0.2669))
])
train = datasets.GTSRB(root=root, split="train", download=True, transform=transform)
train_data, _ = random_split(train, [dataset_len, len(train)-dataset_len])
train_dataloader = DataLoader(train_data, batch_size=batch_size, pin_memory=True, num_workers=4)
return train_dataloader
def load_imagenet_x(batch_size: int=32, root: str=None, dataset_len=1000):
"""
Load Imagenet data
"""
torch.manual_seed(43)
root = root + "datasets/IMAGENET/"
weights = Inception_V3_Weights.DEFAULT
# transform = weights.transforms()
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
# transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
train = ImagenetDataset(
inverse_label_path="/scratch/itee/uqsswain/datasets/IMAGENET/labels/inverse_labels.txt",
img_dir="/scratch/itee/uqsswain/datasets/IMAGENET/images/",
transform=transform
)
train_data, _ = random_split(train, [dataset_len, len(train)-dataset_len])
train_dataloader = DataLoader(train_data, batch_size=batch_size, num_workers=4)
return train_dataloader
def load_fashion_mnist(batch_size: int=64, root: str=None):
"""
Load Fashion-MNIST data
"""
root = root + "datasets/FashionMNIST/"
t = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: torch.flatten(x))]
)
train = datasets.FashionMNIST(root=root, train=True, download=True, transform=t)
train_data, valid_data = random_split(train, [55000, 5000])
test_data = datasets.FashionMNIST(root=root, train=False, download=True, transform=t)
train_dataloader = DataLoader(train_data, batch_size=batch_size, drop_last=True, shuffle=True, num_workers=4)
valid_dataloader = DataLoader(valid_data, batch_size=batch_size, drop_last=True, num_workers=4)
test_dataloader = DataLoader(test_data, batch_size=batch_size, drop_last=True, num_workers=4)
return train_dataloader, valid_dataloader, test_dataloader
DATALOADER_MAPPINGS = {
"mnist": load_mnist,
"mnist_x": load_mnist_x,
"cifar10": load_cifar,
"cifar10_x": load_cifar_x,
"cifar100": load_cifar100,
"cifar100_x": load_cifar100_x,
"celeba": load_celeba,
"celeba_x": load_celeba_x,
"fmnist": load_fashion_mnist,
"gtsrb": load_gtsrb,
"gtsrb_x": load_gtsrb_x,
"imagenet_x": load_imagenet_x,
}
if __name__ == "__main__":
train_dataloader, _, _ = load_cifar100(batch_size=1, root="/scratch/itee/uqsswain/")
for imgs, labels in train_dataloader:
print(imgs.shape)
print(labels)
break