Download OVOW/benchmark/tests/test_big_eval_helpers.py from AIGeeksGroup/PhysicalWM: direct link, hf CLI and curl.
- Browser
- Download file 9.98 kB
-
https://huggingface.co/datasets/AIGeeksGroup/PhysicalWM/resolve/main/OVOW/benchmark/tests/test_big_eval_helpers.py
- Command line
-
hf download hf://datasets/AIGeeksGroup/PhysicalWM/OVOW/benchmark/tests/test_big_eval_helpers.py
-
curl -L -o test_big_eval_helpers.py https://huggingface.co/datasets/AIGeeksGroup/PhysicalWM/resolve/main/OVOW/benchmark/tests/test_big_eval_helpers.py
9.98 kB
| from __future__ import annotations | |
| import importlib.util | |
| import json | |
| import sys | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| from types import SimpleNamespace | |
| import numpy as np | |
| PROJECT_ROOT = Path(__file__).resolve().parents[1] | |
| def _load_script_module(): | |
| script_path = PROJECT_ROOT / "scripts" / "run_big_eval.py" | |
| spec = importlib.util.spec_from_file_location("run_big_eval", script_path) | |
| if spec is None or spec.loader is None: | |
| raise RuntimeError("Unable to load run_big_eval module") | |
| mod = importlib.util.module_from_spec(spec) | |
| sys.modules[spec.name] = mod | |
| spec.loader.exec_module(mod) | |
| return mod | |
| def _mesh(vertices): | |
| return SimpleNamespace(vertices=np.asarray(vertices, dtype=np.float64), faces=np.zeros((0, 3), dtype=np.int64)) | |
| def _box(lo, hi): | |
| x0, y0, z0 = lo | |
| x1, y1, z1 = hi | |
| return np.array( | |
| [ | |
| [x0, y0, z0], | |
| [x1, y0, z0], | |
| [x0, y1, z0], | |
| [x1, y1, z0], | |
| [x0, y0, z1], | |
| [x1, y0, z1], | |
| [x0, y1, z1], | |
| [x1, y1, z1], | |
| ], | |
| dtype=np.float64, | |
| ) | |
| class FakeBBoxMod: | |
| def _to_mesh_map(raw): | |
| return raw | |
| def _stack_vertices(mesh_map): | |
| if not mesh_map: | |
| return np.zeros((0, 3), dtype=np.float64) | |
| return np.concatenate([m.vertices for m in mesh_map.values()], axis=0) | |
| def _aabb_from_vertices(vertices): | |
| if vertices.size == 0: | |
| return None | |
| return SimpleNamespace( | |
| min_bound=np.min(vertices, axis=0), | |
| max_bound=np.max(vertices, axis=0), | |
| volume=float(np.prod(np.max(vertices, axis=0) - np.min(vertices, axis=0))), | |
| ) | |
| def _aabb_iou(a, b): | |
| if a is None and b is None: | |
| return 1.0 | |
| if a is None or b is None: | |
| return 0.0 | |
| inter_min = np.maximum(a.min_bound, b.min_bound) | |
| inter_max = np.minimum(a.max_bound, b.max_bound) | |
| inter_ext = np.maximum(inter_max - inter_min, 0.0) | |
| inter = float(np.prod(inter_ext)) | |
| union = a.volume + b.volume - inter | |
| return inter / union if union > 0.0 else 1.0 | |
| def _obb_from_vertices(vertices): | |
| return FakeBBoxMod._aabb_from_vertices(vertices) | |
| def _obb_iou(a, b): | |
| return FakeBBoxMod._aabb_iou(a, b) | |
| class TestBigEvalHelpers(unittest.TestCase): | |
| def test_asset_loader_prefers_scene_glb_when_no_blend(self) -> None: | |
| mod = _load_script_module() | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) | |
| sample = root / "sam3d" / "s1" | |
| frames = sample / "selected_frames" | |
| frames.mkdir(parents=True) | |
| (sample / "scene.glb").write_bytes(b"glb") | |
| (sample / "description.txt").write_text("scene", encoding="utf-8") | |
| (frames / "000.png").write_bytes(b"png") | |
| assets = mod.load_assets( | |
| root, | |
| "sam3d", | |
| "s1", | |
| image_glob="selected_frames/*,scene.png", | |
| scene_filename=None, | |
| ) | |
| self.assertEqual(assets.scene, (sample / "scene.glb").resolve()) | |
| self.assertEqual(assets.images, ((frames / "000.png").resolve(),)) | |
| self.assertEqual(assets.text, (sample / "description.txt").resolve()) | |
| def test_bbox_alignment_maps_pred_center_to_gt_center(self) -> None: | |
| mod = _load_script_module() | |
| gt_payload = { | |
| "objects_per_frame": [{"gt": _mesh(_box([0, 0, 0], [2, 2, 2]))}], | |
| } | |
| pred_payload = { | |
| "objects_per_frame": [{"pred": _mesh(_box([10, 0, 0], [11, 1, 1]))}], | |
| } | |
| transform = mod.build_alignment_transform( | |
| gt_payload, | |
| pred_payload, | |
| gt_depth=None, | |
| pred_depth=None, | |
| mode="bbox", | |
| bbox_mod=FakeBBoxMod, | |
| ) | |
| matrix = np.asarray(transform["matrix"], dtype=np.float64) | |
| pred_center = np.array([[10.5, 0.5, 0.5]], dtype=np.float64) | |
| mapped = mod._transform_vertices(pred_center, matrix)[0] | |
| np.testing.assert_allclose(mapped, np.array([1.0, 1.0, 1.0]), atol=1e-7) | |
| self.assertAlmostEqual(transform["scale"], 2.0, places=7) | |
| def test_object_bbox_alignment_anchors_best_object_not_scene(self) -> None: | |
| mod = _load_script_module() | |
| gt_payload = { | |
| "objects_per_frame": [ | |
| { | |
| "armchair": _mesh(_box([0, 0, 0], [2, 2, 2])), | |
| "far_object": _mesh(_box([20, 0, 0], [21, 1, 1])), | |
| } | |
| ], | |
| } | |
| pred_payload = { | |
| "objects_per_frame": [{"object_1_armchair": _mesh(_box([10, 0, 0], [11, 1, 1]))}], | |
| } | |
| transform = mod.build_alignment_transform( | |
| gt_payload, | |
| pred_payload, | |
| gt_depth=None, | |
| pred_depth=None, | |
| mode="object_bbox", | |
| bbox_mod=FakeBBoxMod, | |
| ) | |
| matrix = np.asarray(transform["matrix"], dtype=np.float64) | |
| pred_center = np.array([[10.5, 0.5, 0.5]], dtype=np.float64) | |
| mapped = mod._transform_vertices(pred_center, matrix)[0] | |
| np.testing.assert_allclose(mapped, np.array([1.0, 1.0, 1.0]), atol=1e-7) | |
| self.assertAlmostEqual(transform["scale"], 2.0, places=7) | |
| self.assertEqual(transform["scale_source"], "best_object_bbox_diag") | |
| self.assertEqual(transform["alignment_anchor"]["gt_name"], "armchair") | |
| self.assertEqual(transform["alignment_anchor"]["pred_name"], "object_1_armchair") | |
| def test_object_matching_uses_iou_not_name(self) -> None: | |
| mod = _load_script_module() | |
| gt_map = {"chair": _mesh(_box([0, 0, 0], [1, 1, 1]))} | |
| pred_map = {"object_0007": _mesh(_box([0, 0, 0], [1, 1, 1]))} | |
| result = mod._match_objects_by_iou(FakeBBoxMod, gt_map, pred_map) | |
| self.assertAlmostEqual(result["aabb_penalized"], 1.0, places=7) | |
| self.assertEqual(result["matches"][0]["gt_name"], "chair") | |
| self.assertEqual(result["matches"][0]["pred_name"], "object_0007") | |
| def test_depth_camera_alignment_canonicalizes_first_frame_rotation_and_scale(self) -> None: | |
| mod = _load_script_module() | |
| theta = np.pi / 2.0 | |
| rz = np.array( | |
| [ | |
| [np.cos(theta), -np.sin(theta), 0.0], | |
| [np.sin(theta), np.cos(theta), 0.0], | |
| [0.0, 0.0, 1.0], | |
| ], | |
| dtype=np.float64, | |
| ) | |
| gt_w2c = np.eye(4, dtype=np.float64) | |
| gt_w2c[:3, :3] = rz | |
| pred_w2c = np.eye(4, dtype=np.float64) | |
| gt_canonical = _box([1, 0, 0], [3, 1, 1]) | |
| gt_world = gt_canonical @ rz | |
| pred_world = gt_canonical * 0.5 | |
| gt_payload = { | |
| "frame_indices": [0], | |
| "objects_per_frame": [{"gt": _mesh(gt_world)}], | |
| } | |
| pred_payload = { | |
| "frame_indices": [0], | |
| "objects_per_frame": [{"pred": _mesh(pred_world)}], | |
| } | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) | |
| gt_depth = root / "gt_depth.npy" | |
| pred_depth = root / "pred_depth.npy" | |
| gt_camera = root / "gt_camera.json" | |
| pred_camera = root / "pred_camera.json" | |
| np.save(gt_depth, np.full((4, 4), 2.0, dtype=np.float32)) | |
| np.save(pred_depth, np.full((4, 4), 1.0, dtype=np.float32)) | |
| gt_camera.write_text(json.dumps({"world_to_camera": gt_w2c.tolist()}), encoding="utf-8") | |
| pred_camera.write_text(json.dumps({"world_to_camera": pred_w2c.tolist()}), encoding="utf-8") | |
| transform = mod.build_alignment_transform( | |
| gt_payload, | |
| pred_payload, | |
| gt_depth=gt_depth, | |
| pred_depth=pred_depth, | |
| gt_camera=gt_camera, | |
| pred_camera=pred_camera, | |
| mode="depth_camera", | |
| bbox_mod=FakeBBoxMod, | |
| ) | |
| result = mod.ioub_aligned(FakeBBoxMod, gt_payload, pred_payload, transform) | |
| self.assertEqual(transform["scale_source"], "depth_median_ratio") | |
| self.assertAlmostEqual(transform["scale"], 2.0, places=7) | |
| self.assertAlmostEqual(result["scene_aabb_iou_mean"], 1.0, places=7) | |
| self.assertAlmostEqual(result["object_aabb_iou_penalized_mean"], 1.0, places=7) | |
| def test_scene_meta_cache_populates_missing_camera_and_depth(self) -> None: | |
| mod = _load_script_module() | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) | |
| base = root / "gt" / "s1" | |
| base.mkdir(parents=True) | |
| scene = base / "scene.glb" | |
| scene.write_bytes(b"glb") | |
| meta_root = root / "meta" | |
| asset = mod.AssetBundle( | |
| key="gt", | |
| base=base, | |
| scene=scene, | |
| images=(), | |
| text=None, | |
| depth=None, | |
| camera=None, | |
| generated_meta=None, | |
| ) | |
| out_dir = mod._scene_meta_cache_dir(meta_root, asset) | |
| out_dir.mkdir(parents=True) | |
| (out_dir / "camera.json").write_text( | |
| json.dumps({"world_to_camera": np.eye(4).tolist()}), | |
| encoding="utf-8", | |
| ) | |
| np.save(out_dir / "depth.npy", np.ones((4, 4), dtype=np.float32)) | |
| populated = mod.ensure_scene_meta( | |
| asset, | |
| project_root=PROJECT_ROOT, | |
| blender_bin="unused", | |
| scene_meta_dir=meta_root, | |
| ) | |
| self.assertEqual(populated.camera, out_dir / "camera.json") | |
| self.assertEqual(populated.depth, out_dir / "depth.npy") | |
| self.assertEqual(populated.generated_meta, out_dir) | |
| if __name__ == "__main__": | |
| unittest.main() | |