Meehai commited on
Commit
7bc669c
1 Parent(s): a3c6a05

added convert script

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. README.md +20 -2
  3. scripts/convert_m2f_to_dronescapes.py +79 -0
.gitignore CHANGED
@@ -12,4 +12,5 @@ __pycache__
12
  error.txt
13
  sanity_check.py
14
  commands.txt
 
15
 
 
12
  error.txt
13
  sanity_check.py
14
  commands.txt
15
+ raw_data/npz_540p_2/
16
 
README.md CHANGED
@@ -55,7 +55,25 @@ VRE_DEVICE=cuda CUDA_VISIBLE_DEVICES=1 vre raw_data/videos/slanic_DJI_0956_0957_
55
 
56
  ### 1.2.4 Convert Mask2Former from Mapillary classes to segprop8 classes
57
 
58
- TODO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  ### 1.2.5 Check counts for consistency
61
 
@@ -198,5 +216,5 @@ python scripts/dronescapes_viewer.py data/test_set_annotated_only/ # or any of t
198
 
199
  ## TODOs
200
  - convert camera normals to world normals
201
- - mask2former convert
202
  - add semantics for each representation in a DronescapesReader
 
 
55
 
56
  ### 1.2.4 Convert Mask2Former from Mapillary classes to segprop8 classes
57
 
58
+ Since we are using pre-trained Mask2Former which has either mapillary or COCO panoptic classes, we need to convert them to dronescapes-compatible (8) classes.
59
+
60
+ To do this, we use the `scripts/convert_m2f_to_dronescapes.py` script:
61
+ ```
62
+ python scripts/convert_m2f_to_dronescapes.py in_dir out_dir mapillary/coco [--overwrite]
63
+ ```
64
+
65
+ ```
66
+ python scripts/convert_m2f_to_dronescapes.py raw_data/npz_540p/atanasie_DJI_0652_full/semantic_mask2former_swin_mapillary raw_data/npz_540p/atanasie_DJI_0652_full/semantic_mask2former_swin_mapillary_converted mapillary
67
+ python scripts/convert_m2f_to_dronescapes.py raw_data/npz_540p/barsana_DJI_0500_0501_combined_sliced_2700_14700/semantic_mask2former_swin_mapillary raw_data/npz_540p/barsana_DJI_0500_0501_combined_sliced_2700_14700/semantic_mask2former_swin_mapillary_converted mapillary
68
+ python scripts/convert_m2f_to_dronescapes.py raw_data/npz_540p/comana_DJI_0881_full/semantic_mask2former_swin_mapillary raw_data/npz_540p/comana_DJI_0881_full/semantic_mask2former_swin_mapillary_converted mapillary
69
+ python scripts/convert_m2f_to_dronescapes.py raw_data/npz_540p/gradistei_DJI_0787_0788_0789_combined_sliced_3510_13110/semantic_mask2former_swin_mapillary raw_data/npz_540p/gradistei_DJI_0787_0788_0789_combined_sliced_3510_13110/semantic_mask2former_swin_mapillary_converted mapillary
70
+ python scripts/convert_m2f_to_dronescapes.py raw_data/npz_540p/herculane_DJI_0021_full/semantic_mask2former_swin_mapillary raw_data/npz_540p/herculane_DJI_0021_full/semantic_mask2former_swin_mapillary_converted mapillary
71
+ python scripts/convert_m2f_to_dronescapes.py raw_data/npz_540p/jupiter_DJI_0703_0704_0705_combined_sliced_10650_21715/semantic_mask2former_swin_mapillary raw_data/npz_540p/jupiter_DJI_0703_0704_0705_combined_sliced_10650_21715/semantic_mask2former_swin_mapillary_converted mapillary
72
+ python scripts/convert_m2f_to_dronescapes.py raw_data/npz_540p/norway_210821_DJI_0015_full/semantic_mask2former_swin_mapillary raw_data/npz_540p/norway_210821_DJI_0015_full/semantic_mask2former_swin_mapillary_converted mapillary
73
+ python scripts/convert_m2f_to_dronescapes.py raw_data/npz_540p/olanesti_DJI_0416_full/semantic_mask2former_swin_mapillary raw_data/npz_540p/olanesti_DJI_0416_full/semantic_mask2former_swin_mapillary_converted mapillary
74
+ python scripts/convert_m2f_to_dronescapes.py raw_data/npz_540p/petrova_DJI_0525_0526_combined_sliced_2850_11850/semantic_mask2former_swin_mapillary raw_data/npz_540p/petrova_DJI_0525_0526_combined_sliced_2850_11850/semantic_mask2former_swin_mapillary_converted mapillary
75
+ python scripts/convert_m2f_to_dronescapes.py raw_data/npz_540p/slanic_DJI_0956_0957_combined_sliced_780_9780/semantic_mask2former_swin_mapillary raw_data/npz_540p/slanic_DJI_0956_0957_combined_sliced_780_9780/semantic_mask2former_swin_mapillary_converted mapillary
76
+ ```
77
 
78
  ### 1.2.5 Check counts for consistency
79
 
 
216
 
217
  ## TODOs
218
  - convert camera normals to world normals
 
219
  - add semantics for each representation in a DronescapesReader
220
+ - evaluation script for sseg
scripts/convert_m2f_to_dronescapes.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """script that converts predictions (.npz) of mask2former (mapillary or coco_panoptic) to dronescapes labels"""
2
+ from argparse import ArgumentParser, Namespace
3
+ from pathlib import Path
4
+ import shutil
5
+ from functools import partial
6
+ import numpy as np
7
+ from tqdm import tqdm
8
+ from loguru import logger
9
+
10
+ COCO_MAPPING = {
11
+ "land": ["grass-merged", "dirt-merged", "sand", "gravel", "flower", "playingfield", "snow", "platform"],
12
+ "forest": ["tree-merged"],
13
+ "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",
14
+ "floor-other-merged", "floor-wood", "stop sign", "door-stuff", "banner", "light", "net", "surfboard", "frisbee", "rug-merged", "potted plant", "parking meter"],
15
+ "road": ["road", "railroad", "pavement-merged", "stairs"],
16
+ "little-objects": ["truck", "car", "boat", "horse", "person", "train", "elephant", "bus", "bird", "sheep", "cow", "motorcycle", "dog", "bicycle", "airplane", "kite"],
17
+ "water": ["river", "water-other", "sea"],
18
+ "sky": ["sky-other-merged"],
19
+ "hill": ["mountain-merged"]
20
+ }
21
+
22
+ 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"]
23
+
24
+ MAPILLARY_MAPPING = {
25
+ "land": ["Terrain", "Sand", "Snow"],
26
+ "forest": ["Vegetation"],
27
+ "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"],
28
+ "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"],
29
+ "little-objects": ["Car", "Person", "Truck", "Boat", "Wheeled Slow", "Trailer", "Ground Animal", "Bicycle", "Motorcycle", "Bird", "Bus", "Ego Vehicle", "Bicyclist", "Other Rider"],
30
+ "water": ["Water"],
31
+ "sky": ["Sky"],
32
+ "hill": ["Mountain"]
33
+ }
34
+
35
+ 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"]
36
+
37
+ def get_args() -> Namespace:
38
+ parser = ArgumentParser()
39
+ parser.add_argument("input_path", type=lambda p: Path(p).absolute())
40
+ parser.add_argument("output_path", type=lambda p: Path(p).absolute())
41
+ parser.add_argument("mapping_type", choices=["coco", "mapillary"])
42
+ parser.add_argument("--overwrite", action="store_true")
43
+ args = parser.parse_args()
44
+ assert not args.output_path.exists() or args.overwrite, f"{args.output_path} exists. Use --overwrite"
45
+ if args.output_path.exists():
46
+ shutil.rmtree(args.output_path)
47
+ return args
48
+
49
+ def do_one(in_out_path: tuple[Path, Path], mapping_type: str):
50
+ in_path, out_path = in_out_path
51
+ data = np.load(in_path, allow_pickle=False)
52
+ data = data if isinstance(data, np.ndarray) else data["arr_0"] # in case on npz, we need this as well
53
+
54
+ classes = MAPILLARY_CLASSES if mapping_type == "mapillary" else COCO_CLASSES
55
+ mapping = MAPILLARY_MAPPING if mapping_type == "mapillary" else COCO_MAPPING
56
+ mapping_ix = {list(mapping.keys()).index(k): [classes.index(_v) for _v in v] for k, v in mapping.items()}
57
+ mapping_to_dronescapes = {}
58
+ for k, v in mapping_ix.items():
59
+ for _v in v:
60
+ mapping_to_dronescapes[_v] = k
61
+ mapped_data = np.vectorize(mapping_to_dronescapes.get)(data).astype(np.uint8)
62
+ np.savez(out_path, mapped_data)
63
+ return mapped_data
64
+
65
+ def main(args: Namespace):
66
+ in_files = [x for x in args.input_path.iterdir() if x.suffix == ".npz"]
67
+ out_files = [args.output_path / x.name for x in in_files]
68
+ args.output_path.mkdir(exist_ok=False, parents=True)
69
+ assert len(in_files) > 0, "No .npz files found"
70
+ logger.info(f"In dir: '{args.input_path}'")
71
+ logger.info(f"Out dir: '{args.output_path}'")
72
+ logger.info(f"Found {len(in_files)} to convert. Dataset type: '{args.mapping_type}'")
73
+
74
+ items = list(zip(in_files, out_files))
75
+ for item in tqdm(items):
76
+ do_one(item, mapping_type=args.mapping_type)
77
+
78
+ if __name__ == "__main__":
79
+ main(get_args())