File size: 6,728 Bytes
7bc669c 1488a42 7bc669c b5aa314 7bc669c |
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 |
"""script that converts predictions (.npz) of mask2former (mapillary or coco_panoptic) to dronescapes labels"""
from argparse import ArgumentParser, Namespace
from pathlib import Path
import shutil
from functools import partial
import numpy as np
from tqdm import tqdm
from loggez import loggez_logger as logger
COCO_MAPPING = {
"land": ["grass-merged", "dirt-merged", "sand", "gravel", "flower", "playingfield", "snow", "platform"],
"forest": ["tree-merged"],
"residential": ["building-other-merged", "house", "roof", "fence-merged", "wall-other-merged", "wall-brick", "rock-merged", "tent", "bridge", "bench", "window-other", "fire hydrant", "traffic light", "umbrella", "wall-stone", "clock", "chair", "sports ball", "floor-other-merged", "floor-wood", "stop sign", "door-stuff", "banner", "light", "net", "surfboard", "frisbee", "rug-merged", "potted plant", "parking meter"],
"road": ["road", "railroad", "pavement-merged", "stairs"],
"little-objects": ["truck", "car", "boat", "horse", "person", "train", "elephant", "bus", "bird", "sheep", "cow", "motorcycle", "dog", "bicycle", "airplane", "kite"],
"water": ["river", "water-other", "sea"],
"sky": ["sky-other-merged"],
"hill": ["mountain-merged"]
}
COCO_CLASSES = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush", "banner", "blanket", "bridge", "cardboard", "counter", "curtain", "door-stuff", "floor-wood", "flower", "fruit", "gravel", "house", "light", "mirror-stuff", "net", "pillow", "platform", "playingfield", "railroad", "river", "road", "roof", "sand", "sea", "shelf", "snow", "stairs", "tent", "towel", "wall-brick", "wall-stone", "wall-tile", "wall-wood", "water-other", "window-blind", "window-other", "tree-merged", "fence-merged", "ceiling-merged", "sky-other-merged", "cabinet-merged", "table-merged", "floor-other-merged", "pavement-merged", "mountain-merged", "grass-merged", "dirt-merged", "paper-merged", "food-other-merged", "building-other-merged", "rock-merged", "wall-other-merged", "rug-merged"]
MAPILLARY_MAPPING = {
"land": ["Terrain", "Sand", "Snow"],
"forest": ["Vegetation"],
"residential": ["Building", "Utility Pole", "Pole", "Fence", "Wall", "Manhole", "Street Light", "Curb", "Guard Rail", "Caravan", "Junction Box", "Traffic Sign (Front)", "Billboard", "Banner", "Mailbox", "Traffic Sign (Back)", "Bench", "Fire Hydrant", "Trash Can", "CCTV Camera", "Traffic Light", "Barrier", "Rail Track", "Phone Booth", "Curb Cut", "Traffic Sign Frame", "Bike Rack"],
"road": ["Road", "Lane Marking - General", "Sidewalk", "Bridge", "Other Vehicle", "Motorcyclist", "Pothole", "Catch Basin", "Car Mount", "Tunnel", "Parking", "Service Lane", "Lane Marking - Crosswalk", "Pedestrian Area", "On Rails", "Bike Lane", "Crosswalk - Plain"],
"little-objects": ["Car", "Person", "Truck", "Boat", "Wheeled Slow", "Trailer", "Ground Animal", "Bicycle", "Motorcycle", "Bird", "Bus", "Ego Vehicle", "Bicyclist", "Other Rider"],
"water": ["Water"],
"sky": ["Sky"],
"hill": ["Mountain"]
}
MAPILLARY_CLASSES = ["Bird", "Ground Animal", "Curb", "Fence", "Guard Rail", "Barrier", "Wall", "Bike Lane", "Crosswalk - Plain", "Curb Cut", "Parking", "Pedestrian Area", "Rail Track", "Road", "Service Lane", "Sidewalk", "Bridge", "Building", "Tunnel", "Person", "Bicyclist", "Motorcyclist", "Other Rider", "Lane Marking - Crosswalk", "Lane Marking - General", "Mountain", "Sand", "Sky", "Snow", "Terrain", "Vegetation", "Water", "Banner", "Bench", "Bike Rack", "Billboard", "Catch Basin", "CCTV Camera", "Fire Hydrant", "Junction Box", "Mailbox", "Manhole", "Phone Booth", "Pothole", "Street Light", "Pole", "Traffic Sign Frame", "Utility Pole", "Traffic Light", "Traffic Sign (Back)", "Traffic Sign (Front)", "Trash Can", "Bicycle", "Boat", "Bus", "Car", "Caravan", "Motorcycle", "On Rails", "Other Vehicle", "Trailer", "Truck", "Wheeled Slow", "Car Mount", "Ego Vehicle"]
def get_args() -> Namespace:
parser = ArgumentParser()
parser.add_argument("input_path", type=lambda p: Path(p).absolute())
parser.add_argument("output_path", type=lambda p: Path(p).absolute())
parser.add_argument("mapping_type", choices=["coco", "mapillary"])
parser.add_argument("--overwrite", action="store_true")
args = parser.parse_args()
assert not args.output_path.exists() or args.overwrite, f"{args.output_path} exists. Use --overwrite"
if args.output_path.exists():
shutil.rmtree(args.output_path)
return args
def do_one(in_out_path: tuple[Path, Path], mapping_type: str):
in_path, out_path = in_out_path
data = np.load(in_path, allow_pickle=False)
data = data if isinstance(data, np.ndarray) else data["arr_0"] # in case on npz, we need this as well
classes = MAPILLARY_CLASSES if mapping_type == "mapillary" else COCO_CLASSES
mapping = MAPILLARY_MAPPING if mapping_type == "mapillary" else COCO_MAPPING
mapping_ix = {list(mapping.keys()).index(k): [classes.index(_v) for _v in v] for k, v in mapping.items()}
mapping_to_dronescapes = {}
for k, v in mapping_ix.items():
for _v in v:
mapping_to_dronescapes[_v] = k
mapped_data = np.vectorize(mapping_to_dronescapes.get)(data).astype(np.uint8)
np.savez(out_path, mapped_data)
return mapped_data
def main(args: Namespace):
in_files = [x for x in args.input_path.iterdir() if x.suffix == ".npz"]
out_files = [args.output_path / x.name for x in in_files]
args.output_path.mkdir(exist_ok=False, parents=True)
assert len(in_files) > 0, "No .npz files found"
logger.info(f"In dir: '{args.input_path}'")
logger.info(f"Out dir: '{args.output_path}'")
logger.info(f"Found {len(in_files)} to convert. Dataset type: '{args.mapping_type}'")
items = list(zip(in_files, out_files))
for item in tqdm(items):
do_one(item, mapping_type=args.mapping_type)
if __name__ == "__main__":
main(get_args())
|