-
Notifications
You must be signed in to change notification settings - Fork 5
/
dataset.py
256 lines (173 loc) · 8.81 KB
/
dataset.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
import numpy as np
import os
from PIL import Image
from torch.utils.data import Dataset
EXTENSIONS = ['.jpg', '.png']
def load_image(file):
return Image.open(file)
def is_image(filename):
return any(filename.endswith(ext) for ext in EXTENSIONS)
def is_label_city(filename):
return filename.endswith("_labelTrainIds.png")
def is_label_IDD(filename):
return filename.endswith("_labellevel3Ids.png")
def is_label_BDD(filename):
return filename.endswith("_train_id.png")
def image_path(root, basename, extension):
return os.path.join(root, f'{basename}{extension}')
def image_path_city(root, name):
return os.path.join(root, f'{name}')
def image_basename(filename):
return os.path.basename(os.path.splitext(filename)[0])
class VOC12(Dataset):
def __init__(self, root, input_transform=None, target_transform=None):
self.images_root = os.path.join(root, 'images')
self.labels_root = os.path.join(root, 'labels')
self.filenames = [image_basename(f)
for f in os.listdir(self.labels_root) if is_image(f)]
self.filenames.sort()
self.input_transform = input_transform
self.target_transform = target_transform
def __getitem__(self, index):
filename = self.filenames[index]
with open(image_path(self.images_root, filename, '.jpg'), 'rb') as f:
image = load_image(f).convert('RGB')
with open(image_path(self.labels_root, filename, '.png'), 'rb') as f:
label = load_image(f).convert('P')
if self.input_transform is not None:
image = self.input_transform(image)
if self.target_transform is not None:
label = self.target_transform(label)
return image, label
def __len__(self):
return len(self.filenames)
class cityscapes(Dataset):
def __init__(self, root, co_transform=None, subset='train'):
self.images_root = os.path.join(root, 'leftImg8bit/')
self.labels_root = os.path.join(root, 'gtFine/')
self.images_root += subset
self.labels_root += subset
print(self.images_root)
#self.filenames = [image_basename(f) for f in os.listdir(self.images_root) if is_image(f)]
self.filenames = [os.path.join(dp, f) for dp, dn, fn in os.walk(
os.path.expanduser(self.images_root)) for f in fn if is_image(f)]
self.filenames.sort()
#[os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(".")) for f in fn]
#self.filenamesGt = [image_basename(f) for f in os.listdir(self.labels_root) if is_image(f)]
self.filenamesGt = [os.path.join(dp, f) for dp, dn, fn in os.walk(
os.path.expanduser(self.labels_root)) for f in fn if is_label_city(f)]
self.filenamesGt.sort()
self.co_transform = co_transform # ADDED THIS
def __getitem__(self, index):
filename = self.filenames[index]
filenameGt = self.filenamesGt[index]
with open(image_path_city(self.images_root, filename), 'rb') as f:
image = load_image(f).convert('RGB')
with open(image_path_city(self.labels_root, filenameGt), 'rb') as f:
label = load_image(f).convert('P')
if self.co_transform is not None:
image, label = self.co_transform(image, label)
return image, label
def __len__(self):
return len(self.filenames)
# added
class IDD(Dataset):
def __init__(self, root, co_transform=None, subset='train'):
self.images_root = os.path.join(root, 'leftImg8bit/')
self.labels_root = os.path.join(root, 'gtFine/')
self.images_root += subset
self.labels_root += subset
print(self.images_root)
#self.filenames = [image_basename(f) for f in os.listdir(self.images_root) if is_image(f)]
self.filenames = [os.path.join(dp, f) for dp, dn, fn in os.walk(
os.path.expanduser(self.images_root)) for f in fn if is_image(f)]
self.filenames.sort()
#[os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(".")) for f in fn]
#self.filenamesGt = [image_basename(f) for f in os.listdir(self.labels_root) if is_image(f)]
self.filenamesGt = [os.path.join(dp, f) for dp, dn, fn in os.walk(
os.path.expanduser(self.labels_root)) for f in fn if is_label_IDD(f)]
self.filenamesGt.sort()
self.co_transform = co_transform # ADDED THIS
def __getitem__(self, index):
filename = self.filenames[index]
filenameGt = self.filenamesGt[index]
# image_path_city will work for IDD also as the images already have a .png extension
with open(image_path_city(self.images_root, filename), 'rb') as f:
image = load_image(f).convert('RGB')
with open(image_path_city(self.labels_root, filenameGt), 'rb') as f:
label = load_image(f).convert('P')
if self.co_transform is not None:
image, label = self.co_transform(image, label)
return image, label
def __len__(self):
return len(self.filenames)
# added
class IDD_union(Dataset):
def __init__(self, root, co_transform=None, subset='train'):
self.images_root = os.path.join(root, 'leftImg8bit/')
self.labels_root = os.path.join(root, 'gtFine/')
self.images_root += subset
self.labels_root += subset
print(self.images_root)
MAP_dict = {0: 0, 1: 19, 2: 1, 3: 20, 4: 11, 5: 12, 6: 17, 7: 18, 8: 21, 9: 13, 10: 14, 11: 15, 12: 22, 13: 23, 14: 3,
15: 4, 16: 24, 17: 25, 18: 7, 19: 6, 20: 5, 21: 26, 22: 2, 23: 27, 24: 8, 25: 10, 255: 255}
self.k = np.array(list(MAP_dict.keys()))
self.v = np.array(list(MAP_dict.values()))
print('mapping keys:', self.k)
print('mapped values: ', self.v)
#self.filenames = [image_basename(f) for f in os.listdir(self.images_root) if is_image(f)]
self.filenames = [os.path.join(dp, f) for dp, dn, fn in os.walk(
os.path.expanduser(self.images_root)) for f in fn if is_image(f)]
self.filenames.sort()
#[os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(".")) for f in fn]
#self.filenamesGt = [image_basename(f) for f in os.listdir(self.labels_root) if is_image(f)]
self.filenamesGt = [os.path.join(dp, f) for dp, dn, fn in os.walk(
os.path.expanduser(self.labels_root)) for f in fn if is_label_IDD(f)]
self.filenamesGt.sort()
self.co_transform = co_transform # ADDED THIS
def __getitem__(self, index):
filename = self.filenames[index]
filenameGt = self.filenamesGt[index]
# image_path_city will work for IDD also as the images already have a .png extension
with open(image_path_city(self.images_root, filename), 'rb') as f:
image = load_image(f).convert('RGB')
with open(image_path_city(self.labels_root, filenameGt), 'rb') as f:
label = load_image(f).convert('P')
label = np.array(label)
mapping_ar = np.zeros(self.k.max()+1, dtype=self.v.dtype)
mapping_ar[self.k] = self.v # dict bananyi, k mei dalo, v mei chala jaega
label = mapping_ar[label] # map label to new values of IDD label
label = Image.fromarray(np.uint8(label))
if self.co_transform is not None:
image, label = self.co_transform(image, label)
return image, label
def __len__(self):
return len(self.filenames)
# added
class BDD100k(Dataset):
def __init__(self, root, co_transform=None, subset='train'):
self.images_root = os.path.join(root, 'images/')
self.labels_root = os.path.join(root, 'labels/')
self.images_root += subset
self.labels_root += subset
print(self.images_root)
#self.filenames = [image_basename(f) for f in os.listdir(self.images_root) if is_image(f)]
self.filenames = [f for f in os.listdir(self.images_root) if is_image(f)]
self.filenames.sort()
#[os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(".")) for f in fn]
#self.filenamesGt = [image_basename(f) for f in os.listdir(self.labels_root) if is_image(f)]
self.filenamesGt = [fn for fn in os.listdir(self.labels_root) if is_label_BDD(fn)]
self.filenamesGt.sort()
self.co_transform = co_transform # ADDED THIS
def __getitem__(self, index):
filename = self.filenames[index]
filenameGt = self.filenamesGt[index]
with open(image_path_city(self.images_root, filename), 'rb') as f:
image = load_image(f).convert('RGB')
with open(image_path_city(self.labels_root, filenameGt), 'rb') as f:
label = load_image(f).convert('P')
if self.co_transform is not None:
image, label = self.co_transform(image, label)
return image, label
def __len__(self):
return len(self.filenames)