-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatasets.py
1043 lines (828 loc) · 35.8 KB
/
datasets.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
import collections
import glob
import os
import os.path as osp
import numpy as np
import torch
from PIL import Image
from PIL import ImageOps
from torch.utils import data
from torchvision.transforms import Compose
from tqdm import tqdm
from transform import HorizontalFlip, VerticalFlip, ReLabel
from util import emphasize_str
AVAILABLE_DATASET_LIST = ["gta", "city", "test", "ir", "city16", "synthia", "2d3d", "sun", "suncg", "nyu"]
class ConcatDataset(torch.utils.data.Dataset):
def __init__(self, *datasets):
self.datasets = datasets
def __getitem__(self, i):
return tuple(d[i] for d in self.datasets)
def __len__(self):
return min(len(d) for d in self.datasets)
def default_loader(path):
return Image.open(path)
class CityDataSet(data.Dataset):
IMG_SIZE = [2048, 1024]
PSEUDO_IR_DIR = "/data/unagi0/dataset/cityscapes/pseudo_ir/"
def __init__(self, root, split="train", img_transform=None, label_transform=None, test=True, input_ch=3,
label_type=None, joint_transform=None):
assert input_ch in [1, 3, 4]
self.input_ch = input_ch
self.root = root
self.split = split
# self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
self.files = collections.defaultdict(list)
self.img_transform = img_transform
self.label_transform = label_transform
self.joint_transform = joint_transform
self.test = test
self.use_pseudo_ir = True if "pseudo_ir" in split else False # train_pseudo_ir
if self.use_pseudo_ir:
assert input_ch == 4
print ("pseudo_ir will be used!")
split = split.split("_")[0]
self.split = split
data_dir = root
# for split in ["train", "trainval", "val"]:
imgsets_dir = osp.join(data_dir, "leftImg8bit/%s.txt" % self.split)
with open(imgsets_dir) as imgset_file:
for name in imgset_file:
name = name.strip()
img_file = osp.join(data_dir, "leftImg8bit/%s" % name)
if label_type == "label16":
name = name.replace('leftImg8bit', 'gtFine_label16IDs')
else:
name = name.replace('leftImg8bit', 'gtFine_labelTrainIds')
label_file = osp.join(data_dir, "gtFine/%s" % name)
self.files[self.split].append({
"img": img_file,
"label": label_file
})
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
datafiles = self.files[self.split][index]
img_file = datafiles["img"]
img = Image.open(img_file).convert('RGB')
np3ch = np.array(img)
if self.input_ch == 1:
img = ImageOps.grayscale(img)
elif self.input_ch == 4:
if self.use_pseudo_ir:
im_name = os.path.split(img_file)[-1]
pseudo_ir_name = os.path.join(self.PSEUDO_IR_DIR, self.split, im_name)
ir_img = np.array(Image.open(pseudo_ir_name))[:, :, np.newaxis]
extended_np3ch = np.concatenate([np3ch, ir_img], axis=2)
else:
extended_np3ch = np.concatenate([np3ch, np3ch[:, :, 0:1]], axis=2)
img = Image.fromarray(np.uint8(extended_np3ch))
label_file = datafiles["label"]
label = Image.open(label_file).convert("P")
if self.joint_transform:
img, label = self.joint_transform(img, label)
if self.img_transform:
img = self.img_transform(img)
if self.label_transform:
label = self.label_transform(label)
if self.test:
return img, label, img_file
return img, label
# TODO support joint_transform
class GTADataSet(data.Dataset):
IMG_SIZE = [1914, 1052]
def __init__(self, root, split="images", img_transform=None, label_transform=None,
test=False, input_ch=3, joint_transform=None):
# Note; split "train" and "images" are SAME!!!
assert split in ["images", "test", "train"]
assert input_ch in [1, 3, 4]
self.input_ch = input_ch
self.root = root
self.split = split
# self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
self.files = collections.defaultdict(list)
self.img_transform = img_transform
self.label_transform = label_transform
self.joint_transform = joint_transform
self.h_flip = HorizontalFlip()
self.v_flip = VerticalFlip()
self.test = test
data_dir = root
imgsets_dir = osp.join(data_dir, "%s.txt" % split)
with open(imgsets_dir) as imgset_file:
for name in imgset_file:
name = name.strip()
img_file = osp.join(data_dir, "%s" % name)
# name = name.replace('leftImg8bit','gtFine_labelTrainIds')
label_file = osp.join(data_dir, "%s" % name.replace('images', 'labels_gt'))
self.files[split].append({
"img": img_file,
"label": label_file
})
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
datafiles = self.files[self.split][index]
img_file = datafiles["img"]
img = Image.open(img_file).convert('RGB')
np3ch = np.array(img)
if self.input_ch == 1:
img = ImageOps.grayscale(img)
elif self.input_ch == 4:
extended_np3ch = np.concatenate([np3ch, np3ch[:, :, 0:1]], axis=2)
img = Image.fromarray(np.uint8(extended_np3ch))
label_file = datafiles["label"]
label = Image.open(label_file).convert("P")
if self.joint_transform:
img, label = self.joint_transform(img, label)
if self.img_transform:
img = self.img_transform(img)
if self.label_transform:
label = self.label_transform(label)
if self.test:
return img, label, img_file
return img, label
class SynthiaDataSet(data.Dataset):
IMG_SIZE = [1280, 760]
def __init__(self, root, split="all", img_transform=None, label_transform=None,
test=False, input_ch=3, joint_transform=None):
# TODO this does not support "split" parameter
assert input_ch in [1, 3, 4]
self.input_ch = input_ch
self.root = root
self.split = split
self.files = collections.defaultdict(list)
self.img_transform = img_transform
self.label_transform = label_transform
self.joint_transform = joint_transform
self.test = test
rgb_dir = osp.join(root, "RGB")
gt_dir = osp.join(root, "GT", "LABELS16")
rgb_fn_list = glob.glob(osp.join(rgb_dir, "*.png"))
gt_fn_list = glob.glob(osp.join(gt_dir, "*.png"))
for rgb_fn, gt_fn in zip(rgb_fn_list, gt_fn_list):
self.files[split].append({
"rgb": rgb_fn,
"label": gt_fn
})
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
datafiles = self.files[self.split][index]
img_file = datafiles["rgb"]
img = Image.open(img_file).convert('RGB')
np3ch = np.array(img)
if self.input_ch == 1:
img = ImageOps.grayscale(img)
elif self.input_ch == 4:
extended_np3ch = np.concatenate([np3ch, np3ch[:, :, 0:1]], axis=2)
img = Image.fromarray(np.uint8(extended_np3ch))
label_file = datafiles["label"]
label = Image.open(label_file).convert("P")
if self.joint_transform:
img, label = self.joint_transform(img, label)
if self.img_transform:
img = self.img_transform(img)
if self.label_transform:
label = self.label_transform(label)
if self.test:
return img, label, img_file
return img, label
def depth_scaling(np_img, depmin=None, depmax=None):
depmax = depmax if depmax else np_img.max()
depmin = depmax if depmin else np_img.min()
processed_np_img = (np_img.astype(np.float64) - depmin) / (depmax - depmin)
return np.uint8(processed_np_img * 255)
# TODO support joint_transform
class Stanford2D3DSemanticsDataSet(data.Dataset):
"""
{'<UNK>': 255,
'beam': 0,
'board': 1,
'bookcase': 2,
'ceiling': 3,
'chair': 4,
'clutter': 5,
'column': 6,
'door': 7,
'floor': 8,
'sofa': 9,
'table': 10,
'wall': 11,
'window': 12}
"""
## split info: http://buildingparser.stanford.edu/dataset.html#sample
TRAIN_SPLIT_AREA_LIST = ["area_1", "area_2", "area_3", "area_4", "area_6"]
TEST_SPLIT_TRAIN_SPLIT_AREA_LIST = ["area_5a", "area_5b"]
IMG_SIZE = [1080, 1080]
SEED = 42
def check_split_and_input_ch(self, split, input_ch):
modal = split.split("_")[-1]
if modal == "rgbd":
assert input_ch == 4
print ("4ch is Depth channel")
elif modal == "d":
assert input_ch == 1
if modal == "rgb" and input_ch == 4:
print (emphasize_str("4ch is R channel"))
def __init__(self, root, split="train_rgb", img_transform=None, label_transform=None,
test=False, input_ch=3, joint_transform=None):
assert split in ["train_rgb", "train_rgbd", "test_rgb", "test_rgbd", "small_test_rgb", "small_test_rgbd",
"train_d", "small_test_d"] ## "test_rgb", "test_rgbd"
assert input_ch in [1, 3, 4]
self.check_split_and_input_ch(split, input_ch)
self.input_ch = input_ch
self.root = root
self.split = split
self.files = collections.defaultdict(list)
self.img_transform = img_transform
self.label_transform = label_transform
self.joint_transform = joint_transform
self.test = test
def get_fn_list_from_area_list(area_list, modal):
img_fn_list = []
for train_area in area_list:
img_dir = osp.join(root, "XYZ", train_area, "data", modal)
img_fn_list.extend(glob.glob(osp.join(img_dir, "*.png")))
return img_fn_list
self.area_list = self.TRAIN_SPLIT_AREA_LIST if "train" in split else self.TEST_SPLIT_TRAIN_SPLIT_AREA_LIST
rgb_fn_list = sorted(get_fn_list_from_area_list(self.area_list, "rgb"))
depth_fn_list = sorted(get_fn_list_from_area_list(self.area_list, "depth"))
gt_fn_list = sorted(get_fn_list_from_area_list(self.area_list, "cls_lbl"))
len_rgb_fn_list = len(rgb_fn_list)
assert len_rgb_fn_list == len(depth_fn_list)
assert len_rgb_fn_list == len(gt_fn_list)
if "small" in split:
np.random.seed(self.SEED)
on_idxes = np.random.permutation(len_rgb_fn_list)[:self.N_SMALL]
rgb_fn_list = [str(x) for x in list(np.array(rgb_fn_list)[on_idxes])]
depth_fn_list = [str(x) for x in list(np.array(depth_fn_list)[on_idxes])]
gt_fn_list = [str(x) for x in list(np.array(gt_fn_list)[on_idxes])]
import pandas as pd
df = pd.DataFrame({
"rgb": rgb_fn_list,
"depth": depth_fn_list,
"gt": gt_fn_list
})
outfn = osp.join("dataset", "Starnford2d3dSemantics", "small_test_set.csv")
df.to_csv(outfn, index=False)
print ("%s was saved" % outfn)
# Check the order
def get_id_from_filename(filename):
return os.path.split(filename)[-1].split("_")[1]
rand_id = np.random.randint(len(rgb_fn_list))
assert get_id_from_filename(rgb_fn_list[rand_id]) == get_id_from_filename(depth_fn_list[rand_id])
assert get_id_from_filename(rgb_fn_list[rand_id]) == get_id_from_filename(gt_fn_list[rand_id])
for rgb_fn, depth_fn, gt_fn in zip(rgb_fn_list, depth_fn_list, gt_fn_list):
self.files[split].append({
"rgb": rgb_fn,
"depth": depth_fn,
"label": gt_fn
})
print ("N_%s: %s" % (split, len(self.files[split])))
def __len__(self):
return len(self.files[self.split])
def depth_img_preprocess(self, np_depth):
"""
Replace exception value (65535) with maximum depth value
Then Scaile to 0-255
:param np_depth:
:return: processed_np_depth
"""
idxes = np.where(np_depth == 65535)
np_depth[idxes] = -1
max_dep = np_depth.max()
np_depth[idxes] = max_dep
np_depth = depth_scaling(np_depth)
return np_depth
def __getitem__(self, index):
datafiles = self.files[self.split][index]
if self.input_ch == 1 and "d" in self.split:
img_file = datafiles["depth"]
img = Image.open(img_file)
np_img = self.depth_img_preprocess(np.array(img))
img = Image.fromarray(np_img)
# self.img_transform[-1] = Normalize([.485, .456, .406], [.229, .224, .225])
# print ("img transform changed")
else:
img_file = datafiles["rgb"]
img = Image.open(img_file)
np3ch = np.array(img)
if self.input_ch == 1:
img = ImageOps.grayscale(img)
elif self.input_ch == 4:
if "rgbd" in self.split:
depth_file = datafiles["depth"]
np_depth = np.array(Image.open(depth_file))
np_depth = self.depth_img_preprocess(np_depth)
extended_np3ch = np.concatenate([np3ch, np_depth[:, :, np.newaxis]], axis=2)
# print ("4ch is Depth channel")
else:
extended_np3ch = np.concatenate([np3ch, np3ch[:, :, 0:1]], axis=2)
# print ("4ch is R channel")
img = Image.fromarray(np.uint8(extended_np3ch))
# img = Image.fromarray(np.uint32(extended_np3ch))
# img = extended_np3ch
# img = np.array(img)
label_file = datafiles["label"]
label = Image.open(label_file).convert("P")
if self.img_transform:
img = self.img_transform(img)
if self.label_transform:
label = self.label_transform(label)
if self.test:
return img, label, img_file
return img, label
# TODO support joint_transform
class SUNRGBDDataSet(data.Dataset):
"""
{'Bed': 1,
'Books': 2,
'Ceiling': 3,
'Chair': 4,
'Floor': 5,
'Furniture': 6,
'Objects': 7,
'Picture': 8,
'Sofa': 9,
'Table': 10,
'TV': 11,
'Wall': 12,
'Window': 13}
"""
## split info: http://buildingparser.stanford.edu/dataset.html#sample
IMG_SIZE = [730, 530]
SEED = 42
def check_split_and_input_ch(self, split, input_ch):
modal = split.split("_")[-1]
if modal == "rgbd":
assert input_ch == 4
print ("4ch is Depth channel")
elif modal == "d":
assert input_ch == 1
if modal == "rgb" and input_ch == 4:
print (emphasize_str("4ch is R channel"))
def __init__(self, root, split="test_rgbd", img_transform=None, label_transform=None,
test=False, input_ch=3, joint_transform=None):
assert split in ["test_d", "test_rgb", "test_rgbd"]
assert input_ch in [1, 3, 4]
self.check_split_and_input_ch(split, input_ch)
self.input_ch = input_ch
self.root = root
self.split = split
self.files = collections.defaultdict(list)
self.img_transform = img_transform
self.label_transform = label_transform
self.joint_transform = joint_transform
self.test = test
rgb_dir = osp.join(root, "SUNRGBD-test_images")
depth_dir = osp.join(root, "sunrgbd_test_depth_new")
gt_dir = osp.join(root, "test13labels")
rgb_fn_list = sorted(glob.glob(osp.join(rgb_dir, "*")))
depth_fn_list = sorted(glob.glob(osp.join(depth_dir, "*")))
gt_fn_list = sorted(glob.glob(osp.join(gt_dir, "*")))
len_rgb_fn_list = len(rgb_fn_list)
assert len_rgb_fn_list == len(depth_fn_list)
assert len_rgb_fn_list == len(gt_fn_list)
# Check the order
def get_id_from_filename(filename):
return os.path.split(filename)[-1].split("-")[1].replace(".png", "").replace(".jpg", "")
rand_id = np.random.randint(len(rgb_fn_list))
assert get_id_from_filename(rgb_fn_list[rand_id]) == get_id_from_filename(depth_fn_list[rand_id])
assert get_id_from_filename(rgb_fn_list[rand_id]) == get_id_from_filename(gt_fn_list[rand_id])
for rgb_fn, depth_fn, gt_fn in zip(rgb_fn_list, depth_fn_list, gt_fn_list):
self.files[split].append({
"rgb": rgb_fn,
"depth": depth_fn,
"label": gt_fn
})
print ("N_%s: %s" % (split, len(self.files[split])))
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
datafiles = self.files[self.split][index]
if self.input_ch == 1 and "d" in self.split:
img_file = datafiles["depth"]
img = Image.open(img_file)
np_img = depth_scaling(np.array(img))
img = Image.fromarray(np_img)
# self.img_transform[-1] = Normalize([.485, .456, .406], [.229, .224, .225])
# print ("img transform changed")
else:
img_file = datafiles["rgb"]
img = Image.open(img_file)
np3ch = np.array(img)
if self.input_ch == 1:
img = ImageOps.grayscale(img)
elif self.input_ch == 4:
if "rgbd" in self.split:
depth_file = datafiles["depth"]
np_depth = np.array(Image.open(depth_file))
np_depth = depth_scaling(np_depth)
extended_np3ch = np.concatenate([np3ch, np_depth[:, :, np.newaxis]], axis=2)
# print ("4ch is Depth channel")
else:
extended_np3ch = np.concatenate([np3ch, np3ch[:, :, 0:1]], axis=2)
# print ("4ch is R channel")
img = Image.fromarray(np.uint8(extended_np3ch))
# img = Image.fromarray(np.uint32(extended_np3ch))
# img = extended_np3ch
label_file = datafiles["label"]
label = Image.open(label_file).convert("P")
if self.img_transform:
img = self.img_transform(img)
if self.label_transform:
label = self.label_transform(label)
if self.test:
return img, label, label_file
return img, label
# TODO support joint_transform
class SUNCGDataSet(data.Dataset):
"""
40 classes same as NYUDv2
"""
## split info: http://buildingparser.stanford.edu/dataset.html#sample
IMG_SIZE = [640, 480]
SEED = 42
def check_split_and_input_ch(self, split, input_ch):
modal = split.split("_")[-1]
if modal == "rgbd":
assert input_ch == 4
print ("4ch is Depth channel")
elif modal == "d":
assert input_ch == 1
elif modal == "rgbhha":
assert input_ch == 6
if modal == "rgb" and input_ch == 4:
print (emphasize_str("4ch is R channel"))
def __init__(self, root, split="train_rgbhha", img_transform=None, label_transform=None,
test=False, input_ch=3, extra_img_transform=None, joint_transform=None):
assert split in ["train_rgb", "train_hha", "train_rgbhha", "train_rgbhhab"]
assert input_ch in [1, 3, 4, 6, 7]
self.check_split_and_input_ch(split, input_ch)
self.input_ch = input_ch
self.root = root
self.split = split
self.files = collections.defaultdict(list)
self.img_transform = img_transform
self.extra_img_transform = extra_img_transform
self.label_transform = label_transform
self.joint_transform = joint_transform
self.test = test
rgb_dir = osp.join(root, "mlt_v2") # or opengl_ver2
depth_dir = osp.join(root, "depth_v2")
hha_dir = osp.join(root, "hha_v2")
gt_dir = osp.join(root, "category_v2")
boundary_dir = osp.join(root, "boundary_v2")
fn_id_path = osp.join(root, "data_goodlist_v2.txt")
with open(fn_id_path) as f:
fn_id_list = [x.strip() for x in f.readlines()]
print ("SUNCG Dataset Loading filenames...")
for fn_id in tqdm(fn_id_list):
self.files[split].append({
"rgb": osp.join(rgb_dir, fn_id + "_mlt.png"),
"depth": osp.join(depth_dir, fn_id + "_depth.png"),
"hha": osp.join(hha_dir, fn_id + "_hha.png"),
"label": osp.join(gt_dir, fn_id + "_category40.png"),
"boundary": osp.join(boundary_dir, fn_id + "_instance_boundary.png")
})
print ("N_%s: %s" % (split, len(self.files[split])))
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
datafiles = self.files[self.split][index]
if self.input_ch == 1:
if "d" in self.split:
img = Image.open(datafiles["depth"])
np_img = depth_scaling(np.array(img))
img = Image.fromarray(np_img)
if self.img_transform:
img = self.img_transform(img)
else:
raise NotImplementedError()
elif self.input_ch == 3:
if "hha" in self.split:
hha = Image.open(datafiles["hha"])
if self.extra_img_transform:
img = self.extra_img_transform(hha)
else:
img = self.img_transform(hha)
# RGB
else:
img = Image.open(datafiles["rgb"])
if self.img_transform:
img = self.img_transform(img)
elif self.input_ch == 4:
img = Image.open(datafiles["rgb"])
np3ch = np.array(img)
if "rgbd" in self.split:
depth_file = datafiles["depth"]
np_depth = np.array(Image.open(depth_file))
np_depth = depth_scaling(np_depth)
extended_np3ch = np.concatenate([np3ch, np_depth[:, :, np.newaxis]], axis=2)
# print ("4ch is Depth channel")
# RGBR
else:
extended_np3ch = np.concatenate([np3ch, np3ch[:, :, 0:1]], axis=2)
# print ("4ch is R channel")
img = Image.fromarray(np.uint8(extended_np3ch))
if self.img_transform:
img = self.img_transform(img)
# RGBHHA
elif self.input_ch == 6:
rgb = Image.open(datafiles["rgb"])
rgb = self.img_transform(rgb)
hha = Image.open(datafiles["hha"])
if self.extra_img_transform:
hha = self.extra_img_transform(hha)
else:
hha = self.img_transform(hha)
img = torch.cat([rgb, hha])
# RGBHHAB
elif self.input_ch == 7:
rgb = Image.open(datafiles["rgb"])
rgb = self.img_transform(rgb)
hha = Image.open(datafiles["hha"])
if self.extra_img_transform:
hha = self.extra_img_transform(hha)
else:
hha = self.img_transform(hha)
convert_to_torch_tensor = Compose(
self.label_transform.transforms[:-1] + [ReLabel(255, 1)]) # Scale, ToTensor (Without Normalize)
boundary = convert_to_torch_tensor(Image.open(datafiles["boundary"])).unsqueeze(0)
# boundary = self.label_transform(Image.open(datafiles["boundary"]).convert("P")).unsqueeze(0)
img = torch.cat([rgb, hha, boundary.float()])
else:
raise NotImplementedError()
label_file = datafiles["label"]
label = Image.open(label_file).convert("P")
if self.label_transform:
label = self.label_transform(label)
if self.test:
return img, label, label_file
return img, label
# TODO support joint_transform
class NYUDv2(data.Dataset):
"""
40 classes
"""
## split info: http://buildingparser.stanford.edu/dataset.html#sample
IMG_SIZE = [560, 425]
SEED = 42
def check_split_and_input_ch(self, split, input_ch):
modal = split.split("_")[-1]
if modal == "rgbd":
assert input_ch == 4
print ("4ch is Depth channel")
elif modal == "d":
assert input_ch == 1
elif modal == "rgbhha":
assert input_ch == 6
if modal == "rgb" and input_ch == 4:
print (emphasize_str("4ch is R channel"))
def __init__(self, root, split="all_rgbhha", img_transform=None, label_transform=None,
test=False, input_ch=3, extra_img_transform=None, joint_transform=None):
assert split in ["all_rgb", "all_hha", "all_rgbhha", "trainval_rgb", "trainval_hha", "trainval_rgbhha",
"test_rgb", "test_hha", "test_rgbhha"]
raw_split = split.split("_")[0]
assert input_ch in [1, 3, 6]
self.check_split_and_input_ch(split, input_ch)
self.input_ch = input_ch
self.root = root
self.split = split
self.files = collections.defaultdict(list)
self.img_transform = img_transform
self.extra_img_transform = extra_img_transform
self.label_transform = label_transform
self.joint_transform = joint_transform
self.test = test
rgb_dir = osp.join(root, "rgb")
hha_dir = osp.join(root, "hha")
gt_dir = osp.join(root, "gt", "semantic40")
if raw_split == "all":
rgb_fn_list = sorted(glob.glob(osp.join(rgb_dir, "*")))
gt_fn_list = sorted(glob.glob(osp.join(gt_dir, "*")))
hha_fn_list = sorted(glob.glob(osp.join(hha_dir, "*")))
else:
with open("/data/unagi0/dataset/NYUDv2/gupta/%s.txt" % raw_split) as f:
id_list = f.readlines()
id_list = [x.strip() for x in id_list]
rgb_fn_list = [osp.join(rgb_dir, "img_%s.png" % x.strip()) for x in id_list]
gt_fn_list = [osp.join(gt_dir, "img_%s.png" % x.strip()) for x in id_list]
hha_fn_list = [osp.join(hha_dir, "img_%s.png" % x.strip()) for x in id_list]
len_rgb_fn_list = len(rgb_fn_list)
assert len_rgb_fn_list == len(gt_fn_list)
assert len_rgb_fn_list == len(hha_fn_list)
rand_id = np.random.randint(len(rgb_fn_list))
print (rgb_fn_list[rand_id], gt_fn_list[rand_id], hha_fn_list[rand_id])
for rgb_fn, hha_fn, gt_fn in zip(rgb_fn_list, hha_fn_list, gt_fn_list):
self.files[split].append({
"rgb": rgb_fn,
"hha": hha_fn,
"label": gt_fn
})
print ("N_%s: %s" % (split, len(self.files[split])))
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
datafiles = self.files[self.split][index]
if "hha" not in self.split:
if self.input_ch == 1 and "d" in self.split:
img_file = datafiles["depth"]
img = Image.open(img_file)
np_img = depth_scaling(np.array(img))
img = Image.fromarray(np_img)
else:
img_file = datafiles["rgb"]
img = Image.open(img_file)
np3ch = np.array(img)
if self.input_ch == 1:
img = ImageOps.grayscale(img)
elif self.input_ch == 4:
if "rgbd" in self.split:
depth_file = datafiles["depth"]
np_depth = np.array(Image.open(depth_file))
np_depth = depth_scaling(np_depth)
extended_np3ch = np.concatenate([np3ch, np_depth[:, :, np.newaxis]], axis=2)
# print ("4ch is Depth channel")
else:
extended_np3ch = np.concatenate([np3ch, np3ch[:, :, 0:1]], axis=2)
# print ("4ch is R channel")
img = Image.fromarray(np.uint8(extended_np3ch))
if self.img_transform:
img = self.img_transform(img)
else:
hha = Image.open(datafiles["hha"])
if self.extra_img_transform:
hha = self.extra_img_transform(hha)
else:
hha = self.img_transform(hha)
if self.input_ch == 3:
img = hha
elif self.input_ch == 6:
rgb = Image.open(datafiles["rgb"])
rgb = self.img_transform(rgb)
img = torch.cat([rgb, hha])
else:
raise NotImplementedError()
label_file = datafiles["label"]
label = Image.open(label_file).convert("P")
if self.label_transform:
label = self.label_transform(label)
if self.test:
return img, label, label_file
return img, label
class TestDataSet(data.Dataset):
IMG_SIZE = [1280, 720]
def __init__(self, root, split="train", img_transform=None, label_transform=None, test=True, input_ch=3,
joint_transform=None):
assert input_ch == 3
self.root = root
self.split = split
# self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
self.files = collections.defaultdict(list)
self.img_transform = img_transform
self.label_transform = label_transform
self.joint_transform = joint_transform
self.h_flip = HorizontalFlip()
self.v_flip = VerticalFlip()
self.test = test
data_dir = root
# for split in ["train", "trainval", "val"]:
imgsets_dir = os.listdir(data_dir)
for name in imgsets_dir:
img_file = osp.join(data_dir, "%s" % name)
self.files[split].append({
"img": img_file,
})
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
datafiles = self.files[self.split][index]
img_file = datafiles["img"]
img = Image.open(img_file).convert('RGB')
if self.joint_transform:
img, img = self.joint_transform(img, img)
if self.img_transform:
img = self.img_transform(img)
if self.test:
return img, 'hoge', img_file
else:
# TODO fix the second img. img, None causes bug, but this statement is strange.
return img, img
class IRDataSet(data.Dataset):
IMG_SIZE = [640, 480]
def __init__(self, root, split="train", img_transform=None, label_transform=None,
test=False, input_ch=4, joint_transform=None):
assert input_ch in [1, 3, 4]
self.input_ch = input_ch
self.root = root
self.split = split
# self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
self.files = collections.defaultdict(list)
self.img_transform = img_transform
self.label_transform = label_transform
self.joint_transform = joint_transform
self.test = test
img_dir = osp.join(root, "images")
lbl_dir = osp.join(root, "labels_converted2GTA")
# for split in ["train", "trainval", "val"]:
img_set_fn = osp.join(root, "%s.txt" % split)
with open(img_set_fn) as img_set_list:
for name in img_set_list:
name = name.strip()
img_file = osp.join(img_dir, "%s.png" % name)
label_file = osp.join(lbl_dir, "%s.png" % name)
self.files[split].append({
"img": img_file,
"label": label_file
})
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
datafiles = self.files[self.split][index]
img_file = datafiles["img"]
img = Image.open(img_file) # RGB+FIR(4ch)
np4ch = np.array(img)
if self.input_ch == 3:
img = Image.fromarray(np.uint8(np4ch[:, :, :3])) # RGB
elif self.input_ch == 1:
img = Image.fromarray(np.uint8(np4ch[:, :, -1])) # FIR
label_file = datafiles["label"]
label = Image.open(label_file).convert("P")
if self.joint_transform:
img, label = self.joint_transform(img, label)
if self.img_transform:
img = self.img_transform(img)
if self.label_transform:
label = self.label_transform(label)
if self.test:
return img, label, img_file
else:
return img, label
def get_dataset(dataset_name, split, img_transform, label_transform, test, input_ch=3, joint_transform=None):
assert dataset_name in AVAILABLE_DATASET_LIST
name2obj = {
"gta": GTADataSet,
"city": CityDataSet,
"city16": CityDataSet,
"test": TestDataSet,
"ir": IRDataSet,
"synthia": SynthiaDataSet,
"2d3d": Stanford2D3DSemanticsDataSet,
"sun": SUNRGBDDataSet,
"suncg": SUNCGDataSet,
"nyu": NYUDv2,
}
name2root = {
"gta": "/data/ugui0/dataset/adaptation/taskcv-2017-public/segmentation/data/",
"city": "/data/ugui0/ksaito/D_A/image_citiscape/www.cityscapes-dataset.com/file-handling/",
"city16": "/data/ugui0/ksaito/D_A/image_citiscape/www.cityscapes-dataset.com/file-handling/",
"test": "/data/ugui0/dataset/adaptation/segmentation_test",
"ir": "/data/unagi0/inf_data/ir_seg_dataset",
"synthia": "/data/ugui0/dataset/adaptation/synthia/RAND_CITYSCAPES",
"2d3d": "/data/unagi0/dataset/2D-3D-SemanticsData/",
"sun": "/data/unagi0/dataset/SUNRGBD/",
"suncg": "/data/unagi0/dataset/SUNCG-Seg/",
"nyu": "/data/unagi0/dataset/NYUDv2/gupta/"
}
dataset_obj = name2obj[dataset_name]
root = name2root[dataset_name]
if dataset_name == "city16":
return dataset_obj(root=root, split=split, img_transform=img_transform, label_transform=label_transform,
test=test, input_ch=input_ch, label_type="label16", joint_transform=joint_transform)
return dataset_obj(root=root, split=split, img_transform=img_transform, label_transform=label_transform,