Debian Codegraph @

python/examples/custom_incremental_pipeline_test.py

raw
1 # Equivalent tests to src/colmap/controllers/incremental_pipeline_test.cc
2
3 from pathlib import Path
4
5 import custom_incremental_pipeline
6
7 import pycolmap
8
9
10 def expect_equal_reconstructions(
11 gt: pycolmap.Reconstruction,
12 computed: pycolmap.Reconstruction,
13 max_rotation_error_deg: float,
14 max_proj_center_error: float,
15 num_obs_tolerance: float,
16 ) -> None:
17 assert computed.num_cameras() == gt.num_cameras()
18 assert computed.num_images() == gt.num_images()
19 assert computed.num_reg_images() == gt.num_reg_images()
20 assert (
21 computed.compute_num_observations()
22 >= (1 - num_obs_tolerance) * gt.compute_num_observations()
23 )
24
25 result = pycolmap.compare_reconstructions(
26 computed,
27 gt,
28 alignment_error="proj_center",
29 max_proj_center_error=max_proj_center_error,
30 )
31 assert result is not None
32 for error in result["errors"]:
33 assert error.rotation_error_deg < max_rotation_error_deg
34 assert error.proj_center_error < max_proj_center_error
35
36
37 def create_test_options() -> pycolmap.IncrementalPipelineOptions:
38 options = pycolmap.IncrementalPipelineOptions()
39 # Use single thread for deterministic behavior.
40 options.num_threads = 1
41 return options
42
43
44 def test_without_noise(tmp_path: Path) -> None:
45 pycolmap.set_random_seed(0)
46
47 database_path = tmp_path / "database.db"
48 image_path = tmp_path / "images"
49 image_path.mkdir()
50 output_path = tmp_path / "sparse"
51 output_path.mkdir()
52
53 with pycolmap.Database.open(database_path) as database:
54 synthetic_dataset_options = pycolmap.SyntheticDatasetOptions()
55 synthetic_dataset_options.num_cameras_per_rig = 2
56 synthetic_dataset_options.num_frames_per_rig = 7
57 synthetic_dataset_options.num_points3D = 50
58 gt_reconstruction = pycolmap.synthesize_dataset(
59 synthetic_dataset_options, database
60 )
61
62 custom_incremental_pipeline.main(
63 database_path=database_path,
64 image_path=image_path,
65 output_path=output_path,
66 options=create_test_options(),
67 )
68
69 expect_equal_reconstructions(
70 gt_reconstruction,
71 pycolmap.Reconstruction(output_path / "0"),
72 max_rotation_error_deg=1e-2,
73 max_proj_center_error=1e-4,
74 num_obs_tolerance=0,
75 )
76
77
78 def test_with_noise(tmp_path: Path) -> None:
79 pycolmap.set_random_seed(0)
80
81 database_path = tmp_path / "database.db"
82 image_path = tmp_path / "images"
83 image_path.mkdir()
84 output_path = tmp_path / "sparse"
85 output_path.mkdir()
86
87 with pycolmap.Database.open(database_path) as database:
88 synthetic_dataset_options = pycolmap.SyntheticDatasetOptions()
89 synthetic_dataset_options.num_cameras_per_rig = 2
90 synthetic_dataset_options.num_frames_per_rig = 7
91 synthetic_dataset_options.num_points3D = 100
92 gt_reconstruction = pycolmap.synthesize_dataset(
93 synthetic_dataset_options, database
94 )
95 synthetic_noise_options = pycolmap.SyntheticNoiseOptions()
96 synthetic_noise_options.point2D_stddev = 0.5
97 pycolmap.synthesize_noise(synthetic_noise_options, gt_reconstruction)
98
99 custom_incremental_pipeline.main(
100 database_path=database_path,
101 image_path=image_path,
102 output_path=output_path,
103 options=create_test_options(),
104 )
105
106 expect_equal_reconstructions(
107 gt_reconstruction,
108 pycolmap.Reconstruction(output_path / "0"),
109 max_rotation_error_deg=1e-1,
110 max_proj_center_error=1e-1,
111 num_obs_tolerance=0.02,
112 )
113
114
115 def test_multi_reconstruction(tmp_path: Path) -> None:
116 pycolmap.set_random_seed(0)
117
118 database_path = tmp_path / "database.db"
119 image_path = tmp_path / "images"
120 image_path.mkdir()
121 output_path = tmp_path / "sparse"
122 output_path.mkdir()
123
124 with pycolmap.Database.open(database_path) as database:
125 synthetic_dataset_options = pycolmap.SyntheticDatasetOptions()
126 synthetic_dataset_options.num_cameras_per_rig = 1
127 synthetic_dataset_options.num_frames_per_rig = 5
128 synthetic_dataset_options.num_points3D = 50
129 gt_reconstruction1 = pycolmap.synthesize_dataset(
130 synthetic_dataset_options, database
131 )
132 synthetic_dataset_options.num_frames_per_rig = 4
133 gt_reconstruction2 = pycolmap.synthesize_dataset(
134 synthetic_dataset_options, database
135 )
136
137 options = create_test_options()
138 options.min_model_size = 4
139 custom_incremental_pipeline.main(
140 database_path=database_path,
141 image_path=image_path,
142 output_path=output_path,
143 options=options,
144 )
145
146 assert len(list(output_path.iterdir())) == 2
147 reconstruction1 = pycolmap.Reconstruction(output_path / "0")
148 reconstruction2 = pycolmap.Reconstruction(output_path / "1")
149 if reconstruction1 == gt_reconstruction2.num_reg_images():
150 reconstruction1, reconstruction2 = reconstruction2, reconstruction1
151
152 expect_equal_reconstructions(
153 gt_reconstruction1,
154 reconstruction1,
155 max_rotation_error_deg=1e-2,
156 max_proj_center_error=1e-4,
157 num_obs_tolerance=0,
158 )
159 expect_equal_reconstructions(
160 gt_reconstruction2,
161 reconstruction2,
162 max_rotation_error_deg=1e-2,
163 max_proj_center_error=1e-4,
164 num_obs_tolerance=0,
165 )
166
167
168 def test_chained_matches(tmp_path: Path) -> None:
169 pycolmap.set_random_seed(0)
170
171 database_path = tmp_path / "database.db"
172 image_path = tmp_path / "images"
173 image_path.mkdir()
174 output_path = tmp_path / "sparse"
175 output_path.mkdir()
176
177 with pycolmap.Database.open(database_path) as database:
178 synthetic_dataset_options = pycolmap.SyntheticDatasetOptions()
179 synthetic_dataset_options.num_cameras_per_rig = 1
180 synthetic_dataset_options.num_frames_per_rig = 4
181 synthetic_dataset_options.num_points3D = 100
182 synthetic_dataset_options.match_config = (
183 pycolmap.SyntheticDatasetMatchConfig.CHAINED
184 )
185 gt_reconstruction = pycolmap.synthesize_dataset(
186 synthetic_dataset_options, database
187 )
188
189 custom_incremental_pipeline.main(
190 database_path=database_path,
191 image_path=image_path,
192 output_path=output_path,
193 options=create_test_options(),
194 )
195
196 expect_equal_reconstructions(
197 gt_reconstruction,
198 pycolmap.Reconstruction(output_path / "0"),
199 max_rotation_error_deg=1e-2,
200 max_proj_center_error=1e-4,
201 num_obs_tolerance=0,
202 )