KITTI-C2L-Dataset / code /tests /test_loader.py
mcocheteux's picture
Release KITTI-C2L v1.0.0: documented self-contained benchmark, loader and baselines
db613b6 verified
Raw History Blame Contribute Delete
6.37 kB
from __future__ import annotations
from pathlib import Path
import numpy as np
import pytest
from kitti_c2l.assets import generate_sequence_assets
from kitti_c2l.generate import generate_sequence_rows
from kitti_c2l.loader.dataset import (
materialize_sample,
materialize_sample_from_assets,
materialize_window_frame,
materialize_window_frame_from_assets,
)
from kitti_c2l.loader.projection import rasterize_inverse_depth
from kitti_c2l.sequence_mapping import SequenceInfo
from kitti_c2l.stages import STAGES
from kitti_c2l.windows import generate_sequence_windows
from kitti_c2l.writer import asset_rows_to_table, rows_to_table, windows_to_table
def test_rasterize_inverse_depth_near_point_wins_collision():
pixels = np.array([[5.0, 5.0], [5.0, 5.0]])
depths = np.array([1000.0, 500.0]) # far, then near -- near must win the pixel
depth_map = rasterize_inverse_depth(pixels, depths, image_size=(10, 10))
assert depth_map.shape == (1, 10, 10)
assert depth_map[0, 5, 5] == np.float32(1.0 / 500.0)
def test_rasterize_inverse_depth_clips_out_of_range():
pixels = np.array([[1.0, 1.0], [2.0, 2.0]])
depths = np.array([50.0, 9000.0]) # both outside [200, 8000] cm
depth_map = rasterize_inverse_depth(pixels, depths, image_size=(10, 10))
assert np.all(depth_map == 0.0)
def test_rasterize_inverse_depth_clips_out_of_bounds_pixels():
pixels = np.array([[-1.0, 5.0], [5.0, 100.0]])
depths = np.array([500.0, 500.0])
depth_map = rasterize_inverse_depth(pixels, depths, image_size=(10, 10))
assert np.all(depth_map == 0.0)
def _first_row_dict(fake_kitti_root: Path) -> dict:
seq_info = SequenceInfo(sequence="04", date="2011_09_30", drive="0016", start=0, end=3, frame_count=4, split="train")
rows = list(generate_sequence_rows(seq_info, fake_kitti_root, stages=STAGES[:1]))
table = rows_to_table(rows)
return table.to_pylist()[0]
def test_materialize_sample_shapes(fake_kitti_root: Path):
row = _first_row_dict(fake_kitti_root)
sample = materialize_sample(row, fake_kitti_root)
assert sample.image.shape == (32, 32, 3)
assert sample.image.dtype == np.uint8
assert sample.points.shape[1] == 3
assert sample.lidar_depth.shape == (1, 32, 32)
assert sample.rotation.shape == (3, 3)
assert sample.translation_cm.shape == (3,)
def test_materialize_sample_ground_truth_vs_miscalibrated_differ(fake_kitti_root: Path):
row = _first_row_dict(fake_kitti_root)
gt_sample = materialize_sample(row, fake_kitti_root, use_ground_truth=True)
miscal_sample = materialize_sample(row, fake_kitti_root, use_ground_truth=False)
assert not np.allclose(gt_sample.rotation, miscal_sample.rotation)
assert not np.allclose(gt_sample.translation_cm, miscal_sample.translation_cm)
def _first_asset_row_dict(fake_kitti_root: Path) -> dict:
seq_info = SequenceInfo(sequence="04", date="2011_09_30", drive="0016", start=0, end=3, frame_count=4, split="train")
rows = list(generate_sequence_assets(seq_info, fake_kitti_root))
table = asset_rows_to_table(rows)
return table.to_pylist()[0]
def test_materialize_sample_from_assets_matches_from_disk(fake_kitti_root: Path):
"""The materialized-assets path must produce byte-identical results to reading from disk."""
row = _first_row_dict(fake_kitti_root)
asset_row = _first_asset_row_dict(fake_kitti_root)
assert asset_row["frame_id"] == f"{row['sequence']}_{row['frame_index']:06d}"
from_disk = materialize_sample(row, fake_kitti_root)
from_assets = materialize_sample_from_assets(row, asset_row)
np.testing.assert_array_equal(from_disk.image, from_assets.image)
np.testing.assert_array_equal(from_disk.points, from_assets.points)
np.testing.assert_array_equal(from_disk.lidar_depth, from_assets.lidar_depth)
np.testing.assert_array_equal(from_disk.rotation, from_assets.rotation)
def _first_window_row_dict(fake_kitti_root: Path, window_length: int = 4) -> dict:
seq_info = SequenceInfo(sequence="04", date="2011_09_30", drive="0016", start=0, end=3, frame_count=4, split="train")
rows = list(generate_sequence_windows(seq_info, fake_kitti_root, window_length=window_length, stages=STAGES[:1]))
table = windows_to_table(rows)
return table.to_pylist()[0]
def _asset_row_dict_for_frame(fake_kitti_root: Path, frame_index: int) -> dict:
seq_info = SequenceInfo(sequence="04", date="2011_09_30", drive="0016", start=0, end=3, frame_count=4, split="train")
rows = list(generate_sequence_assets(seq_info, fake_kitti_root))
table = asset_rows_to_table(rows)
return next(r for r in table.to_pylist() if r["frame_index"] == frame_index)
def test_materialize_window_frame_shares_decalibration_across_offsets(fake_kitti_root: Path):
"""Every frame_offset within a window must use the SAME (miscalibrated) extrinsic."""
window_row = _first_window_row_dict(fake_kitti_root, window_length=4)
sample_0 = materialize_window_frame(window_row, frame_offset=0, kitti_raw_root=fake_kitti_root)
sample_1 = materialize_window_frame(window_row, frame_offset=1, kitti_raw_root=fake_kitti_root)
np.testing.assert_array_equal(sample_0.rotation, sample_1.rotation)
np.testing.assert_array_equal(sample_0.translation_cm, sample_1.translation_cm)
# But the actual image/points differ -- different frames, same calibration.
assert not np.array_equal(sample_0.image, sample_1.image)
def test_materialize_window_frame_out_of_range_raises(fake_kitti_root: Path):
window_row = _first_window_row_dict(fake_kitti_root, window_length=4)
with pytest.raises(IndexError):
materialize_window_frame(window_row, frame_offset=4, kitti_raw_root=fake_kitti_root)
def test_materialize_window_frame_from_assets_matches_from_disk(fake_kitti_root: Path):
window_row = _first_window_row_dict(fake_kitti_root, window_length=4)
asset_row = _asset_row_dict_for_frame(fake_kitti_root, frame_index=2)
from_disk = materialize_window_frame(window_row, frame_offset=2, kitti_raw_root=fake_kitti_root)
from_assets = materialize_window_frame_from_assets(window_row, asset_row)
np.testing.assert_array_equal(from_disk.image, from_assets.image)
np.testing.assert_array_equal(from_disk.points, from_assets.points)
np.testing.assert_array_equal(from_disk.rotation, from_assets.rotation)