File size: 9,771 Bytes
26c7c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
"""
verify_pack.py — post-install verification for the OOD-PerceptionBench asset pack v0.9.

WHY THIS EXISTS
---------------
A missing CARLA asset does not crash anything. If a blueprint ID is absent from the
library, the benchmark harness substitutes a *different* actor and the route still
finishes with a plausible Driving Score. Nothing in the result JSON says the prop was
wrong. This script is the only cheap thing standing between a botched install and a
silently meaningless number.

WHAT IT CHECKS
--------------
1. Each of the six shipped blueprint IDs is registered.
2. Each one actually SPAWNS. This is the load-bearing check: the four walkers are
   registered by the cooked WalkerFactory, which the pack overwrites. A WalkerFactory
   entry whose content directory is missing still shows up in the blueprint library but
   returns None from try_spawn_actor. Registration alone therefore proves nothing.
3. The spawned actor's type_id is the one asked for.
4. The bounding box matches the recorded reference within tolerance, which catches a
   blueprint ID that resolved to the wrong mesh.
5. Every blueprint ID the shipped WalkerFactory registers WITHOUT shipping its content
   ("phantoms") is reported, and asserted to be non-spawnable. These are not usable and
   must never appear in a route you intend to score.

USAGE
-----
    # with a CARLA server already running
    python3 verify_pack.py --host 127.0.0.1 --port 2000

Exit code 0 = pack is installed correctly. Non-zero = do not trust any results.
"""
from __future__ import annotations

import argparse
import json
import sys

try:
    import carla
except ImportError:  # pragma: no cover
    sys.exit("ERROR: the 'carla' python module is not importable. Install the "
             "PythonAPI wheel/egg shipped with your CARLA 0.9.15 build first.")

# ---------------------------------------------------------------------------
# The six assets this pack ships. bbox_extent is the half-extent (x, y, z) in
# metres reported by actor.bounding_box.extent, measured on CARLA 0.9.15.
# ---------------------------------------------------------------------------
SHIPPED = {
    "walker.pedestrian.astronaut":        {"group": "walkers-ccby",   "extent": None},
    "walker.pedestrian.deliveryrobot":    {"group": "walkers-ccby",   "extent": None},
    "walker.pedestrian.boar":             {"group": "walkers-ccby",   "extent": None},
    "walker.pedestrian.firefighter":      {"group": "walkers-ccbync", "extent": None},
    "static.prop.concreteroadbarrier":    {"group": "props",          "extent": None},
    "static.prop.roadclosedbarricade":    {"group": "props",          "extent": None},
}

# Blueprint IDs the shipped WalkerFactory registers but whose cooked content is NOT in
# this pack. They appear in the blueprint library and are NOT usable. See README.md
# "Phantom blueprint IDs".
PHANTOMS = [
    "walker.pedestrian.soldier",       # not redistributable (see ASSETS.tsv)
    "walker.pedestrian.wheelchair",    # not redistributable (see ASSETS.tsv)
    "walker.pedestrian.ball",          # unrelated experiment, not part of the benchmark
    "walker.pedestrian.caneman",
    "walker.pedestrian.cow",
    "walker.pedestrian.crutcheswoman",
    "walker.pedestrian.deer",
    "walker.pedestrian.labrador",
    "walker.pedestrian.tire",
]

# Blueprint IDs the benchmark's route XMLs reference that this pack does NOT provide and
# does NOT register. These fail LOUDLY-ish (absent from the library) but the harness will
# substitute a default actor, so the routes using them are simply not runnable at v0.9.
NOT_SHIPPED_AT_ALL = [
    "static.prop.roadclosedsign",
    "static.prop.trafficarrowboard",
    "static.prop.trafficmessageboard",
    "static.prop.europianarrowboardtrailer",
    "vehicle.ood.sedan", "vehicle.ood.hatchback", "vehicle.ood.suv",
    "vehicle.ood.armoredvan", "vehicle.ood.dumptruck", "vehicle.ood.roadroller",
]

TOL = 0.05  # metres, on each half-extent


def main() -> int:
    ap = argparse.ArgumentParser(description=__doc__,
                                 formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("--host", default="127.0.0.1")
    ap.add_argument("--port", type=int, default=2000)
    ap.add_argument("--timeout", type=float, default=120.0)
    ap.add_argument("--goldens", default=None,
                    help="path to goldens.json (default: alongside this script)")
    ap.add_argument("--emit-goldens", action="store_true",
                    help="write the measured extents to --goldens instead of checking")
    ap.add_argument("--without-nc", action="store_true",
                    help="you deliberately did not install the CC BY-NC tarball; treat "
                         "walker.pedestrian.firefighter as an expected phantom instead of "
                         "a required asset")
    a = ap.parse_args()

    shipped = dict(SHIPPED)
    phantoms = list(PHANTOMS)
    if a.without_nc:
        shipped.pop("walker.pedestrian.firefighter", None)
        phantoms.insert(0, "walker.pedestrian.firefighter")
        print("NOTE: --without-nc — the CC BY-NC firefighter is treated as not installed. "
              "18 pedestrian routes are unrunnable in this configuration.")

    import os
    goldens_path = a.goldens or os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                             "goldens.json")
    goldens = {}
    if not a.emit_goldens and os.path.exists(goldens_path):
        with open(goldens_path) as fh:
            goldens = json.load(fh)

    client = carla.Client(a.host, a.port)
    client.set_timeout(a.timeout)
    print(f"CARLA server version: {client.get_server_version()}")
    world = client.get_world()
    lib = world.get_blueprint_library()
    spawn_points = world.get_map().get_spawn_points()
    if not spawn_points:
        return _fail("map has no spawn points; load a town map before verifying")

    failures: list[str] = []
    measured: dict[str, list[float]] = {}

    print("\n--- shipped assets " + "-" * 52)
    for i, bp_id in enumerate(sorted(shipped)):
        found = [b for b in lib.filter(bp_id) if b.id == bp_id]
        if not found:
            failures.append(f"{bp_id}: NOT REGISTERED — content dir or WalkerFactory missing")
            print(f"  FAIL  {bp_id}: not in blueprint library")
            continue
        tf = spawn_points[(i * 7) % len(spawn_points)]
        tf.location.z += 1.0
        actor = world.try_spawn_actor(found[0], tf)
        if actor is None:
            failures.append(f"{bp_id}: registered but FAILED TO SPAWN — the blueprint ID "
                            f"exists (WalkerFactory) but its cooked content is missing")
            print(f"  FAIL  {bp_id}: registered but spawn returned None")
            continue
        try:
            if actor.type_id != bp_id:
                failures.append(f"{bp_id}: spawned a different actor ({actor.type_id})")
                print(f"  FAIL  {bp_id}: spawned {actor.type_id}")
                continue
            e = actor.bounding_box.extent
            ext = [round(e.x, 3), round(e.y, 3), round(e.z, 3)]
            measured[bp_id] = ext
            g = goldens.get(bp_id)
            if g is None:
                print(f"  OK    {bp_id}  extent={ext}  (no golden recorded)")
            elif max(abs(x - y) for x, y in zip(ext, g)) > TOL:
                failures.append(f"{bp_id}: bounding box {ext} != reference {g} "
                                f"(tolerance {TOL} m) — wrong mesh?")
                print(f"  FAIL  {bp_id}  extent={ext} expected {g}")
            else:
                print(f"  OK    {bp_id}  extent={ext}")
        finally:
            actor.destroy()

    print("\n--- phantom IDs (registered, content NOT shipped) " + "-" * 21)
    for bp_id in phantoms:
        found = [b for b in lib.filter(bp_id) if b.id == bp_id]
        if not found:
            print(f"  note  {bp_id}: not registered (fine — your WalkerFactory differs)")
            continue
        tf = spawn_points[3]
        tf.location.z += 1.0
        actor = world.try_spawn_actor(found[0], tf)
        if actor is None:
            print(f"  OK    {bp_id}: registered but unusable, as expected")
        else:
            actor.destroy()
            failures.append(f"{bp_id}: expected to be unusable but it SPAWNED — your "
                            f"install mixes this pack with other content; results using "
                            f"it are not comparable to the published baselines")
            print(f"  FAIL  {bp_id}: unexpectedly spawnable")

    print("\n--- not shipped at v0.9 (routes using these are not runnable) " + "-" * 9)
    for bp_id in NOT_SHIPPED_AT_ALL:
        present = any(b.id == bp_id for b in lib.filter(bp_id))
        print(f"  {'PRESENT (unexpected)' if present else 'absent (expected)':<22} {bp_id}")

    if a.emit_goldens:
        with open(goldens_path, "w") as fh:
            json.dump(measured, fh, indent=2, sort_keys=True)
            fh.write("\n")
        print(f"\nwrote goldens -> {goldens_path}")
        return 0

    print()
    if failures:
        print(f"VERIFY FAILED — {len(failures)} problem(s):")
        for f in failures:
            print(f"  * {f}")
        print("\nDo not run the benchmark until these are resolved: a missing prop does "
              "not fail the route, it silently changes what was measured.")
        return 1
    print("VERIFY OK — all six shipped assets registered, spawned and matched reference "
          "dimensions; no phantom ID is spawnable.")
    return 0


def _fail(msg: str) -> int:
    print(f"ERROR: {msg}", file=sys.stderr)
    return 2


if __name__ == "__main__":
    sys.exit(main())