-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmap_files.py
393 lines (285 loc) · 14.4 KB
/
map_files.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
import re
from imagenet_dict import map_dict
import torch
import os
import numpy as np
import glob
def get_imagenet_visda_mapping(visda_dir, map_dict):
matching_names = dict()
matching_labels = dict()
map_dict_visda = dict()
count = 0
#if True:
label = 0
visda = os.listdir(visda_dir)
for item in sorted(visda):
map_dict_visda[item] = label
item_split = item.split("_")
for ii in item_split:
for j in map_dict:
if re.search(r'\b' + ii + r'\b', map_dict[j]):
try:
matching_names[item].append([map_dict[j]])
matching_labels[str(label)].append(j)
except:
matching_names[item] = list()
matching_names[item].append([map_dict[j]])
matching_labels[str(label)] = list()
matching_labels[str(label)].append(j)
label += 1
matching_names, matching_labels = clean_dataset(matching_names, matching_labels, map_dict_visda)
return matching_names, matching_labels
def create_symlinks_and_get_imagenet_visda_mapping(visda_location, map_dict):
# initial mapping and cleaning
matching_names, matching_labels = get_imagenet_visda_mapping(visda_location, map_dict)
# some classes are ambiguous
ambiguous_matching = get_ambiguous_classes(matching_names)
# create symlinks for all valid classes
if not os.path.exists('./visda_symlinks/'):
os.makedirs('./visda_symlinks/')
else:
print('Path ./visda_symlinks/ exists, skipping.')
target_folder = './visda_symlinks/' + visda_location.split('/')[-2] + '/'
if not os.path.exists(target_folder):
os.makedirs(target_folder)
else:
print('Path ', target_folder, ' exists, skipping.')
for folder in matching_names.keys():
target_folder_class = os.path.join(target_folder, ambiguous_matching[folder])
if not os.path.exists(target_folder_class):
os.makedirs(target_folder_class)
try:
allFiles_path_jpg = visda_location + folder + '/*.jpg'
allFiles_path_png = visda_location + folder + '/*.png'
allFiles_jpg = glob.glob(allFiles_path_jpg)
allFiles_png = glob.glob(allFiles_path_png)
allFiles = allFiles_jpg + allFiles_png
for file in allFiles:
newFile = target_folder_class + '/' + file.split('/')[-1]
os.symlink(file, newFile)
except FileExistsError:
pass
# final mapping and cleaning with the symlinks
matching_names, matching_labels = get_imagenet_visda_mapping(target_folder, map_dict)
mapping_vector = torch.zeros((1000))
if torch.cuda.is_available():
mapping_vector = torch.zeros((1000)).cuda()
mapping_vector -= 1
mapping_vector_counts = dict()
for i in range(1000):
if i not in mapping_vector_counts.keys():
mapping_vector_counts[i] = list()
for j in matching_labels:
if i in matching_labels[j]:
mapping_vector[i] = int(j)
mapping_vector_counts[i].append(j)
# if classes are mapped to more than one class, we want to know about it:
for i in mapping_vector_counts.keys():
if len(mapping_vector_counts[i]) > 1:
print(map_dict[i], i, 'is mapped to visda classes: ', mapping_vector_counts[i])
return mapping_vector, matching_names, matching_labels
def clean_dataset(matching_names, matching_labels, map_dict_visda):
# delete labels completely
del_list = ['mouse', 'fish', 'light_bulb', 'leaf', 'face', 'wine_glass', 'hockey_stick', 'star', 'see_saw', 'pencil', 'grass', 'fire_hydrant', 'brain', 'apple', 'river', 'rhinoceros', 'power_outlet', 'rain', 'pool', 'picture_frame', 'paper_clip', 'palm_tree', 'paint_can', 'mouth', 'The_Great_Wall_of_China',
'garden', 'garden_hose', 'hand', 'house_plant', 'jacket', 'tree', 'sun', 'smiley_face', 'beach', 'diving_board', 'mountain']
for item in del_list:
try:
del matching_names[item]
del matching_labels[str(map_dict_visda[item])]
except:
pass
# delete some imagenet labels
del matching_names['cat'][5:]
del matching_labels[str(map_dict_visda['cat'])][5:]
del matching_names['dog'][-1]
del matching_labels[str(map_dict_visda['dog'])][-1]
del matching_names['pig'][0]
del matching_labels[str(map_dict_visda['pig'])][0]
del matching_names['bear'][-1]
del matching_labels[str(map_dict_visda['bear'])][-1]
del matching_names['horse'][0]
del matching_labels[str(map_dict_visda['horse'])][0]
del matching_names['hot_air_balloon'][0:2]
del matching_labels[str(map_dict_visda['hot_air_balloon'])][0:2]
del matching_names['hot_dog'][2:15]
del matching_labels[str(map_dict_visda['hot_dog'])][2:15]
del matching_names['house'][0]
del matching_labels[str(map_dict_visda['house'])][0]
del matching_names['ice_cream'][0]
del matching_labels[str(map_dict_visda['ice_cream'])][0]
del matching_names['kangaroo'][1]
del matching_labels[str(map_dict_visda['kangaroo'])][1]
del matching_names['washing_machine'][1:-1]
del matching_labels[str(map_dict_visda['washing_machine'])][1:-1]
del matching_names['traffic_light'][1:-1]
del matching_labels[str(map_dict_visda['traffic_light'])][1:-1]
del matching_names['table'][-1]
del matching_labels[str(map_dict_visda['table'])][-1]
del matching_names['stop_sign'][0]
del matching_labels[str(map_dict_visda['stop_sign'])][0]
del matching_names['spider'][-2]
del matching_labels[str(map_dict_visda['spider'])][-2]
del matching_names['snake'][-2:]
del matching_labels[str(map_dict_visda['snake'])][-2:]
del matching_names['sleeping_bag'][1]
del matching_labels[str(map_dict_visda['sleeping_bag'])][1]
del matching_names['sleeping_bag'][1] # not a bug that this comes twice
del matching_labels[str(map_dict_visda['sleeping_bag'])][1]
del matching_names['sheep'][0]
del matching_labels[str(map_dict_visda['sheep'])][0]
del matching_names['sea_turtle'][:-4]
del matching_labels[str(map_dict_visda['sea_turtle'])][:-4]
del matching_names['squirrel'][1]
del matching_labels[str(map_dict_visda['squirrel'])][1]
del matching_names['lion'][0]
del matching_labels[str(map_dict_visda['lion'])][0]
del matching_names['bee'][0]
del matching_labels[str(map_dict_visda['bee'])][0]
del matching_names['bee'][-1]
del matching_labels[str(map_dict_visda['bee'])][-1]
del matching_names['soccer_ball'][1:]
del matching_labels[str(map_dict_visda['soccer_ball'])][1:]
del matching_names['tractor'][1]
del matching_labels[str(map_dict_visda['tractor'])][1]
del matching_names['oven'][-1]
del matching_labels[str(map_dict_visda['oven'])][-1]
del matching_names['piano'][0]
del matching_labels[str(map_dict_visda['piano'])][0]
del matching_names['barn'][0]
del matching_labels[str(map_dict_visda['barn'])][0]
del matching_names['tiger'][0:2]
del matching_labels[str(map_dict_visda['tiger'])][0:2]
del matching_names['tiger'][-1]
del matching_labels[str(map_dict_visda['tiger'])][-1]
del matching_names['monkey'][0]
del matching_labels[str(map_dict_visda['monkey'])][0]
del matching_names['bear'][-2:]
del matching_labels[str(map_dict_visda['bear'])][-2:]
del matching_names['car'][2]
del matching_labels[str(map_dict_visda['car'])][2]
del matching_names['car'][-1]
del matching_labels[str(map_dict_visda['car'])][-1]
# add items:
matching_names['airplane'] = [['warplane, military plane'], ['airliner'], ['airship, dirigible']]
matching_labels[str(map_dict_visda['airplane'])] = [895, 404, 405]
matching_names['t-shirt'] = ['jersey, T-shirt, tee shirt']
matching_labels[str(map_dict_visda['t-shirt'])] = [610]
matching_names['teddy-bear'] = ['teddy, teddy bear']
matching_labels[str(map_dict_visda['teddy-bear'])] = [850]
matching_names['bicycle'].append(['mountain bike, all-terrain bike, off-roader'])
matching_labels[str(map_dict_visda['bicycle'])].extend([671])
matching_names['bus'].append(['trolleybus, trolley coach, trackless trolley'])
matching_labels[str(map_dict_visda['bus'])].extend([874])
matching_names['bus'].append(['minibus'])
matching_labels[str(map_dict_visda['bus'])].extend([654])
matching_names['frog'].append(['bullfrog, Rana catesbeiana'])
matching_labels[str(map_dict_visda['frog'])].extend([30])
matching_names['rabbit'].append(['hare'])
matching_labels[str(map_dict_visda['rabbit'])].extend([331])
matching_names['sea_turtle'].append(['terrapin'])
matching_labels[str(map_dict_visda['sea_turtle'])].extend([36])
matching_names['whale'].append(['dugong, Dugong dugon'])
matching_labels[str(map_dict_visda['whale'])].extend([149])
matching_names['pig'].append(['wild boar, boar, Sus scrofa'])
matching_labels[str(map_dict_visda['pig'])].extend([342])
matching_names['pig'].append(['warthog'])
matching_labels[str(map_dict_visda['pig'])].extend([343])
matching_names['pig'].append(['piggy bank, penny bank'])
matching_labels[str(map_dict_visda['pig'])].extend([719])
matching_names['car'].append(['police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria'])
matching_labels[str(map_dict_visda['car'])].extend([734])
# add dogs to dog label:
matching_labels[str(map_dict_visda['dog'])].extend(list(np.arange(151, 269)))
for i in np.arange(151, 269):
if map_dict[i] not in matching_names['dog']:
matching_names['dog'].append([map_dict[i]])
# add more butterflies:
matching_labels[str(map_dict_visda['butterfly'])].extend(list(np.arange(320, 322)))
for i in np.arange(320, 322):
if map_dict[i] not in matching_names['butterfly']:
matching_names['butterfly'].append([map_dict[i]])
# add more mosquitos:
matching_labels[str(map_dict_visda['mosquito'])].extend(list(np.arange(318, 320)))
for i in np.arange(318, 320):
if map_dict[i] not in matching_names['mosquito']:
matching_names['mosquito'].append([map_dict[i]])
# add more monkeys:
matching_labels[str(map_dict_visda['monkey'])].extend(list(np.arange(365, 385)))
for i in np.arange(365, 385):
if map_dict[i] not in matching_names['monkey']:
matching_names['monkey'].append([map_dict[i]])
# add more snakes:
matching_labels[str(map_dict_visda['snake'])].extend(list(np.arange(52, 69)))
for i in np.arange(52, 69):
if map_dict[i] not in matching_names['snake']:
matching_names['snake'].append([map_dict[i]])
# add more spiders:
matching_labels[str(map_dict_visda['spider'])].extend(list(np.arange(72, 79)))
for i in np.arange(72, 79):
if map_dict[i] not in matching_names['spider']:
matching_names['spider'].append([map_dict[i]])
matching_names['spider'].append(['harvestman, daddy longlegs, Phalangium opilio'])
matching_labels[str(map_dict_visda['spider'])].extend([70])
# add more birds:
matching_labels[str(map_dict_visda['bird'])].extend(list(np.arange(80, 101)))
for i in np.arange(80, 101):
if map_dict[i] not in matching_names['bird']:
matching_names['bird'].append([map_dict[i]])
# add more birds:
matching_labels[str(map_dict_visda['bird'])].extend(list(np.arange(7, 24)))
for i in np.arange(7, 24):
if map_dict[i] not in matching_names['bird']:
matching_names['bird'].append([map_dict[i]])
# remove dublicates from labels:
for item in matching_labels:
tmp = set(matching_labels[item])
matching_labels[item] = list(tmp)
return matching_names, matching_labels
def map_imagenet_class_to_visda_class(pred_label, mapping_vector):
pred_label_visda_tensor = mapping_vector[pred_label].long()
return pred_label_visda_tensor
def map_visda_class_to_imagenet_class(pred_label, mapping_vector):
pred_label_visda_tensor = mapping_vector[pred_label].long()
return pred_label_visda_tensor
def get_ambiguous_classes(matching_names):
# these are the ambiguous classes
ambiguous_classes = [['alarm_clock', 'clock'], ['baseball', 'baseball_bat'], ['bed', 'couch'], ['car', 'police_car'],
['coffee_cup', 'cup', 'mug'], ['computer', 'keyboard', 'laptop'], ['ice_cream', 'lollipop', 'popsicle'],
['bus', 'school_bus'], ['truck', 'pickup_truck', 'firetruck', 'van'], ['bird', 'swan'], ['hot_tub', 'bathtub'],
['telephone', 'cell_phone'], ['ceiling_fan', 'fan']]
ambiguous_matching = {}
ambiguous_matching['telephone'] = 'telephone'
ambiguous_matching['cell_phone'] = 'telephone'
ambiguous_matching['fan'] = 'fan'
ambiguous_matching['ceiling_fan'] = 'fan'
ambiguous_matching['clock'] = 'clock'
ambiguous_matching['alarm_clock'] = 'clock'
ambiguous_matching['bathtub'] = 'bathtub'
ambiguous_matching['hot_tub'] = 'bathtub'
ambiguous_matching['baseball'] = 'baseball'
ambiguous_matching['baseball_bat'] = 'baseball'
ambiguous_matching['bed'] = 'bed'
ambiguous_matching['couch'] = 'bed'
ambiguous_matching['car'] = 'car'
ambiguous_matching['police_car'] = 'car'
ambiguous_matching['coffee_cup'] = 'cup'
ambiguous_matching['cup'] = 'cup'
ambiguous_matching['mug'] = 'cup'
ambiguous_matching['computer'] = 'computer'
ambiguous_matching['keyboard'] = 'computer'
ambiguous_matching['laptop'] = 'computer'
ambiguous_matching['ice_cream'] = 'ice_cream'
ambiguous_matching['lollipop'] = 'ice_cream'
ambiguous_matching['popsicle'] = 'ice_cream'
ambiguous_matching['bus'] = 'bus'
ambiguous_matching['school_bus'] = 'bus'
ambiguous_matching['truck'] = 'truck'
ambiguous_matching['pickup_truck'] = 'truck'
ambiguous_matching['van'] = 'truck'
ambiguous_matching['firetruck'] = 'truck'
ambiguous_matching['bird'] = 'bird'
ambiguous_matching['swan'] = 'bird'
for key in matching_names.keys():
if key not in ambiguous_matching:
ambiguous_matching[key] = key
return ambiguous_matching