Cut a scene category back to the pixels that agree with it

A flag in the sky came out weighted as sky, and no feather setting fixed it.

The scene model's logits are `[1, 150, 80, 80]`, so one cell is eight input
pixels; at the 1600px proxy the letterbox scale is 0.4 and **one cell is 20
proxy pixels**, which `rasterise`'s bilinear then spreads across one more
either side. A flag is a handful of cells whose softmax is dominated by the
sky around it. The information was never in the grid, so nothing downstream
of the grid can recover it.

Tiling is the answer for an instance and is not available here: a category
has no bounding box to tile over — sky is wherever the sky is. But the
photograph is at full proxy resolution even though the weights are not, and
it knows exactly where the flag is. So the model says *what*, and the
pixels say *which of them*, which is the division of labour arm C already
draws between the instance model and the watershed.

## Seeds, and why the erosion radius is not a guess

Threshold the weights high, take `signed_distance`, and keep what is more
than 1.5 cells inside. One cell *is* the model's resolution and the bilinear
spreads it across one more, so the band either side of the boundary is smear
rather than evidence. Deriving the radius from `Scene::cell_pixels` rather
than picking a pixel count means it stays right if the proxy edge or the
export changes.

The mirror of that set is a confident *exterior*, free from the same field.

## Dropping small modes is the step that makes it work

Four k-means modes per side, not one Gaussian: sky is blue at the zenith,
white where the cloud is and pale at the horizon, and one blob over all
three rejects two of them.

Then modes holding under 3% of a side are discarded, and without that step
the whole thing fails on the case it was built for. A small flag deep in the
sky has both a high weight and a large distance from the boundary, so it
lands in the interior sample and teaches the model its own colour. It cannot
be excluded geometrically. It can be excluded by share.

Luminance is weighted at a quarter against chrominance for the same reason
the watershed's gradient is. Sky's variance is dominated by luminance, so at
equal weight the distribution is a long bright streak that a mid-grey flag
sits comfortably inside. A flag is separated by chrominance; a cloud is
separated by luminance alone. Not zero, or a dark bird against a bright sky
survives.

## Two tests, because either alone is wrong

Absolute — is this colour plausible under the category, as a chi-square on
the Mahalanobis distance. Comparative — is it likelier inside than outside.
A pixel must pass both.

The absolute test is what catches the flag, whose colour is far from *both*
sides and which the comparative test alone would leave at even odds. The
comparative test is what stops the absolute one needing a constant tuned per
category.

## What this cannot do, written down rather than left to be discovered

An intruder large enough to hold its own mode is kept. By share, a flag over
a fifth of the sky and a cloud bank over a fifth of the sky are the same
object, and colour does not separate them either — a white cloud is as far
from blue sky in chrominance as many intruders are.

So `min_cluster` is not a threshold with a correct value waiting to be
found; it is the trade-off itself, set where a photographic intruder falls.
Both ends are pinned by tests — `a_flag_in_the_sky_is_removed` and
`an_intruder_larger_than_min_cluster_survives` — so that moving the number
reads as moving the trade-off rather than as fixing a bug. The case left
open is a large unrecognised object in a clean category, which wants the
boundary snapped to watershed basins and is a different mechanism.

## Safe to apply without a control

It is subtractive: the output is the input times a factor in `0..=1`. The
worst failure available to it is losing part of a real sky, never gaining a
region, so a blue car below the horizon that was never in the mask cannot be
pulled into it. And a factor in `0..=1` cannot raise a sum, so `scene.rs`'s
partition still holds when every category is refined independently — the
weight taken off the flag lands in the unlisted remainder, which is where a
flag belongs, ADE20K having no class for one.

Every path without the evidence to judge returns the weights untouched and
says which path it took. A refinement that silently did nothing is
indistinguishable from the feature being off, and an empty seed set fitted
to a distribution would reject every pixel.

The signature is deliberately unchanged: categories are addressed by name,
not by index, so a sharper mask cannot create the stale-index hazard the
signature exists to guard against.

The example writes `<prefix>-<category>-refined.ppm` beside the coarse one,
never instead of it — whether this is an improvement is a comparative
judgement and one image cannot answer it.

Verified: fmt clean, clippy -D warnings clean, 57 dr-segment tests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-30 18:30:16 +02:00
co-authored by Claude Opus 5
parent 8131706394
commit 4f4abd335f
7 changed files with 1014 additions and 5 deletions
+55 -1
View File
@@ -271,6 +271,22 @@ pub struct Options {
/// answer. It is the right answer for a bird against sky, so it is offered
/// per-image rather than chosen once for all of them.
pub fine: bool,
/// TRACES: FR-DEV-3
/// Cut each scene category back to the pixels whose colour agrees with it.
///
/// The scene model's counterpart to [`Options::fine`], and it exists
/// because tiling is not available here: a category has no bounding box to
/// tile over — sky is wherever the sky is — so the only route to a sharper
/// category edge is the photograph itself. See [`dr_segment::refine`].
///
/// On by default where `fine` is off, because the two have opposite costs.
/// Tiling is six inferences and a five-second wait; this is a distance
/// transform and a k-means over a subsample, tens of milliseconds against
/// a precompute already measured in hundreds. And it is subtractive, so
/// the worst it can do is take too much of a category rather than invent
/// one.
pub refine: bool,
}
impl Default for Options {
@@ -278,6 +294,7 @@ impl Default for Options {
Self {
confidence: 0.30,
fine: false,
refine: true,
}
}
}
@@ -353,7 +370,7 @@ pub fn compute(
// way. Failures here are logged and dropped rather than propagated: no
// scene model is an ordinary state, and a photograph that can be masked by
// subject should not become unopenable because the categories are absent.
let categories = match scene_categories(&stood_up, uw, uh, orientation) {
let categories = match scene_categories(&stood_up, uw, uh, orientation, options.refine) {
Ok(c) => c,
Err(e) => {
log::info!("no scene categories for this frame: {e}");
@@ -539,6 +556,7 @@ fn scene_categories(
width: usize,
height: usize,
orientation: Orientation,
refine: bool,
) -> Result<Vec<CategorySummary>, String> {
let mut model = load_scene_model()?;
let scene = model
@@ -558,6 +576,42 @@ fn scene_categories(
let Some(weights) = scene.rasterise(index, width, height) else {
continue;
};
// Cut the category back to the pixels whose colour agrees with it.
//
// Here rather than after `lay_down` because this reads the photograph,
// and the photograph is upright at this point — refining on the far
// side of the permutation would mean carrying a second, rotated copy
// of the proxy across for it to read.
let weights = if refine {
let (refined, what) = dr_segment::refine_category(
&weights,
rgb,
width,
height,
scene.cell_pixels(),
&dr_segment::RefineOptions::default(),
);
match what {
dr_segment::Refined::Applied { removed } => {
log::debug!(
"refined '{name}': cut {:.1}% of its weight",
removed * 100.0
);
}
// An ordinary state, not a failure — see `dr_segment::refine`.
// Logged all the same, because a refinement that silently did
// nothing is indistinguishable from the feature being off, and
// "the sky looks the way it did before" is what both look like.
dr_segment::Refined::Skipped(why) => {
log::debug!("left '{name}' coarse: {why:?}");
}
}
refined
} else {
weights
};
// Back into sensor space, exactly as an instance mask is: the grid a
// stored layer indexes into has to be the sensor's whatever the model
// was shown. `lay_down` wants a box too, so it gets the whole frame —