FromSim2Real / gpudrive-main /data_utils /process_nuscenes_files.py
lzhts1's picture
Upload 385 files
9897e20 verified
Raw
History Blame Contribute Delete
5.66 kB
"""Convert NuScenes scenes to GPUDrive-compatible JSON files."""
from __future__ import annotations
import argparse
import logging
try:
from .nuscenes_adapter import (
NuScenesAdapterConfig,
convert_nuscenes_dataset,
)
except ImportError: # pragma: no cover - supports running as a script.
from nuscenes_adapter import NuScenesAdapterConfig, convert_nuscenes_dataset
def _parse_scene_names(value: str | None) -> set[str] | None:
if not value:
return None
return {item.strip() for item in value.split(",") if item.strip()}
def build_arg_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description=(
"Convert NuScenes scenes into the Waymo-style JSON schema consumed "
"by GPUDrive. Trajectories are interpolated to a fixed 10 Hz, "
"91-step grid by default."
)
)
parser.add_argument(
"--dataroot",
required=True,
help="Path to the NuScenes dataset root.",
)
parser.add_argument(
"--version",
default="v1.0-mini",
help="NuScenes version, e.g. v1.0-mini or v1.0-trainval.",
)
parser.add_argument(
"--output-dir",
default="data/processed/nuscenes",
help="Directory where GPUDrive JSON files will be written.",
)
parser.add_argument(
"--split",
default=None,
help="Optional NuScenes split name, e.g. mini_train, train, val.",
)
parser.add_argument(
"--scene-names",
default=None,
help="Optional comma-separated scene names, e.g. scene-0061,scene-0103.",
)
parser.add_argument(
"--max-scenes",
type=int,
default=None,
help="Optional cap on the number of scenes to convert.",
)
parser.add_argument(
"--file-prefix",
default="tfrecord-nuscenes",
help=(
"Output file prefix. Keep the default if you want SceneDataLoader's "
"default file_prefix='tfrecord' to discover the files."
),
)
parser.add_argument(
"--target-hz",
type=float,
default=10.0,
help="Target interpolation rate in Hz.",
)
parser.add_argument(
"--num-steps",
type=int,
default=91,
help="Number of timesteps per generated GPUDrive scenario.",
)
parser.add_argument(
"--max-annotation-gap-sec",
type=float,
default=1.1,
help="Do not interpolate across raw annotation gaps larger than this.",
)
parser.add_argument(
"--window-stride-sec",
type=float,
default=None,
help=(
"If set, export multiple 9-second windows per scene using this "
"stride. If omitted, only the first full window is exported."
),
)
parser.add_argument(
"--allow-partial-window",
action="store_true",
help="Allow windows that extend beyond the final NuScenes keyframe.",
)
parser.add_argument(
"--no-ego",
action="store_true",
help="Do not synthesize an ego vehicle from NuScenes ego poses.",
)
parser.add_argument(
"--no-map",
action="store_true",
help="Do not export NuScenes map layers.",
)
parser.add_argument(
"--fail-on-missing-map",
action="store_true",
help="Fail conversion if map extraction fails for a scene.",
)
parser.add_argument(
"--map-radius-m",
type=float,
default=120.0,
help="Radius around the scenario center for extracting map features.",
)
parser.add_argument(
"--map-discretization-m",
type=float,
default=1.0,
help="Lane centerline discretization spacing in meters.",
)
parser.add_argument(
"--max-road-elements",
type=int,
default=900,
help="Maximum road records to write per scenario.",
)
parser.add_argument(
"--max-geometry-points",
type=int,
default=512,
help="Maximum points to keep per road geometry.",
)
parser.add_argument(
"--quiet",
action="store_true",
help="Reduce NuScenes SDK output.",
)
return parser
def main() -> int:
logging.basicConfig(level=logging.INFO)
parser = build_arg_parser()
args = parser.parse_args()
config = NuScenesAdapterConfig(
target_hz=args.target_hz,
num_steps=args.num_steps,
max_annotation_gap_sec=args.max_annotation_gap_sec,
require_full_window=not args.allow_partial_window,
include_ego=not args.no_ego,
include_map=not args.no_map,
fail_on_missing_map=args.fail_on_missing_map,
map_radius_m=args.map_radius_m,
map_discretization_m=args.map_discretization_m,
max_road_elements=args.max_road_elements,
max_geometry_points=args.max_geometry_points,
)
stats = convert_nuscenes_dataset(
dataroot=args.dataroot,
version=args.version,
output_dir=args.output_dir,
config=config,
split=args.split,
scene_names=_parse_scene_names(args.scene_names),
max_scenes=args.max_scenes,
file_prefix=args.file_prefix,
window_stride_sec=args.window_stride_sec,
verbose=not args.quiet,
)
logging.info(
"NuScenes conversion complete: selected=%d, skipped=%d, written=%d",
stats["scenes_selected"],
stats["scenes_without_outputs"],
stats["files_written"],
)
return 0
if __name__ == "__main__":
raise SystemExit(main())