File size: 4,740 Bytes
2230fab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""Build the samples of centerpoint-p150 from the research data (provenance in samples/README.md and
staging_samples_pandaset/README.md).

    python -I code/scripts/make_samples.py --research /home/ubuntu/experiments/tt-models/research/centerpoint \
        --autoware /home/ubuntu/experiments/tt-models/upstream/autoware_universe

- ``samples/test_pcd.npz`` (shipped): autoware_universe
  ``perception/autoware_ground_segmentation/test/data/test.pcd`` (Apache-2.0):
  the non-zero returns of the organized 64 x 1024 ASCII cloud moved from ``velodyne_top`` to base_link by the
  translation (0.6, 0, 2.0) (``test_scan_ground_filter.cpp:141-153``) with Autoware's float32 sweep arithmetic,
  fields x, y, z, intensity: the input of the research reference (``centerpoint_ref.py --tf 0.6,0,2.0,0,0,0``).
- ``staging_samples_pandaset/pandaset_019_f40.npz`` (NOT shipped: PandaSet-derived samples wait for the user's OK,
  research/DATASETS.md N5; the directory is git-ignored and outside code/):
  ``research/centerpoint/public_data/sample_ship/pandaset_019_f40/points_xyzt.npy`` (PandaSet 019 frames 40 + 39 in
  Autoware's densified base_link contract) as a structured array with fields x, y, z, time_lag (float32, values
  unchanged; sha256 of the (N, 4) array data checked against meta.json); ``PANDASET_LICENSE.txt`` is the verbatim
  PandaSet Dataset Terms next to it. ``--pandaset-out`` writes it elsewhere (``code/tt_centerpoint/samples`` once
  the user approves; see the staging README for the other swap-in steps).

numpy only; reads data, executes nothing from it.
"""
import argparse
import hashlib
import json
import shutil
import sys
from pathlib import Path

import numpy as np

HERE = Path(__file__).resolve().parent
SAMPLES = HERE.parent / "tt_centerpoint" / "samples"
PANDASET_STAGING = HERE.parents[1] / "staging_samples_pandaset"      # git-ignored, never staged by tt-model


def structured(cols, names):
    a = np.zeros(len(cols[0]), dtype=[(n, "<f4") for n in names])
    for c, n in zip(cols, names):
        a[n] = c
    return a


def pandaset(research: Path, out: Path) -> None:
    src = research / "public_data" / "sample_ship" / "pandaset_019_f40"
    pts = np.load(src / "points_xyzt.npy", allow_pickle=False)
    meta = json.loads((src / "meta.json").read_text())
    digest = hashlib.sha256(np.ascontiguousarray(pts).tobytes()).hexdigest()
    if digest != meta["sha256_npy_data"]:
        sys.exit(f"pandaset: array sha256 {digest} != meta {meta['sha256_npy_data']}")
    out.mkdir(parents=True, exist_ok=True)
    np.savez_compressed(out / "pandaset_019_f40.npz",
                        points=structured([pts[:, i] for i in range(4)], ("x", "y", "z", "time_lag")))
    shutil.copyfile(src / "LICENSE.txt", out / "PANDASET_LICENSE.txt")
    print(f"{out / 'pandaset_019_f40.npz'}: {len(pts)} points, data sha256 {digest}")


def test_pcd(autoware: Path) -> None:
    path = autoware / "perception" / "autoware_ground_segmentation" / "test" / "data" / "test.pcd"
    lines = path.read_text().split("\n")
    end = next(i for i, l in enumerate(lines) if l.startswith("DATA"))
    fields = next(l for l in lines[:end] if l.startswith("FIELDS")).split()[1:]
    a = np.array([[float(v) for v in l.split()] for l in lines[end + 1:] if l.strip()], dtype=np.float32)
    col = {f: a[:, i] for i, f in enumerate(fields)}
    xyz = np.stack([col["x"], col["y"], col["z"]], 1)
    keep = np.abs(xyz).sum(1) > 0                       # organized-cloud (0, 0, 0) fillers
    m = np.eye(4, dtype=np.float32)
    m[:3, 3] = (0.6, 0.0, 2.0)                           # base_link <- velodyne_top, identity rotation
    x, y, z = xyz[keep, 0], xyz[keep, 1], xyz[keep, 2]
    out = [((m[r, 0] * x + m[r, 1] * y) + m[r, 2] * z) + m[r, 3] for r in range(3)]
    np.savez_compressed(SAMPLES / "test_pcd.npz",
                        points=structured(out + [col["intensity"][keep]], ("x", "y", "z", "intensity")))
    print(f"test_pcd.npz: {int(keep.sum())} of {len(xyz)} rows, sha256 of test.pcd "
          f"{hashlib.sha256(path.read_bytes()).hexdigest()}")


def main() -> int:
    ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
    ap.add_argument("--research", type=Path, required=True)
    ap.add_argument("--autoware", type=Path, required=True)
    ap.add_argument("--pandaset-out", type=Path, default=PANDASET_STAGING,
                    help="where the PandaSet sample goes (default: the git-ignored staging directory, not shipped)")
    a = ap.parse_args()
    SAMPLES.mkdir(parents=True, exist_ok=True)
    pandaset(a.research, a.pandaset_out)
    test_pcd(a.autoware)
    return 0


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