-
Notifications
You must be signed in to change notification settings - Fork 1
/
cifar.py
590 lines (476 loc) · 27 KB
/
cifar.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import logging
import math
import os
import numpy as np
from PIL import Image
from torchvision import datasets
from torch.utils.data import Dataset
from torchvision import transforms
import torch
from .randaugment import RandAugmentMC
import sys
from utils.misc import cifar100_to_cifar20
from mypath import MyPath
cifar10_mean = (0.4914, 0.4822, 0.4465)
cifar10_std = (0.2023, 0.1994, 0.2010)
cifar100_mean = (0.5071, 0.4867, 0.4408)
cifar100_std = (0.2675, 0.2565, 0.2761)
cinic10_mean = (0.47889522, 0.47227842, 0.43047404)
cinic10_std = (0.24205776, 0.23828046, 0.25874835)
def majority_vote(preds, targets, num_k):
res = []
for i in range(num_k):
targets_c = targets[preds==i]
cnt = np.zeros(num_k)
for j in range(num_k):
cnt[j] = np.sum(targets_c==j)
res.append((i, np.argmax(cnt)))
return res
def noise_x_u_split(args, noise_labels, cluster_labels):
# unlabeled data: all data (https://github.com/kekmodel/FixMatch-pytorch/issues/10)
unlabeled_idx = np.array(range(len(noise_labels)))
all_indices = np.arange(len(noise_labels))
labeled_idx = all_indices[cluster_labels==noise_labels]
print("DenoiseSSL:initial selected samples %d"%(len(labeled_idx)))
args.num_labeled = len(labeled_idx)
if args.expand_labels or args.num_labeled < args.batch_size:
num_expand_x = math.ceil(
args.batch_size * args.eval_step / args.num_labeled)
labeled_idx = np.hstack([labeled_idx for _ in range(num_expand_x)])
np.random.shuffle(labeled_idx)
print("DnoiseSSL:final labeled samples %d; unlabeled samples %d"%(len(labeled_idx), len(unlabeled_idx)))
return labeled_idx, unlabeled_idx
def x_u_split(args, labels):
label_per_class = args.num_labeled // args.num_classes
labels = np.array(labels)
labeled_idx = []
# unlabeled data: all data (https://github.com/kekmodel/FixMatch-pytorch/issues/10)
unlabeled_idx = np.array(range(len(labels)))
if args.num_labeled == len(labels):
labeled_idx = np.arange(len(labels))
else:
labeled_idx = []
for i in range(args.num_classes):
idx = np.where(labels == i)[0]
idx = np.random.choice(idx, label_per_class, False)
labeled_idx.extend(idx)
labeled_idx = np.array(labeled_idx)
assert len(labeled_idx) == args.num_labeled
print("LDPSSL:initial selected samples %d"%(len(labeled_idx)))
if args.expand_labels or args.num_labeled < args.batch_size:
num_expand_x = math.ceil(
args.batch_size * args.eval_step / args.num_labeled)
labeled_idx = np.hstack([labeled_idx for _ in range(num_expand_x)])
np.random.shuffle(labeled_idx)
print("LDPSSL: final labeled samples %d; unlabeled samples %d"%(len(labeled_idx), len(unlabeled_idx)))
return labeled_idx, unlabeled_idx
def label_match(raw_cluster_label, metric, N):
cluster_label = raw_cluster_label.copy()
for i in range(N):
cluster_label[raw_cluster_label==metric[i][0]] = metric[i][1]
return cluster_label
def get_cifar10_denoisessl(args):
transform_labeled = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=32,
padding=int(32*0.125),
padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(mean=cifar10_mean, std=cifar10_std)
])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=cifar10_mean, std=cifar10_std)
])
base_db_root = MyPath.db_root_dir(args.dataset)
base_ckpt_root = MyPath.ckpt_root_dir()
base_dataset = datasets.CIFAR10(base_db_root, train=True, download=True)
if args.noisemode =='pate':
noise_label_file = os.path.join(base_db_root, "dplabel", "pate","eps_"+str(args.epsilon))
print("Loading (noise) DP label by PATE: ", noise_label_file)
noise_train_label = torch.load(noise_label_file)
elif args.noisemode == 'randres':
noise_label_file = os.path.join(base_db_root, "dplabel", "rr", "eps"+str(args.epsilon)+".npy")
print("Loading (noise) DP label by RandRes: ", noise_label_file)
noise_train_label = np.load(noise_label_file)
print("Eps: %.2f. Noisy label acc: %.6f"%(args.epsilon, np.mean(base_dataset.targets==noise_train_label)))
all_train_label = np.array(base_dataset.targets)
cluster_path = os.path.join(base_ckpt_root, "SCAN", args.dataset, args.arch, "selflabel", "train_cluster_pred.npy")
print("Loading cluster from %s model: %s"%(args.arch, cluster_path))
raw_cluster_label = np.load(cluster_path)
tran_metric = majority_vote(raw_cluster_label, noise_train_label, args.num_classes)
cluster_label = label_match(raw_cluster_label, tran_metric, args.num_classes)
print("Eps: %.2f. Cluster label acc: %.6f. Selected label ratio: %.6f."%(args.epsilon, np.mean(cluster_label==all_train_label), np.mean(cluster_label==noise_train_label)))
print("Selected label correctness: %4f"%(np.mean(all_train_label[cluster_label==noise_train_label]==noise_train_label[cluster_label==noise_train_label])))
noise_train_label = (noise_train_label.astype(np.int32)).tolist()
train_labeled_idxs, train_unlabeled_idxs = noise_x_u_split(args, noise_train_label, cluster_label)
train_labeled_dataset = CIFAR10SSL(base_db_root, train_labeled_idxs, noise=noise_train_label, train=True, transform=transform_labeled)
train_unlabeled_dataset = CIFAR10SSL(base_db_root, train_unlabeled_idxs, noise=noise_train_label, train=True, transform=TransformFixMatch(mean=cifar10_mean, std=cifar10_std))
test_dataset = datasets.CIFAR10(base_db_root, train=False, transform=transform_val, download=False)
return train_labeled_dataset, train_unlabeled_dataset, test_dataset
def get_cifar10_ldpssl(args):
transform_labeled = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=32,
padding=int(32*0.125),
padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(mean=cifar10_mean, std=cifar10_std)
])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=cifar10_mean, std=cifar10_std)
])
base_db_root = MyPath.db_root_dir(args.dataset)
base_ckpt_root = MyPath.ckpt_root_dir()
base_dataset = datasets.CIFAR10(base_db_root, train=True, download=True)
if args.noisemode == 'pate':
noise_label_file = os.path.join(base_db_root, "dplabel", "pate","eps_"+str(args.epsilon))
print("Loading (noise) DP label by PATE:", noise_label_file)
noise_train_label = torch.load(noise_label_file)
elif args.noisemode == 'randres':
noise_label_file = os.path.join(base_db_root, "dplabel", "rr", "eps"+str(args.epsilon)+".npy")
print("Loading (noise) DP label by RandRes: ", noise_label_file)
noise_train_label = np.load(noise_label_file)
print("Eps: %.2f. Noisy label acc: %.6f"%(args.epsilon, np.mean(base_dataset.targets==noise_train_label)))
noise_train_label = (noise_train_label.astype(np.int32)).tolist()
train_labeled_idxs, train_unlabeled_idxs = x_u_split(args, noise_train_label)
train_labeled_dataset = CIFAR10SSL(base_db_root, train_labeled_idxs, noise=noise_train_label, train=True, transform=transform_labeled)
train_unlabeled_dataset = CIFAR10SSL(base_db_root, train_unlabeled_idxs, noise=noise_train_label, train=True, transform=TransformFixMatch(mean=cifar10_mean, std=cifar10_std))
test_dataset = datasets.CIFAR10(base_db_root, train=False, transform=transform_val, download=False)
return train_labeled_dataset, train_unlabeled_dataset, test_dataset
def get_cifar10(args):
transform_labeled = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=32,
padding=int(32*0.125),
padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(mean=cifar10_mean, std=cifar10_std)
])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=cifar10_mean, std=cifar10_std)
])
base_db_root = MyPath.db_root_dir(args.dataset)
base_dataset = datasets.CIFAR10(base_db_root, train=True, download=True)
train_labeled_idxs, train_unlabeled_idxs = x_u_split(args, base_dataset.targets)
train_labeled_dataset = CIFAR10SSL(base_db_root, train_labeled_idxs, train=True, transform=transform_labeled)
train_unlabeled_dataset = CIFAR10SSL(base_db_root, train_unlabeled_idxs, train=True, transform=TransformFixMatch(mean=cifar10_mean, std=cifar10_std))
test_dataset = datasets.CIFAR10(base_db_root, train=False, transform=transform_val, download=False)
return train_labeled_dataset, train_unlabeled_dataset, test_dataset
def get_cifar100_denoisessl(args):
transform_labeled = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=32,
padding=int(32*0.125),
padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(mean=cifar100_mean, std=cifar100_std)])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=cifar100_mean, std=cifar100_std)])
base_db_root = MyPath.db_root_dir(args.dataset)
base_ckpt_root = MyPath.ckpt_root_dir()
base_dataset = datasets.CIFAR100(base_db_root, train=True, download=True)
if args.noisemode == 'pate':
noise_label_file = os.path.join(base_db_root, "dplabel", "pate","eps_"+str(args.epsilon))
print("Loading (noise) DP label by PATE: ", noise_label_file)
noise_train_label = torch.load(noise_label_file)
elif args.noisemode == 'randres':
noise_label_file = os.path.join(base_db_root, "dplabel", "rr", "eps"+str(args.epsilon)+".npy")
print("Loading (noise) DP label by RandRes: ", noise_label_file)
noise_train_label = np.load(noise_label_file)
print("Eps: %.2f. Noisy label acc: %.6f"%(args.epsilon, np.mean(base_dataset.targets==noise_train_label)))
subclass_train_label = []
subclass_noise_train_label = []
for i in range(len(base_dataset.targets)):
subclass_train_label.append(cifar100_to_cifar20._convert(base_dataset.targets[i]))
subclass_noise_train_label.append(cifar100_to_cifar20._convert(noise_train_label[i]))
subclass_train_label = np.array(subclass_train_label)
subclass_noise_train_label = np.array(subclass_noise_train_label)
print("Sub-class noisy label accuracy %.6f"%(np.mean(subclass_train_label==subclass_noise_train_label)))
cluster_path = os.path.join(base_ckpt_root, "SCAN", "cifar20", args.arch, "selflabel", "train_cluster_pred.npy")
print("Loading cluster from %s model: %s"%(args.arch, cluster_path))
raw_cluster_label = np.load(cluster_path)
tran_metric = majority_vote(raw_cluster_label, subclass_noise_train_label, args.num_classes)
cluster_label = label_match(raw_cluster_label, tran_metric, args.num_classes)
all_train_label = np.array(base_dataset.targets)
print("Eps: %.2f. Cluster label accuracy: %.6f. Selected ratio: %.6f."%(args.epsilon, np.mean(cluster_label==subclass_train_label), np.mean(cluster_label==subclass_noise_train_label)))
print("Finegrained Label Accuracy by matched sub-class label: %.6f"%(np.mean(noise_train_label[cluster_label==subclass_noise_train_label] == all_train_label[cluster_label==subclass_noise_train_label])))
noise_train_label = (noise_train_label.astype(np.int32)).tolist()
train_labeled_idxs, train_unlabeled_idxs = noise_x_u_split(args, subclass_noise_train_label, cluster_label)
train_labeled_dataset = CIFAR100SSL(base_db_root, train_labeled_idxs, noise = noise_train_label, train=True, transform=transform_labeled)
train_unlabeled_dataset = CIFAR100SSL(base_db_root, train_unlabeled_idxs, noise = noise_train_label, train=True, transform=TransformFixMatch(mean=cifar100_mean, std=cifar100_std))
test_dataset = datasets.CIFAR100(base_db_root, train=False, transform=transform_val, download=False)
return train_labeled_dataset, train_unlabeled_dataset, test_dataset
def get_cifar100_ldpssl(args):
transform_labeled = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=32,
padding=int(32*0.125),
padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(mean=cifar100_mean, std=cifar100_std)])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=cifar100_mean, std=cifar100_std)])
base_db_root = MyPath.db_root_dir(args.dataset)
base_ckpt_root = MyPath.ckpt_root_dir()
base_dataset = datasets.CIFAR100(base_db_root, train=True, download=True)
if args.noisemode == 'pate':
noise_label_file = os.path.join(base_db_root, "dplabel", "pate","eps_"+str(args.epsilon))
print("Loading (noise) DP label by PATE: ", noise_label_file)
noise_train_label = torch.load(noise_label_file)
elif args.noisemode == 'randres':
noise_label_file = os.path.join(base_db_root, "dplabel", "rr", "eps"+str(args.epsilon)+".npy")
print("Loading (noise) DP label by RandRes: ", noise_label_file)
noise_train_label = np.load(noise_label_file)
print("Eps: %.2f. Noisy label acc: %.6f"%(args.epsilon, np.mean(base_dataset.targets==noise_train_label)))
noise_train_label = (noise_train_label.astype(np.int32)).tolist()
train_labeled_idxs, train_unlabeled_idxs = x_u_split(args, noise_train_label)
train_labeled_dataset = CIFAR100SSL(base_db_root, train_labeled_idxs, noise = noise_train_label, train=True, transform=transform_labeled)
train_unlabeled_dataset = CIFAR100SSL(base_db_root, train_unlabeled_idxs, noise = noise_train_label, train=True, transform=TransformFixMatch(mean=cifar100_mean, std=cifar100_std))
test_dataset = datasets.CIFAR100(base_db_root, train=False, transform=transform_val, download=False)
return train_labeled_dataset, train_unlabeled_dataset, test_dataset
def get_cifar100(args):
transform_labeled = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=32,
padding=int(32*0.125),
padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(mean=cifar100_mean, std=cifar100_std)])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=cifar100_mean, std=cifar100_std)])
base_db_root = MyPath.db_root_dir(args.dataset)
base_ckpt_root = MyPath.ckpt_root_dir()
base_dataset = datasets.CIFAR100(base_db_root, train=True, download=True)
train_labeled_idxs, train_unlabeled_idxs = x_u_split(args, base_dataset.targets)
train_labeled_dataset = CIFAR100SSL(base_db_root, train_labeled_idxs, train=True, transform=transform_labeled)
train_unlabeled_dataset = CIFAR100SSL(base_db_root, train_unlabeled_idxs, train=True, transform=TransformFixMatch(mean=cifar100_mean, std=cifar100_std))
test_dataset = datasets.CIFAR100(base_db_root, train=False, transform=transform_val, download=False)
return train_labeled_dataset, train_unlabeled_dataset, test_dataset
def get_cinic10_denoisessl(args):
transform_labeled = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=32,
padding=int(32*0.125),
padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(mean=cinic10_mean, std=cinic10_std)
])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=cinic10_mean, std=cinic10_std)
])
base_db_root = MyPath.db_root_dir(args.dataset)
base_ckpt_root = MyPath.ckpt_root_dir()
base_dataset = CINIC10(base_db_root, filen = 'train')
if args.noisemode =='pate':
noise_label_file = os.path.join(base_db_root, "dplabel", "pate","eps_"+str(args.epsilon))
print("Loading (noise) DP label by PATE: ", noise_label_file)
noise_train_label = torch.load(noise_label_file)
elif args.noisemode == 'randres':
noise_label_file = os.path.join(base_db_root, "dplabel", "rr", "eps"+str(args.epsilon)+".npy")
print("Loading (noise) DP label by RandRes: ", noise_label_file)
noise_train_label = np.load(noise_label_file)
print("Eps: %.2f. Noisy label acc: %.6f"%(args.epsilon, np.mean(base_dataset.targets==noise_train_label)))
all_train_label = np.array(base_dataset.targets)
cluster_path = os.path.join(base_ckpt_root, "SCAN", args.dataset, args.arch, "selflabel", "train_cluster_pred.npy")
print("Loading cluster from %s model: %s"%(args.arch, cluster_path))
raw_cluster_label = np.load(cluster_path)
tran_metric = majority_vote(raw_cluster_label, noise_train_label, args.num_classes)
cluster_label = label_match(raw_cluster_label, tran_metric, args.num_classes)
print("Eps: %.2f. Cluster label acc: %.6f. Selected label ratio: %.6f."%(args.epsilon, np.mean(cluster_label==all_train_label), np.mean(cluster_label==noise_train_label)))
print("Selected label correctness: %4f"%(np.mean(all_train_label[cluster_label==noise_train_label]==noise_train_label[cluster_label==noise_train_label])))
noise_train_label = (noise_train_label.astype(np.int32)).tolist()
train_labeled_idxs, train_unlabeled_idxs = noise_x_u_split(args, noise_train_label, cluster_label, all_train_label)
train_labeled_dataset = CINICSSL(base_db_root, filen='train', indexs=train_labeled_idxs, noise=noise_train_label, transform=transform_labeled)
train_unlabeled_dataset = CINICSSL(base_db_root, filen='train', indexs=train_unlabeled_idxs, noise=noise_train_label, transform=TransformFixMatch(mean=cinic10_mean, std=cinic10_std))
test_dataset = CINICSSL(base_db_root, filen='valid', transform=transform_val)
return train_labeled_dataset, train_unlabeled_dataset, test_dataset
def get_cinic10_ldpssl(args):
transform_labeled = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=32,
padding=int(32*0.125),
padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(mean=cinic10_mean, std=cinic10_std)
])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=cinic10_mean, std=cinic10_std)
])
base_db_root = MyPath.db_root_dir(args.dataset)
base_dataset = CINIC10(base_db_root, filen = 'train')
if args.noisemode =='pate':
noise_label_file = os.path.join(base_db_root, "dplabel", "pate","eps_"+str(args.epsilon))
print("Loading (noise) DP label by PATE: ", noise_label_file)
noise_train_label = torch.load(noise_label_file)
elif args.noisemode == 'randres':
noise_label_file = os.path.join(base_db_root, "dplabel", "rr", "eps"+str(args.epsilon)+".npy")
print("Loading (noise) DP label by RandRes: ", noise_label_file)
noise_train_label = np.load(noise_label_file)
print("Eps: %.2f. Noisy label acc: %.6f"%(args.epsilon, np.mean(base_dataset.targets==noise_train_label)))
noise_train_label = (noise_train_label.astype(np.int32)).tolist()
train_labeled_idxs, train_unlabeled_idxs = x_u_split(args, noise_train_label)
train_labeled_dataset = CINICSSL(base_db_root, filen='train', indexs=train_labeled_idxs, noise=noise_train_label, transform=transform_labeled)
train_unlabeled_dataset = CINICSSL(base_db_root, filen='train', indexs=train_unlabeled_idxs, noise=noise_train_label, transform=TransformFixMatch(mean=cinic10_mean, std=cinic10_std))
test_dataset = CINICSSL(base_db_root, filen='valid', transform=transform_val)
return train_labeled_dataset, train_unlabeled_dataset, test_dataset
def get_cinic10(args):
transform_labeled = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=32,
padding=int(32*0.125),
padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(mean=cinic10_mean, std=cinic10_std)
])
transform_val = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=cinic10_mean, std=cinic10_std)
])
base_db_root = MyPath.db_root_dir(args.dataset)
base_dataset = CINIC10(base_db_root, filen = 'train')
train_labeled_idxs, train_unlabeled_idxs = x_u_split(args, base_dataset.targets)
train_labeled_dataset = CINICSSL(base_db_root, filen='train',indexs=train_labeled_idxs,transform=transform_labeled)
train_unlabeled_dataset = CINICSSL(base_db_root, filen='train', indexs=train_unlabeled_idxs, transform=TransformFixMatch(mean=cinic10_mean, std=cinic10_std))
test_dataset = CINICSSL(base_db_root, filen='valid', transform=transform_val)
return train_labeled_dataset, train_unlabeled_dataset, test_dataset
class TransformFixMatch(object):
def __init__(self, mean, std):
self.weak = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=32,
padding=int(32*0.125),
padding_mode='reflect')])
self.strong = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=32,
padding=int(32*0.125),
padding_mode='reflect'),
RandAugmentMC(n=2, m=10)])
self.normalize = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=mean, std=std)])
def __call__(self, x):
weak = self.weak(x)
strong = self.strong(x)
return self.normalize(weak), self.normalize(strong)
class CIFAR10SSL(datasets.CIFAR10):
def __init__(self, root, indexs, noise=None, train=True,
transform=None, target_transform=None,
download=False):
super().__init__(root, train=train,
transform=transform,
target_transform=target_transform,
download=download)
if noise is not None:
self.targets = noise
if indexs is not None:
self.data = self.data[indexs]
self.targets = np.array(self.targets)[indexs]
def __getitem__(self, index):
img, target = self.data[index], self.targets[index]
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
class CIFAR100SSL(datasets.CIFAR100):
def __init__(self, root, indexs, noise = None, train=True,
transform=None, target_transform=None,
download=False):
super().__init__(root, train=train,
transform=transform,
target_transform=target_transform,
download=download)
if noise is not None:
self.targets = noise
if indexs is not None:
self.data = self.data[indexs]
self.targets = np.array(self.targets)[indexs]
def __getitem__(self, index):
img, target = self.data[index], self.targets[index]
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
class CINIC10(Dataset):
"""`adapt from CIFAR10 <https://www.cs.toronto.edu/~kriz/cifar.html>`_ Dataset.
Args:
root (string): Root directory of dataset where directory
``cifar-10-batches-py`` exists or will be saved to if download is set to True.
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``
download (bool, optional): If true, downloads the dataset from the internet and
puts it in root directory. If dataset is already downloaded, it is not
downloaded again.
"""
def __init__(self, root, filen=True, transform=None, target_transform=None):
super(CINIC10, self).__init__()
self.root = os.path.join(root, 'npy')
self.transform = transform
self.target_transform = target_transform
self.file_n = filen # training set or test set
self.classes = ['frog', 'airplane', 'horse', 'truck', 'cat', 'deer', 'automobile', 'dog', 'bird', 'ship']
self.data = np.load(os.path.join(self.root, filen+"_data.npy"))
self.targets = np.load(os.path.join(self.root, filen+"_label.npy"))
self.targets = self.targets.tolist()
self._load_meta()
def _load_meta(self):
self.class_to_idx = {_class: i for i, _class in enumerate(self.classes)}
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
dict: {'image': image, 'target': index of target class, 'meta': dict}
"""
img, target = self.data[index], self.targets[index]
img_size = (img.shape[0], img.shape[1])
img = Image.fromarray(img)
class_name = self.classes[target]
if self.transform is not None:
img = self.transform(img)
return img, target
def get_image(self, index):
img = self.data[index]
return img
def __len__(self):
return len(self.data)
class CINICSSL(CINIC10):
def __init__(self, root, filen, indexs=None, noise=None,
transform=None, target_transform=None):
super().__init__(root=root, filen=filen, transform=transform,target_transform=target_transform)
if noise is not None:
self.targets = noise
if indexs is not None:
self.data = self.data[indexs]
self.targets = np.array(self.targets)[indexs]
def __getitem__(self, index):
img, target = self.data[index], self.targets[index]
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
DATASET_GETTERS = {'cifar10ssl': get_cifar10,
'cifar100ssl': get_cifar100,
'cifar10denoisessl': get_cifar10_denoisessl,
'cifar100denoisessl': get_cifar100_denoisessl,
'cifar10ldpssl': get_cifar10_ldpssl,
'cifar100ldpssl': get_cifar100_ldpssl,
'cinic10ssl': get_cinic10,
'cinic10denoisessl': get_cinic10_denoisessl,
'cinic10ldpssl': get_cinic10_ldpssl}