Skip to content

Commit b5ff5c3

Browse files
aswanth-07ahojnnes
andauthored
Report executed RANSAC trials (colmap#4974)
`RANSAC::Report::num_trials` currently includes reservations that fail the trial-budget or abort check before sampling. For example, a one-trial budget reports two iterations with one thread and five with four threads. A zero-trial budget also reports work that never ran. `LORANSAC` shares the same reporting error. I count the iterations that pass both checks with an OpenMP reduction. The reservation counter, sampling, stopping conditions and model selection remain unchanged. This also gives accurate trial totals to the existing fundamental-matrix benchmark. The two regression tests cover zero, one and five trials with one and four threads. All 12 count assertions fail on pristine production; all 12 tests across both native modules pass with the fix, both with OpenMP and with it disabled. A separate 900-call counting matrix confirms that each report equals the actual minimal-estimator calls. All 108 seeded similarity-transform model, mask and support controls match pristine output exactly. I validated MSVC Release on Windows. Six alternating timing rounds show median changes from -3.4% to +5.8% across the serial paths, including a +4.4 us/call no-OpenMP LORANSAC diagnostic. The per-round timings are noisy; this is a reporting correction without a speed claim. Changed-file formatting, scoped Clang-Tidy and whitespace checks pass. Full application, reconstruction-dataset, GPU and other-platform validation remain unrun. AI assistance disclosure: AI-assisted development tools were used during investigation and implementation. The reported tests were run against the final diff. --------- Co-authored-by: Johannes Schönberger <[email protected]>
1 parent f0ddf77 commit b5ff5c3

4 files changed

Lines changed: 96 additions & 0 deletions

File tree

‎src/colmap/optim/loransac.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ LORANSAC<Estimator, LocalEstimator, SupportMeasurer, Sampler>::Estimate(
192192
trial_counter.fetch_add(1, std::memory_order_relaxed);
193193
if (curr_thread_trial >= max_num_trials ||
194194
abort_flag.load(std::memory_order_relaxed)) {
195+
// Undo the reservation, so that trial_counter counts only the
196+
// trials that pass the checks above. A reused index fails the
197+
// gate again: the abort flag is never cleared, and a budget
198+
// failure means max_num_trials was already reached.
199+
trial_counter.fetch_sub(1, std::memory_order_relaxed);
195200
break;
196201
}
197202

‎src/colmap/optim/loransac_test.cc‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,50 @@ TEST(LORANSAC, SimilarityTransform) {
129129
ValidateReport(report, data);
130130
}
131131

132+
TEST(LORANSAC, ReportsExecutedTrials) {
133+
const auto data = GenerateTestData(10, 0);
134+
for (const int num_threads : {1, 4}) {
135+
for (const int max_num_trials : {0, 1, 5}) {
136+
RANSACOptions options;
137+
options.max_error = 10;
138+
// confidence = 1 disables dynamic stopping, so the trial budget is
139+
// exhausted exactly and the reported count must equal it.
140+
options.confidence = 1;
141+
options.max_num_trials = max_num_trials;
142+
options.num_threads = num_threads;
143+
options.random_seed = kDefaultPRNGSeed;
144+
LORANSAC<SimilarityTransformEstimator<3>, SimilarityTransformEstimator<3>>
145+
loransac(options);
146+
const auto report = loransac.Estimate(data.src, data.tgt);
147+
EXPECT_EQ(report.num_trials, max_num_trials);
148+
EXPECT_EQ(report.success, max_num_trials > 0);
149+
}
150+
}
151+
}
152+
153+
TEST(LORANSAC, DynamicStoppingCountsExecutedTrialsOnly) {
154+
const auto data = GenerateTestData(10, 0);
155+
const size_t max_num_trials = 5;
156+
// With dynamic stopping, threads may reserve a trial after the abort
157+
// flag is set. Those reservations must not be counted, so the reported
158+
// count never exceeds the budget even though the exact count depends
159+
// on thread scheduling.
160+
for (int rep = 0; rep < 50; ++rep) {
161+
RANSACOptions options;
162+
options.max_error = 10;
163+
options.confidence = 0.99;
164+
options.max_num_trials = max_num_trials;
165+
options.num_threads = 4;
166+
options.random_seed = kDefaultPRNGSeed;
167+
LORANSAC<SimilarityTransformEstimator<3>, SimilarityTransformEstimator<3>>
168+
loransac(options);
169+
const auto report = loransac.Estimate(data.src, data.tgt);
170+
EXPECT_TRUE(report.success);
171+
EXPECT_GT(report.num_trials, 0);
172+
EXPECT_LE(report.num_trials, max_num_trials);
173+
}
174+
}
175+
132176
TEST(LORANSAC, ParallelSimilarityTransform) {
133177
const auto data = GenerateTestData();
134178

‎src/colmap/optim/ransac.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ RANSAC<Estimator, SupportMeasurer, Sampler>::Estimate(
267267
trial_counter.fetch_add(1, std::memory_order_relaxed);
268268
if (curr_thread_trial >= max_num_trials ||
269269
abort_flag.load(std::memory_order_relaxed)) {
270+
// Undo the reservation, so that trial_counter counts only the
271+
// trials that pass the checks above. A reused index fails the
272+
// gate again: the abort flag is never cleared, and a budget
273+
// failure means max_num_trials was already reached.
274+
trial_counter.fetch_sub(1, std::memory_order_relaxed);
270275
break;
271276
}
272277

‎src/colmap/optim/ransac_test.cc‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,48 @@ TEST(RANSAC, SimilarityTransform) {
126126
ValidateReport(report, data);
127127
}
128128

129+
TEST(RANSAC, ReportsExecutedTrials) {
130+
const auto data = GenerateTestData(10, 0);
131+
for (const int num_threads : {1, 4}) {
132+
for (const int max_num_trials : {0, 1, 5}) {
133+
RANSACOptions options;
134+
options.max_error = 10;
135+
// confidence = 1 disables dynamic stopping, so the trial budget is
136+
// exhausted exactly and the reported count must equal it.
137+
options.confidence = 1;
138+
options.max_num_trials = max_num_trials;
139+
options.num_threads = num_threads;
140+
options.random_seed = kDefaultPRNGSeed;
141+
RANSAC<SimilarityTransformEstimator<3>> ransac(options);
142+
const auto report = ransac.Estimate(data.src, data.tgt);
143+
EXPECT_EQ(report.num_trials, max_num_trials);
144+
EXPECT_EQ(report.success, max_num_trials > 0);
145+
}
146+
}
147+
}
148+
149+
TEST(RANSAC, DynamicStoppingCountsExecutedTrialsOnly) {
150+
const auto data = GenerateTestData(10, 0);
151+
const size_t max_num_trials = 5;
152+
// With dynamic stopping, threads may reserve a trial after the abort
153+
// flag is set. Those reservations must not be counted, so the reported
154+
// count never exceeds the budget even though the exact count depends
155+
// on thread scheduling.
156+
for (int rep = 0; rep < 50; ++rep) {
157+
RANSACOptions options;
158+
options.max_error = 10;
159+
options.confidence = 0.99;
160+
options.max_num_trials = max_num_trials;
161+
options.num_threads = 4;
162+
options.random_seed = kDefaultPRNGSeed;
163+
RANSAC<SimilarityTransformEstimator<3>> ransac(options);
164+
const auto report = ransac.Estimate(data.src, data.tgt);
165+
EXPECT_TRUE(report.success);
166+
EXPECT_GT(report.num_trials, 0);
167+
EXPECT_LE(report.num_trials, max_num_trials);
168+
}
169+
}
170+
129171
TEST(RANSAC, ParallelSimilarityTransform) {
130172
const auto data = GenerateTestData();
131173

0 commit comments

Comments
 (0)