-
Notifications
You must be signed in to change notification settings - Fork 10
/
dataset_loader.py
executable file
·318 lines (221 loc) · 13.1 KB
/
dataset_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
import numpy as np
import os
import random
from PIL import Image
import torch
from torch.utils.data import Dataset
import glob
class Relabel:
def __init__(self, olabel, nlabel):
self.olabel = olabel
self.nlabel = nlabel
def __call__(self, tensor):
assert (isinstance(tensor, torch.LongTensor) or isinstance(tensor, torch.ByteTensor)) , 'tensor needs to be LongTensor'
tensor[tensor == self.olabel] = self.nlabel
return tensor
class SegmentationDataset(Dataset):
def __init__(self, root, subset,
img_path, label_path, pattern, img_suffix, label_suffix, file_path=False, transform=None, num_images=None):
# print(img_path)
self.images_root = f'{root}/{img_path}/{subset}'
self.labels_root = f'{root}/{label_path}/{subset}'
self.image_paths = glob.glob(f'{self.images_root}/{pattern}')
self.label_paths = [ img.replace(self.images_root, self.labels_root).replace(img_suffix, label_suffix) for img in self.image_paths ]
if "idd" in root:
self.image_paths = self.image_paths[:4000]
self.label_paths = self.label_paths[:4000]
if num_images is not None:
self.image_paths = self.image_paths[:num_images]
self.label_paths = self.label_paths[:num_images]
self.file_path = file_path
self.transform = transform
self.relabel = Relabel(255, self.num_classes) if transform != None else None
def __getitem__(self, index):
filename = self.image_paths[index]
filenameGt = self.label_paths[index]
with Image.open(filename) as f:
image = f.convert('RGB')
if self.mode == 'labeled':
with Image.open(filenameGt) as f:
label = f.convert('P')
else:
label = image
# print(image.size, label.size)
if self.transform !=None:
image, label = self.transform(image, label)
if self.d_idx == 'NYUv2_s': ## Wrap around the void class
label = label-1
label[label<0] = 255
if self.relabel != None and self.mode == 'labeled':
label = self.relabel(label)
if self.mode == 'unlabeled':
return image
else:
return image, label
def __len__(self):
return len(self.image_paths)
class CityscapesDataset(SegmentationDataset):
num_classes = 19
label_names = ['road', 'sidewalk', 'building', 'wall', 'fence', 'pole', 'traffic light', 'traffic sign', 'vegetation', 'terrain', 'sky', 'person', 'rider', 'car', 'truck', 'bus', 'train', 'motorcycle', 'bicycle']
color_map = np.array([
[128, 64,128],
[244, 35,232],
[ 70, 70, 70],
[102,102,156],
[190,153,153],
[153,153,153],
[250,170, 30],
[220,220, 0],
[107,142, 35],
[152,251,152],
[ 70,130,180],
[220, 20, 60],
[255, 0, 0],
[ 0, 0,142],
[ 0, 0, 70],
[ 0, 60,100],
[ 0, 80,100],
[ 0, 0,230],
[119, 11, 32]
], dtype=np.uint8)
def __init__(self, root, subset='train', transform=None, file_path=False, num_images=None , mode='labeled'):
self.d_idx = 'CS'
self.mode = mode
super(CityscapesDataset, self).__init__(root, subset,
img_path = 'leftImg8bit', label_path='gtFine', pattern='*/*',
img_suffix = '_leftImg8bit.png' , label_suffix='_gtFine_labelTrainIds.png', transform=transform, file_path=file_path, num_images=num_images)
class ANL4Transform(object):
def __call__(self, image, label):
indices = label >= 30
label[indices] = 255
return image, label
class ANUEDatasetL4(SegmentationDataset):
num_classes = 30
label_names = ['road', 'parking', 'drivable fallback', 'sidewalk', 'non-drivable fallback', 'person', 'animal', 'rider', 'motorcycle', 'bicycle', 'autorickshaw', 'car', 'truck', 'bus', 'caravan', 'vehicle fallback', 'curb', 'wall', 'fence', 'guard rail', 'billboard', 'traffic sign', 'traffic light', 'pole', 'obs-str-bar-fallback', 'building', 'bridge', 'vegetation', 'sky', 'fallback background']
color_map = np.array([[128, 64, 128], [250, 170, 160], [81, 0, 81], [244, 35, 232], [152, 251, 152], [220, 20, 60], [246, 198, 145], [255, 0, 0], [0, 0, 230], [119, 11, 32], [255, 204, 54], [0, 0, 142], [0, 0, 70], [0, 60, 100], [0, 0, 90], [136, 143, 153], [220, 190, 40], [102, 102, 156], [190, 153, 153], [180, 165, 180], [174, 64, 67], [220, 220, 0], [250, 170, 30], [153, 153, 153], [0, 0, 0], [70, 70, 70], [150, 100, 100], [107, 142, 35], [70, 130, 180], [169, 187, 214]], dtype=np.uint8)
def __init__(self, root, subset='train', transform=None, file_path=False, num_images=None):
self.d_idx = 'ANUE'
super(ANUEDatasetL4, self).__init__(root, subset,
img_path = 'leftImg8bit', label_path='gtFine', pattern='*/*',
img_suffix = '_leftImg8bit.png' , label_suffix='_gtFine_labellevel4Ids.png', transform=transform, file_path=file_path, num_images=num_images)
class IDD_Dataset(SegmentationDataset):
num_classes = 26
label_names = ['road', 'drivable fallback', 'sidewalk', 'non-drivable fallback', 'animal', 'rider', 'motorcycle', 'bicycle', 'autorickshaw', 'car', 'truck', 'bus', 'vehicle fallback', 'curb', 'wall', 'fence', 'guard rail', 'billboard', 'traffic sign', 'traffic light', 'pole', 'obs-str-bar-fallback', 'building', 'bridge', 'vegetation', 'sky']
color_map = np.array([
[128, 64, 128], #road
[ 81, 0, 81], #drivable fallback
[244, 35, 232], #sidewalk
[152, 251, 152], #nondrivable fallback
[220, 20, 60], #pedestrian
[255, 0, 0], #rider
[0, 0, 230], #motorcycle
[119, 11, 32], #bicycle
[255, 204, 54], #autorickshaw
[0, 0, 142], #car
[0, 0, 70], #truck
[0, 60, 100], #bus
[136, 143, 153], #vehicle fallback
[220, 190, 40], #curb
[102, 102, 156], #wall
[190, 153, 153], #fence
[180, 165, 180], #guard rail
[174, 64, 67], #billboard
[220, 220, 0], #traffic sign
[250, 170, 30], #traffic light
[153, 153, 153], #pole
[169, 187, 214], #obs-str-bar-fallback
[70, 70, 70], #building
[150, 120, 90], #bridge
[107, 142, 35], #vegetation
[70, 130, 180] #sky
], dtype=np.uint8)
def __init__(self, root, subset='train', transform=None, file_path=False, num_images=None, mode='labeled'):
self.d_idx = 'IDD'
self.mode = mode
super().__init__(root, subset,
img_path = 'leftImg8bit', label_path='gtFine', pattern='*/*',
img_suffix = '_leftImg8bit.png' , label_suffix='_gtFine_labellevel3Ids.png', transform=transform, file_path=file_path, num_images=num_images)
class CamVid(SegmentationDataset):
num_classes = 11
# label_names = ["Animal", "Archway", "Bicyclist", "Bridge", "Building", "Car", "CartLuggagePram", "Child", "Column_Pole", "Fence", "LaneMkgsDriv", "LaneMkgsNonDriv", "Misc_Text", "MotorcycleScooter", "OtherMoving", "ParkingBlock", "Pedestrian", "Road", "RoadShoulder", "Sidewalk", "SignSymbol", "Sky", "SUVPickupTruck", "TrafficCone", "TrafficLight", "Train", "Tree", "Truck_Bus", "Tunnel", "VegetationMisc", "Void", "Wall"]
# color_map = np.array([64,128,64], [192,0,128], [0,128,192], [0,128,64], [128,0,0], [64,0,128], [64,0,192], [192,128,64], [192,192,128], [64,64,128], [128,0,192], [192,0,64], [128,128,64], [192,0,192], [128,64,64], [64,192,128], [64,64,0], [128,64,128], [128,128,192], [0,0,192], [192,128,128], [128,128,128], [64,128,192], [0,0,64], [0,64,64], [192,64,128], [128,128,0], [192,128,192], [64,0,64], [192,192,0], [0,0,0], [64,192,0])
def __init__(self, root, subset='train', transform=None, file_path=False, num_images=None, mode="labeled"):
self.d_idx = 'CVD'
self.mode = mode
self.images_root = f"{root}/{subset}/"
self.labels_root = f"{root}/{subset}annot/"
self.image_paths = glob.glob(f'{self.images_root}/*.png')
self.label_paths = glob.glob(f'{self.labels_root}/*.png')
if num_images is not None:
self.image_paths = self.image_paths[:num_images]
self.label_paths = self.label_paths[:num_images]
self.file_path = file_path
self.transform = transform
self.relabel = Relabel(255, self.num_classes) if transform != None else None
class SunRGB(SegmentationDataset):
num_classes = 37
# label_names = ["Animal", "Archway", "Bicyclist", "Bridge", "Building", "Car", "CartLuggagePram", "Child", "Column_Pole", "Fence", "LaneMkgsDriv", "LaneMkgsNonDriv", "Misc_Text", "MotorcycleScooter", "OtherMoving", "ParkingBlock", "Pedestrian", "Road", "RoadShoulder", "Sidewalk", "SignSymbol", "Sky", "SUVPickupTruck", "TrafficCone", "TrafficLight", "Train", "Tree", "Truck_Bus", "Tunnel", "VegetationMisc", "Void", "Wall"]
# color_map = np.array([64,128,64], [192,0,128], [0,128,192], [0,128,64], [128,0,0], [64,0,128], [64,0,192], [192,128,64], [192,192,128], [64,64,128], [128,0,192], [192,0,64], [128,128,64], [192,0,192], [128,64,64], [64,192,128], [64,64,0], [128,64,128], [128,128,192], [0,0,192], [192,128,128], [128,128,128], [64,128,192], [0,0,64], [0,64,64], [192,64,128], [128,128,0], [192,128,192], [64,0,64], [192,192,0], [0,0,0], [64,192,0])
def __init__(self, root, subset='train', transform=None, file_path=False, num_images=None, mode="labeled"):
self.d_idx = 'SUN'
self.mode = mode
listname = f"{root}/{subset}37.txt"
with open(listname , 'r') as fh:
self.image_paths = [os.path.join(root , l.split()[0]) for l in fh]
with open(listname , 'r') as fh:
self.label_paths = [os.path.join(root , l.split()[-1]) for l in fh]
if num_images is not None:
self.image_paths = self.image_paths[:num_images]
self.label_paths = self.label_paths[:num_images]
self.file_path = file_path
self.transform = transform
self.relabel = Relabel(255, self.num_classes) if transform != None else None
class NYUv2_seg(SegmentationDataset):
num_classes = 13
# label_names = ["Animal", "Archway", "Bicyclist", "Bridge", "Building", "Car", "CartLuggagePram", "Child", "Column_Pole", "Fence", "LaneMkgsDriv", "LaneMkgsNonDriv", "Misc_Text", "MotorcycleScooter", "OtherMoving", "ParkingBlock", "Pedestrian", "Road", "RoadShoulder", "Sidewalk", "SignSymbol", "Sky", "SUVPickupTruck", "TrafficCone", "TrafficLight", "Train", "Tree", "Truck_Bus", "Tunnel", "VegetationMisc", "Void", "Wall"]
# color_map = np.array([64,128,64], [192,0,128], [0,128,192], [0,128,64], [128,0,0], [64,0,128], [64,0,192], [192,128,64], [192,192,128], [64,64,128], [128,0,192], [192,0,64], [128,128,64], [192,0,192], [128,64,64], [64,192,128], [64,64,0], [128,64,128], [128,128,192], [0,0,192], [192,128,128], [128,128,128], [64,128,192], [0,0,64], [0,64,64], [192,64,128], [128,128,0], [192,128,192], [64,0,64], [192,192,0], [0,0,0], [64,192,0])
def __init__(self, root, subset='train', transform=None, file_path=False, num_images=None, mode="labeled"):
self.d_idx = 'NYU_s'
self.mode = mode
# listname = f"{root}/{subset}13.txt"
images = os.listdir(os.path.join(root , subset , 'images'))
labels = os.listdir(os.path.join(root , subset , 'labels'))
self.image_paths = [f"{root}/{subset}/images/"+im_id for im_id in images]
self.label_paths = [f"{root}/{subset}/labels/"+lb_id for lb_id in labels]
# with open(listname , 'r') as fh:
# self.image_paths = [os.path.join(root , l.split()[0]) for l in fh]
# with open(listname , 'r') as fh:
# self.label_paths = [os.path.join(root , l.split()[-1]) for l in fh]
if num_images is not None:
self.image_paths = self.image_paths[:num_images]
self.label_paths = self.label_paths[:num_images]
self.file_path = file_path
self.transform = transform
self.relabel = Relabel(255, self.num_classes) if transform != None else None
def colorize(img, color, fallback_color=[0,0,0]):
img = np.array(img)
W,H = img.shape
view = np.tile(np.array(fallback_color, dtype = np.uint8), (W,H, 1) )
for i, c in enumerate(color):
indices = (img == i)
view[indices] = c
return view
if __name__ == "__main__":
import matplotlib.pyplot as plt
def show_data(ds):
print(len(ds))
i = random.randrange(len(ds))
img, gt = ds[i]
color_gt = colorize(gt, ds.color_map)
print(img.size,color_gt.shape)
plt.imshow(img)
plt.imshow(color_gt, alpha=0.25)
plt.show()
# cs = CityscapesDataset('/ssd_scratch/cvit/girish.varma/dataset/cityscapes')
# show_data(cs)
# an = ANUEDataset('/ssd_scratch/cvit/girish.varma/dataset/anue')
# show_data(an)
# bd = BDDataset('/ssd_scratch/cvit/girish.varma/dataset/bdd100k')
# show_data(bd)
# mv = MVDataset('/ssd_scratch/cvit/girish.varma/dataset/mvd')
# show_data(mv)