-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_loader.py
1126 lines (964 loc) · 49.9 KB
/
data_loader.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
import torch
import torch.utils.data as data
import torchvision
from torchvision import transforms
import random
import os
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
import numpy as np
#resize功能
from scipy import misc
# pytorch
import torch
import torch.nn as nn
import torchvision
from datasets.svhn import load_svhn
from datasets.mnist import load_mnist
from datasets.mnist_m import load_mnistm
from datasets.usps_ import load_usps
from datasets.gtsrb import load_gtsrb
from datasets.synth_number import load_syn
from datasets.office import load_office
from datasets.domainnet import load_domainnet
from datasets.pacs import load_pacs
from datasets.cifar import load_cifar
class Multi_Source_Base_Dataset(data.Dataset):
def __init__(self, root, partition):
super(Multi_Source_Base_Dataset, self).__init__()
# set dataset info
self.root = root
self.partition = partition
self.mean_pix = [0.485, 0.456, 0.406]
self.std_pix = [0.229, 0.224, 0.225]
self.scale = 256
self.crop_scale = 224
normalize = transforms.Normalize(mean=self.mean_pix, std=self.std_pix)
if self.partition == 'train':
self.transformer = transforms.Compose([transforms.Resize(self.scale),
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(self.crop_scale),
transforms.ToTensor(),
normalize])
else:
self.transformer = transforms.Compose([transforms.Resize(self.scale),
transforms.CenterCrop(self.crop_scale),
transforms.ToTensor(),
normalize])
def __len__(self):
if self.partition == 'train':
return int(min(sum(self.alpha), len(self.target_image)) / (self.num_class - 1))
elif self.partition == 'test':
return int(len(self.target_image) / (self.num_class - 1))
def __getitem__(self, item):
image_data = []
label_data = []
target_real_label = []
class_index_target = []
domain_label = []
ST_split = [] # Mask of targets to be evaluated
# select index for support class
num_class_index_target = int(self.target_ratio * (self.num_class - 1))
if self.target_ratio > 0:
available_index = [key for key in self.target_image_list.keys() if len(self.target_image_list[key]) > 0
and key < self.num_class - 1]
class_index_target = random.sample(available_index, min(num_class_index_target, len(available_index)))
class_index_source = list(set(range(self.num_class - 1)) - set(class_index_target))
random.shuffle(class_index_source)
for classes in class_index_source:
# select support samples from source domain or target domain
image = Image.open(random.choice(self.source_image[classes])).convert('RGB')
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(classes)
domain_label.append(1)
ST_split.append(0)
# target_real_label.append(classes)
for classes in class_index_target:
# select support samples from source domain or target domain
image = Image.open(random.choice(self.target_image_list[classes])).convert('RGB')
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(classes)
domain_label.append(0)
ST_split.append(0)
# target_real_label.append(classes)
# adding target samples
for i in range(self.num_class - 1):
if self.partition == 'train':
if self.target_ratio > 0:
index = random.choice(list(range(len(self.label_flag))))
else:
index = random.choice(list(range(len(self.target_image))))
# index = random.choice(list(range(len(self.label_flag))))
target_image = Image.open(self.target_image[index]).convert('RGB')
if self.transformer is not None:
target_image = self.transformer(target_image)
image_data.append(target_image)
label_data.append(self.label_flag[index])
target_real_label.append(self.target_label[index])
domain_label.append(0)
ST_split.append(1)
elif self.partition == 'test':
# For last batch
# if item * (self.num_class - 1) + i >= len(self.target_image):
# break
target_image = Image.open(self.target_image[item * (self.num_class - 1) + i]).convert('RGB')
if self.transformer is not None:
target_image = self.transformer(target_image)
image_data.append(target_image)
label_data.append(self.num_class)
target_real_label.append(self.target_label[item * (self.num_class - 1) + i])
domain_label.append(0)
ST_split.append(1)
image_data = torch.stack(image_data)
label_data = torch.LongTensor(label_data)
real_label_data = torch.tensor(target_real_label)
domain_label = torch.tensor(domain_label)
ST_split = torch.tensor(ST_split)
return image_data, label_data, real_label_data, domain_label, ST_split
def load_dataset(self):
source_image_list = {key: [] for key in range(self.num_class - 1)}
target_image_list = []
target_label_list = []
with open(self.source_path) as f:
for ind, line in enumerate(f.readlines()):
image_dir, label = line.split(' ')
label = label.strip()
if label == str(self.num_class-1):
continue
source_image_list[int(label)].append(image_dir)
# source_image_list.append(image_dir)
with open(self.target_path) as f:
for ind, line in enumerate(f.readlines()):
image_dir, label = line.split(' ')
label = label.strip()
# target_image_list[int(label)].append(image_dir)
target_image_list.append(image_dir)
target_label_list.append(int(label))
return source_image_list, target_image_list, target_label_list
class Digits_Dataset(Multi_Source_Base_Dataset):
def __init__(self, args, partition):
super(Digits_Dataset, self).__init__(args, partition)
# set dataset info
self.class_name = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
self.domain_name = ['mnistm', 'mnist', 'usps', 'svhn', 'syn']
self.target = args.target_domain
self.unk_class = args.unk_class
self.openset = True if args.unk_class is not None else False
self.num_class = args.unk_class + 1 if self.openset else len(self.class_name)
self.num_domains = len(self.domain_name)
self.mean_pix = torch.tensor((0.,))
self.std_pix = torch.tensor((1.,))
self.scale = 32
self.crop_scale = 32
self.partition = partition
self.args = args
normalize = transforms.Normalize(self.mean_pix, self.std_pix)
if self.partition == 'train':
self.transformer = transforms.Compose([transforms.Resize(self.scale),
# transforms.RandomHorizontalFlip(),
# transforms.RandomRotation(10),
transforms.ToTensor(),
normalize])
else:
self.transformer = transforms.Compose([transforms.Resize(self.scale),
# transforms.CenterCrop(self.crop_scale),
transforms.ToTensor(),
normalize])
self.source_data, self.target_data = self.load_multi_datasets(self.target)
def __len__(self):
full_set = self.source_data + self.target_data
return min([len(full_set[domain]["labels"]) for domain in range(len(full_set))])
def __getitem__(self, item):
image_data = []
label_data = []
domain_labels = []
for source_id in range(self.num_domains - 1):
image = self.source_data[source_id]["imgs"][item].transpose(1, 2, 0)
label = self.source_data[source_id]["labels"][item]
if list(image.shape)[-1] == 3:
image = Image.fromarray(np.uint8(image)).convert('RGB')
elif list(image.shape)[-1] == 1:
# test = Image.fromarray(np.uint8(image.squeeze()))
image = Image.fromarray(np.uint8(np.repeat(image, 3, axis=-1)))
else:
print("Domain {} Image #{} error".format(source_id, item))
# image.show()
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(label)
domain_labels.append(source_id)
image = self.target_data[0]["imgs"][item].transpose(1, 2, 0)
label = self.target_data[0]["labels"][item]
if list(image.shape)[-1] == 3:
image = Image.fromarray(np.uint8(image)).convert('RGB')
elif list(image.shape)[-1] == 1:
# test = Image.fromarray(np.uint8(image.squeeze()))
image = Image.fromarray(np.uint8(np.repeat(image, 3, axis=-1)))
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(label)
domain_labels.append(self.num_domains - 1)
image_data = torch.stack(image_data)
label_data = torch.LongTensor(label_data)
domain_labels = torch.tensor(domain_labels)
return image_data, label_data, domain_labels
def shuffle(self, data):
# new_data = {}
zip_data = list(zip(data["imgs"], data["labels"]))
random.shuffle(zip_data)
shuffled_imgs, shuffled_labels = zip(*zip_data)
new_data = {"imgs": shuffled_imgs, "labels": shuffled_labels}
return new_data
def shuffle_datasets(self):
self.target_data[0] = self.shuffle(self.target_data[0])
for source_id in range(self.num_domains - 1):
self.source_data[source_id] = self.shuffle(self.source_data[source_id])
def load_dataset(self, domain_name):
if domain_name == 'svhn':
train_image, train_label, \
test_image, test_label = load_svhn(self.args)
if domain_name == 'mnist':
train_image, train_label, \
test_image, test_label = load_mnist(self.args)
if domain_name == 'mnistm':
train_image, train_label, \
test_image, test_label = load_mnistm(self.args)
if domain_name == 'usps':
train_image, train_label, \
test_image, test_label = load_usps(self.args)
if domain_name == 'syn':
train_image, train_label, \
test_image, test_label = load_syn(self.args)
return train_image, train_label, test_image, test_label
def load_multi_datasets(self, target):
source_data_list = []
target_data_list = []
source_name = self.domain_name
source_name.remove(target)
if self.partition == "train":
target_train, target_train_label, _, _ = self.load_dataset(target)
if self.openset:
target_train_label[target_train_label >= self.unk_class] = self.unk_class
target_data_list.append({"imgs": target_train, "labels": target_train_label})
for source_id, source in enumerate(source_name):
source_train, source_train_label, _, _ = self.load_dataset(source)
if self.openset:
train_idx_del = np.array([source_train_label[j] == source_id or source_train_label[j] >= self.unk_class
for j in range(len(source_train_label))])
source_train = source_train[~train_idx_del]
source_train_label = source_train_label[~train_idx_del]
source_data_list.append({"imgs": source_train, "labels": source_train_label})
elif self.partition == "test":
_, _, target_test, target_test_label = self.load_dataset(target)
if self.openset:
target_test_label[target_test_label >= self.unk_class] = self.unk_class
target_length = len(target_test_label)
target_data_list.append({"imgs": target_test, "labels": target_test_label})
for source_id, source in enumerate(source_name):
# _, _, source_test, source_test_label = self.load_dataset(source)
# get more source samples to match the target samples
# if len(source_test_label) < target_length:
# idx = random.choices(range(len(source_test_label)), k=target_length-len(source_test_label))
# source_test = np.concatenate((source_test, source_test[idx]), axis=0)
# source_test_label = np.concatenate((source_test_label, source_test_label[idx]))
# if self.openset:
# test_idx_del = np.array([source_test_label[j] == source_id or source_test_label[j] >= self.unk_class
# for j in range(len(source_test_label))])
# source_test = source_test[~test_idx_del]
# source_test_label = source_test_label[~test_idx_del]
source_data_list.append({"imgs": target_test, "labels": target_test_label})
return source_data_list, target_data_list
class Office_Dataset(Multi_Source_Base_Dataset):
def __init__(self, args, partition):
super(Office_Dataset, self).__init__(args, partition)
# set dataset info
self.args = args
self.class_name = ["back_pack", "bike", "bike_helmet", "bookcase", "bottle",
"calculator", "desk_chair", "desk_lamp", "desktop_computer", "file_cabinet", "unk"]
self.domain_name = ['amazon', 'dslr', 'webcam']
self.target = args.target_domain
self.unk_class = args.unk_class
self.openset = True if args.unk_class is not None else False
self.rm_idx = [[0, 1, 2, 3], [4, 5, 6, 7]] if args.strict_setting else None
self.num_class = len(self.class_name)
self.num_domains = len(self.domain_name)
self.mean_pix = torch.tensor((0.,))
self.std_pix = torch.tensor((1.,))
self.scale = 224
self.partition = partition
normalize = transforms.Normalize(self.mean_pix, self.std_pix)
if self.partition == 'train':
self.transformer = transforms.Compose([transforms.Resize(self.scale),
# transforms.RandomHorizontalFlip(),
# transforms.RandomRotation(10),
transforms.ToTensor(),
normalize])
else:
self.transformer = transforms.Compose([transforms.Resize(self.scale),
# transforms.CenterCrop(self.crop_scale),
transforms.ToTensor(),
normalize])
self.source_data, self.target_data = self.load_multi_datasets(self.target)
def __len__(self):
full_set = self.source_data + self.target_data
return min([len(full_set[domain]["labels"]) for domain in range(len(full_set))])
def __getitem__(self, item):
image_data = []
label_data = []
domain_labels = []
for source_id in range(self.num_domains - 1):
image = Image.open(self.source_data[source_id]["imgs"][item]).convert('RGB')
label = self.source_data[source_id]["labels"][item]
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(label)
domain_labels.append(source_id)
image = Image.open(self.target_data[0]["imgs"][item]).convert('RGB')
label = self.target_data[0]["labels"][item]
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(label)
domain_labels.append(self.num_domains - 1)
image_data = torch.stack(image_data)
label_data = torch.LongTensor(label_data)
domain_labels = torch.tensor(domain_labels)
return image_data, label_data, domain_labels
def shuffle(self, data):
# new_data = {}
zip_data = list(zip(data["imgs"], data["labels"]))
random.shuffle(zip_data)
shuffled_imgs, shuffled_labels = zip(*zip_data)
new_data = {"imgs": shuffled_imgs, "labels": shuffled_labels}
return new_data
def shuffle_datasets(self):
self.target_data[0] = self.shuffle(self.target_data[0])
for source_id in range(self.num_domains - 1):
self.source_data[source_id] = self.shuffle(self.source_data[source_id])
def load_dataset(self, domain_name):
args = self.args
train_image, train_label, test_image, test_label = load_office(args, domain_name)
return train_image, train_label, test_image, test_label
def load_multi_datasets(self, target):
source_data_list = []
target_data_list = []
source_name = self.domain_name
source_name.remove(target)
if self.partition == "train":
target_train, target_train_label, _, _ = self.load_dataset(target)
target_data_list.append({"imgs": target_train, "labels": target_train_label})
for source_id, source in enumerate(source_name):
source_train, source_train_label, _, _ = self.load_dataset(source)
if self.openset:
if self.args.strict_setting:
train_idx_del = np.array(
[source_train_label[j] in self.rm_idx[source_id] for j in range(len(source_train_label))])
else:
train_idx_del = np.array([source_train_label[j] == source_id for j in range(len(source_train_label))])
idx = np.where(train_idx_del == False)
source_train = [source_train[i] for i in idx[0]]
source_train_label = source_train_label[idx] # @note 0,1,2,3 as unk
source_data_list.append({"imgs": source_train, "labels": source_train_label})
elif self.partition == "test":
_, _, target_test, target_test_label = self.load_dataset(target)
target_data_list.append({"imgs": target_test, "labels": target_test_label})
for source_id, source in enumerate(source_name):
source_data_list.append({"imgs": target_test, "labels": target_test_label})
return source_data_list, target_data_list
class DomainNet_Dataset(Multi_Source_Base_Dataset):
def __init__(self, args, partition):
super(DomainNet_Dataset, self).__init__(args, partition)
# set dataset info
self.args = args
self.class_name = list(np.arange(345))
self.domain_name = ['clipart', 'infograph', 'painting', 'quickdraw', 'real', 'sketch']
self.target = args.target
self.unk_class = args.unk_class
self.openset = True if args.unk_class is not None else False
self.rm_idx = [list(np.arange(50)), list(np.arange(50, 100)), list(np.arange(100, 150)), list(np.arange(150, 200)),
list(np.arange(200, 250))] if args.strict_setting else None
self.num_class = self.unk_class + 1
self.num_domains = len(self.domain_name)
self.mean_pix = torch.tensor((0.,))
self.std_pix = torch.tensor((1.,))
self.scale = 32
self.partition = partition
normalize = transforms.Normalize(self.mean_pix, self.std_pix)
if self.partition == 'train':
self.transformer = transforms.Compose([transforms.Resize(size=(self.scale, self.scale)),
# transforms.RandomHorizontalFlip(),
# transforms.RandomRotation(10),
transforms.ToTensor(),
normalize])
else:
self.transformer = transforms.Compose([transforms.Resize(self.scale),
# transforms.CenterCrop(self.crop_scale),
transforms.ToTensor(),
normalize])
self.source_data, self.target_data = self.load_multi_datasets(self.target)
def __len__(self):
full_set = self.source_data + self.target_data
return min([len(full_set[domain]["labels"]) for domain in range(len(full_set))])
def __getitem__(self, item):
image_data = []
label_data = []
domain_labels = []
for source_id in range(self.num_domains - 1):
image = Image.open(self.source_data[source_id]["imgs"][item]).convert('RGB')
label = self.source_data[source_id]["labels"][item]
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(label)
domain_labels.append(source_id)
image = Image.open(self.target_data[0]["imgs"][item]).convert('RGB')
label = self.target_data[0]["labels"][item]
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(label)
domain_labels.append(self.num_domains - 1)
image_data = torch.stack(image_data)
label_data = torch.LongTensor(label_data)
domain_labels = torch.tensor(domain_labels)
return image_data, label_data, domain_labels
def shuffle(self, data):
# new_data = {}
zip_data = list(zip(data["imgs"], data["labels"]))
random.shuffle(zip_data)
shuffled_imgs, shuffled_labels = zip(*zip_data)
new_data = {"imgs": shuffled_imgs, "labels": shuffled_labels}
return new_data
def shuffle_datasets(self):
self.target_data[0] = self.shuffle(self.target_data[0])
for source_id in range(self.num_domains - 1):
self.source_data[source_id] = self.shuffle(self.source_data[source_id])
def load_dataset(self, domain_name):
args = self.args
train_image, train_label, test_image, test_label = load_domainnet(args, domain_name)
return train_image, train_label, test_image, test_label
def load_multi_datasets(self, target):
source_data_list = []
target_data_list = []
source_name = self.domain_name
source_name.remove(target)
if self.partition == "train":
target_train, target_train_label, _, _ = self.load_dataset(target)
if self.openset:
target_train_label[target_train_label >= self.unk_class] = self.unk_class
target_data_list.append({"imgs": target_train, "labels": target_train_label})
for source_id, source in enumerate(source_name):
source_train, source_train_label, _, _ = self.load_dataset(source)
if self.openset:
if self.args.strict_setting:
train_idx_del = np.array(
[(source_train_label[j] in self.rm_idx[source_id] or source_train_label[j] >= self.unk_class) for j in range(len(source_train_label))])
else:
train_idx_del = np.array([source_train_label[j] == source_id for j in range(len(source_train_label))])
idx = np.where(train_idx_del == False)
source_train = [source_train[i] for i in idx[0]]
source_train_label = source_train_label[idx]
source_data_list.append({"imgs": source_train, "labels": source_train_label})
elif self.partition == "test":
_, _, target_test, target_test_label = self.load_dataset(target)
if self.openset:
target_test_label[target_test_label >= self.unk_class] = self.unk_class
target_data_list.append({"imgs": target_test, "labels": target_test_label})
for source_id, source in enumerate(source_name):
source_data_list.append({"imgs": target_test, "labels": target_test_label})
return source_data_list, target_data_list
class Base_Dataset(data.Dataset):
def __init__(self, root, partition, target_ratio=0.0):
super(Base_Dataset, self).__init__()
# set dataset info
self.root = root
self.partition = partition
self.target_ratio = target_ratio
# self.target_ratio=0 no mixup
mean_pix = [0.485, 0.456, 0.406]
std_pix = [0.229, 0.224, 0.225]
normalize = transforms.Normalize(mean=mean_pix, std=std_pix)
if self.partition == 'train':
self.transformer = transforms.Compose([transforms.Resize(256),
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(224),
transforms.ToTensor(),
normalize])
else:
self.transformer = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize])
def __len__(self):
if self.partition == 'train':
return int(min(sum(self.alpha), len(self.target_image)) / (self.num_class - 1))
elif self.partition == 'test':
return int(len(self.target_image) / (self.num_class - 1))
def __getitem__(self, item):
image_data = []
label_data = []
target_real_label = []
class_index_target = []
domain_label = []
ST_split = [] # Mask of targets to be evaluated
# select index for support class
num_class_index_target = int(self.target_ratio * (self.num_class - 1))
if self.target_ratio > 0:
available_index = [key for key in self.target_image_list.keys() if len(self.target_image_list[key]) > 0
and key < self.num_class - 1]
class_index_target = random.sample(available_index, min(num_class_index_target, len(available_index)))
class_index_source = list(set(range(self.num_class - 1)) - set(class_index_target))
random.shuffle(class_index_source)
for classes in class_index_source:
# select support samples from source domain or target domain
image = Image.open(random.choice(self.source_image[classes])).convert('RGB')
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(classes)
domain_label.append(1)
ST_split.append(0)
# target_real_label.append(classes)
for classes in class_index_target:
# select support samples from source domain or target domain
image = Image.open(random.choice(self.target_image_list[classes])).convert('RGB')
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(classes)
domain_label.append(0)
ST_split.append(0)
# target_real_label.append(classes)
# adding target samples
for i in range(self.num_class - 1):
if self.partition == 'train':
if self.target_ratio > 0:
index = random.choice(list(range(len(self.label_flag))))
else:
index = random.choice(list(range(len(self.target_image))))
# index = random.choice(list(range(len(self.label_flag))))
target_image = Image.open(self.target_image[index]).convert('RGB')
if self.transformer is not None:
target_image = self.transformer(target_image)
image_data.append(target_image)
label_data.append(self.label_flag[index])
target_real_label.append(self.target_label[index])
domain_label.append(0)
ST_split.append(1)
elif self.partition == 'test':
# For last batch
# if item * (self.num_class - 1) + i >= len(self.target_image):
# break
target_image = Image.open(self.target_image[item * (self.num_class - 1) + i]).convert('RGB')
if self.transformer is not None:
target_image = self.transformer(target_image)
image_data.append(target_image)
label_data.append(self.num_class)
target_real_label.append(self.target_label[item * (self.num_class - 1) + i])
domain_label.append(0)
ST_split.append(1)
image_data = torch.stack(image_data)
label_data = torch.LongTensor(label_data)
real_label_data = torch.tensor(target_real_label)
domain_label = torch.tensor(domain_label)
ST_split = torch.tensor(ST_split)
return image_data, label_data, real_label_data, domain_label, ST_split
def load_dataset(self):
source_image_list = {key: [] for key in range(self.num_class - 1)}
target_image_list = []
target_label_list = []
with open(self.source_path) as f:
for ind, line in enumerate(f.readlines()):
image_dir, label = line.split(' ')
label = label.strip()
if label == str(self.num_class-1):
continue
source_image_list[int(label)].append(image_dir)
# source_image_list.append(image_dir)
with open(self.target_path) as f:
for ind, line in enumerate(f.readlines()):
image_dir, label = line.split(' ')
label = label.strip()
# target_image_list[int(label)].append(image_dir)
target_image_list.append(image_dir)
target_label_list.append(int(label))
return source_image_list, target_image_list, target_label_list
class PACS_Dataset(Multi_Source_Base_Dataset):
def __init__(self, args, partition):
super(PACS_Dataset, self).__init__(args, partition)
# set dataset info
self.args = args
# @todo
self.class_name = ["0", "1", "2", "3", "4", "5"] # need to remove some of them as unk
self.domain_name = ['art_painting', 'cartoon', 'photo', 'sketch']
self.target = args.target_domain
self.unk_class = args.unk_class
self.openset = True if args.unk_class is not None else False
# @note this is for remove some of the classes in domains to make it as openset setting, the method could be vary
self.rm_idx = [[0], [1], [2]] if args.strict_setting else None
self.num_class = len(self.class_name)
self.num_domains = len(self.domain_name)
self.mean_pix = torch.tensor((0.,))
self.std_pix = torch.tensor((1.,))
self.scale = 64
self.partition = partition
# self.crop_scale = 224 # zixin: define new values for crop_scale
normalize = transforms.Normalize(self.mean_pix, self.std_pix)
if self.partition == 'train':
self.transformer = transforms.Compose([transforms.Resize(self.scale),
transforms.ToTensor(),
normalize])
else:
self.transformer = transforms.Compose([transforms.Resize(self.scale),
transforms.ToTensor(),
normalize])
self.source_data, self.target_data = self.load_multi_datasets(self.target)
def __len__(self):
full_set = self.source_data + self.target_data
return min([len(full_set[domain]["labels"]) for domain in range(len(full_set))])
def __getitem__(self, item):
image_data = []
label_data = []
domain_labels = []
for source_id in range(self.num_domains - 1):
image = Image.open(self.source_data[source_id]["imgs"][item]).convert('RGB')
label = self.source_data[source_id]["labels"][item]
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(label)
domain_labels.append(source_id)
image = Image.open(self.target_data[0]["imgs"][item]).convert('RGB')
label = self.target_data[0]["labels"][item]
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(label)
domain_labels.append(self.num_domains - 1)
image_data = torch.stack(image_data)
label_data = torch.LongTensor(label_data)
domain_labels = torch.tensor(domain_labels)
return image_data, label_data, domain_labels
def shuffle(self, data):
# new_data = {}
zip_data = list(zip(data["imgs"], data["labels"]))
random.shuffle(zip_data)
shuffled_imgs, shuffled_labels = zip(*zip_data)
new_data = {"imgs": shuffled_imgs, "labels": shuffled_labels}
return new_data
def shuffle_datasets(self):
self.target_data[0] = self.shuffle(self.target_data[0])
for source_id in range(self.num_domains - 1):
self.source_data[source_id] = self.shuffle(self.source_data[source_id])
def load_dataset(self, domain_name):
args = self.args
train_image, train_label, test_image, test_label = load_pacs(args, domain_name)
return train_image, train_label, test_image, test_label
def load_multi_datasets(self, target):
source_data_list = []
target_data_list = []
source_name = self.domain_name # 4 domains
source_name.remove(target) # under multi-source setting, we still have 3 domains
if self.partition == "train":
target_train, target_train_label, _, _ = self.load_dataset(target)
# target_train_label = np.array([x-1 for x in target_train_label])
target_data_list.append({"imgs": target_train, "labels": target_train_label})
for source_id, source in enumerate(source_name):
source_train, source_train_label, _, _ = self.load_dataset(source)
if self.openset:
# source and target only share a part of data classes
if self.args.strict_setting:
train_idx_del = np.array(
[source_train_label[j] in self.rm_idx[source_id] for j in range(len(source_train_label))])
else:
train_idx_del = np.array([source_train_label[j] == source_id for j in range(len(source_train_label))])
idx = np.where(train_idx_del == False)
source_train = [source_train[i] for i in idx[0]]
source_train_label = source_train_label[idx]
# source_train_label = np.array([x-1 for x in source_train_label])
source_data_list.append({"imgs": source_train, "labels": source_train_label})
elif self.partition == "test":
_, _, target_test, target_test_label = self.load_dataset(target)
# target_test_label = np.array([x-1 for x in target_test_label])
target_data_list.append({"imgs": target_test, "labels": target_test_label})
for source_id, source in enumerate(source_name):
source_data_list.append({"imgs": target_test, "labels": target_test_label})
return source_data_list, target_data_list
class CIFAR_Dataset(Multi_Source_Base_Dataset):
def __init__(self, args, partition):
super(CIFAR_Dataset, self).__init__(args, partition)
# set dataset info
self.args = args
self.scale = 32
self.class_name = ['0','1','2','3','4','5']
self.domain_name = ['brightness', 'contrast', 'fog', 'defocus_blur', 'frost']
# , 'gaussian_blur', 'gaussian_noise', 'elastic_transform',
# 'glass_blur', 'impulse_noise', 'jpeg_compression', 'motion_blur', 'pixelate', 'saturate', 'shot_noise', 'snow',
# 'spatter', 'speckle_noise', 'zoom_blur'
self.target = args.target_domain
self.unk_class = args.unk_class
self.openset = True if args.unk_class is not None else False
# @note this is for remove some of the classes in domains to make it as openset setting, the method could be vary
self.rm_idx = [[0], [1], [2], [3]] if args.strict_setting else None
self.num_class = len(self.class_name)
self.num_domains = len(self.domain_name)
self.mean_pix = torch.tensor((0.,))
self.std_pix = torch.tensor((1.,))
self.scale = 32
self.partition = partition
# self.crop_scale = 224 # zixin: define new values for crop_scale
normalize = transforms.Normalize(self.mean_pix, self.std_pix)
if self.partition == 'train':
self.transformer = transforms.Compose([transforms.ToTensor(),
transforms.Resize(self.scale),
normalize])
else:
self.transformer = transforms.Compose([transforms.ToTensor(),
transforms.Resize(self.scale),
normalize])
self.source_data, self.target_data = self.load_multi_datasets(self.target)
def __len__(self):
full_set = self.source_data + self.target_data
return min([len(full_set[domain]["labels"]) for domain in range(len(full_set))])
def __getitem__(self, item):
image_data = []
label_data = []
domain_labels = []
for source_id in range(self.num_domains - 1):
image = self.source_data[source_id]["imgs"][item]
label = self.source_data[source_id]["labels"][item]
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(label)
domain_labels.append(source_id)
image = self.target_data[0]["imgs"][item]
label = self.target_data[0]["labels"][item]
if self.transformer is not None:
image = self.transformer(image)
image_data.append(image)
label_data.append(label)
domain_labels.append(self.num_domains - 1)
image_data = torch.stack(image_data)
label_data = torch.LongTensor(label_data)
domain_labels = torch.tensor(domain_labels)
return image_data, label_data, domain_labels
def shuffle(self, data):
# new_data = {}
zip_data = list(zip(data["imgs"], data["labels"]))
random.shuffle(zip_data)
shuffled_imgs, shuffled_labels = zip(*zip_data)
new_data = {"imgs": shuffled_imgs, "labels": shuffled_labels}
return new_data
def shuffle_datasets(self):
self.target_data[0] = self.shuffle(self.target_data[0])
for source_id in range(self.num_domains - 1):
self.source_data[source_id] = self.shuffle(self.source_data[source_id])
def load_dataset(self, domain_name):
args = self.args
train_image, train_label, test_image, test_label = load_cifar(args, domain_name)
return train_image, train_label, test_image, test_label
def load_multi_datasets(self, target):
source_data_list = []
target_data_list = []
source_name = self.domain_name
source_name.remove(target)
if self.partition == "train":
target_train, target_train_label, _, _ = self.load_dataset(target)
target_data_list.append({"imgs": target_train, "labels": target_train_label})
for source_id, source in enumerate(source_name):
source_train, source_train_label, _, _ = self.load_dataset(source)
if self.openset:
# source and target only share a part of data classes
if self.args.strict_setting:
train_idx_del = np.array(
[source_train_label[j] in self.rm_idx[source_id] for j in range(len(source_train_label))])
else:
train_idx_del = np.array([source_train_label[j] == source_id for j in range(len(source_train_label))])
idx = np.where(train_idx_del == False)
source_train = [source_train[i] for i in idx[0]]
source_train_label = source_train_label[idx]
source_data_list.append({"imgs": source_train, "labels": source_train_label})
elif self.partition == "test":
_, _, target_test, target_test_label = self.load_dataset(target)
target_data_list.append({"imgs": target_test, "labels": target_test_label})
for source_id, source in enumerate(source_name):
source_data_list.append({"imgs": target_test, "labels": target_test_label})
return source_data_list, target_data_list
# class Office_Dataset(Base_Dataset):
#
# def __init__(self, root, partition, label_flag=None, source='A', target='W', target_ratio=0.0):
# super(Office_Dataset, self).__init__(root, partition, target_ratio)
# # set dataset info
# src_name, tar_name = self.getFilePath(source, target)
# self.source_path = os.path.join(root, src_name)
# self.target_path = os.path.join(root, tar_name)
# self.class_name = ["back_pack", "bike", "bike_helmet", "bookcase", "bottle",
# "calculator", "desk_chair", "desk_lamp", "desktop_computer", "file_cabinet", "unk"]
# self.num_class = len(self.class_name)
# self.source_image, self.target_image, self.target_label = self.load_dataset()
# self.alpha = [len(self.source_image[key]) for key in self.source_image.keys()]
# self.label_flag = label_flag
#
# # create the unlabeled tag
# if self.label_flag is None:
# self.label_flag = torch.ones(len(self.target_image)) * self.num_class
#
# else:
# # if pseudo label comes
# self.target_image_list = {key: [] for key in range(self.num_class + 1)}
# for i in range(len(self.label_flag)):
# self.target_image_list[self.label_flag[i].item()].append(self.target_image[i])
#
# if self.target_ratio > 0:
# self.alpha_value = [len(self.source_image[key]) + len(self.target_image_list[key]) for key in self.source_image.keys()]
# else:
# self.alpha_value = self.alpha
#
# self.alpha_value = np.array(self.alpha_value)
# self.alpha_value = (self.alpha_value.max() + 1 - self.alpha_value) / self.alpha_value.mean()
# self.alpha_value = torch.tensor(self.alpha_value).float().cuda()
#
# def getFilePath(self, source, target):
#
# if source == 'A':
# src_name = 'amazon_src_list.txt'
# elif source == 'W':
# src_name = 'webcam_src_list.txt'
# elif source == 'D':
# src_name = 'dslr_src_list.txt'
# else:
# print("Unknown Source Type, only supports A W D.")