-
Notifications
You must be signed in to change notification settings - Fork 208
/
data.py
496 lines (408 loc) · 15.1 KB
/
data.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import sys
from datetime import datetime
import torch
import torchvision
from torch.utils.data import ConcatDataset
from code.datasets.clustering.truncated_dataset import TruncatedDataset
from code.utils.cluster.transforms import sobel_make_transforms, \
greyscale_make_transforms
from code.utils.semisup.dataset import TenCropAndFinish
from .general import reorder_train_deterministic
# Used by sobel and greyscale clustering twohead scripts -----------------------
def cluster_twohead_create_dataloaders(config):
assert (config.mode == "IID")
assert (config.twohead)
target_transform = None
if "CIFAR" in config.dataset:
config.train_partitions_head_A = [True, False]
config.train_partitions_head_B = config.train_partitions_head_A
config.mapping_assignment_partitions = [True, False]
config.mapping_test_partitions = [True, False]
if config.dataset == "CIFAR10":
dataset_class = torchvision.datasets.CIFAR10
elif config.dataset == "CIFAR100":
dataset_class = torchvision.datasets.CIFAR100
elif config.dataset == "CIFAR20":
dataset_class = torchvision.datasets.CIFAR100
target_transform = _cifar100_to_cifar20
else:
assert (False)
# datasets produce either 2 or 5 channel images based on config.include_rgb
tf1, tf2, tf3 = sobel_make_transforms(config)
elif config.dataset == "STL10":
assert (config.mix_train)
if not config.stl_leave_out_unlabelled:
print("adding unlabelled data for STL10")
config.train_partitions_head_A = ["train+unlabeled", "test"]
else:
print("not using unlabelled data for STL10")
config.train_partitions_head_A = ["train", "test"]
config.train_partitions_head_B = ["train", "test"]
config.mapping_assignment_partitions = ["train", "test"]
config.mapping_test_partitions = ["train", "test"]
dataset_class = torchvision.datasets.STL10
# datasets produce either 2 or 5 channel images based on config.include_rgb
tf1, tf2, tf3 = sobel_make_transforms(config)
elif config.dataset == "MNIST":
config.train_partitions_head_A = [True, False]
config.train_partitions_head_B = config.train_partitions_head_A
config.mapping_assignment_partitions = [True, False]
config.mapping_test_partitions = [True, False]
dataset_class = torchvision.datasets.MNIST
tf1, tf2, tf3 = greyscale_make_transforms(config)
else:
assert (False)
print("Making datasets with %s and %s" % (dataset_class, target_transform))
sys.stdout.flush()
dataloaders_head_A = \
_create_dataloaders(config, dataset_class, tf1, tf2,
partitions=config.train_partitions_head_A,
target_transform=target_transform)
dataloaders_head_B = \
_create_dataloaders(config, dataset_class, tf1, tf2,
partitions=config.train_partitions_head_B,
target_transform=target_transform)
mapping_assignment_dataloader = \
_create_mapping_loader(config, dataset_class, tf3,
partitions=config.mapping_assignment_partitions,
target_transform=target_transform)
mapping_test_dataloader = \
_create_mapping_loader(config, dataset_class, tf3,
partitions=config.mapping_test_partitions,
target_transform=target_transform)
return dataloaders_head_A, dataloaders_head_B, \
mapping_assignment_dataloader, mapping_test_dataloader
# Used by sobel and greyscale clustering single head scripts -------------------
def cluster_create_dataloaders(config):
assert (config.mode == "IID+")
assert (not config.twohead)
target_transform = None
# separate train/test sets
if "CIFAR" in config.dataset:
config.train_partitions = [True]
config.mapping_assignment_partitions = [True]
config.mapping_test_partitions = [False]
if config.dataset == "CIFAR10":
dataset_class = torchvision.datasets.CIFAR10
elif config.dataset == "CIFAR100":
dataset_class = torchvision.datasets.CIFAR100
elif config.dataset == "CIFAR20":
dataset_class = torchvision.datasets.CIFAR100
target_transform = _cifar100_to_cifar20
else:
assert (False)
# datasets produce either 2 or 5 channel images based on config.include_rgb
tf1, tf2, tf3 = sobel_make_transforms(config)
elif config.dataset == "STL10":
config.train_partitions = ["train+unlabeled"]
config.mapping_assignment_partitions = ["train"]
config.mapping_test_partitions = ["test"]
dataset_class = torchvision.datasets.STL10
# datasets produce either 2 or 5 channel images based on config.include_rgb
tf1, tf2, tf3 = sobel_make_transforms(config)
elif config.dataset == "MNIST":
config.train_partitions = [True]
config.mapping_assignment_partitions = [True]
config.mapping_test_partitions = [False]
dataset_class = torchvision.datasets.MNIST
tf1, tf2, tf3 = greyscale_make_transforms(config)
else:
assert (False)
print("Making datasets with %s and %s" % (dataset_class, target_transform))
sys.stdout.flush()
dataloaders = \
_create_dataloaders(config, dataset_class, tf1, tf2,
partitions=config.train_partitions,
target_transform=target_transform)
mapping_assignment_dataloader = \
_create_mapping_loader(config, dataset_class, tf3,
partitions=config.mapping_assignment_partitions,
target_transform=target_transform)
mapping_test_dataloader = \
_create_mapping_loader(config, dataset_class, tf3,
partitions=config.mapping_test_partitions,
target_transform=target_transform)
return dataloaders, mapping_assignment_dataloader, mapping_test_dataloader
# Other generic data creation functions ----------------------------------------
def make_STL_data(config, tf1=None, tf2=None, tf3=None,
truncate_assign=False, truncate_pc=None):
assert (tf3 is not None)
if (tf1 is not None) and (tf2 is not None):
dataloaders = _create_dataloaders(config, torchvision.datasets.STL10, tf1,
tf2,
partitions=config.train_partitions_head_B)
mapping_assignment_dataloader = _create_mapping_loader(
config, torchvision.datasets.STL10, tf3,
partitions=config.mapping_assignment_partitions,
truncate=truncate_assign, truncate_pc=truncate_pc)
mapping_test_dataloader = _create_mapping_loader(
config, torchvision.datasets.STL10, tf3,
partitions=config.mapping_test_partitions)
if (tf1 is not None) and (tf2 is not None):
return dataloaders, mapping_assignment_dataloader, mapping_test_dataloader
else:
return mapping_assignment_dataloader, mapping_test_dataloader
def make_CIFAR_data(config, tf1=None, tf2=None, tf3=None,
truncate_assign=False, truncate_pc=None):
target_transform = None
if config.dataset == "CIFAR10":
dataset_class = torchvision.datasets.CIFAR10
elif config.dataset == "CIFAR100":
dataset_class = torchvision.datasets.CIFAR100
elif config.dataset == "CIFAR20":
dataset_class = torchvision.datasets.CIFAR100
target_transform = _cifar100_to_cifar20
else:
assert (False)
assert (tf3 is not None)
if (tf1 is not None) and (tf2 is not None):
dataloaders = _create_dataloaders(config, dataset_class, tf1, tf2,
partitions=config.train_partitions_head_B,
target_transform=target_transform)
mapping_assignment_dataloader = _create_mapping_loader(
config, dataset_class, tf3, config.mapping_assignment_partitions,
target_transform=target_transform,
truncate=truncate_assign, truncate_pc=truncate_pc)
mapping_test_dataloader = _create_mapping_loader(
config, dataset_class, tf3, config.mapping_test_partitions,
target_transform=target_transform)
if (tf1 is not None) and (tf2 is not None):
return dataloaders, mapping_assignment_dataloader, mapping_test_dataloader
else:
return mapping_assignment_dataloader, mapping_test_dataloader
def make_MNIST_data(config, tf1=None, tf2=None, tf3=None,
truncate_assign=False, truncate_pc=None):
assert (tf3 is not None)
if (tf1 is not None) and (tf2 is not None):
dataloaders = _create_dataloaders(config, torchvision.datasets.MNIST, tf1,
tf2,
partitions=config.train_partitions_head_B)
mapping_assignment_dataloader = _create_mapping_loader(
config, torchvision.datasets.MNIST, tf3,
config.mapping_assignment_partitions,
truncate=truncate_assign, truncate_pc=truncate_pc)
mapping_test_dataloader = _create_mapping_loader(
config, torchvision.datasets.MNIST, tf3,
config.mapping_test_partitions)
if (tf1 is not None) and (tf2 is not None):
return dataloaders, mapping_assignment_dataloader, mapping_test_dataloader
else:
return mapping_assignment_dataloader, mapping_test_dataloader
# Data creation helpers --------------------------------------------------------
def _create_dataloaders(config, dataset_class, tf1, tf2,
partitions,
target_transform=None,
shuffle=False):
train_imgs_list = []
for train_partition in partitions:
if "STL10" == config.dataset:
train_imgs_curr = dataset_class(
root=config.dataset_root,
transform=tf1,
split=train_partition,
target_transform=target_transform)
else:
train_imgs_curr = dataset_class(
root=config.dataset_root,
transform=tf1,
train=train_partition,
target_transform=target_transform)
if hasattr(config, "mix_train"):
if config.mix_train and (train_partition == "train+unlabeled"):
train_imgs_curr = reorder_train_deterministic(train_imgs_curr)
train_imgs_list.append(train_imgs_curr)
train_imgs = ConcatDataset(train_imgs_list)
train_dataloader = torch.utils.data.DataLoader(train_imgs,
batch_size=config.dataloader_batch_sz,
shuffle=shuffle,
num_workers=0,
drop_last=False)
if not shuffle:
assert (isinstance(train_dataloader.sampler,
torch.utils.data.sampler.SequentialSampler))
dataloaders = [train_dataloader]
for d_i in xrange(config.num_dataloaders):
print("Creating auxiliary dataloader ind %d out of %d time %s" %
(d_i, config.num_dataloaders, datetime.now()))
sys.stdout.flush()
train_tf_imgs_list = []
for train_partition in partitions:
if "STL10" == config.dataset:
train_imgs_tf_curr = dataset_class(
root=config.dataset_root,
transform=tf2, # random per call
split=train_partition,
target_transform=target_transform)
else:
train_imgs_tf_curr = dataset_class(
root=config.dataset_root,
transform=tf2,
train=train_partition,
target_transform=target_transform)
if hasattr(config, "mix_train"):
if config.mix_train and (train_partition == "train+unlabeled"):
train_imgs_tf_curr = reorder_train_deterministic(train_imgs_tf_curr)
train_tf_imgs_list.append(train_imgs_tf_curr)
train_imgs_tf = ConcatDataset(train_tf_imgs_list)
train_tf_dataloader = \
torch.utils.data.DataLoader(train_imgs_tf,
batch_size=config.dataloader_batch_sz,
shuffle=shuffle,
num_workers=0,
drop_last=False)
if not shuffle:
assert (isinstance(train_tf_dataloader.sampler,
torch.utils.data.sampler.SequentialSampler))
assert (len(train_dataloader) == len(train_tf_dataloader))
dataloaders.append(train_tf_dataloader)
num_train_batches = len(dataloaders[0])
print("Length of datasets vector %d" % len(dataloaders))
print("Number of batches per epoch: %d" % num_train_batches)
sys.stdout.flush()
return dataloaders
def _create_mapping_loader(config, dataset_class, tf3, partitions,
target_transform=None,
truncate=False, truncate_pc=None,
tencrop=False,
shuffle=False):
if truncate:
print("Note: creating mapping loader with truncate == True")
if tencrop:
assert (tf3 is None)
imgs_list = []
for partition in partitions:
if "STL10" == config.dataset:
imgs_curr = dataset_class(
root=config.dataset_root,
transform=tf3,
split=partition,
target_transform=target_transform)
else:
imgs_curr = dataset_class(
root=config.dataset_root,
transform=tf3,
train=partition,
target_transform=target_transform)
if truncate:
print("shrinking dataset from %d" % len(imgs_curr))
imgs_curr = TruncatedDataset(imgs_curr, pc=truncate_pc)
print("... to %d" % len(imgs_curr))
if tencrop:
imgs_curr = TenCropAndFinish(imgs_curr, input_sz=config.input_sz,
include_rgb=config.include_rgb)
imgs_list.append(imgs_curr)
imgs = ConcatDataset(imgs_list)
dataloader = torch.utils.data.DataLoader(imgs,
batch_size=config.batch_sz,
# full batch
shuffle=shuffle,
num_workers=0,
drop_last=False)
if not shuffle:
assert (isinstance(dataloader.sampler,
torch.utils.data.sampler.SequentialSampler))
return dataloader
def _cifar100_to_cifar20(target):
# obtained from cifar_test script
_dict = \
{0: 4,
1: 1,
2: 14,
3: 8,
4: 0,
5: 6,
6: 7,
7: 7,
8: 18,
9: 3,
10: 3,
11: 14,
12: 9,
13: 18,
14: 7,
15: 11,
16: 3,
17: 9,
18: 7,
19: 11,
20: 6,
21: 11,
22: 5,
23: 10,
24: 7,
25: 6,
26: 13,
27: 15,
28: 3,
29: 15,
30: 0,
31: 11,
32: 1,
33: 10,
34: 12,
35: 14,
36: 16,
37: 9,
38: 11,
39: 5,
40: 5,
41: 19,
42: 8,
43: 8,
44: 15,
45: 13,
46: 14,
47: 17,
48: 18,
49: 10,
50: 16,
51: 4,
52: 17,
53: 4,
54: 2,
55: 0,
56: 17,
57: 4,
58: 18,
59: 17,
60: 10,
61: 3,
62: 2,
63: 12,
64: 12,
65: 16,
66: 12,
67: 1,
68: 9,
69: 19,
70: 2,
71: 10,
72: 0,
73: 1,
74: 16,
75: 12,
76: 9,
77: 13,
78: 15,
79: 13,
80: 16,
81: 19,
82: 2,
83: 4,
84: 6,
85: 19,
86: 5,
87: 5,
88: 8,
89: 19,
90: 18,
91: 1,
92: 2,
93: 15,
94: 6,
95: 0,
96: 17,
97: 8,
98: 14,
99: 13}
return _dict[target]