| 1 |
# Copyright (c), ETH Zurich and UNC Chapel Hill. |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# Redistribution and use in source and binary forms, with or without |
| 5 |
# modification, are permitted provided that the following conditions are met: |
| 6 |
# |
| 7 |
# * Redistributions of source code must retain the above copyright |
| 8 |
# notice, this list of conditions and the following disclaimer. |
| 9 |
# |
| 10 |
# * Redistributions in binary form must reproduce the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer in the |
| 12 |
# documentation and/or other materials provided with the distribution. |
| 13 |
# |
| 14 |
# * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of |
| 15 |
# its contributors may be used to endorse or promote products derived |
| 16 |
# from this software without specific prior written permission. |
| 17 |
# |
| 18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
# POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
import argparse |
| 31 |
from pathlib import Path |
| 32 |
|
| 33 |
import numpy as np |
| 34 |
import pytest |
| 35 |
|
| 36 |
import pycolmap |
| 37 |
|
| 38 |
from . import utils |
| 39 |
from .utils import ( |
| 40 |
OUTLIER_COMPONENT_ID, |
| 41 |
Metrics, |
| 42 |
SceneInfo, |
| 43 |
_parse_gpu_index, |
| 44 |
aggregate_scene_metrics, |
| 45 |
compute_abs_errors, |
| 46 |
compute_auc, |
| 47 |
compute_avg_metrics, |
| 48 |
compute_grouped_abs_errors, |
| 49 |
compute_grouped_rel_errors, |
| 50 |
compute_recall, |
| 51 |
diff_metrics, |
| 52 |
filter_smallest_scenes_per_category, |
| 53 |
get_scores, |
| 54 |
panorama_reconstruction, |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
def _make_scene_info(category: str, scene: str, num_images: int) -> SceneInfo: |
| 59 |
return SceneInfo( |
| 60 |
dataset="dummy", |
| 61 |
category=category, |
| 62 |
scene=scene, |
| 63 |
num_images=num_images, |
| 64 |
workspace_path=Path("/tmp/workspace"), |
| 65 |
image_path=Path("/tmp/images"), |
| 66 |
sparse_gt_path=Path("/tmp/sparse_gt"), |
| 67 |
has_camera_priors=False, |
| 68 |
colmap_extra_args=[], |
| 69 |
) |
| 70 |
|
| 71 |
|
| 72 |
def test_panorama_reconstruction_uses_library_api(tmp_path, monkeypatch): |
| 73 |
covisibility_path = tmp_path / "covisibility.npz" |
| 74 |
covisibility_path.touch() |
| 75 |
scene_info = SceneInfo( |
| 76 |
dataset="tartanair-v2-spherical", |
| 77 |
category="test", |
| 78 |
scene="scene", |
| 79 |
num_images=2, |
| 80 |
workspace_path=tmp_path / "workspace", |
| 81 |
image_path=tmp_path / "images", |
| 82 |
sparse_gt_path=tmp_path / "sparse_gt", |
| 83 |
has_camera_priors=False, |
| 84 |
colmap_extra_args=[], |
| 85 |
reconstruction_backend="panorama-spherical", |
| 86 |
covisibility_path=covisibility_path, |
| 87 |
covisibility_min_shared_points=1, |
| 88 |
) |
| 89 |
args = argparse.Namespace( |
| 90 |
overwrite_reconstruction=False, |
| 91 |
feature="sift", |
| 92 |
mapper="global", |
| 93 |
uncalibrated=False, |
| 94 |
filter_covisibility=True, |
| 95 |
covisibility_min_shared_points=5, |
| 96 |
random_seed=7, |
| 97 |
use_gpu=False, |
| 98 |
) |
| 99 |
call = {} |
| 100 |
|
| 101 |
def reconstruct(input_image_path, output_path, options): |
| 102 |
call.update( |
| 103 |
input_image_path=input_image_path, |
| 104 |
output_path=output_path, |
| 105 |
options=options, |
| 106 |
) |
| 107 |
return {} |
| 108 |
|
| 109 |
monkeypatch.setattr(utils.panorama, "reconstruct", reconstruct) |
| 110 |
panorama_reconstruction(args, scene_info, num_threads=3, gpu_index="2") |
| 111 |
|
| 112 |
assert call["input_image_path"] == scene_info.image_path |
| 113 |
assert call["output_path"] == scene_info.workspace_path |
| 114 |
options = call["options"] |
| 115 |
assert options.mapper.value == "global" |
| 116 |
assert options.render_type.value == "spherical" |
| 117 |
assert options.random_seed == 7 |
| 118 |
assert options.num_threads == 3 |
| 119 |
assert options.gpu_index == "2" |
| 120 |
assert not options.use_gpu |
| 121 |
assert options.covisibility_path == covisibility_path |
| 122 |
assert options.covisibility_min_shared_points == 1 |
| 123 |
assert not options.show_progress |
| 124 |
|
| 125 |
|
| 126 |
class TestFilterSmallestScenesPerCategory: |
| 127 |
def test_picks_smallest_per_category(self): |
| 128 |
scenes = [ |
| 129 |
_make_scene_info("a", "a3", 30), |
| 130 |
_make_scene_info("a", "a1", 10), |
| 131 |
_make_scene_info("a", "a2", 20), |
| 132 |
_make_scene_info("b", "b2", 5), |
| 133 |
_make_scene_info("b", "b1", 1), |
| 134 |
] |
| 135 |
result = filter_smallest_scenes_per_category(scenes, num_scenes=2) |
| 136 |
names = [(s.category, s.scene) for s in result] |
| 137 |
assert names == [("a", "a1"), ("a", "a2"), ("b", "b2"), ("b", "b1")] |
| 138 |
|
| 139 |
def test_preserves_input_order(self): |
| 140 |
scenes = [ |
| 141 |
_make_scene_info("a", "a3", 30), |
| 142 |
_make_scene_info("a", "a1", 10), |
| 143 |
_make_scene_info("a", "a2", 20), |
| 144 |
] |
| 145 |
result = filter_smallest_scenes_per_category(scenes, num_scenes=2) |
| 146 |
# Smallest are a1 and a2, but the original order (a3, a1, a2) must |
| 147 |
# be preserved among the kept scenes. |
| 148 |
assert [s.scene for s in result] == ["a1", "a2"] |
| 149 |
|
| 150 |
def test_num_scenes_larger_than_category_size(self): |
| 151 |
scenes = [ |
| 152 |
_make_scene_info("a", "a1", 10), |
| 153 |
_make_scene_info("a", "a2", 20), |
| 154 |
_make_scene_info("b", "b1", 5), |
| 155 |
] |
| 156 |
result = filter_smallest_scenes_per_category(scenes, num_scenes=10) |
| 157 |
# All scenes are kept since each category has fewer than num_scenes. |
| 158 |
assert [s.scene for s in result] == ["a1", "a2", "b1"] |
| 159 |
|
| 160 |
def test_num_scenes_one(self): |
| 161 |
scenes = [ |
| 162 |
_make_scene_info("a", "a1", 10), |
| 163 |
_make_scene_info("a", "a2", 5), |
| 164 |
_make_scene_info("b", "b1", 100), |
| 165 |
_make_scene_info("b", "b2", 50), |
| 166 |
] |
| 167 |
result = filter_smallest_scenes_per_category(scenes, num_scenes=1) |
| 168 |
assert sorted((s.category, s.scene) for s in result) == [ |
| 169 |
("a", "a2"), |
| 170 |
("b", "b2"), |
| 171 |
] |
| 172 |
|
| 173 |
def test_empty_input(self): |
| 174 |
assert filter_smallest_scenes_per_category([], num_scenes=3) == [] |
| 175 |
|
| 176 |
def test_ties_broken_stably(self): |
| 177 |
# When several scenes share the same num_images, sorting must be |
| 178 |
# stable so we keep the ones that appeared first in the input. |
| 179 |
scenes = [ |
| 180 |
_make_scene_info("a", "a1", 10), |
| 181 |
_make_scene_info("a", "a2", 10), |
| 182 |
_make_scene_info("a", "a3", 10), |
| 183 |
] |
| 184 |
result = filter_smallest_scenes_per_category(scenes, num_scenes=2) |
| 185 |
assert [s.scene for s in result] == ["a1", "a2"] |
| 186 |
|
| 187 |
|
| 188 |
class TestParseGpuIndex: |
| 189 |
@staticmethod |
| 190 |
def _make_args(gpu_index: str) -> argparse.Namespace: |
| 191 |
return argparse.Namespace(gpu_index=gpu_index) |
| 192 |
|
| 193 |
def test_single_gpu(self): |
| 194 |
assert _parse_gpu_index(self._make_args("0")) == [0] |
| 195 |
|
| 196 |
def test_multiple_gpus(self): |
| 197 |
assert _parse_gpu_index(self._make_args("0,1,2")) == [0, 1, 2] |
| 198 |
|
| 199 |
def test_trailing_comma(self): |
| 200 |
assert _parse_gpu_index(self._make_args("1,")) == [1] |
| 201 |
|
| 202 |
def test_empty_string(self): |
| 203 |
assert _parse_gpu_index(self._make_args("")) == [-1] |
| 204 |
|
| 205 |
def test_only_commas(self): |
| 206 |
assert _parse_gpu_index(self._make_args(",")) == [-1] |
| 207 |
|
| 208 |
def test_auto_detect(self, monkeypatch): |
| 209 |
monkeypatch.setattr(pycolmap, "has_cuda", True) |
| 210 |
monkeypatch.setattr( |
| 211 |
pycolmap, "get_num_cuda_devices", lambda: 3, raising=False |
| 212 |
) |
| 213 |
assert _parse_gpu_index(self._make_args("-1")) == [0, 1, 2] |
| 214 |
|
| 215 |
def test_auto_detect_no_devices(self, monkeypatch): |
| 216 |
monkeypatch.setattr(pycolmap, "has_cuda", True) |
| 217 |
monkeypatch.setattr( |
| 218 |
pycolmap, "get_num_cuda_devices", lambda: 0, raising=False |
| 219 |
) |
| 220 |
assert _parse_gpu_index(self._make_args("-1")) == [-1] |
| 221 |
|
| 222 |
|
| 223 |
class TestComputeAuc: |
| 224 |
def test_simple_uniform_errors(self): |
| 225 |
errors = np.array([0.1, 0.2, 0.3, 0.4, 0.5]) |
| 226 |
thresholds = np.array([0.25, 0.5, 1.0]) |
| 227 |
aucs = compute_auc(errors, thresholds) |
| 228 |
np.testing.assert_almost_equal(aucs[0], 24.0, decimal=5) |
| 229 |
np.testing.assert_almost_equal(aucs[1], 50.0, decimal=5) |
| 230 |
np.testing.assert_almost_equal(aucs[2], 75.0, decimal=5) |
| 231 |
|
| 232 |
def test_all_errors_zero(self): |
| 233 |
errors = np.array([0.0, 0.0, 0.0]) |
| 234 |
thresholds = np.array([0.5, 1.0]) |
| 235 |
aucs = compute_auc(errors, thresholds) |
| 236 |
np.testing.assert_array_almost_equal(aucs, [100.0, 100.0]) |
| 237 |
|
| 238 |
def test_empty_errors(self): |
| 239 |
errors = np.array([]) |
| 240 |
thresholds = np.array([0.5, 1.0]) |
| 241 |
with pytest.raises(ValueError, match="No errors to evaluate"): |
| 242 |
compute_auc(errors, thresholds) |
| 243 |
|
| 244 |
def test_all_errors_above_threshold(self): |
| 245 |
errors = np.array([10.0, 20.0, 30.0]) |
| 246 |
thresholds = np.array([5.0]) |
| 247 |
aucs = compute_auc(errors, thresholds) |
| 248 |
np.testing.assert_almost_equal(aucs[0], 0.0) |
| 249 |
|
| 250 |
def test_all_errors_below_threshold(self): |
| 251 |
errors = np.array([0.1, 0.2, 0.3]) |
| 252 |
thresholds = np.array([1.0]) |
| 253 |
aucs = compute_auc(errors, thresholds) |
| 254 |
np.testing.assert_almost_equal(aucs[0], 85.0, decimal=5) |
| 255 |
|
| 256 |
def test_inf_errors(self): |
| 257 |
errors = np.array([0.1, 0.2, np.inf, np.inf]) |
| 258 |
thresholds = np.array([0.5, 1.0]) |
| 259 |
aucs = compute_auc(errors, thresholds) |
| 260 |
assert np.all(aucs >= 0) |
| 261 |
assert np.all(aucs <= 100) |
| 262 |
|
| 263 |
def test_single_error(self): |
| 264 |
errors = np.array([0.5]) |
| 265 |
thresholds = np.array([0.3, 1.0]) |
| 266 |
aucs = compute_auc(errors, thresholds) |
| 267 |
assert len(aucs) == 2 |
| 268 |
np.testing.assert_almost_equal(aucs[0], 0.0) |
| 269 |
np.testing.assert_almost_equal(aucs[1], 75.0) |
| 270 |
|
| 271 |
|
| 272 |
class TestComputeRecall: |
| 273 |
def test_basic_recall(self): |
| 274 |
errors = np.array([0.1, 0.2, 0.3, 0.4, 0.5]) |
| 275 |
thresholds = np.array([0.05, 0.25, 0.5, 1.0]) |
| 276 |
recalls = compute_recall(errors, thresholds) |
| 277 |
assert len(recalls) == 4 |
| 278 |
assert recalls[3] >= recalls[2] >= recalls[1] >= recalls[0] |
| 279 |
assert np.all(recalls >= 0) |
| 280 |
assert np.all(recalls <= 100) |
| 281 |
|
| 282 |
def test_empty_errors(self): |
| 283 |
errors = np.array([]) |
| 284 |
thresholds = np.array([0.5, 1.0]) |
| 285 |
with pytest.raises(ValueError, match="No errors to evaluate"): |
| 286 |
compute_recall(errors, thresholds) |
| 287 |
|
| 288 |
def test_all_errors_above_threshold(self): |
| 289 |
errors = np.array([10.0, 20.0, 30.0]) |
| 290 |
thresholds = np.array([5.0]) |
| 291 |
recalls = compute_recall(errors, thresholds) |
| 292 |
np.testing.assert_almost_equal(recalls[0], 0.0) |
| 293 |
|
| 294 |
def test_all_errors_below_threshold(self): |
| 295 |
errors = np.array([0.1, 0.2, 0.3]) |
| 296 |
thresholds = np.array([1.0]) |
| 297 |
recalls = compute_recall(errors, thresholds) |
| 298 |
np.testing.assert_almost_equal(recalls[0], 100.0) |
| 299 |
|
| 300 |
def test_exact_threshold(self): |
| 301 |
errors = np.array([0.1, 0.5, 0.9]) |
| 302 |
thresholds = np.array([0.5]) |
| 303 |
recalls = compute_recall(errors, thresholds) |
| 304 |
np.testing.assert_almost_equal(recalls[0], 200.0 / 3.0) |
| 305 |
|
| 306 |
def test_multiple_thresholds(self): |
| 307 |
errors = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) |
| 308 |
thresholds = np.array([2.0, 3.0, 4.0]) |
| 309 |
recalls = compute_recall(errors, thresholds) |
| 310 |
np.testing.assert_almost_equal(recalls[0], 40.0) |
| 311 |
np.testing.assert_almost_equal(recalls[1], 60.0) |
| 312 |
np.testing.assert_almost_equal(recalls[2], 80.0) |
| 313 |
|
| 314 |
|
| 315 |
class TestComputeAvgMetrics: |
| 316 |
def test_single_scene(self): |
| 317 |
scene_metrics = { |
| 318 |
"scene1": Metrics( |
| 319 |
aucs=np.array([10.0, 20.0, 30.0]), |
| 320 |
recalls=np.array([15.0, 25.0, 35.0]), |
| 321 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 322 |
error_type="relative_auc", |
| 323 |
num_images=100, |
| 324 |
num_reg_images=90, |
| 325 |
num_components=1, |
| 326 |
largest_component=90, |
| 327 |
) |
| 328 |
} |
| 329 |
aucs, recalls = compute_avg_metrics(scene_metrics) |
| 330 |
np.testing.assert_array_equal(aucs, [10.0, 20.0, 30.0]) |
| 331 |
np.testing.assert_array_equal(recalls, [15.0, 25.0, 35.0]) |
| 332 |
|
| 333 |
def test_multiple_scenes(self): |
| 334 |
scene_metrics = { |
| 335 |
"scene1": Metrics( |
| 336 |
aucs=np.array([10.0, 20.0, 30.0]), |
| 337 |
recalls=np.array([15.0, 25.0, 35.0]), |
| 338 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 339 |
error_type="relative_auc", |
| 340 |
num_images=100, |
| 341 |
num_reg_images=90, |
| 342 |
num_components=1, |
| 343 |
largest_component=90, |
| 344 |
), |
| 345 |
"scene2": Metrics( |
| 346 |
aucs=np.array([20.0, 30.0, 40.0]), |
| 347 |
recalls=np.array([25.0, 35.0, 45.0]), |
| 348 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 349 |
error_type="relative_auc", |
| 350 |
num_images=100, |
| 351 |
num_reg_images=90, |
| 352 |
num_components=1, |
| 353 |
largest_component=90, |
| 354 |
), |
| 355 |
} |
| 356 |
aucs, recalls = compute_avg_metrics(scene_metrics) |
| 357 |
np.testing.assert_array_equal(aucs, [15.0, 25.0, 35.0]) |
| 358 |
np.testing.assert_array_equal(recalls, [20.0, 30.0, 40.0]) |
| 359 |
|
| 360 |
def test_skip_special_scenes(self): |
| 361 |
scene_metrics = { |
| 362 |
"scene1": Metrics( |
| 363 |
aucs=np.array([10.0, 20.0, 30.0]), |
| 364 |
recalls=np.array([15.0, 25.0, 35.0]), |
| 365 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 366 |
error_type="relative_auc", |
| 367 |
num_images=100, |
| 368 |
num_reg_images=90, |
| 369 |
num_components=1, |
| 370 |
largest_component=90, |
| 371 |
), |
| 372 |
"__avg__": Metrics( |
| 373 |
aucs=np.array([50.0, 60.0, 70.0]), |
| 374 |
recalls=np.array([55.0, 65.0, 75.0]), |
| 375 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 376 |
error_type="relative_auc", |
| 377 |
num_images=100, |
| 378 |
num_reg_images=90, |
| 379 |
num_components=1, |
| 380 |
largest_component=90, |
| 381 |
), |
| 382 |
"__all__": Metrics( |
| 383 |
aucs=np.array([80.0, 90.0, 100.0]), |
| 384 |
recalls=np.array([85.0, 95.0, 105.0]), |
| 385 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 386 |
error_type="relative_auc", |
| 387 |
num_images=100, |
| 388 |
num_reg_images=90, |
| 389 |
num_components=1, |
| 390 |
largest_component=90, |
| 391 |
), |
| 392 |
} |
| 393 |
aucs, recalls = compute_avg_metrics(scene_metrics) |
| 394 |
# Should only average scene1, not __avg__ or __all__ |
| 395 |
np.testing.assert_array_equal(aucs, [10.0, 20.0, 30.0]) |
| 396 |
np.testing.assert_array_equal(recalls, [15.0, 25.0, 35.0]) |
| 397 |
|
| 398 |
|
| 399 |
class TestAggregateSceneMetrics: |
| 400 |
@staticmethod |
| 401 |
def _make_metrics(aucs, recalls, errors, num_images=100, num_reg_images=90): |
| 402 |
return Metrics( |
| 403 |
aucs=np.array(aucs, dtype=float), |
| 404 |
recalls=np.array(recalls, dtype=float), |
| 405 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 406 |
error_type="relative_auc", |
| 407 |
num_images=num_images, |
| 408 |
num_reg_images=num_reg_images, |
| 409 |
num_components=1, |
| 410 |
largest_component=num_reg_images, |
| 411 |
errors=np.array(errors, dtype=float), |
| 412 |
position_accuracy_gt=0.01, |
| 413 |
) |
| 414 |
|
| 415 |
def test_avg_and_all(self): |
| 416 |
scene_metrics = [ |
| 417 |
( |
| 418 |
"scene1", |
| 419 |
self._make_metrics([10, 20, 30], [15, 25, 35], [0.1, 0.5]), |
| 420 |
), |
| 421 |
( |
| 422 |
"scene2", |
| 423 |
self._make_metrics([20, 30, 40], [25, 35, 45], [0.2, 1.5]), |
| 424 |
), |
| 425 |
] |
| 426 |
summary = aggregate_scene_metrics( |
| 427 |
scene_metrics, |
| 428 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 429 |
error_type="relative_auc", |
| 430 |
) |
| 431 |
np.testing.assert_array_equal( |
| 432 |
summary["__avg__"].aucs, [15.0, 25.0, 35.0] |
| 433 |
) |
| 434 |
np.testing.assert_array_equal( |
| 435 |
summary["__avg__"].recalls, [20.0, 30.0, 40.0] |
| 436 |
) |
| 437 |
assert summary["__avg__"].num_images == 100 |
| 438 |
assert summary["__avg__"].num_reg_images == 90 |
| 439 |
np.testing.assert_array_equal( |
| 440 |
summary["__all__"].errors, [0.1, 0.5, 0.2, 1.5] |
| 441 |
) |
| 442 |
# __all__ aggregates totals (not means). |
| 443 |
assert summary["__all__"].num_images == 200 |
| 444 |
assert summary["__all__"].num_reg_images == 180 |
| 445 |
|
| 446 |
def test_skips_special_entries(self): |
| 447 |
real = self._make_metrics([10, 20, 30], [15, 25, 35], [0.1]) |
| 448 |
special = self._make_metrics( |
| 449 |
[99, 99, 99], [99, 99, 99], [9.0], num_images=999 |
| 450 |
) |
| 451 |
summary = aggregate_scene_metrics( |
| 452 |
[("scene1", real), ("__avg__", special), ("__all__", special)], |
| 453 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 454 |
error_type="relative_auc", |
| 455 |
) |
| 456 |
np.testing.assert_array_equal(summary["__avg__"].aucs, real.aucs) |
| 457 |
np.testing.assert_array_equal(summary["__all__"].errors, [0.1]) |
| 458 |
|
| 459 |
def test_empty_input(self): |
| 460 |
assert ( |
| 461 |
aggregate_scene_metrics( |
| 462 |
[], |
| 463 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 464 |
error_type="relative_auc", |
| 465 |
) |
| 466 |
== {} |
| 467 |
) |
| 468 |
|
| 469 |
def test_no_errors_omits_all(self): |
| 470 |
# When no scene carries raw errors (e.g. metrics restored without |
| 471 |
# the errors field), __all__ cannot be reconstructed. |
| 472 |
scene_metrics = [ |
| 473 |
("scene1", self._make_metrics([10, 20, 30], [15, 25, 35], [])), |
| 474 |
("scene2", self._make_metrics([20, 30, 40], [25, 35, 45], [])), |
| 475 |
] |
| 476 |
summary = aggregate_scene_metrics( |
| 477 |
scene_metrics, |
| 478 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 479 |
error_type="relative_auc", |
| 480 |
) |
| 481 |
assert "__all__" not in summary |
| 482 |
assert "__avg__" in summary |
| 483 |
|
| 484 |
|
| 485 |
class TestGetScores: |
| 486 |
def test_get_auc_scores(self): |
| 487 |
metrics = Metrics( |
| 488 |
aucs=np.array([10.0, 20.0, 30.0]), |
| 489 |
recalls=np.array([15.0, 25.0, 35.0]), |
| 490 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 491 |
error_type="relative_auc", |
| 492 |
num_images=100, |
| 493 |
num_reg_images=90, |
| 494 |
num_components=1, |
| 495 |
largest_component=90, |
| 496 |
) |
| 497 |
scores = get_scores("relative_auc", metrics) |
| 498 |
np.testing.assert_array_equal(scores, metrics.aucs) |
| 499 |
|
| 500 |
def test_get_recall_scores(self): |
| 501 |
metrics = Metrics( |
| 502 |
aucs=np.array([10.0, 20.0, 30.0]), |
| 503 |
recalls=np.array([15.0, 25.0, 35.0]), |
| 504 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 505 |
error_type="relative_recall", |
| 506 |
num_images=100, |
| 507 |
num_reg_images=90, |
| 508 |
num_components=1, |
| 509 |
largest_component=90, |
| 510 |
) |
| 511 |
scores = get_scores("relative_recall", metrics) |
| 512 |
np.testing.assert_array_equal(scores, metrics.recalls) |
| 513 |
|
| 514 |
|
| 515 |
class TestDiffMetrics: |
| 516 |
def test_nominal(self): |
| 517 |
metrics_a = { |
| 518 |
"dataset1": { |
| 519 |
"category1": { |
| 520 |
"scene1": Metrics( |
| 521 |
aucs=np.array([20.0, 30.0, 40.0]), |
| 522 |
recalls=np.array([25.0, 35.0, 45.0]), |
| 523 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 524 |
error_type="relative_auc", |
| 525 |
num_images=100, |
| 526 |
num_reg_images=90, |
| 527 |
num_components=2, |
| 528 |
largest_component=80, |
| 529 |
) |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
metrics_b = { |
| 534 |
"dataset1": { |
| 535 |
"category1": { |
| 536 |
"scene1": Metrics( |
| 537 |
aucs=np.array([10.0, 20.0, 30.0]), |
| 538 |
recalls=np.array([15.0, 25.0, 35.0]), |
| 539 |
error_thresholds=np.array([0.5, 1.0, 2.0]), |
| 540 |
error_type="relative_auc", |
| 541 |
num_images=100, |
| 542 |
num_reg_images=85, |
| 543 |
num_components=1, |
| 544 |
largest_component=85, |
| 545 |
) |
| 546 |
} |
| 547 |
} |
| 548 |
} |
| 549 |
diff = diff_metrics(metrics_a, metrics_b) |
| 550 |
|
| 551 |
scene_diff = diff["dataset1"]["category1"]["scene1"] |
| 552 |
np.testing.assert_array_equal(scene_diff.aucs, [10.0, 10.0, 10.0]) |
| 553 |
np.testing.assert_array_equal(scene_diff.recalls, [10.0, 10.0, 10.0]) |
| 554 |
assert scene_diff.num_reg_images == 5 |
| 555 |
assert scene_diff.num_components == 1 |
| 556 |
|
| 557 |
|
| 558 |
def create_test_reconstruction(): |
| 559 |
pycolmap.set_random_seed(0) |
| 560 |
synthetic_dataset_options = pycolmap.SyntheticDatasetOptions() |
| 561 |
synthetic_dataset_options.num_cameras_per_rig = 1 |
| 562 |
synthetic_dataset_options.num_frames_per_rig = 5 |
| 563 |
synthetic_dataset_options.num_points3D = 0 |
| 564 |
return pycolmap.synthesize_dataset(synthetic_dataset_options) |
| 565 |
|
| 566 |
|
| 567 |
def extract_sub_reconstruction(reconstruction, keep_names): |
| 568 |
"""Build a copy of reconstruction containing only the given image names. |
| 569 |
|
| 570 |
Mirrors what a real sub-model loaded from disk looks like: its images map |
| 571 |
holds exactly the registered subset. Rebuilding from a private copy keeps |
| 572 |
the source reconstruction untouched. |
| 573 |
""" |
| 574 |
keep_names = set(keep_names) |
| 575 |
source = pycolmap.Reconstruction(reconstruction) |
| 576 |
keep_frame_ids = { |
| 577 |
image.frame_id |
| 578 |
for image in source.images.values() |
| 579 |
if image.name in keep_names |
| 580 |
} |
| 581 |
sub = pycolmap.Reconstruction() |
| 582 |
for camera in source.cameras.values(): |
| 583 |
sub.add_camera(camera) |
| 584 |
for rig in source.rigs.values(): |
| 585 |
sub.add_rig(rig) |
| 586 |
for frame in source.frames.values(): |
| 587 |
if frame.frame_id in keep_frame_ids: |
| 588 |
frame.reset_rig_ptr() |
| 589 |
sub.add_frame(frame) |
| 590 |
for image in source.images.values(): |
| 591 |
if image.name in keep_names: |
| 592 |
image.reset_camera_ptr() |
| 593 |
image.reset_frame_ptr() |
| 594 |
sub.add_image(image) |
| 595 |
for frame_id in keep_frame_ids: |
| 596 |
sub.register_frame(frame_id) |
| 597 |
return sub |
| 598 |
|
| 599 |
|
| 600 |
class TestComputeAbsErrors: |
| 601 |
def test_identical_reconstruction(self): |
| 602 |
reconstruction = create_test_reconstruction() |
| 603 |
|
| 604 |
dts, dRs = compute_abs_errors( |
| 605 |
sparse_gt=reconstruction, sparse=reconstruction |
| 606 |
) |
| 607 |
|
| 608 |
assert len(dts) == reconstruction.num_images() |
| 609 |
assert len(dRs) == reconstruction.num_images() |
| 610 |
np.testing.assert_allclose(dts, 0.0, atol=1e-10) |
| 611 |
np.testing.assert_allclose(dRs, 0.0, atol=1e-10) |
| 612 |
|
| 613 |
def test_transformed_reconstruction(self): |
| 614 |
gt_reconstruction = create_test_reconstruction() |
| 615 |
reconstruction = create_test_reconstruction() |
| 616 |
translation = np.array([1, 2, 3]) |
| 617 |
for frame in reconstruction.frames.values(): |
| 618 |
world_from_rig = frame.rig_from_world.inverse() |
| 619 |
world_from_rig.rotation = ( |
| 620 |
world_from_rig.rotation * pycolmap.Rotation3d([0, 1, 0, 0]) |
| 621 |
) |
| 622 |
world_from_rig.translation += translation |
| 623 |
frame.rig_from_world = world_from_rig.inverse() |
| 624 |
|
| 625 |
dts, dRs = compute_abs_errors( |
| 626 |
sparse_gt=gt_reconstruction, sparse=reconstruction |
| 627 |
) |
| 628 |
|
| 629 |
assert len(dts) == reconstruction.num_images() |
| 630 |
assert len(dRs) == reconstruction.num_images() |
| 631 |
np.testing.assert_allclose(dts, np.linalg.norm(translation), atol=1e-10) |
| 632 |
np.testing.assert_allclose(dRs, 180.0, atol=1e-10) |
| 633 |
|
| 634 |
|
| 635 |
def _single_gt_cluster(reconstruction) -> dict[str, int]: |
| 636 |
"""Map every image of a reconstruction to a single GT cluster (id 0).""" |
| 637 |
return {image.name: 0 for image in reconstruction.images.values()} |
| 638 |
|
| 639 |
|
| 640 |
class TestComputeGroupedRelErrors: |
| 641 |
def test_identical_reconstruction(self): |
| 642 |
reconstruction = create_test_reconstruction() |
| 643 |
|
| 644 |
errors = compute_grouped_rel_errors( |
| 645 |
sparse_gt=reconstruction, |
| 646 |
sub_models=[reconstruction], |
| 647 |
image_name_to_component=_single_gt_cluster(reconstruction), |
| 648 |
min_proj_center_dist=0.01, |
| 649 |
) |
| 650 |
|
| 651 |
num_images = reconstruction.num_images() |
| 652 |
# A n B covers every ordered pair; A - B and B - A are empty. |
| 653 |
assert len(errors) == num_images * (num_images - 1) |
| 654 |
np.testing.assert_allclose(errors, 0.0, atol=1e-5) |
| 655 |
|
| 656 |
def test_transformed_reconstruction(self): |
| 657 |
gt_reconstruction = create_test_reconstruction() |
| 658 |
reconstruction = create_test_reconstruction() |
| 659 |
# A global similarity transform leaves relative poses unchanged. |
| 660 |
reconstruction.transform( |
| 661 |
pycolmap.Sim3d( |
| 662 |
1.0, |
| 663 |
pycolmap.Rotation3d(np.array([0, 1, 0, 0])), |
| 664 |
np.array([1, 2, 3]), |
| 665 |
) |
| 666 |
) |
| 667 |
|
| 668 |
errors = compute_grouped_rel_errors( |
| 669 |
sparse_gt=gt_reconstruction, |
| 670 |
sub_models=[reconstruction], |
| 671 |
image_name_to_component=_single_gt_cluster(gt_reconstruction), |
| 672 |
min_proj_center_dist=0.01, |
| 673 |
) |
| 674 |
|
| 675 |
num_images = reconstruction.num_images() |
| 676 |
assert len(errors) == num_images * (num_images - 1) |
| 677 |
np.testing.assert_allclose(errors, 0.0, atol=1e-5) |
| 678 |
|
| 679 |
def test_different_reconstructions(self): |
| 680 |
gt_reconstruction = create_test_reconstruction() |
| 681 |
reconstruction = create_test_reconstruction() |
| 682 |
for image in reconstruction.images.values(): |
| 683 |
image.frame.rig_from_world.rotation = ( |
| 684 |
pycolmap.Rotation3d(np.array([0, 1, 0, 0])) |
| 685 |
* image.frame.rig_from_world.rotation |
| 686 |
) |
| 687 |
image.frame.rig_from_world.translation += np.array([1, 2, 3]) |
| 688 |
|
| 689 |
errors = compute_grouped_rel_errors( |
| 690 |
sparse_gt=gt_reconstruction, |
| 691 |
sub_models=[reconstruction], |
| 692 |
image_name_to_component=_single_gt_cluster(gt_reconstruction), |
| 693 |
min_proj_center_dist=0.01, |
| 694 |
) |
| 695 |
|
| 696 |
num_images = reconstruction.num_images() |
| 697 |
assert len(errors) == num_images * (num_images - 1) |
| 698 |
assert np.all(errors > 0.1) |
| 699 |
|
| 700 |
def test_nothing_registered_maxes_all_gt_edges(self): |
| 701 |
# No estimated sub-models: set A is empty, so every GT edge is in |
| 702 |
# B - A and must be scored as the maximum (180 degrees). |
| 703 |
reconstruction = create_test_reconstruction() |
| 704 |
|
| 705 |
errors = compute_grouped_rel_errors( |
| 706 |
sparse_gt=reconstruction, |
| 707 |
sub_models=[], |
| 708 |
image_name_to_component=_single_gt_cluster(reconstruction), |
| 709 |
min_proj_center_dist=0.01, |
| 710 |
) |
| 711 |
|
| 712 |
num_images = reconstruction.num_images() |
| 713 |
assert len(errors) == num_images * (num_images - 1) |
| 714 |
np.testing.assert_allclose(errors, 180.0) |
| 715 |
|
| 716 |
def test_merged_estimate_of_separate_gt_clusters_maxes_cross_edges(self): |
| 717 |
# Two GT clusters, each perfectly reconstructed in its own sub-model. |
| 718 |
# There are no cross-cluster edges in B, and the within-cluster edges |
| 719 |
# are all in A n B with ~0 error. |
| 720 |
reconstruction = create_test_reconstruction() |
| 721 |
names = sorted(image.name for image in reconstruction.images.values()) |
| 722 |
half = len(names) // 2 |
| 723 |
image_name_to_component = { |
| 724 |
name: (0 if i < half else 1) for i, name in enumerate(names) |
| 725 |
} |
| 726 |
|
| 727 |
errors = compute_grouped_rel_errors( |
| 728 |
sparse_gt=reconstruction, |
| 729 |
sub_models=[reconstruction], |
| 730 |
image_name_to_component=image_name_to_component, |
| 731 |
min_proj_center_dist=0.01, |
| 732 |
) |
| 733 |
|
| 734 |
# Edges within a cluster: A n B (scored). Cross-cluster edges are in |
| 735 |
# A - B (the single sub-model connects everything) and set to 180. |
| 736 |
n0 = half |
| 737 |
n1 = len(names) - half |
| 738 |
num_within = n0 * (n0 - 1) + n1 * (n1 - 1) |
| 739 |
num_cross = len(names) * (len(names) - 1) - num_within |
| 740 |
assert len(errors) == len(names) * (len(names) - 1) |
| 741 |
np.testing.assert_allclose(np.sort(errors)[:num_within], 0.0, atol=1e-5) |
| 742 |
assert int(np.sum(np.isclose(errors, 180.0))) == num_cross |
| 743 |
|
| 744 |
def test_fragmented_estimate_of_merged_gt_maxes_cross_edges(self): |
| 745 |
# One GT cluster (merged reconstruction) that the estimate splits into |
| 746 |
# two disjoint sub-models (fragmented estimation). Within-fragment edges |
| 747 |
# are in A n B (scored ~0); the GT edges bridging the two fragments are |
| 748 |
# in B - A and set to 180. |
| 749 |
reconstruction = create_test_reconstruction() |
| 750 |
names = sorted(image.name for image in reconstruction.images.values()) |
| 751 |
half = len(names) // 2 |
| 752 |
sub_model_0 = extract_sub_reconstruction(reconstruction, names[:half]) |
| 753 |
sub_model_1 = extract_sub_reconstruction(reconstruction, names[half:]) |
| 754 |
image_name_to_component = {name: 0 for name in names} |
| 755 |
|
| 756 |
errors = compute_grouped_rel_errors( |
| 757 |
sparse_gt=reconstruction, |
| 758 |
sub_models=[sub_model_0, sub_model_1], |
| 759 |
image_name_to_component=image_name_to_component, |
| 760 |
min_proj_center_dist=0.01, |
| 761 |
) |
| 762 |
|
| 763 |
n0 = half |
| 764 |
n1 = len(names) - half |
| 765 |
num_within = n0 * (n0 - 1) + n1 * (n1 - 1) |
| 766 |
num_cross = len(names) * (len(names) - 1) - num_within |
| 767 |
assert len(errors) == len(names) * (len(names) - 1) |
| 768 |
np.testing.assert_allclose(np.sort(errors)[:num_within], 0.0, atol=1e-5) |
| 769 |
assert int(np.sum(np.isclose(errors, 180.0))) == num_cross |
| 770 |
|
| 771 |
def test_mismatched_gt_and_estimate_cluster_boundaries(self): |
| 772 |
# GT splits the images 1/3 vs 2/3, but the estimate splits them 2/3 vs |
| 773 |
# 1/3, so the two cluster boundaries disagree. Ordered pairs that share |
| 774 |
# both an estimated sub-model and a GT cluster are scored (~0 for a |
| 775 |
# perfect estimate); pairs grouped by only one of the two are maxed. |
| 776 |
reconstruction = create_test_reconstruction() |
| 777 |
names = sorted(image.name for image in reconstruction.images.values()) |
| 778 |
n = len(names) |
| 779 |
third = n // 3 |
| 780 |
two_thirds = 2 * n // 3 |
| 781 |
# GT: first third -> cluster 0, remaining two thirds -> cluster 1. |
| 782 |
image_name_to_component = { |
| 783 |
name: (0 if i < third else 1) for i, name in enumerate(names) |
| 784 |
} |
| 785 |
# Estimate: first two thirds and remaining third form two sub-models. |
| 786 |
sub_model_0 = extract_sub_reconstruction( |
| 787 |
reconstruction, names[:two_thirds] |
| 788 |
) |
| 789 |
sub_model_1 = extract_sub_reconstruction( |
| 790 |
reconstruction, names[two_thirds:] |
| 791 |
) |
| 792 |
|
| 793 |
errors = compute_grouped_rel_errors( |
| 794 |
sparse_gt=reconstruction, |
| 795 |
sub_models=[sub_model_0, sub_model_1], |
| 796 |
image_name_to_component=image_name_to_component, |
| 797 |
min_proj_center_dist=0.01, |
| 798 |
) |
| 799 |
|
| 800 |
# Three groups by (sub-model, GT cluster): P = names[:third] (sub 0, |
| 801 |
# cluster 0), Q = names[third:two_thirds] (sub 0, cluster 1), R = |
| 802 |
# names[two_thirds:] (sub 1, cluster 1). Only intra-group edges share |
| 803 |
# both a sub-model and a cluster (A n B, ~0). P-Q share a sub-model but |
| 804 |
# not a cluster (A - B), and Q-R share a cluster but not a sub-model |
| 805 |
# (B - A); both are maxed to 180. |
| 806 |
n_p = third |
| 807 |
n_q = two_thirds - third |
| 808 |
n_r = n - two_thirds |
| 809 |
num_scored = n_p * (n_p - 1) + n_q * (n_q - 1) + n_r * (n_r - 1) |
| 810 |
num_maxed = 2 * (n_p * n_q) + 2 * (n_q * n_r) |
| 811 |
assert len(errors) == num_scored + num_maxed |
| 812 |
np.testing.assert_allclose(np.sort(errors)[:num_scored], 0.0, atol=1e-5) |
| 813 |
assert int(np.sum(np.isclose(errors, 180.0))) == num_maxed |
| 814 |
|
| 815 |
def test_registered_outlier_edges_are_maxed(self): |
| 816 |
# A single sub-model connects an outlier to every real image. The |
| 817 |
# outlier is never in a GT reconstruction, so all edges touching it are |
| 818 |
# in A - B and set to 180; the remaining real edges are A n B (~0). |
| 819 |
reconstruction = create_test_reconstruction() |
| 820 |
names = sorted(image.name for image in reconstruction.images.values()) |
| 821 |
outlier = names[0] |
| 822 |
image_name_to_component = { |
| 823 |
name: (OUTLIER_COMPONENT_ID if name == outlier else 0) |
| 824 |
for name in names |
| 825 |
} |
| 826 |
|
| 827 |
errors = compute_grouped_rel_errors( |
| 828 |
sparse_gt=reconstruction, |
| 829 |
sub_models=[reconstruction], |
| 830 |
image_name_to_component=image_name_to_component, |
| 831 |
min_proj_center_dist=0.01, |
| 832 |
) |
| 833 |
|
| 834 |
num_images = reconstruction.num_images() |
| 835 |
assert len(errors) == num_images * (num_images - 1) |
| 836 |
# The 2 * (num_images - 1) ordered edges touching the outlier are maxed. |
| 837 |
num_maxed = int(np.sum(np.isclose(errors, 180.0))) |
| 838 |
assert num_maxed == 2 * (num_images - 1) |
| 839 |
|
| 840 |
def test_outlier_present_in_gt_maxes_its_edges(self): |
| 841 |
# An image that exists in the GT reconstruction and is perfectly |
| 842 |
# registered, but is flagged as an outlier, must still have large |
| 843 |
# edges: every edge touching it is in A - B and set to 180, while the |
| 844 |
# edges among the real images stay in A n B (~0). |
| 845 |
reconstruction = create_test_reconstruction() |
| 846 |
gt_names = [image.name for image in reconstruction.images.values()] |
| 847 |
outlier = sorted(gt_names)[0] |
| 848 |
assert outlier in gt_names # The outlier is present in the GT. |
| 849 |
image_name_to_component = { |
| 850 |
name: (OUTLIER_COMPONENT_ID if name == outlier else 0) |
| 851 |
for name in gt_names |
| 852 |
} |
| 853 |
|
| 854 |
errors = compute_grouped_rel_errors( |
| 855 |
sparse_gt=reconstruction, |
| 856 |
sub_models=[reconstruction], |
| 857 |
image_name_to_component=image_name_to_component, |
| 858 |
min_proj_center_dist=0.01, |
| 859 |
) |
| 860 |
|
| 861 |
n = reconstruction.num_images() |
| 862 |
assert len(errors) == n * (n - 1) |
| 863 |
# All 2 * (n - 1) ordered edges touching the outlier are maxed; the |
| 864 |
# (n - 1) * (n - 2) edges among the real images are ~0. |
| 865 |
assert int(np.sum(np.isclose(errors, 180.0))) == 2 * (n - 1) |
| 866 |
np.testing.assert_allclose( |
| 867 |
np.sort(errors)[: (n - 1) * (n - 2)], 0.0, atol=1e-5 |
| 868 |
) |
| 869 |
|
| 870 |
def test_outliers_excluded_from_gt_edges(self): |
| 871 |
# Nothing is registered, so every scored pair comes from B - A. The |
| 872 |
# outlier forms no GT edges, so it contributes none of them. |
| 873 |
reconstruction = create_test_reconstruction() |
| 874 |
names = sorted(image.name for image in reconstruction.images.values()) |
| 875 |
outlier = names[0] |
| 876 |
image_name_to_component = { |
| 877 |
name: (OUTLIER_COMPONENT_ID if name == outlier else 0) |
| 878 |
for name in names |
| 879 |
} |
| 880 |
|
| 881 |
errors = compute_grouped_rel_errors( |
| 882 |
sparse_gt=reconstruction, |
| 883 |
sub_models=[], |
| 884 |
image_name_to_component=image_name_to_component, |
| 885 |
min_proj_center_dist=0.01, |
| 886 |
) |
| 887 |
|
| 888 |
num_real = len(names) - 1 |
| 889 |
assert len(errors) == num_real * (num_real - 1) |
| 890 |
np.testing.assert_allclose(errors, 180.0) |
| 891 |
|
| 892 |
def test_gt_names_absent_from_sparse_gt_form_no_edges(self): |
| 893 |
# A name in image_name_to_component that does not exist in sparse_gt |
| 894 |
# cannot form a measurable GT edge and must not add spurious B - A |
| 895 |
# edges. Nothing is registered, so every scored pair comes from B - A. |
| 896 |
reconstruction = create_test_reconstruction() |
| 897 |
names = sorted(image.name for image in reconstruction.images.values()) |
| 898 |
phantom = "phantom_not_in_sparse_gt" |
| 899 |
assert phantom not in names |
| 900 |
image_name_to_component = {name: 0 for name in names} |
| 901 |
image_name_to_component[phantom] = 0 |
| 902 |
|
| 903 |
errors = compute_grouped_rel_errors( |
| 904 |
sparse_gt=reconstruction, |
| 905 |
sub_models=[], |
| 906 |
image_name_to_component=image_name_to_component, |
| 907 |
min_proj_center_dist=0.01, |
| 908 |
) |
| 909 |
|
| 910 |
# Only edges among the real images count; the phantom adds none. |
| 911 |
num_real = len(names) |
| 912 |
assert len(errors) == num_real * (num_real - 1) |
| 913 |
np.testing.assert_allclose(errors, 180.0) |
| 914 |
|
| 915 |
|
| 916 |
class TestComputeGroupedAbsErrors: |
| 917 |
def test_identical_single_cluster(self): |
| 918 |
reconstruction = create_test_reconstruction() |
| 919 |
|
| 920 |
errors = compute_grouped_abs_errors( |
| 921 |
sparse_gt=reconstruction, |
| 922 |
sub_models=[reconstruction], |
| 923 |
image_name_to_component=_single_gt_cluster(reconstruction), |
| 924 |
) |
| 925 |
|
| 926 |
assert len(errors) == reconstruction.num_images() |
| 927 |
np.testing.assert_allclose(errors, 0.0, atol=1e-10) |
| 928 |
|
| 929 |
def test_keeps_one_error_per_reconstruction(self): |
| 930 |
# An image registered in n sub-models contributes n errors (not just |
| 931 |
# the smallest). Here every image appears in both sub-models. |
| 932 |
reconstruction = create_test_reconstruction() |
| 933 |
|
| 934 |
errors = compute_grouped_abs_errors( |
| 935 |
sparse_gt=reconstruction, |
| 936 |
sub_models=[reconstruction, reconstruction], |
| 937 |
image_name_to_component=_single_gt_cluster(reconstruction), |
| 938 |
) |
| 939 |
|
| 940 |
assert len(errors) == 2 * reconstruction.num_images() |
| 941 |
np.testing.assert_allclose(errors, 0.0, atol=1e-10) |
| 942 |
|
| 943 |
def test_keeps_only_best_cluster(self): |
| 944 |
# Cluster 0 is reconstructed perfectly; cluster 1 is offset. The |
| 945 |
# best-mean cluster (0) is kept intact and cluster 1 is maxed out. |
| 946 |
gt_reconstruction = create_test_reconstruction() |
| 947 |
reconstruction = create_test_reconstruction() |
| 948 |
|
| 949 |
names = sorted( |
| 950 |
image.name for image in gt_reconstruction.images.values() |
| 951 |
) |
| 952 |
half = len(names) // 2 |
| 953 |
image_name_to_component = { |
| 954 |
name: (0 if i < half else 1) for i, name in enumerate(names) |
| 955 |
} |
| 956 |
cluster1_names = {name for name in names[half:]} |
| 957 |
for image in reconstruction.images.values(): |
| 958 |
if image.name in cluster1_names: |
| 959 |
image.frame.rig_from_world.translation += np.array([5, 5, 5]) |
| 960 |
|
| 961 |
errors = compute_grouped_abs_errors( |
| 962 |
sparse_gt=gt_reconstruction, |
| 963 |
sub_models=[reconstruction], |
| 964 |
image_name_to_component=image_name_to_component, |
| 965 |
) |
| 966 |
|
| 967 |
assert len(errors) == gt_reconstruction.num_images() |
| 968 |
finite = errors[np.isfinite(errors)] |
| 969 |
# Only the (perfect) best cluster remains finite. |
| 970 |
assert len(finite) == half |
| 971 |
np.testing.assert_allclose(finite, 0.0, atol=1e-10) |
| 972 |
assert int(np.sum(~np.isfinite(errors))) == len(names) - half |
| 973 |
|
| 974 |
def test_images_without_cluster_id_are_maxed(self): |
| 975 |
# GT images missing from the mapping (cluster id None) are always maxed |
| 976 |
# out, even when perfectly registered. |
| 977 |
reconstruction = create_test_reconstruction() |
| 978 |
names = sorted(image.name for image in reconstruction.images.values()) |
| 979 |
half = len(names) // 2 |
| 980 |
# Only the second half is mapped (all to cluster 1); the first half is |
| 981 |
# left unmapped (None). |
| 982 |
image_name_to_component = {name: 1 for name in names[half:]} |
| 983 |
|
| 984 |
errors = compute_grouped_abs_errors( |
| 985 |
sparse_gt=reconstruction, |
| 986 |
sub_models=[reconstruction], |
| 987 |
image_name_to_component=image_name_to_component, |
| 988 |
) |
| 989 |
|
| 990 |
assert len(errors) == reconstruction.num_images() |
| 991 |
finite = errors[np.isfinite(errors)] |
| 992 |
# The mapped (best) cluster stays finite; unmapped images are maxed. |
| 993 |
assert len(finite) == len(names) - half |
| 994 |
np.testing.assert_allclose(finite, 0.0, atol=1e-10) |
| 995 |
|
| 996 |
def test_outlier_cluster_is_maxed(self): |
| 997 |
# An outlier is never a selectable cluster, so it is maxed out even |
| 998 |
# though it is perfectly aligned; the real cluster is kept intact. |
| 999 |
reconstruction = create_test_reconstruction() |
| 1000 |
names = sorted(image.name for image in reconstruction.images.values()) |
| 1001 |
outlier = names[0] |
| 1002 |
image_name_to_component = { |
| 1003 |
name: (OUTLIER_COMPONENT_ID if name == outlier else 0) |
| 1004 |
for name in names |
| 1005 |
} |
| 1006 |
|
| 1007 |
errors = compute_grouped_abs_errors( |
| 1008 |
sparse_gt=reconstruction, |
| 1009 |
sub_models=[reconstruction], |
| 1010 |
image_name_to_component=image_name_to_component, |
| 1011 |
) |
| 1012 |
|
| 1013 |
assert len(errors) == reconstruction.num_images() |
| 1014 |
finite = errors[np.isfinite(errors)] |
| 1015 |
assert len(finite) == len(names) - 1 |
| 1016 |
np.testing.assert_allclose(finite, 0.0, atol=1e-10) |
| 1017 |
|
| 1018 |
def test_outlier_present_in_gt_gets_large_error(self): |
| 1019 |
# An image that exists in the GT reconstruction and is perfectly |
| 1020 |
# registered, but is flagged as an outlier, must still receive a large |
| 1021 |
# error: being present and well-aligned does not rescue an outlier. |
| 1022 |
reconstruction = create_test_reconstruction() |
| 1023 |
gt_names = [image.name for image in reconstruction.images.values()] |
| 1024 |
outlier = sorted(gt_names)[0] |
| 1025 |
assert outlier in gt_names # The outlier is present in the GT. |
| 1026 |
image_name_to_component = { |
| 1027 |
name: (OUTLIER_COMPONENT_ID if name == outlier else 0) |
| 1028 |
for name in gt_names |
| 1029 |
} |
| 1030 |
|
| 1031 |
errors = compute_grouped_abs_errors( |
| 1032 |
sparse_gt=reconstruction, |
| 1033 |
sub_models=[reconstruction], |
| 1034 |
image_name_to_component=image_name_to_component, |
| 1035 |
) |
| 1036 |
|
| 1037 |
# Every GT image is registered exactly once, so errors map 1:1 to |
| 1038 |
# gt_names in order. |
| 1039 |
assert len(errors) == reconstruction.num_images() |
| 1040 |
error_by_name = dict(zip(gt_names, errors, strict=True)) |
| 1041 |
assert not np.isfinite(error_by_name[outlier]) |
| 1042 |
others = [v for n, v in error_by_name.items() if n != outlier] |
| 1043 |
np.testing.assert_allclose(others, 0.0, atol=1e-10) |
| 1044 |
|
| 1045 |
def test_credits_multiple_clusters_across_sub_models(self): |
| 1046 |
# Two sub-models each perfectly reconstruct a different GT cluster and |
| 1047 |
# offset the other. Per-sub-model selection credits both clusters, |
| 1048 |
# which a single global choice could not do. |
| 1049 |
gt_reconstruction = create_test_reconstruction() |
| 1050 |
sub_model_0 = create_test_reconstruction() |
| 1051 |
sub_model_1 = create_test_reconstruction() |
| 1052 |
|
| 1053 |
names = sorted( |
| 1054 |
image.name for image in gt_reconstruction.images.values() |
| 1055 |
) |
| 1056 |
half = len(names) // 2 |
| 1057 |
image_name_to_component = { |
| 1058 |
name: (0 if i < half else 1) for i, name in enumerate(names) |
| 1059 |
} |
| 1060 |
cluster0_names = set(names[:half]) |
| 1061 |
cluster1_names = set(names[half:]) |
| 1062 |
# sub_model_0 offsets cluster 1 -> its best cluster is 0. |
| 1063 |
for image in sub_model_0.images.values(): |
| 1064 |
if image.name in cluster1_names: |
| 1065 |
image.frame.rig_from_world.translation += np.array([5, 5, 5]) |
| 1066 |
# sub_model_1 offsets cluster 0 -> its best cluster is 1. |
| 1067 |
for image in sub_model_1.images.values(): |
| 1068 |
if image.name in cluster0_names: |
| 1069 |
image.frame.rig_from_world.translation += np.array([5, 5, 5]) |
| 1070 |
|
| 1071 |
errors = compute_grouped_abs_errors( |
| 1072 |
sparse_gt=gt_reconstruction, |
| 1073 |
sub_models=[sub_model_0, sub_model_1], |
| 1074 |
image_name_to_component=image_name_to_component, |
| 1075 |
) |
| 1076 |
|
| 1077 |
# Each GT image is registered in both sub-models -> two errors each. |
| 1078 |
assert len(errors) == 2 * gt_reconstruction.num_images() |
| 1079 |
finite = errors[np.isfinite(errors)] |
| 1080 |
# Exactly one finite error per GT image: cluster 0 via sub_model_0 and |
| 1081 |
# cluster 1 via sub_model_1, so both clusters are credited. |
| 1082 |
assert len(finite) == gt_reconstruction.num_images() |
| 1083 |
np.testing.assert_allclose(finite, 0.0, atol=1e-10) |
| 1084 |
|
| 1085 |
# The errors must map back to the right image and sub-model: they are |
| 1086 |
# emitted in sparse_gt image order, each image contributing its two |
| 1087 |
# errors in sub_model order [sub_model_0, sub_model_1]. A cluster-0 |
| 1088 |
# image is credited only by sub_model_0 (finite, sub_model_1 maxed) and |
| 1089 |
# a cluster-1 image only by sub_model_1 (sub_model_0 maxed, finite). |
| 1090 |
gt_names = [image.name for image in gt_reconstruction.images.values()] |
| 1091 |
for i, name in enumerate(gt_names): |
| 1092 |
err_sub0, err_sub1 = errors[2 * i], errors[2 * i + 1] |
| 1093 |
if name in cluster0_names: |
| 1094 |
np.testing.assert_allclose(err_sub0, 0.0, atol=1e-10) |
| 1095 |
assert not np.isfinite(err_sub1) |
| 1096 |
else: |
| 1097 |
assert not np.isfinite(err_sub0) |
| 1098 |
np.testing.assert_allclose(err_sub1, 0.0, atol=1e-10) |
| 1099 |
|
| 1100 |
def test_mismatched_gt_and_estimate_cluster_boundaries(self): |
| 1101 |
# GT splits the images 1/3 vs 2/3 while the estimate splits them 2/3 vs |
| 1102 |
# 1/3. sub_model_0 spans all of GT cluster 0 plus the cluster-1 images |
| 1103 |
# that leaked in; it can credit only one GT cluster, so the leaked |
| 1104 |
# cluster-1 images are maxed. sub_model_1 holds the rest of cluster 1 |
| 1105 |
# and credits them. |
| 1106 |
reconstruction = create_test_reconstruction() |
| 1107 |
names = sorted(image.name for image in reconstruction.images.values()) |
| 1108 |
n = len(names) |
| 1109 |
third = n // 3 |
| 1110 |
two_thirds = 2 * n // 3 |
| 1111 |
image_name_to_component = { |
| 1112 |
name: (0 if i < third else 1) for i, name in enumerate(names) |
| 1113 |
} |
| 1114 |
sub_model_0 = extract_sub_reconstruction( |
| 1115 |
reconstruction, names[:two_thirds] |
| 1116 |
) |
| 1117 |
sub_model_1 = extract_sub_reconstruction( |
| 1118 |
reconstruction, names[two_thirds:] |
| 1119 |
) |
| 1120 |
# Offset the cluster-1 images that leaked into sub_model_0 so its best |
| 1121 |
# cluster is unambiguously cluster 0. |
| 1122 |
leaked_cluster1 = set(names[third:two_thirds]) |
| 1123 |
for image in sub_model_0.images.values(): |
| 1124 |
if image.name in leaked_cluster1: |
| 1125 |
image.frame.rig_from_world.translation += np.array([5, 5, 5]) |
| 1126 |
|
| 1127 |
errors = compute_grouped_abs_errors( |
| 1128 |
sparse_gt=reconstruction, |
| 1129 |
sub_models=[sub_model_0, sub_model_1], |
| 1130 |
image_name_to_component=image_name_to_component, |
| 1131 |
) |
| 1132 |
|
| 1133 |
# Sub-models are disjoint, so each GT image contributes exactly one |
| 1134 |
# error, emitted in sparse_gt image order. |
| 1135 |
assert len(errors) == reconstruction.num_images() |
| 1136 |
gt_names = [image.name for image in reconstruction.images.values()] |
| 1137 |
error_by_name = dict(zip(gt_names, errors, strict=True)) |
| 1138 |
# Cluster 0 (first third) credited by sub_model_0. |
| 1139 |
for name in names[:third]: |
| 1140 |
np.testing.assert_allclose(error_by_name[name], 0.0, atol=1e-10) |
| 1141 |
# Cluster-1 images that leaked into sub_model_0 are maxed. |
| 1142 |
for name in names[third:two_thirds]: |
| 1143 |
assert not np.isfinite(error_by_name[name]) |
| 1144 |
# Remaining cluster 1 credited by sub_model_1. |
| 1145 |
for name in names[two_thirds:]: |
| 1146 |
np.testing.assert_allclose(error_by_name[name], 0.0, atol=1e-10) |