#!/usr/bin/env python3 """Export a config/split to image and label folders (the mmsegmentation layout), for training code that reads files instead of `datasets`. /img_dir//.png images, written as stored (syn renders are RGBA) /ann_dir//.png label maps: pixel value = class id /depth_dir//.npy syn only, with --with-depth (float16 metres) /normal_dir//.png syn only, with --with-normals /classes.json class names and colors in the label order used --label-order checkpoint writes labels in the order the released checkpoints were trained with (see navable.CHECKPOINT_CLASSES), so their mmseg configs can be reused unchanged. Examples: python tools/export.py --config real_ours --split train --out data/navable python tools/export.py --config real_ours --split test --out data/navable python tools/export.py --config syn --classes escalator --max-rows 2000 --with-depth --out data/navable_syn """ from __future__ import annotations import argparse import io import json import sys from pathlib import Path import numpy as np from PIL import Image sys.path.insert(0, str(Path(__file__).resolve().parent)) import navable as nv # noqa: E402 def main() -> int: p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) p.add_argument("--config", choices=list(nv.CONFIGS), required=True) p.add_argument("--split", default="train") p.add_argument("--out", type=Path, required=True) p.add_argument("--classes", nargs="+", choices=nv.CLASSES[1:], help="syn / real_ours: only rows of these classes") p.add_argument("--max-rows", type=int, help="stop after this many rows") p.add_argument("--label-order", choices=["dataset", "checkpoint"], default="dataset") p.add_argument("--with-depth", action="store_true", help="syn: also write depth (.npy, float16, metres)") p.add_argument("--with-normals", action="store_true", help="syn: also write surface normals (.png)") p.add_argument("--data-dir", type=Path, help="downloaded copy of the repo (see tools/download.py)") args = p.parse_args() if args.split not in nv.CONFIGS[args.config]: p.error(f"{args.config} has splits {nv.CONFIGS[args.config]}") if (args.with_depth or args.with_normals) and args.config != "syn": p.error("--with-depth / --with-normals only apply to syn") if args.classes and args.config == "real_curated": p.error("real_curated rows have no object_class; export it whole") columns = ["image", "mask"] + (["depth"] if args.with_depth else []) + (["normals"] if args.with_normals else []) dirs = {"img": "img_dir", "ann": "ann_dir", "depth": "depth_dir", "normals": "normal_dir"} for key in ["img", "ann"] + (["depth"] if args.with_depth else []) + (["normals"] if args.with_normals else []): (args.out / dirs[key] / args.split).mkdir(parents=True, exist_ok=True) order = nv.CHECKPOINT_CLASSES if args.label_order == "checkpoint" else nv.CLASSES (args.out / "classes.json").write_text(json.dumps( {"label_order": args.label_order, "classes": order, "palette": [nv.PALETTE[nv.CLASS_TO_ID[c]] for c in order], "ignore_index": nv.IGNORE_INDEX}, indent=1)) files = nv.parquet_files(args.config, args.split, args.data_dir, args.classes) n = 0 for row in nv.iter_rows(files, columns, args.classes): stem = Path(nv.row_name(row)).stem (args.out / "img_dir" / args.split / f"{stem}.png").write_bytes(row["image"]["bytes"]) ann = args.out / "ann_dir" / args.split / f"{stem}.png" if args.label_order == "dataset": ann.write_bytes(row["mask"]["bytes"]) # palettized PNG; pixel value = dataset class id else: Image.fromarray(nv.dataset_to_checkpoint_labels(nv.mask_array(row["mask"]))).save(ann) if args.with_depth: (args.out / "depth_dir" / args.split / f"{stem}.npy").write_bytes(row["depth"]) if args.with_normals: (args.out / "normal_dir" / args.split / f"{stem}.png").write_bytes(row["normals"]["bytes"]) n += 1 if n % 500 == 0: print(f" {n} rows", flush=True) if args.max_rows and n >= args.max_rows: break print(f"exported {n} rows of {args.config}/{args.split} -> {args.out}") return 0 if __name__ == "__main__": sys.exit(main())