adding these here
Browse files
scripts/semantic_mapper/semantic_mapper.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
scripts/semantic_mapper/semantic_mapper.py
ADDED
|
@@ -0,0 +1,448 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""semantic_mapper.py -- primivites for new tasks based on existing CV/dronescapes tasks"""
|
| 3 |
+
from overrides import overrides
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from functools import reduce
|
| 6 |
+
from pprint import pprint
|
| 7 |
+
import numpy as np
|
| 8 |
+
import torch as tr
|
| 9 |
+
|
| 10 |
+
from vre.utils import (semantic_mapper, colorize_semantic_segmentation, DiskData, MemoryData, ReprOut, reorder_dict,
|
| 11 |
+
collage_fn, image_add_title, lo)
|
| 12 |
+
from vre.readers.multitask_dataset import MultiTaskDataset, MultiTaskItem
|
| 13 |
+
from vre.representations import TaskMapper, NpIORepresentation, Representation, build_representations_from_cfg
|
| 14 |
+
from vre.representations.cv_representations import DepthRepresentation, NormalsRepresentation, SemanticRepresentation
|
| 15 |
+
|
| 16 |
+
def plot_one(data: MultiTaskItem, title: str, order: list[str] | None,
|
| 17 |
+
name_to_task: dict[str, Representation]) -> np.ndarray:
|
| 18 |
+
"""simple plot function: plot_one(reader[0][0], reader[0][1], None, reader.name_to_task)"""
|
| 19 |
+
def vre_plot_fn(rgb: tr.Tensor, x: tr.Tensor, node: Representation) -> np.ndarray:
|
| 20 |
+
node.data = ReprOut(rgb.cpu().detach().numpy()[None], MemoryData(x.cpu().detach().numpy()[None]), [0])
|
| 21 |
+
return node.make_images()[0]
|
| 22 |
+
img_data = {k: vre_plot_fn(data["rgb"], v, name_to_task[k]) for k, v in data.items()}
|
| 23 |
+
img_data = reorder_dict(img_data, order) if order is not None else img_data
|
| 24 |
+
titles = [title if len(title) < 40 else f"{title[0:19]}..{title[-19:]}" for title in img_data]
|
| 25 |
+
collage = collage_fn(list(img_data.values()), titles=titles, size_px=40)
|
| 26 |
+
collage = image_add_title(collage, title, size_px=55, top_padding=110)
|
| 27 |
+
return collage
|
| 28 |
+
|
| 29 |
+
coco_classes = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
|
| 30 |
+
"fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
|
| 31 |
+
"elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
|
| 32 |
+
"skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
|
| 33 |
+
"tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
|
| 34 |
+
"sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
|
| 35 |
+
"potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard",
|
| 36 |
+
"cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase",
|
| 37 |
+
"scissors", "teddy bear", "hair drier", "toothbrush", "banner", "blanket", "bridge", "cardboard",
|
| 38 |
+
"counter", "curtain", "door-stuff", "floor-wood", "flower", "fruit", "gravel", "house", "light",
|
| 39 |
+
"mirror-stuff", "net", "pillow", "platform", "playingfield", "railroad", "river", "road", "roof",
|
| 40 |
+
"sand", "sea", "shelf", "snow", "stairs", "tent", "towel", "wall-brick", "wall-stone", "wall-tile",
|
| 41 |
+
"wall-wood", "water-other", "window-blind", "window-other", "tree-merged", "fence-merged",
|
| 42 |
+
"ceiling-merged", "sky-other-merged", "cabinet-merged", "table-merged", "floor-other-merged",
|
| 43 |
+
"pavement-merged", "mountain-merged", "grass-merged", "dirt-merged", "paper-merged",
|
| 44 |
+
"food-other-merged", "building-other-merged", "rock-merged", "wall-other-merged", "rug-merged"]
|
| 45 |
+
coco_color_map = [[220, 20, 60], [119, 11, 32], [0, 0, 142], [0, 0, 230], [106, 0, 228], [0, 60, 100], [0, 80, 100],
|
| 46 |
+
[0, 0, 70], [0, 0, 192], [250, 170, 30], [100, 170, 30], [220, 220, 0], [175, 116, 175], [250, 0, 30],
|
| 47 |
+
[165, 42, 42], [255, 77, 255], [0, 226, 252], [182, 182, 255], [0, 82, 0], [120, 166, 157],
|
| 48 |
+
[110, 76, 0], [174, 57, 255], [199, 100, 0], [72, 0, 118], [255, 179, 240], [0, 125, 92],
|
| 49 |
+
[209, 0, 151], [188, 208, 182], [0, 220, 176], [255, 99, 164], [92, 0, 73], [133, 129, 255],
|
| 50 |
+
[78, 180, 255], [0, 228, 0], [174, 255, 243], [45, 89, 255], [134, 134, 103], [145, 148, 174],
|
| 51 |
+
[255, 208, 186], [197, 226, 255], [171, 134, 1], [109, 63, 54], [207, 138, 255], [151, 0, 95],
|
| 52 |
+
[9, 80, 61], [84, 105, 51], [74, 65, 105], [166, 196, 102], [208, 195, 210], [255, 109, 65],
|
| 53 |
+
[0, 143, 149], [179, 0, 194], [209, 99, 106], [5, 121, 0], [227, 255, 205], [147, 186, 208],
|
| 54 |
+
[153, 69, 1], [3, 95, 161], [163, 255, 0], [119, 0, 170], [0, 182, 199], [0, 165, 120],
|
| 55 |
+
[183, 130, 88], [95, 32, 0], [130, 114, 135], [110, 129, 133], [166, 74, 118], [219, 142, 185],
|
| 56 |
+
[79, 210, 114], [178, 90, 62], [65, 70, 15], [127, 167, 115], [59, 105, 106], [142, 108, 45],
|
| 57 |
+
[196, 172, 0], [95, 54, 80], [128, 76, 255], [201, 57, 1], [246, 0, 122], [191, 162, 208],
|
| 58 |
+
[255, 255, 128], [147, 211, 203], [150, 100, 100], [168, 171, 172], [146, 112, 198],
|
| 59 |
+
[210, 170, 100], [92, 136, 89], [218, 88, 184], [241, 129, 0], [217, 17, 255], [124, 74, 181],
|
| 60 |
+
[70, 70, 70], [255, 228, 255], [154, 208, 0], [193, 0, 92], [76, 91, 113], [255, 180, 195],
|
| 61 |
+
[106, 154, 176], [230, 150, 140], [60, 143, 255], [128, 64, 128], [92, 82, 55], [254, 212, 124],
|
| 62 |
+
[73, 77, 174], [255, 160, 98], [255, 255, 255], [104, 84, 109], [169, 164, 131], [225, 199, 255],
|
| 63 |
+
[137, 54, 74], [135, 158, 223], [7, 246, 231], [107, 255, 200], [58, 41, 149], [183, 121, 142],
|
| 64 |
+
[255, 73, 97], [107, 142, 35], [190, 153, 153], [146, 139, 141], [70, 130, 180], [134, 199, 156],
|
| 65 |
+
[209, 226, 140], [96, 36, 108], [96, 96, 96], [64, 170, 64], [152, 251, 152], [208, 229, 228],
|
| 66 |
+
[206, 186, 171], [152, 161, 64], [116, 112, 0], [0, 114, 143], [102, 102, 156], [250, 141, 255]]
|
| 67 |
+
mapillary_classes = ["Bird", "Ground Animal", "Curb", "Fence", "Guard Rail", "Barrier", "Wall", "Bike Lane",
|
| 68 |
+
"Crosswalk - Plain", "Curb Cut", "Parking", "Pedestrian Area", "Rail Track", "Road",
|
| 69 |
+
"Service Lane", "Sidewalk", "Bridge", "Building", "Tunnel", "Person", "Bicyclist",
|
| 70 |
+
"Motorcyclist", "Other Rider", "Lane Marking - Crosswalk", "Lane Marking - General",
|
| 71 |
+
"Mountain", "Sand", "Sky", "Snow", "Terrain", "Vegetation", "Water", "Banner", "Bench",
|
| 72 |
+
"Bike Rack", "Billboard", "Catch Basin", "CCTV Camera", "Fire Hydrant", "Junction Box",
|
| 73 |
+
"Mailbox", "Manhole", "Phone Booth", "Pothole", "Street Light", "Pole", "Traffic Sign Frame",
|
| 74 |
+
"Utility Pole", "Traffic Light", "Traffic Sign (Back)", "Traffic Sign (Front)", "Trash Can",
|
| 75 |
+
"Bicycle", "Boat", "Bus", "Car", "Caravan", "Motorcycle", "On Rails", "Other Vehicle", "Trailer",
|
| 76 |
+
"Truck", "Wheeled Slow", "Car Mount", "Ego Vehicle"]
|
| 77 |
+
mapillary_color_map = [[165, 42, 42], [0, 192, 0], [196, 196, 196], [190, 153, 153], [180, 165, 180], [90, 120, 150],
|
| 78 |
+
[102, 102, 156], [128, 64, 255], [140, 140, 200], [170, 170, 170], [250, 170, 160], [96, 96, 96],
|
| 79 |
+
[230, 150, 140], [128, 64, 128], [110, 110, 110], [244, 35, 232], [150, 100, 100], [70, 70, 70],
|
| 80 |
+
[150, 120, 90], [220, 20, 60], [255, 0, 0], [255, 0, 100], [255, 0, 200], [200, 128, 128],
|
| 81 |
+
[255, 255, 255], [64, 170, 64], [230, 160, 50], [70, 130, 180], [190, 255, 255], [152, 251, 152],
|
| 82 |
+
[107, 142, 35], [0, 170, 30], [255, 255, 128], [250, 0, 30], [100, 140, 180], [220, 220, 220],
|
| 83 |
+
[220, 128, 128], [222, 40, 40], [100, 170, 30], [40, 40, 40], [33, 33, 33], [100, 128, 160],
|
| 84 |
+
[142, 0, 0], [70, 100, 150], [210, 170, 100], [153, 153, 153], [128, 128, 128], [0, 0, 80],
|
| 85 |
+
[250, 170, 30], [192, 192, 192], [220, 220, 0], [140, 140, 20], [119, 11, 32], [150, 0, 255],
|
| 86 |
+
[0, 60, 100], [0, 0, 142], [0, 0, 90], [0, 0, 230], [0, 80, 100], [128, 64, 64], [0, 0, 110],
|
| 87 |
+
[0, 0, 70], [0, 0, 192], [32, 32, 32], [120, 10, 10]]
|
| 88 |
+
|
| 89 |
+
m2f_coco = SemanticRepresentation("semantic_mask2former_coco_47429163_0", classes=coco_classes,
|
| 90 |
+
color_map=coco_color_map)
|
| 91 |
+
m2f_mapillary = SemanticRepresentation("semantic_mask2former_mapillary_49189528_0", classes=mapillary_classes,
|
| 92 |
+
color_map=mapillary_color_map)
|
| 93 |
+
m2f_r50_mapillary = SemanticRepresentation("semantic_mask2former_mapillary_49189528_1", classes=mapillary_classes,
|
| 94 |
+
color_map=mapillary_color_map)
|
| 95 |
+
marigold = DepthRepresentation("depth_marigold", min_depth=0, max_depth=1)
|
| 96 |
+
normals_svd_marigold = NormalsRepresentation("normals_svd(depth_marigold)")
|
| 97 |
+
|
| 98 |
+
class SemanticMask2FormerMapillaryConvertedPaper(TaskMapper, NpIORepresentation):
|
| 99 |
+
def __init__(self, name: str, dependencies: list[Representation]):
|
| 100 |
+
TaskMapper.__init__(self, name=name, n_channels=8, dependencies=dependencies)
|
| 101 |
+
NpIORepresentation.__init__(self)
|
| 102 |
+
self.mapping = {
|
| 103 |
+
"land": ["Terrain", "Sand", "Snow"],
|
| 104 |
+
"forest": ["Vegetation"],
|
| 105 |
+
"residential": ["Building", "Utility Pole", "Pole", "Fence", "Wall", "Manhole", "Street Light", "Curb",
|
| 106 |
+
"Guard Rail", "Caravan", "Junction Box", "Traffic Sign (Front)", "Billboard", "Banner",
|
| 107 |
+
"Mailbox", "Traffic Sign (Back)", "Bench", "Fire Hydrant", "Trash Can", "CCTV Camera",
|
| 108 |
+
"Traffic Light", "Barrier", "Rail Track", "Phone Booth", "Curb Cut", "Traffic Sign Frame",
|
| 109 |
+
"Bike Rack"],
|
| 110 |
+
"road": ["Road", "Lane Marking - General", "Sidewalk", "Bridge", "Other Vehicle", "Motorcyclist", "Pothole",
|
| 111 |
+
"Catch Basin", "Car Mount", "Tunnel", "Parking", "Service Lane", "Lane Marking - Crosswalk",
|
| 112 |
+
"Pedestrian Area", "On Rails", "Bike Lane", "Crosswalk - Plain"],
|
| 113 |
+
"little-objects": ["Car", "Person", "Truck", "Boat", "Wheeled Slow", "Trailer", "Ground Animal", "Bicycle",
|
| 114 |
+
"Motorcycle", "Bird", "Bus", "Ego Vehicle", "Bicyclist", "Other Rider"],
|
| 115 |
+
"water": ["Water"],
|
| 116 |
+
"sky": ["Sky"],
|
| 117 |
+
"hill": ["Mountain"]
|
| 118 |
+
}
|
| 119 |
+
self.color_map = [[0, 255, 0], [0, 127, 0], [255, 255, 0], [255, 255, 255],
|
| 120 |
+
[255, 0, 0], [0, 0, 255], [0, 255, 255], [127, 127, 63]]
|
| 121 |
+
self.original_classes = dependencies[0].classes
|
| 122 |
+
assert set(reduce(lambda x, y: x + y, self.mapping.values(), [])) == set(self.original_classes)
|
| 123 |
+
self.classes = list(self.mapping.keys())
|
| 124 |
+
self.n_classes = len(self.classes)
|
| 125 |
+
self.output_dtype = "uint8"
|
| 126 |
+
|
| 127 |
+
@property
|
| 128 |
+
@overrides
|
| 129 |
+
def n_channels(self) -> int:
|
| 130 |
+
return self.n_classes
|
| 131 |
+
|
| 132 |
+
@overrides
|
| 133 |
+
def make_images(self) -> np.ndarray:
|
| 134 |
+
return colorize_semantic_segmentation(self.data.output.argmax(-1), self.classes, self.color_map)
|
| 135 |
+
|
| 136 |
+
@overrides
|
| 137 |
+
def merge_fn(self, dep_data: list[MemoryData]) -> MemoryData:
|
| 138 |
+
m2f_mapillary = dep_data[0].argmax(-1)
|
| 139 |
+
m2f_mapillary_converted = semantic_mapper(m2f_mapillary, self.mapping, self.original_classes)
|
| 140 |
+
return self.disk_to_memory_fmt(m2f_mapillary_converted)
|
| 141 |
+
|
| 142 |
+
@overrides
|
| 143 |
+
def memory_to_disk_fmt(self, memory_data: MemoryData) -> DiskData:
|
| 144 |
+
return memory_data.argmax(-1).astype(np.uint8)
|
| 145 |
+
|
| 146 |
+
@overrides
|
| 147 |
+
def disk_to_memory_fmt(self, disk_data: DiskData) -> MemoryData:
|
| 148 |
+
return MemoryData(np.eye(self.n_classes)[disk_data.astype(int)])
|
| 149 |
+
|
| 150 |
+
class SemanticMask2FormerCOCOConverted(TaskMapper, NpIORepresentation):
|
| 151 |
+
def __init__(self, name: str, dependencies: list[Representation]):
|
| 152 |
+
TaskMapper.__init__(self, name=name, n_channels=8, dependencies=dependencies)
|
| 153 |
+
NpIORepresentation.__init__(self)
|
| 154 |
+
self.mapping = {
|
| 155 |
+
"land": ["grass-merged", "dirt-merged", "sand", "gravel", "flower", "playingfield", "snow", "platform"],
|
| 156 |
+
"forest": ["tree-merged"],
|
| 157 |
+
"residential": ["building-other-merged", "house", "roof", "fence-merged", "wall-other-merged", "wall-brick",
|
| 158 |
+
"rock-merged", "tent", "bridge", "bench", "window-other", "fire hydrant", "traffic light",
|
| 159 |
+
"umbrella", "wall-stone", "clock", "chair", "sports ball", "floor-other-merged",
|
| 160 |
+
"floor-wood", "stop sign", "door-stuff", "banner", "light", "net", "surfboard", "frisbee",
|
| 161 |
+
"rug-merged", "potted plant", "parking meter", "tennis racket", "sink", "hair drier",
|
| 162 |
+
"food-other-merged", "curtain", "mirror-stuff", "baseball glove", "baseball bat", "zebra",
|
| 163 |
+
"spoon", "towel", "donut", "apple", "handbag", "couch", "orange", "wall-wood",
|
| 164 |
+
"window-blind", "pizza", "cabinet-merged", "skateboard", "remote", "bottle", "bed",
|
| 165 |
+
"table-merged", "backpack", "bear", "wall-tile", "cup", "scissors", "ceiling-merged",
|
| 166 |
+
"oven", "cell phone", "microwave", "toaster", "carrot", "fork", "giraffe", "paper-merged",
|
| 167 |
+
"cat", "book", "sandwich", "wine glass", "pillow", "blanket", "tie", "bowl", "snowboard",
|
| 168 |
+
"vase", "toothbrush", "toilet", "dining table", "laptop", "tv", "cardboard", "keyboard",
|
| 169 |
+
"hot dog", "cake", "knife", "suitcase", "refrigerator", "fruit", "shelf", "counter", "skis",
|
| 170 |
+
"banana", "teddy bear", "broccoli", "mouse"],
|
| 171 |
+
"road": ["road", "railroad", "pavement-merged", "stairs"],
|
| 172 |
+
"little-objects": ["truck", "car", "boat", "horse", "person", "train", "elephant", "bus", "bird", "sheep",
|
| 173 |
+
"cow", "motorcycle", "dog", "bicycle", "airplane", "kite"],
|
| 174 |
+
"water": ["river", "water-other", "sea"],
|
| 175 |
+
"sky": ["sky-other-merged"],
|
| 176 |
+
"hill": ["mountain-merged"]
|
| 177 |
+
}
|
| 178 |
+
self.color_map = [[0, 255, 0], [0, 127, 0], [255, 255, 0], [255, 255, 255],
|
| 179 |
+
[255, 0, 0], [0, 0, 255], [0, 255, 255], [127, 127, 63]]
|
| 180 |
+
self.original_classes = dependencies[0].classes
|
| 181 |
+
assert set(reduce(lambda x, y: x + y, self.mapping.values(), [])) == set(self.original_classes)
|
| 182 |
+
self.classes = list(self.mapping.keys())
|
| 183 |
+
self.n_classes = len(self.classes)
|
| 184 |
+
self.output_dtype = "uint8"
|
| 185 |
+
|
| 186 |
+
@property
|
| 187 |
+
@overrides
|
| 188 |
+
def n_channels(self) -> int:
|
| 189 |
+
return self.n_classes
|
| 190 |
+
|
| 191 |
+
@overrides
|
| 192 |
+
def make_images(self) -> np.ndarray:
|
| 193 |
+
return colorize_semantic_segmentation(self.data.output.argmax(-1), self.classes, self.color_map)
|
| 194 |
+
|
| 195 |
+
@overrides
|
| 196 |
+
def merge_fn(self, dep_data: list[MemoryData]) -> MemoryData:
|
| 197 |
+
m2f_mapillary = dep_data[0].argmax(-1)
|
| 198 |
+
m2f_mapillary_converted = semantic_mapper(m2f_mapillary, self.mapping, self.original_classes)
|
| 199 |
+
res = self.disk_to_memory_fmt(m2f_mapillary_converted)
|
| 200 |
+
return res
|
| 201 |
+
|
| 202 |
+
@overrides
|
| 203 |
+
def memory_to_disk_fmt(self, memory_data: MemoryData) -> DiskData:
|
| 204 |
+
return memory_data.argmax(-1).astype(np.uint8)
|
| 205 |
+
|
| 206 |
+
@overrides
|
| 207 |
+
def disk_to_memory_fmt(self, disk_data: DiskData) -> MemoryData:
|
| 208 |
+
return MemoryData(np.eye(self.n_classes)[disk_data.astype(int)])
|
| 209 |
+
|
| 210 |
+
class BinaryMapper(TaskMapper, NpIORepresentation):
|
| 211 |
+
"""
|
| 212 |
+
Note for future self: this is never generic enough to be in VRE -- we'll keep it in this separate code only
|
| 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 = "all_agree", 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", "at_least_one"), mode
|
| 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 = [[255, 255, 255], [0, 0, 0]]
|
| 231 |
+
self.output_dtype = "bool"
|
| 232 |
+
|
| 233 |
+
@overrides
|
| 234 |
+
def make_images(self) -> np.ndarray:
|
| 235 |
+
x = self.data.output.argmax(-1) if self.load_mode == "one_hot" else (self.data.output > 0.5).astype(int)
|
| 236 |
+
x = x[..., 0] if x.shape[-1] == 1 else x
|
| 237 |
+
return colorize_semantic_segmentation(x, self.classes, self.color_map)
|
| 238 |
+
|
| 239 |
+
@overrides
|
| 240 |
+
def disk_to_memory_fmt(self, disk_data: DiskData) -> MemoryData:
|
| 241 |
+
assert len(disk_data.shape) == 2 and disk_data.dtype == bool, f"{self.name}: {lo(disk_data)}"
|
| 242 |
+
y = np.eye(2)[disk_data.astype(int)] if self.load_mode == "one_hot" else disk_data
|
| 243 |
+
return MemoryData(y.astype(np.float32))
|
| 244 |
+
|
| 245 |
+
@overrides
|
| 246 |
+
def memory_to_disk_fmt(self, memory_data: MemoryData) -> DiskData:
|
| 247 |
+
return memory_data.argmax(-1).astype(bool) if self.load_mode == "one_hot" else memory_data.astype(bool)
|
| 248 |
+
|
| 249 |
+
@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 |
+
res_argmax = sum(dep_data_converted) > (0 if self.mode == "all_agree" else 1)
|
| 254 |
+
return self.disk_to_memory_fmt(res_argmax)
|
| 255 |
+
|
| 256 |
+
class BuildingsFromM2FDepth(BinaryMapper, NpIORepresentation):
|
| 257 |
+
def __init__(self, name: str, original_classes: tuple[list[str], list[str]], load_mode: str = "binary"):
|
| 258 |
+
buildings_mapping = [
|
| 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 = [[255, 255, 255], [0, 0, 0]]
|
| 273 |
+
self.original_classes = original_classes
|
| 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 |
+
m2f_mapillary, m2f_coco = dep_data[0].argmax(-1), dep_data[1].argmax(-1)
|
| 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 |
+
combined = (m2f_mapillary_converted + m2f_coco_converted + (depth > thr)) != 0
|
| 287 |
+
return self.disk_to_memory_fmt(combined)
|
| 288 |
+
|
| 289 |
+
class SafeLandingAreas(BinaryMapper, NpIORepresentation):
|
| 290 |
+
def __init__(self, name: str, original_classes: tuple[list[str], list[str]], include_semantics: bool = False,
|
| 291 |
+
sky_water: BinaryMapper | None = None, load_mode: str = "binary"):
|
| 292 |
+
self.include_semantics = include_semantics
|
| 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 = [[0, 255, 0], [255, 0, 0]]
|
| 297 |
+
self.original_classes = original_classes
|
| 298 |
+
self.classes = ["safe-landing", "unsafe-landing"]
|
| 299 |
+
self.n_classes = len(self.classes)
|
| 300 |
+
self.sky_water = sky_water
|
| 301 |
+
self.load_mode = load_mode
|
| 302 |
+
self.output_dtype = "bool"
|
| 303 |
+
|
| 304 |
+
@overrides
|
| 305 |
+
def merge_fn(self, dep_data: list[MemoryData]) -> MemoryData:
|
| 306 |
+
normals, depth = dep_data[3], dep_data[2].squeeze()
|
| 307 |
+
v1, v2, v3 = normals.transpose(2, 0, 1)
|
| 308 |
+
where_safe = (v2 > 0.8) * ((v1 + v3) < 1.2)
|
| 309 |
+
if self.include_semantics:
|
| 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(~where_safe)
|
| 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 |
+
"buildings": (cls := ["Building", "Utility Pole", "Pole", "Fence", "Wall"]),
|
| 320 |
+
"others": [x for x in mapillary_classes if x not in cls],
|
| 321 |
+
},
|
| 322 |
+
{
|
| 323 |
+
"buildings": (cls := ["building-other-merged", "house", "roof"]),
|
| 324 |
+
"others": [x for x in coco_classes if x not in cls]
|
| 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 |
+
"living": (cls := ["person", "bird", "cat", "dog", "horse", "sheep", "cow",
|
| 335 |
+
"elephant", "bear", "zebra", "giraffe"]),
|
| 336 |
+
"others": [c for c in coco_classes if c not in cls],
|
| 337 |
+
}
|
| 338 |
+
]
|
| 339 |
+
|
| 340 |
+
sky_and_water_mapping = [
|
| 341 |
+
{
|
| 342 |
+
"sky-and-water": (cls := ["Sky", "Water"]),
|
| 343 |
+
"others": [c for c in mapillary_classes if c not in cls],
|
| 344 |
+
},
|
| 345 |
+
{
|
| 346 |
+
"sky-and-water": (cls := ["sky-other-merged", "water-other"]),
|
| 347 |
+
"others": [c for c in coco_classes if c not in cls],
|
| 348 |
+
},
|
| 349 |
+
]
|
| 350 |
+
|
| 351 |
+
transportation_mapping = [
|
| 352 |
+
{
|
| 353 |
+
"transportation": (cls := ["Bike Lane", "Crosswalk - Plain", "Curb Cut", "Parking", "Rail Track",
|
| 354 |
+
"Road", "Service Lane", "Sidewalk", "Bridge", "Tunnel", "Bicyclist",
|
| 355 |
+
"Motorcyclist",
|
| 356 |
+
"Other Rider", "Lane Marking - Crosswalk", "Lane Marking - General",
|
| 357 |
+
"Traffic Light",
|
| 358 |
+
"Traffic Sign (Back)", "Traffic Sign (Front)", "Bicycle", "Boat", "Bus", "Car",
|
| 359 |
+
"Caravan", "Motorcycle", "On Rails", "Other Vehicle", "Trailer", "Truck",
|
| 360 |
+
"Wheeled Slow", "Car Mount", "Ego Vehicle"]),
|
| 361 |
+
"others": [c for c in mapillary_classes if c not in cls]
|
| 362 |
+
},
|
| 363 |
+
{
|
| 364 |
+
"transportation": (cls := ["bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat"]),
|
| 365 |
+
"others": [c for c in coco_classes if c not in cls]
|
| 366 |
+
}
|
| 367 |
+
]
|
| 368 |
+
|
| 369 |
+
containing_mapping = [
|
| 370 |
+
{
|
| 371 |
+
"containing": (cls := [
|
| 372 |
+
"Terrain", "Sand", "Mountain", "Road", "Sidewalk", "Pedestrian Area", "Rail Track", "Parking",
|
| 373 |
+
"Service Lane", "Bridge", "Water", "Vegetation", "Curb", "Fence", "Wall", "Guard Rail",
|
| 374 |
+
"Barrier", "Curb Cut", "Snow"
|
| 375 |
+
]),
|
| 376 |
+
"contained": [c for c in mapillary_classes if c not in cls], # Buildings and constructions will be here
|
| 377 |
+
},
|
| 378 |
+
{
|
| 379 |
+
"containing": (cls := [
|
| 380 |
+
"floor-wood", "floor-other-merged", "pavement-merged", "mountain-merged", "sand", "road",
|
| 381 |
+
"sea", "river", "railroad", "platform", "grass-merged", "snow", "stairs", "tent"
|
| 382 |
+
]),
|
| 383 |
+
"contained": [c for c in coco_classes if c not in cls], # Buildings and constructions will be here
|
| 384 |
+
}
|
| 385 |
+
]
|
| 386 |
+
|
| 387 |
+
vegetation_mapping = [
|
| 388 |
+
{
|
| 389 |
+
"vegetation": (cls := ["Mountain", "Sand", "Sky", "Snow", "Terrain", "Vegetation"]),
|
| 390 |
+
"others": [x for x in mapillary_classes if x not in cls],
|
| 391 |
+
},
|
| 392 |
+
{
|
| 393 |
+
"vegetation": (cls := ["tree-merged", "grass-merged", "dirt-merged", "flower", "potted plant", "river",
|
| 394 |
+
"sea", "water-other", "mountain-merged", "rock-merged"]),
|
| 395 |
+
"others": [x for x in coco_classes if x not in cls],
|
| 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], buildings_mapping),
|
| 404 |
+
BinaryMapper("living-vs-non-living", [m2f_mapillary, m2f_coco], living_mapping),
|
| 405 |
+
sky_water := BinaryMapper("sky-and-water", [m2f_mapillary, m2f_coco], sky_and_water_mapping,
|
| 406 |
+
mode="at_least_one"),
|
| 407 |
+
BinaryMapper("transportation", [m2f_mapillary, m2f_coco], transportation_mapping, mode="at_least_one"),
|
| 408 |
+
BinaryMapper("containing", [m2f_mapillary, m2f_coco], containing_mapping),
|
| 409 |
+
BinaryMapper("vegetation", [m2f_mapillary, m2f_coco], vegetation_mapping),
|
| 410 |
+
BuildingsFromM2FDepth("buildings(nearby)", [mapillary_classes, coco_classes]),
|
| 411 |
+
SafeLandingAreas("safe-landing-no-sseg", [mapillary_classes, coco_classes]),
|
| 412 |
+
SafeLandingAreas("safe-landing-semantics", [mapillary_classes, coco_classes],
|
| 413 |
+
include_semantics=True, sky_water=sky_water),
|
| 414 |
+
]
|
| 415 |
+
if tasks_subset is None:
|
| 416 |
+
return {t.name: t for t in available_tasks}
|
| 417 |
+
return {t.name: t for t in available_tasks if t.name in tasks_subset}
|
| 418 |
+
|
| 419 |
+
if __name__ == "__main__":
|
| 420 |
+
cfg_path = Path.cwd() / "cfg.yaml"
|
| 421 |
+
data_path = Path.cwd() / "data"
|
| 422 |
+
vre_dir = data_path
|
| 423 |
+
|
| 424 |
+
task_names = ["rgb", "depth_marigold", "normals_svd(depth_marigold)", "opticalflow_rife",
|
| 425 |
+
"semantic_mask2former_coco_47429163_0", "semantic_mask2former_mapillary_49189528_0"]
|
| 426 |
+
order = ["rgb", "semantic_mask2former_mapillary_49189528_0", "semantic_mask2former_coco_47429163_0",
|
| 427 |
+
"depth_marigold", "normals_svd(depth_marigold)"]
|
| 428 |
+
|
| 429 |
+
representations = build_representations_from_cfg(cfg_path)
|
| 430 |
+
reader = MultiTaskDataset(vre_dir, task_names=task_names,
|
| 431 |
+
task_types=representations, handle_missing_data="fill_nan",
|
| 432 |
+
normalization="min_max", cache_task_stats=True, batch_size_stats=100)
|
| 433 |
+
orig_task_names = list(reader.task_types.keys())
|
| 434 |
+
|
| 435 |
+
new_tasks = get_new_semantic_mapped_tasks()
|
| 436 |
+
for task_name in reader.task_names:
|
| 437 |
+
if task_name not in orig_task_names:
|
| 438 |
+
reader.remove_task(task_name)
|
| 439 |
+
for new_task in new_tasks.values():
|
| 440 |
+
reader.add_task(new_task, overwrite=True)
|
| 441 |
+
|
| 442 |
+
print("== Random loaded item ==")
|
| 443 |
+
ixs = np.random.permutation(range(len(reader))).tolist()
|
| 444 |
+
for ix in ixs:
|
| 445 |
+
data, name = reader[ix]
|
| 446 |
+
pprint(data)
|
| 447 |
+
print(plot_one(data, title=name, order=order, name_to_task=reader.name_to_task).shape)
|
| 448 |
+
break
|
vre_dronescapes/cfg.yaml
CHANGED
|
@@ -42,6 +42,15 @@ representations:
|
|
| 42 |
compute_parameters:
|
| 43 |
batch_size: 1
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
depth_marigold:
|
| 46 |
type: depth/marigold
|
| 47 |
dependencies: []
|
|
|
|
| 42 |
compute_parameters:
|
| 43 |
batch_size: 1
|
| 44 |
|
| 45 |
+
semantic_mask2former_mapillary_49189528_1:
|
| 46 |
+
type: semantic-segmentation/mask2former
|
| 47 |
+
dependencies: []
|
| 48 |
+
parameters:
|
| 49 |
+
model_id: "49189528_1"
|
| 50 |
+
semantic_argmax_only: True
|
| 51 |
+
compute_parameters:
|
| 52 |
+
batch_size: 1
|
| 53 |
+
|
| 54 |
depth_marigold:
|
| 55 |
type: depth/marigold
|
| 56 |
dependencies: []
|