wip
Browse files
scripts/semantic_mapper/semantic_mapper.ipynb
CHANGED
The diff for this file is too large to render.
See raw diff
|
|
scripts/semantic_mapper/semantic_mapper.py
CHANGED
@@ -213,21 +213,21 @@ class BinaryMapper(TaskMapper, NpIORepresentation):
|
|
213 |
TaskMapper is the only high level interface that makes sense, so we should focus on keeping that generic and easy.
|
214 |
"""
|
215 |
def __init__(self, name: str, dependencies: list[Representation], mapping: list[dict[str, list]],
|
216 |
-
mode: str
|
217 |
TaskMapper.__init__(self, name=name, dependencies=dependencies, n_channels=2)
|
218 |
NpIORepresentation.__init__(self)
|
219 |
-
assert mode in ("all_agree", "
|
220 |
-
assert load_mode in ("one_hot", "binary")
|
221 |
-
assert len(mapping[0]) == 2, mapping
|
222 |
-
assert len(mapping) == len(dependencies), (len(mapping), len(dependencies))
|
223 |
-
assert all(mapping[0].keys() == m.keys() for m in mapping), [m.keys() for m in mapping]
|
224 |
-
self.original_classes = [dep.classes for dep in dependencies]
|
225 |
self.mapping = mapping
|
226 |
self.mode = mode
|
227 |
self.load_mode = load_mode
|
228 |
self.classes = list(mapping[0].keys())
|
229 |
self.n_classes = len(self.classes)
|
230 |
-
self.color_map = [[
|
231 |
self.output_dtype = "bool"
|
232 |
|
233 |
@overrides
|
@@ -250,41 +250,29 @@ class BinaryMapper(TaskMapper, NpIORepresentation):
|
|
250 |
def merge_fn(self, dep_data: list[MemoryData]) -> MemoryData:
|
251 |
dep_data_converted = [semantic_mapper(x.argmax(-1), mapping, oc)
|
252 |
for x, mapping, oc in zip(dep_data, self.mapping, self.original_classes)]
|
253 |
-
|
|
|
|
|
|
|
|
|
|
|
254 |
return self.disk_to_memory_fmt(res_argmax)
|
255 |
|
256 |
class BuildingsFromM2FDepth(BinaryMapper, NpIORepresentation):
|
257 |
-
def __init__(self, name: str,
|
258 |
-
|
259 |
-
{
|
260 |
-
"buildings": (cls := ["Building", "Utility Pole", "Pole", "Fence", "Wall"]),
|
261 |
-
"others": [x for x in mapillary_classes if x not in cls],
|
262 |
-
},
|
263 |
-
{
|
264 |
-
"buildings": (cls := ["building-other-merged", "house", "roof"]),
|
265 |
-
"others": [x for x in coco_classes if x not in cls]
|
266 |
-
}
|
267 |
-
]
|
268 |
-
|
269 |
-
dependencies = [m2f_mapillary, m2f_coco, marigold]
|
270 |
TaskMapper.__init__(self, name=name, dependencies=dependencies, n_channels=2)
|
271 |
NpIORepresentation.__init__(self)
|
272 |
-
self.color_map = [[
|
273 |
-
self.
|
274 |
-
self.mapping = buildings_mapping
|
275 |
-
self.classes = list(buildings_mapping[0].keys())
|
276 |
-
self.n_classes = len(self.classes)
|
277 |
self.load_mode = load_mode
|
278 |
self.output_dtype = "bool"
|
279 |
|
280 |
def merge_fn(self, dep_data: list[MemoryData]) -> MemoryData:
|
281 |
-
|
282 |
-
depth = dep_data[2].squeeze()
|
283 |
-
m2f_mapillary_converted = semantic_mapper(m2f_mapillary, self.mapping[0], self.original_classes[0])
|
284 |
-
m2f_coco_converted = semantic_mapper(m2f_coco, self.mapping[1], self.original_classes[1])
|
285 |
thr = 0.3 # np.percentile(depth.numpy(), 0.8)
|
286 |
-
|
287 |
-
return self.disk_to_memory_fmt(
|
288 |
|
289 |
class SafeLandingAreas(BinaryMapper, NpIORepresentation):
|
290 |
def __init__(self, name: str, original_classes: tuple[list[str], list[str]], include_semantics: bool = False,
|
@@ -293,9 +281,9 @@ class SafeLandingAreas(BinaryMapper, NpIORepresentation):
|
|
293 |
dependencies = [m2f_mapillary, m2f_coco, marigold, normals_svd_marigold]
|
294 |
TaskMapper.__init__(self, name, dependencies=dependencies, n_channels=2)
|
295 |
NpIORepresentation.__init__(self)
|
296 |
-
self.color_map = [[
|
297 |
self.original_classes = original_classes
|
298 |
-
self.classes = ["
|
299 |
self.n_classes = len(self.classes)
|
300 |
self.sky_water = sky_water
|
301 |
self.load_mode = load_mode
|
@@ -310,107 +298,130 @@ class SafeLandingAreas(BinaryMapper, NpIORepresentation):
|
|
310 |
sw = self.sky_water.merge_fn(dep_data)
|
311 |
sw = sw.argmax(-1) if self.sky_water.load_mode == "one_hot" else sw
|
312 |
where_safe = (where_safe * sw * (depth < 0.9)).astype(bool)
|
313 |
-
return self.disk_to_memory_fmt(
|
314 |
|
315 |
def get_new_semantic_mapped_tasks(tasks_subset: list[str] | None = None) -> dict[str, TaskMapper]:
|
316 |
"""The exported function for VRE!"""
|
317 |
buildings_mapping = [
|
318 |
{
|
319 |
-
"
|
320 |
-
|
|
|
321 |
},
|
322 |
{
|
323 |
-
"
|
324 |
-
"
|
325 |
-
}
|
326 |
-
]
|
327 |
-
|
328 |
-
living_mapping = [
|
329 |
-
{
|
330 |
-
"living": (cls := ["Person", "Bicyclist", "Motorcyclist", "Other Rider", "Bird", "Ground Animal"]),
|
331 |
-
"others": [c for c in mapillary_classes if c not in cls],
|
332 |
},
|
333 |
{
|
334 |
-
"
|
335 |
-
|
336 |
-
"
|
337 |
-
}
|
338 |
]
|
339 |
|
340 |
sky_and_water_mapping = [
|
341 |
{
|
342 |
-
"
|
343 |
-
"
|
344 |
},
|
345 |
{
|
346 |
-
"
|
347 |
-
|
|
|
|
|
|
|
|
|
|
|
348 |
},
|
349 |
]
|
350 |
|
351 |
transportation_mapping = [
|
352 |
{
|
353 |
-
"
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
"others": [c for c in mapillary_classes if c not in cls]
|
362 |
},
|
363 |
{
|
364 |
-
"
|
365 |
-
|
366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
367 |
]
|
368 |
|
369 |
containing_mapping = [
|
370 |
{
|
371 |
-
"
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
"contained": [c for c in mapillary_classes if c not in cls], # Buildings and constructions will be here
|
377 |
},
|
378 |
{
|
379 |
-
"
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
"
|
384 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
385 |
]
|
386 |
|
387 |
vegetation_mapping = [
|
388 |
{
|
389 |
-
"
|
390 |
-
|
|
|
391 |
},
|
392 |
{
|
393 |
-
"
|
394 |
-
|
395 |
-
|
396 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
397 |
]
|
398 |
|
399 |
available_tasks: list[TaskMapper] = [
|
400 |
SemanticMask2FormerMapillaryConvertedPaper("semantic_mask2former_swin_mapillary_converted", [m2f_mapillary]),
|
401 |
SemanticMask2FormerMapillaryConvertedPaper("semantic_mask2former_r50_mapillary_converted", [m2f_r50_mapillary]),
|
402 |
SemanticMask2FormerCOCOConverted("semantic_mask2former_swin_coco_converted", [m2f_coco]),
|
403 |
-
BinaryMapper("buildings", [m2f_mapillary, m2f_coco
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
BinaryMapper("transportation", [m2f_mapillary, m2f_coco
|
408 |
-
|
409 |
-
BinaryMapper("
|
410 |
-
|
|
|
411 |
SafeLandingAreas("safe-landing-no-sseg", [mapillary_classes, coco_classes]),
|
412 |
-
SafeLandingAreas("safe-landing-semantics", [mapillary_classes, coco_classes],
|
413 |
-
|
414 |
]
|
415 |
if tasks_subset is None:
|
416 |
return {t.name: t for t in available_tasks}
|
|
|
213 |
TaskMapper is the only high level interface that makes sense, so we should focus on keeping that generic and easy.
|
214 |
"""
|
215 |
def __init__(self, name: str, dependencies: list[Representation], mapping: list[dict[str, list]],
|
216 |
+
mode: str, load_mode: str = "binary"):
|
217 |
TaskMapper.__init__(self, name=name, dependencies=dependencies, n_channels=2)
|
218 |
NpIORepresentation.__init__(self)
|
219 |
+
assert mode in ("all_agree", "majority", "majority"), (name, mode)
|
220 |
+
assert load_mode in ("one_hot", "binary"), (name, load_mode)
|
221 |
+
assert len(mapping[0]) == 2, (name, mapping)
|
222 |
+
assert len(mapping) == len(dependencies), (name, len(mapping), len(dependencies))
|
223 |
+
assert all(mapping[0].keys() == m.keys() for m in mapping), (name, [m.keys() for m in mapping])
|
224 |
+
self.original_classes: list[list[str]] = [dep.classes for dep in dependencies]
|
225 |
self.mapping = mapping
|
226 |
self.mode = mode
|
227 |
self.load_mode = load_mode
|
228 |
self.classes = list(mapping[0].keys())
|
229 |
self.n_classes = len(self.classes)
|
230 |
+
self.color_map = [[0, 0, 0], [255, 255, 255]]
|
231 |
self.output_dtype = "bool"
|
232 |
|
233 |
@overrides
|
|
|
250 |
def merge_fn(self, dep_data: list[MemoryData]) -> MemoryData:
|
251 |
dep_data_converted = [semantic_mapper(x.argmax(-1), mapping, oc)
|
252 |
for x, mapping, oc in zip(dep_data, self.mapping, self.original_classes)]
|
253 |
+
if self.mode == "all_agree":
|
254 |
+
res_argmax = sum(dep_data_converted) == len(self.dependencies)
|
255 |
+
elif self.mode == "majority":
|
256 |
+
res_argmax = sum(dep_data_converted) > 0
|
257 |
+
else:
|
258 |
+
res_argmax = sum(dep_data_converted) > len(dep_data_converted) // 2
|
259 |
return self.disk_to_memory_fmt(res_argmax)
|
260 |
|
261 |
class BuildingsFromM2FDepth(BinaryMapper, NpIORepresentation):
|
262 |
+
def __init__(self, name: str, dependencies: list[Representation], load_mode: str = "binary"):
|
263 |
+
assert len(dependencies) == 2, dependencies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
TaskMapper.__init__(self, name=name, dependencies=dependencies, n_channels=2)
|
265 |
NpIORepresentation.__init__(self)
|
266 |
+
self.color_map = [[0, 0, 0], [255, 255, 255]]
|
267 |
+
self.classes = ["others", name]
|
|
|
|
|
|
|
268 |
self.load_mode = load_mode
|
269 |
self.output_dtype = "bool"
|
270 |
|
271 |
def merge_fn(self, dep_data: list[MemoryData]) -> MemoryData:
|
272 |
+
buildings, depth = dep_data
|
|
|
|
|
|
|
273 |
thr = 0.3 # np.percentile(depth.numpy(), 0.8)
|
274 |
+
buildings_depth = buildings * (depth <= thr)
|
275 |
+
return self.disk_to_memory_fmt(buildings_depth)
|
276 |
|
277 |
class SafeLandingAreas(BinaryMapper, NpIORepresentation):
|
278 |
def __init__(self, name: str, original_classes: tuple[list[str], list[str]], include_semantics: bool = False,
|
|
|
281 |
dependencies = [m2f_mapillary, m2f_coco, marigold, normals_svd_marigold]
|
282 |
TaskMapper.__init__(self, name, dependencies=dependencies, n_channels=2)
|
283 |
NpIORepresentation.__init__(self)
|
284 |
+
self.color_map = [[255, 0, 0], [0, 255, 0]]
|
285 |
self.original_classes = original_classes
|
286 |
+
self.classes = ["unsafe-landing", "safe-landing"]
|
287 |
self.n_classes = len(self.classes)
|
288 |
self.sky_water = sky_water
|
289 |
self.load_mode = load_mode
|
|
|
298 |
sw = self.sky_water.merge_fn(dep_data)
|
299 |
sw = sw.argmax(-1) if self.sky_water.load_mode == "one_hot" else sw
|
300 |
where_safe = (where_safe * sw * (depth < 0.9)).astype(bool)
|
301 |
+
return self.disk_to_memory_fmt(where_safe)
|
302 |
|
303 |
def get_new_semantic_mapped_tasks(tasks_subset: list[str] | None = None) -> dict[str, TaskMapper]:
|
304 |
"""The exported function for VRE!"""
|
305 |
buildings_mapping = [
|
306 |
{
|
307 |
+
"others": [x for x in mapillary_classes if x not in
|
308 |
+
(cls := ["Building", "Utility Pole", "Pole", "Fence", "Wall"])],
|
309 |
+
"buildings": cls,
|
310 |
},
|
311 |
{
|
312 |
+
"others": [x for x in coco_classes if x not in (cls := ["building-other-merged", "house", "roof"])],
|
313 |
+
"buildings": cls,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
},
|
315 |
{
|
316 |
+
"others": [x for x in mapillary_classes if x not in
|
317 |
+
(cls := ["Building", "Utility Pole", "Pole", "Fence", "Wall"])],
|
318 |
+
"buildings": cls,
|
319 |
+
},
|
320 |
]
|
321 |
|
322 |
sky_and_water_mapping = [
|
323 |
{
|
324 |
+
"others": [c for c in mapillary_classes if c not in (cls := ["Sky", "Water"])],
|
325 |
+
"sky-and-water": cls,
|
326 |
},
|
327 |
{
|
328 |
+
"others": [c for c in coco_classes if c not in
|
329 |
+
(cls := ["sky-other-merged", "water-other", "sea", "river"])],
|
330 |
+
"sky-and-water": cls,
|
331 |
+
},
|
332 |
+
{
|
333 |
+
"others": [c for c in mapillary_classes if c not in (cls := ["Sky", "Water"])],
|
334 |
+
"sky-and-water": cls,
|
335 |
},
|
336 |
]
|
337 |
|
338 |
transportation_mapping = [
|
339 |
{
|
340 |
+
"others": [c for c in mapillary_classes if c not in
|
341 |
+
(cls := ["Bike Lane", "Crosswalk - Plain", "Curb Cut", "Parking", "Rail Track", "Road",
|
342 |
+
"Service Lane", "Sidewalk", "Bridge", "Tunnel", "Bicyclist", "Motorcyclist",
|
343 |
+
"Other Rider", "Lane Marking - Crosswalk", "Lane Marking - General", "Traffic Light",
|
344 |
+
"Traffic Sign (Back)", "Traffic Sign (Front)", "Bicycle", "Boat", "Bus", "Car",
|
345 |
+
"Caravan", "Motorcycle", "On Rails", "Other Vehicle", "Trailer", "Truck",
|
346 |
+
"Wheeled Slow", "Car Mount", "Ego Vehicle"])],
|
347 |
+
"transportation": cls,
|
|
|
348 |
},
|
349 |
{
|
350 |
+
"others": [c for c in coco_classes if c not in
|
351 |
+
(cls := ["bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat"])],
|
352 |
+
"transportation": cls,
|
353 |
+
},
|
354 |
+
{
|
355 |
+
"others": [c for c in mapillary_classes if c not in
|
356 |
+
(cls := ["Bike Lane", "Crosswalk - Plain", "Curb Cut", "Parking", "Rail Track", "Road",
|
357 |
+
"Service Lane", "Sidewalk", "Bridge", "Tunnel", "Bicyclist", "Motorcyclist",
|
358 |
+
"Other Rider", "Lane Marking - Crosswalk", "Lane Marking - General", "Traffic Light",
|
359 |
+
"Traffic Sign (Back)", "Traffic Sign (Front)", "Bicycle", "Boat", "Bus", "Car",
|
360 |
+
"Caravan", "Motorcycle", "On Rails", "Other Vehicle", "Trailer", "Truck",
|
361 |
+
"Wheeled Slow", "Car Mount", "Ego Vehicle"])],
|
362 |
+
"transportation": cls,
|
363 |
+
},
|
364 |
]
|
365 |
|
366 |
containing_mapping = [
|
367 |
{
|
368 |
+
"contained": [c for c in mapillary_classes if c not in
|
369 |
+
(cls := ["Terrain", "Sand", "Mountain", "Road", "Sidewalk", "Pedestrian Area", "Rail Track",
|
370 |
+
"Parking", "Service Lane", "Bridge", "Water", "Vegetation", "Curb", "Fence", "Wall",
|
371 |
+
"Guard Rail", "Barrier", "Curb Cut", "Snow"])],
|
372 |
+
"containing": cls,
|
|
|
373 |
},
|
374 |
{
|
375 |
+
"contained": [c for c in coco_classes if c not in
|
376 |
+
(cls := ["floor-wood", "floor-other-merged", "pavement-merged", "mountain-merged", "platform",
|
377 |
+
"sand", "road", "sea", "river", "railroad", "grass-merged", "snow", "stairs",
|
378 |
+
"tent"])],
|
379 |
+
"containing": cls,
|
380 |
+
},
|
381 |
+
{
|
382 |
+
"contained": [c for c in mapillary_classes if c not in
|
383 |
+
(cls := ["Terrain", "Sand", "Mountain", "Road", "Sidewalk", "Pedestrian Area", "Rail Track",
|
384 |
+
"Parking", "Service Lane", "Bridge", "Water", "Vegetation", "Curb", "Fence", "Wall",
|
385 |
+
"Guard Rail", "Barrier", "Curb Cut", "Snow"])],
|
386 |
+
"containing": cls,
|
387 |
+
},
|
388 |
]
|
389 |
|
390 |
vegetation_mapping = [
|
391 |
{
|
392 |
+
"others": [x for x in mapillary_classes if x not in
|
393 |
+
(cls := ["Mountain", "Sand", "Sky", "Snow", "Terrain", "Vegetation"])],
|
394 |
+
"vegetation": cls,
|
395 |
},
|
396 |
{
|
397 |
+
"others": [x for x in coco_classes if x not in
|
398 |
+
(cls := ["tree-merged", "grass-merged", "dirt-merged", "flower", "potted plant", "river",
|
399 |
+
"sea", "water-other", "mountain-merged", "rock-merged"])],
|
400 |
+
"vegetation": cls,
|
401 |
+
},
|
402 |
+
{
|
403 |
+
"others": [x for x in mapillary_classes if x not in
|
404 |
+
(cls := ["Mountain", "Sand", "Sky", "Snow", "Terrain", "Vegetation"])],
|
405 |
+
"vegetation": cls,
|
406 |
+
},
|
407 |
]
|
408 |
|
409 |
available_tasks: list[TaskMapper] = [
|
410 |
SemanticMask2FormerMapillaryConvertedPaper("semantic_mask2former_swin_mapillary_converted", [m2f_mapillary]),
|
411 |
SemanticMask2FormerMapillaryConvertedPaper("semantic_mask2former_r50_mapillary_converted", [m2f_r50_mapillary]),
|
412 |
SemanticMask2FormerCOCOConverted("semantic_mask2former_swin_coco_converted", [m2f_coco]),
|
413 |
+
buildings := BinaryMapper("buildings", [m2f_mapillary, m2f_coco, m2f_r50_mapillary],
|
414 |
+
buildings_mapping, mode="majority"),
|
415 |
+
BinaryMapper("sky-and-water", [m2f_mapillary, m2f_coco, m2f_r50_mapillary],
|
416 |
+
sky_and_water_mapping, mode="majority"),
|
417 |
+
BinaryMapper("transportation", [m2f_mapillary, m2f_coco, m2f_r50_mapillary],
|
418 |
+
transportation_mapping, mode="majority"),
|
419 |
+
BinaryMapper("containing", [m2f_mapillary, m2f_coco, m2f_r50_mapillary], containing_mapping, mode="majority"),
|
420 |
+
BinaryMapper("vegetation", [m2f_mapillary, m2f_coco, m2f_r50_mapillary], vegetation_mapping, mode="majority"),
|
421 |
+
BuildingsFromM2FDepth("buildings(nearby)", [buildings, marigold]),
|
422 |
SafeLandingAreas("safe-landing-no-sseg", [mapillary_classes, coco_classes]),
|
423 |
+
# SafeLandingAreas("safe-landing-semantics", [mapillary_classes, coco_classes],
|
424 |
+
# include_semantics=True, sky_water=sky_water),
|
425 |
]
|
426 |
if tasks_subset is None:
|
427 |
return {t.name: t for t in available_tasks}
|