# 13 — Better RANSAC fitting evidence

Date: 20 July 2026

Status: implementation, development audit, frozen held-out evaluation, and
recommendation complete

Plan: [`12.1-BETTER_RANSAC_FITTING_PLAN.md`](12.1-BETTER_RANSAC_FITTING_PLAN.md)

## Executive summary

Checkpoint 13 asked whether Stage 7 should see evidence earlier than the
cleaned Stage-6 binary image. The answer from this experiment is **not yet**.

- **What I built:** one explicit input flag now supports the unchanged
  `stage6_mask` baseline, raw `stage5_points`, all Stage-4 road cells sampled
  uniformly, and all Stage-4 road cells sampled with bounded reflectivity and
  road-height guidance.
- **How raw points are made fair:** returns are grouped into fixed `288 x 288`
  BEV cells. One occupied cell gets one sampling vote. Point count, maximum and
  median reflectivity, and median road-height residual are retained, but their
  influence is bounded.
- **What “guided” means:** bright, road-consistent cells are tried earlier and
  more often. The candidate pool expands during the same 60-trial-per-degree
  budget used by uniform RANSAC. Every finite Stage-4 cell keeps a non-zero
  chance and weight; there is no new hard reflectivity or height threshold.
- **Held-out result:** the Checkpoint-11 route remains best at **19.44%**
  one-cell-tolerant F1. Stage 5 reaches **17.28%**, raw uniform Stage 4
  **9.87%**, and guided Stage 4 **13.62%**.
- **What guidance did achieve:** compared with uniform Stage 4, guided Stage 4
  removes 4,512 tolerant false-positive cells, adds 724 true-positive cells,
  and cuts median route time from 2,258.9 ms to 1,829.4 ms. This proves that the
  soft evidence is useful, but not sufficient.
- **Why it still loses:** raw road cells produce long, plausible-looking
  parallel chains in clutter. Guided Stage 4 nearly matches baseline recall
  (14.73% versus 14.91%) but has only 12.66% precision versus 27.93%.
- **Decision:** keep `stage6_mask` as the recommended input. Preserve all four
  modes for future experiments. Do not start Checkpoint 14 until this report is
  reviewed and a new green light is given.

This is a negative replacement result, not a failed checkpoint: the planned
method, ablations, tests, development/held-out discipline, artifacts, and stop
gate are complete, and they prevent a materially worse input route from being
adopted.

## Delivered artifacts

Frozen held-out results:

- [artifact index](../artifacts/13_better_ransac_heldout/README.md);
- [benchmark CSV](../artifacts/13_better_ransac_heldout/benchmark.csv);
- [aggregate JSON](../artifacts/13_better_ransac_heldout/benchmark.json);
- [largest guided gain](../artifacts/13_better_ransac_heldout/images/seq_02_000986888908590_inputs.png);
  and
- [largest guided regression](../artifacts/13_better_ransac_heldout/images/seq_03_001854416774610_inputs.png).

Development results:

- [artifact index](../artifacts/13_better_ransac/README.md);
- [benchmark CSV](../artifacts/13_better_ransac/benchmark.csv); and
- [aggregate JSON](../artifacts/13_better_ransac/benchmark.json).

Every successful frame has a six-panel PNG and a JSON report containing all
four fits, aggregation statistics, coefficients, support, stability, timing,
and metrics.

## How to read a panel

1. **Stage-6 mask baseline:** green is the unchanged Checkpoint-11 adaptive
   output.
2. **Stage-5 point cells:** yellow is the adaptive fit from raw global-Otsu
   cells before Stage-6 cleanup.
3. **Stage-4 uniform:** purple is the adaptive fit from every occupied Stage-4
   road cell with uniform RANSAC.
4. **Stage-4 guided:** blue is the adaptive fit from the same Stage-4 cells with
   progressive soft-evidence sampling and bounded consensus scoring.
5. **Guided groups:** each bright color is one accepted point group; gray cells
   were available but unassigned.
6. **Guided strict error map:** white is an exact true positive, blue a false
   positive, and magenta a false negative.

Panels 1–4 overlay ground truth in magenta and show prediction/ground-truth
overlap in white, so every output can be compared directly. Forward is up and
positive lateral `y` is left. All four fits finish before the label is loaded;
the overlay is visualization-only.

The [held-out ranked-sample index](../artifacts/13_better_ransac_heldout/ranked_samples/README.md)
collects the three best, three median-like, and three worst guided-mode frames.
The development artifact directory contains the same ranking structure.

## Frozen held-out comparison

The 40 held-out frames are disjoint from the development timestamps. No
configuration changed after starting this run.

### K-Lane one-cell-tolerant result

| Input mode | TP | FP | FN | Precision | Recall | F1 |
|---|---:|---:|---:|---:|---:|---:|
| `stage6_mask` | 3,349 | 8,643 | 19,116 | 27.93% | 14.91% | **19.44%** |
| `stage5_points` | 2,997 | 9,221 | 19,468 | 24.53% | 13.34% | 17.28% |
| `stage4_uniform` | 2,586 | 27,343 | 19,879 | 8.64% | 11.51% | 9.87% |
| `stage4_guided` | 3,310 | 22,831 | 19,155 | 12.66% | 14.73% | 13.62% |

Stage 6 earns its place primarily through precision. Stage 5 is the closest
alternative, but removing cleanup still costs 352 tolerant true positives and
adds 578 false positives. Guidance recovers most of raw uniform's lost recall,
but the remaining clutter more than doubles baseline false positives.

### Strict same-cell result

| Input mode | TP | FP | FN | Precision | Recall | F1 |
|---|---:|---:|---:|---:|---:|---:|
| `stage6_mask` | 2,268 | 12,093 | 20,476 | 15.79% | 9.97% | **12.22%** |
| `stage5_points` | 2,071 | 12,254 | 20,673 | 14.46% | 9.11% | 11.17% |
| `stage4_uniform` | 1,236 | 30,178 | 21,508 | 3.93% | 5.43% | 4.56% |
| `stage4_guided` | 1,736 | 26,348 | 21,008 | 6.18% | 7.63% | 6.83% |

The exact-cell comparison gives the same ordering. The new route does not hide
an alignment gain behind the tolerant metric.

## Development-versus-held-out check

| Input mode | Development tolerant F1 | Held-out tolerant F1 |
|---|---:|---:|
| `stage6_mask` | **17.76%** | **19.44%** |
| `stage5_points` | 15.56% | 17.28% |
| `stage4_uniform` | 9.84% | 9.87% |
| `stage4_guided` | 10.18% | 13.62% |

The ordering is stable. Guided Stage 4 generalizes better than its development
aggregate, but remains far below both filtered routes.

## Near and far evidence

| Held-out range | Stage 6 | Stage 5 | Stage 4 uniform | Stage 4 guided |
|---|---:|---:|---:|---:|
| Near half | **18.03%** | 16.09% | 10.22% | 14.33% |
| Far half | **20.70%** | 18.34% | 9.33% | 12.68% |

Raw input does not solve the far-range weakness. Guidance helps more nearby,
where reflectivity evidence is denser, but the baseline still wins in both
halves.

## Straight and curved subsets

The label determines this tag only after fitting. A frame is called curved
when a quadratic label fit departs from its straight approximation by at least
`0.30 m` on any lane.

| Held-out label shape | Frames | Stage 6 | Stage 5 | Uniform | Guided |
|---|---:|---:|---:|---:|---:|
| Straight | 30 | **20.57%** | 17.76% | 10.00% | 13.47% |
| Curved | 10 | 15.36% | **15.59%** | 9.47% | 14.19% |

Stage 5 narrowly leads on the curved subset, but its gain is only 0.23 F1
points and does not survive the aggregate accuracy/stability decision. Guided
Stage 4 comes closer on curves than straight frames, which is a useful future
signal, not enough evidence for replacement.

## Per-sequence held-out result

| Sequence | Stage 6 | Stage 5 | Uniform | Guided |
|---|---:|---:|---:|---:|
| `seq_01` | **22.51%** | 22.46% | 13.57% | 15.26% |
| `seq_02` | **18.11%** | 17.25% | 13.10% | 15.00% |
| `seq_03` | **22.25%** | 18.18% | 5.94% | 13.85% |
| `seq_04` | **13.03%** | 8.64% | 5.27% | 8.68% |

Guidance beats uniform in every sequence but never beats Stage 6. It improves
13 held-out frames, ties 3, and regresses 24 relative to the baseline.

## Fit support, stability, and model choices

| Held-out mode | Chains | Linear / quadratic / cubic | Assigned cells* | Median span | Median residual | Median stability |
|---|---:|---:|---:|---:|---:|---:|
| Stage 6 | 162 | 110 / 44 / 8 | 33.48% | 23.04 m | 0.089 m | 0.082 m |
| Stage 5 | 151 | 100 / 37 / 14 | 30.25% | 23.04 m | 0.091 m | 0.078 m |
| Uniform | 188 | 167 / 18 / 3 | 16.44% | 31.60 m | 0.116 m | 0.026 m |
| Guided | 162 | 144 / 13 / 5 | 13.43% | 33.28 m | 0.115 m | 0.026 m |

`*` Mean per-frame fraction of evidence cells assigned to accepted groups.

The raw modes produce longer, mostly linear chains with worse residuals. Their
low alternating-band stability error is not proof of correct lanes: a stable
clutter edge can be geometrically smooth. The 33 m median guided span explains
why a wrong chain can create many false-positive cells despite support-aware
rendering.

## Runtime

Route timing includes Stage-4 aggregation for both Stage-4 modes.

| Held-out mode | Median | Mean | P95 |
|---|---:|---:|---:|
| Stage 6 | **814.6 ms** | 1,238.8 ms | 3,529.6 ms |
| Stage 5 | 1,002.3 ms | **1,055.9 ms** | **2,383.1 ms** |
| Uniform | 2,258.9 ms | 2,464.1 ms | 4,703.7 ms |
| Guided | 1,829.4 ms | 1,900.5 ms | 3,330.3 ms |

Guidance is much faster than uniform raw sampling because the model-level soft
consensus gate stops weak searches earlier. Both raw routes remain slower than
the filtered inputs.

## How the implementation works

### 1. Four explicit input modes

`stage6_mask` calls the existing Checkpoint-11 mask fitter unchanged.
`stage5_points` calls the same fitter on Stage 6's raw high-resolution Otsu
occupancy. `stage4_uniform` calls it on all occupied Stage-4 cells.
`stage4_guided` changes only sampling and consensus ranking; accepted groups
still use the same degree comparison, physical checks, and renderer.

The held-out Stage-6 result exactly reproduces Checkpoint 11's adaptive counts:
3,349 tolerant TP, 8,643 FP, and 19,116 FN. That is the baseline-preservation
check.

### 2. Cell aggregation

All aligned Stage-4 road points are mapped into fixed BEV cells. Each occupied
cell stores its center, return count, maximum reflectivity, median
reflectivity, and median absolute residual from the Stage-3 road plane. Cell
count grows only through a bounded logarithmic factor, so a dense scan arc
cannot create unlimited votes.

### 3. Progressive sampling

Reflectivity is converted to a within-frame rank. A bounded nonlinear rank
weight and a small road-height agreement factor order the cells. Sampling
starts from the top 5% pool and expands to all cells by 80% of the fixed trial
budget. Even the weakest finite cell retains positive probability.

### 4. Bounded consensus

Consensus weight combines reflectivity, height agreement, and bounded cell
density, then clips the normalized result. Repeated votes are capped within
each forward-distance band. Accepted models must have useful forward span and
bins and a mean soft-consensus weight at least equal to the frame average.
This rejects weak models; it does not delete individual low-strength points.

### 5. Existing safeguards remain authoritative

Straight, quadratic, and cubic hypotheses retain the Checkpoint-11 residual,
span, forward-bin, slope, curvature, curvature-change, parallel-direction,
minimum-separation/non-crossing, and unsupported-gap rules. Curves render only
over their observed support segments.

## Representative cases

The [largest held-out guided gain](../artifacts/13_better_ransac_heldout/images/seq_02_000986888908590_inputs.png)
improves from 10.47% to 23.67% tolerant F1. Guidance finds several long lane
traces that Stage 6 fragments.

The [largest held-out guided regression](../artifacts/13_better_ransac_heldout/images/seq_03_001854416774610_inputs.png)
falls from 33.12% to 0.00%. The soft gate accepts no group aligned with the
label; raw reflectivity ranks are not a reliable lane identity signal in that
frame.

Uniform Stage 4 returns no chain in two held-out frames and falls below the
Stage-6 F1 on 31 of 40 frames. Its main failure is not RANSAC convergence alone:
when it converges, it often converges to abundant road clutter.

## Tests and verification

All **49** repository tests pass. The nine new or expanded Checkpoint-13 tests
verify that:

- multiple returns aggregate into one cell while retaining maximum, robust,
  count, and road-height evidence;
- low-reflectivity cells retain non-zero sampling weight and density influence
  remains bounded;
- guided sampling recovers a bright, forward-supported synthetic lane amid
  heavy clutter; and
- all four input routes run, the Stage-6 route exactly preserves its baseline,
  and every report names its true input;
- near/far tolerant counts exactly partition the full-frame counts while
  retaining matches across the diagnostic midpoint;
- held-out timestamps and incompatible/missing resume provenance are rejected;
- every output panel distinguishes prediction, ground truth, and overlap, while
  ranked selection produces nine distinct best, median, and worst samples; and
- invalid, non-finite, and misaligned evidence/configuration is rejected.

Source and tests pass Python bytecode compilation. Artifact checks cover 39
successful development frames plus the already known malformed source PCD, and
40 successful disjoint held-out frames with no failures.

Reproduce the frozen runs with:

```bash
uv run klane-stage7-inputs /mnt/arnavs_hdd/data/02-lane-detection/K-Lane \
  --sequence 1,2,3,4 --sample-count 10 \
  --output-dir artifacts/13_better_ransac

uv run klane-stage7-inputs /mnt/arnavs_hdd/data/02-lane-detection/K-Lane \
  --sequence 1,2,3,4 --sample-count 10 --selection heldout \
  --output-dir artifacts/13_better_ransac_heldout

uv run python -m unittest discover -s tests -v
```

## Recommendation and stop gate

Keep `stage6_mask` as the production/research baseline. It has the best
accuracy, the most stable cross-sequence result, and lower runtime than either
Stage-4 mode. Keep `stage5_points`, `stage4_uniform`, and `stage4_guided` as
explicit diagnostic ablations; do not silently delete them or substitute them
for the baseline.

The raw-point experiment identifies the missing constraint: reflectivity and
height can rank evidence, but they cannot reliably decide which of several
smooth, road-supported traces belong to the ordered lane family. That is
exactly the separate question posed by the Checkpoint-14 known-geometry plan.
This report does **not** authorize that work.

Checkpoint 13 stops here for review. Checkpoint 14 starts only after explicit
user approval.
