File size: 3,664 Bytes
db613b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Verify the smallest uploaded train/test shards against local KITTI Raw bytes."""

from __future__ import annotations

import argparse
import io
import json
from pathlib import Path

import numpy as np
import pyarrow.parquet as pq
from huggingface_hub import hf_hub_download
from PIL import Image

from kitti_c2l.release import require, sha256, validate_hub_manifest


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[1])
    parser.add_argument("--raw-root", type=Path, required=True)
    parser.add_argument("--output", type=Path, required=True)
    args = parser.parse_args()
    if args.output.exists():
        raise FileExistsError(args.output)
    manifest = validate_hub_manifest(args.root)
    selected = [
        min(
            (
                item
                for item in manifest["shards"]
                if item["path"].startswith(f"data/frames_{split}-")
            ),
            key=lambda item: item["bytes"],
        )
        for split in ("train", "test")
    ]
    results = []
    for item in selected:
        path = Path(
            hf_hub_download(
                manifest["repo_id"],
                item["path"],
                repo_type="dataset",
                revision=manifest["revision"],
            )
        )
        require(sha256(path) == item["sha256"], f"shard checksum mismatch: {item['path']}")
        count = 0
        for batch in pq.ParquetFile(path).iter_batches(batch_size=16):
            for row in batch.to_pylist():
                drive = (
                    args.raw_root
                    / row["raw_date"]
                    / f"{row['raw_date']}_drive_{row['raw_drive']}_sync"
                )
                frame = f"{row['raw_frame_index']:010d}"
                require(
                    row["image_png"] == (drive / "image_02/data" / f"{frame}.png").read_bytes(),
                    "image source bytes mismatch",
                )
                require(
                    row["points_bin"]
                    == (drive / "velodyne_points/data" / f"{frame}.bin").read_bytes(),
                    "scan source bytes mismatch",
                )
                with Image.open(io.BytesIO(row["image_png"])) as image:
                    require(
                        image.size == (row["image_width"], row["image_height"]),
                        "image dimensions mismatch",
                    )
                    image.verify()
                points = np.frombuffer(row["points_bin"], dtype=np.float32).reshape(-1, 4)
                require(len(points) > 0 and np.isfinite(points).all(), "invalid scan values")
                count += 1
        results.append(
            {
                "path": item["path"],
                "sha256": item["sha256"],
                "bytes": item["bytes"],
                "verified_frame_payloads": count,
                "raw_source_bytes_and_decoding": "pass",
            }
        )
        print(f"{item['path']}: {count} image/scan pairs match local KITTI Raw", flush=True)
    report = {
        "repo_id": manifest["repo_id"],
        "revision": manifest["revision"],
        "status": "pass",
        "scope": "Smallest train and test shards downloaded in full, SHA256 verified, every sensor payload compared byte-for-byte to local KITTI Raw and decoded. Not every shard payload was downloaded.",
        "shards": results,
    }
    args.output.parent.mkdir(parents=True, exist_ok=True)
    args.output.write_text(json.dumps(report, indent=2) + "\n")


if __name__ == "__main__":
    main()