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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Deep structural validation for the selected MA-CARLA 1000-sequence delivery."""
|
| 3 |
+
|
| 4 |
+
import argparse
|
| 5 |
+
import concurrent.futures
|
| 6 |
+
import csv
|
| 7 |
+
import json
|
| 8 |
+
import subprocess
|
| 9 |
+
from collections import Counter
|
| 10 |
+
from datetime import datetime, timezone
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
CAMERAS = ("FRONT", "FRONT_LEFT", "FRONT_RIGHT", "REAR")
|
| 17 |
+
FRAME_COUNT = 256
|
| 18 |
+
WIDTH = 800
|
| 19 |
+
HEIGHT = 600
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def parse_args():
|
| 23 |
+
parser = argparse.ArgumentParser()
|
| 24 |
+
parser.add_argument("--selection-manifest", required=True)
|
| 25 |
+
parser.add_argument("--report", required=True)
|
| 26 |
+
parser.add_argument("--workers", type=int, default=12)
|
| 27 |
+
return parser.parse_args()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def probe_video(path):
|
| 31 |
+
command = [
|
| 32 |
+
"ffprobe", "-v", "error", "-select_streams", "v:0",
|
| 33 |
+
"-show_entries", "stream=codec_name,pix_fmt,width,height,nb_frames",
|
| 34 |
+
"-of", "json", str(path),
|
| 35 |
+
]
|
| 36 |
+
result = json.loads(subprocess.check_output(command, text=True))
|
| 37 |
+
streams = result.get("streams", [])
|
| 38 |
+
if len(streams) != 1:
|
| 39 |
+
raise RuntimeError("{} has {} video streams".format(path, len(streams)))
|
| 40 |
+
return streams[0]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def validate_npz(path, agent_ids):
|
| 44 |
+
required_suffixes = {
|
| 45 |
+
"frame_index": (FRAME_COUNT,),
|
| 46 |
+
"carla_frame": (FRAME_COUNT,),
|
| 47 |
+
"timestamp": (FRAME_COUNT,),
|
| 48 |
+
"pose": (FRAME_COUNT, 6),
|
| 49 |
+
"camera_pose": (FRAME_COUNT, 6),
|
| 50 |
+
"all_camera_names": (4,),
|
| 51 |
+
"all_camera_pose": (FRAME_COUNT, 4, 6),
|
| 52 |
+
"all_camera_extrinsic": (FRAME_COUNT, 4, 4, 4),
|
| 53 |
+
"velocity": (FRAME_COUNT, 3),
|
| 54 |
+
"angular_velocity": (FRAME_COUNT, 3),
|
| 55 |
+
"acceleration": (FRAME_COUNT, 3),
|
| 56 |
+
"speed_mps": (FRAME_COUNT,),
|
| 57 |
+
"horizontal_speed_mps": (FRAME_COUNT,),
|
| 58 |
+
"control": (FRAME_COUNT, 7),
|
| 59 |
+
"other_agent_id": (FRAME_COUNT,),
|
| 60 |
+
"other_visible": (FRAME_COUNT,),
|
| 61 |
+
"other_bbox": (FRAME_COUNT, 4),
|
| 62 |
+
}
|
| 63 |
+
with np.load(str(path), allow_pickle=False) as arrays:
|
| 64 |
+
for agent_id in agent_ids:
|
| 65 |
+
for suffix, shape in required_suffixes.items():
|
| 66 |
+
key = "agent{}_{}".format(agent_id, suffix)
|
| 67 |
+
if key not in arrays:
|
| 68 |
+
raise RuntimeError("missing NPZ key {}".format(key))
|
| 69 |
+
if arrays[key].shape != shape:
|
| 70 |
+
raise RuntimeError("{} has shape {}, expected {}".format(key, arrays[key].shape, shape))
|
| 71 |
+
names = tuple(str(name) for name in arrays["agent{}_all_camera_names".format(agent_id)])
|
| 72 |
+
if names != CAMERAS:
|
| 73 |
+
raise RuntimeError("unexpected camera names for agent {}: {}".format(agent_id, names))
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def validate_csv(path, agent_ids):
|
| 77 |
+
required = {
|
| 78 |
+
"frame_index", "carla_frame", "timestamp", "agent_id", "image", "camera_name",
|
| 79 |
+
"vehicle_x", "vehicle_y", "vehicle_z", "vehicle_roll", "vehicle_pitch", "vehicle_yaw",
|
| 80 |
+
"camera_x", "camera_y", "camera_z", "camera_roll", "camera_pitch", "camera_yaw",
|
| 81 |
+
"all_camera_poses_json", "all_camera_extrinsics_json", "velocity_x", "velocity_y",
|
| 82 |
+
"velocity_z", "angular_velocity_x", "angular_velocity_y", "angular_velocity_z",
|
| 83 |
+
"acceleration_x", "acceleration_y", "acceleration_z", "speed_mps", "throttle", "steer",
|
| 84 |
+
"brake", "other_visible", "other_bbox_min_u", "other_bbox_min_v", "other_bbox_max_u",
|
| 85 |
+
"other_bbox_max_v",
|
| 86 |
+
}
|
| 87 |
+
counts = Counter()
|
| 88 |
+
frames = {agent_id: set() for agent_id in agent_ids}
|
| 89 |
+
with path.open(newline="") as file_obj:
|
| 90 |
+
reader = csv.DictReader(file_obj)
|
| 91 |
+
if not required.issubset(set(reader.fieldnames or [])):
|
| 92 |
+
raise RuntimeError("frames.csv missing columns: {}".format(sorted(required - set(reader.fieldnames or []))))
|
| 93 |
+
for row in reader:
|
| 94 |
+
agent_id = int(row["agent_id"])
|
| 95 |
+
counts[agent_id] += 1
|
| 96 |
+
frames.setdefault(agent_id, set()).add(int(row["frame_index"]))
|
| 97 |
+
if not row["all_camera_poses_json"] or not row["all_camera_extrinsics_json"]:
|
| 98 |
+
raise RuntimeError("empty all-camera pose/extrinsic field")
|
| 99 |
+
for agent_id in agent_ids:
|
| 100 |
+
if counts[agent_id] != FRAME_COUNT or frames[agent_id] != set(range(FRAME_COUNT)):
|
| 101 |
+
raise RuntimeError("agent {} has invalid CSV frame coverage".format(agent_id))
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def validate_record(record):
|
| 105 |
+
sequence = Path(record["source_sequence"])
|
| 106 |
+
metadata_path = sequence / "metadata" / "sequence.json"
|
| 107 |
+
csv_path = sequence / "annotations" / "frames.csv"
|
| 108 |
+
npz_path = sequence / "annotations" / "trajectory.npz"
|
| 109 |
+
videos_path = sequence / "videos" / "camera_videos.json"
|
| 110 |
+
for path in (metadata_path, csv_path, npz_path, videos_path):
|
| 111 |
+
if not path.is_file() or path.stat().st_size == 0:
|
| 112 |
+
raise RuntimeError("missing or empty {}".format(path))
|
| 113 |
+
metadata = json.loads(metadata_path.read_text())
|
| 114 |
+
if int(metadata.get("frames_per_sequence", 0)) != FRAME_COUNT:
|
| 115 |
+
raise RuntimeError("invalid metadata frame count")
|
| 116 |
+
agents = metadata.get("agents", [])
|
| 117 |
+
agent_ids = tuple(int(agent["agent_id"]) for agent in agents)
|
| 118 |
+
if agent_ids != (0, 1):
|
| 119 |
+
raise RuntimeError("unexpected agent ids {}".format(agent_ids))
|
| 120 |
+
cameras = metadata.get("cameras", [])
|
| 121 |
+
if tuple(camera.get("name") for camera in cameras) != CAMERAS:
|
| 122 |
+
raise RuntimeError("invalid camera rig")
|
| 123 |
+
for camera in cameras:
|
| 124 |
+
intrinsics = camera.get("intrinsics", {})
|
| 125 |
+
if int(intrinsics.get("width", 0)) != WIDTH or int(intrinsics.get("height", 0)) != HEIGHT:
|
| 126 |
+
raise RuntimeError("invalid camera intrinsics resolution")
|
| 127 |
+
if not all(key in intrinsics for key in ("fx", "fy", "cx", "cy", "fov")):
|
| 128 |
+
raise RuntimeError("incomplete camera intrinsics")
|
| 129 |
+
if not all(key in camera.get("transform", {}) for key in ("x", "y", "z", "roll", "pitch", "yaw")):
|
| 130 |
+
raise RuntimeError("incomplete camera transform")
|
| 131 |
+
replay = metadata.get("trajectory_replay")
|
| 132 |
+
if replay is None:
|
| 133 |
+
if record.get("quality_tier") != "legacy_kinematic":
|
| 134 |
+
raise RuntimeError("missing trajectory replay audit metadata")
|
| 135 |
+
replay_audit = "legacy_metadata_unavailable"
|
| 136 |
+
else:
|
| 137 |
+
if int(replay.get("captured_frames", 0)) != FRAME_COUNT or int(replay.get("collision_events", 0)) != 0:
|
| 138 |
+
raise RuntimeError("invalid replay frame or collision metadata")
|
| 139 |
+
replay_audit = "present_zero_collisions"
|
| 140 |
+
|
| 141 |
+
video_manifest = json.loads(videos_path.read_text())
|
| 142 |
+
streams = video_manifest.get("streams", [])
|
| 143 |
+
if len(streams) != len(agent_ids) * len(CAMERAS):
|
| 144 |
+
raise RuntimeError("invalid camera stream count")
|
| 145 |
+
expected_pairs = {("agent_{}".format(agent_id), camera) for agent_id in agent_ids for camera in CAMERAS}
|
| 146 |
+
actual_pairs = {(stream.get("agent"), stream.get("camera")) for stream in streams}
|
| 147 |
+
if actual_pairs != expected_pairs:
|
| 148 |
+
raise RuntimeError("invalid agent/camera stream set")
|
| 149 |
+
for stream in streams:
|
| 150 |
+
video = sequence / stream["path"]
|
| 151 |
+
if not video.is_file() or video.stat().st_size == 0:
|
| 152 |
+
raise RuntimeError("missing video {}".format(video))
|
| 153 |
+
probe = probe_video(video)
|
| 154 |
+
if int(probe.get("nb_frames", 0)) != FRAME_COUNT:
|
| 155 |
+
raise RuntimeError("{} has {} frames".format(video, probe.get("nb_frames")))
|
| 156 |
+
if int(probe["width"]) != WIDTH or int(probe["height"]) != HEIGHT:
|
| 157 |
+
raise RuntimeError("{} has invalid resolution".format(video))
|
| 158 |
+
validate_csv(csv_path, agent_ids)
|
| 159 |
+
validate_npz(npz_path, agent_ids)
|
| 160 |
+
return {
|
| 161 |
+
"package_id": record["package_id"],
|
| 162 |
+
"trajectory_index": record["trajectory_index"],
|
| 163 |
+
"weather": record["weather"],
|
| 164 |
+
"quality_tier": record["quality_tier"],
|
| 165 |
+
"video_streams": len(streams),
|
| 166 |
+
"frames_per_stream": FRAME_COUNT,
|
| 167 |
+
"replay_audit": replay_audit,
|
| 168 |
+
"status": "valid",
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def main():
|
| 173 |
+
args = parse_args()
|
| 174 |
+
manifest_path = Path(args.selection_manifest).resolve()
|
| 175 |
+
records = json.loads(manifest_path.read_text())
|
| 176 |
+
results = []
|
| 177 |
+
errors = []
|
| 178 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=max(1, args.workers)) as pool:
|
| 179 |
+
futures = {pool.submit(validate_record, record): record for record in records}
|
| 180 |
+
for index, future in enumerate(concurrent.futures.as_completed(futures), 1):
|
| 181 |
+
record = futures[future]
|
| 182 |
+
try:
|
| 183 |
+
results.append(future.result())
|
| 184 |
+
except Exception as exc:
|
| 185 |
+
errors.append({"package_id": record.get("package_id"), "error": str(exc)})
|
| 186 |
+
if index % 25 == 0 or index == len(records):
|
| 187 |
+
print("validated {}/{} errors={}".format(index, len(records), len(errors)), flush=True)
|
| 188 |
+
report = {
|
| 189 |
+
"created_at": datetime.now(timezone.utc).isoformat(),
|
| 190 |
+
"selection_manifest": str(manifest_path),
|
| 191 |
+
"sequence_count": len(records),
|
| 192 |
+
"valid_count": len(results),
|
| 193 |
+
"error_count": len(errors),
|
| 194 |
+
"video_stream_count": sum(result["video_streams"] for result in results),
|
| 195 |
+
"decoded_frame_count": sum(result["video_streams"] * result["frames_per_stream"] for result in results),
|
| 196 |
+
"quality_tiers": dict(Counter(result["quality_tier"] for result in results)),
|
| 197 |
+
"replay_audit": dict(Counter(result["replay_audit"] for result in results)),
|
| 198 |
+
"checks": {
|
| 199 |
+
"agents_per_sequence": 2,
|
| 200 |
+
"cameras_per_agent": list(CAMERAS),
|
| 201 |
+
"frames_per_camera": FRAME_COUNT,
|
| 202 |
+
"resolution": [WIDTH, HEIGHT],
|
| 203 |
+
"zero_recorded_collisions_for_audited_renders": True,
|
| 204 |
+
"legacy_renders_without_replay_collision_audit": sum(
|
| 205 |
+
result["replay_audit"] == "legacy_metadata_unavailable" for result in results
|
| 206 |
+
),
|
| 207 |
+
"camera_intrinsics_and_mounts": True,
|
| 208 |
+
"per_frame_camera_poses_and_extrinsics": True,
|
| 209 |
+
"vehicle_pose_velocity_acceleration_controls": True,
|
| 210 |
+
"visibility_and_bounding_boxes": True,
|
| 211 |
+
},
|
| 212 |
+
"errors": errors,
|
| 213 |
+
}
|
| 214 |
+
report_path = Path(args.report).resolve()
|
| 215 |
+
report_path.parent.mkdir(parents=True, exist_ok=True)
|
| 216 |
+
report_path.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n")
|
| 217 |
+
print(json.dumps(report, indent=2, sort_keys=True))
|
| 218 |
+
if errors:
|
| 219 |
+
raise SystemExit("delivery verification failed")
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
if __name__ == "__main__":
|
| 223 |
+
main()
|