Skip to content

A segmentation model for Object Selection, a fill model for Content-Aware Fill - #75

Merged
IAmJSD merged 1 commit into
mainfrom
t3code/segment-and-fill
Aug 29, 2026
Merged

IAmJSD merged 1 commit into
mainfrom
t3code/segment-and-fill

Conversation

@IAmJSD

@IAmJSD IAmJSD commented Aug 29, 2026

Copy link
Copy Markdown
Member

The last two things in the README that were heuristics rather than models. Both run networks now, and neither network is Adobe's. The bullet is gone; what each one does is described where the feature is.

Object Selection

Runs U^2-Net (4.6 MB, Apache-2.0, fetched from Filter ▸ Neural Filters ▸ Manage Models) over a crop around the box you drew — a box round an object is a picture whose subject is that object, which is the one question a salient-object network answers, and it is why the network gets a crop rather than the layer.

Two things differ from the reference implementation, both because a selection tool has different needs from a background remover:

  • The map is not stretched over its own range. Stretching is right when the answer is going to be an alpha channel and something is definitely there. Here it is wrong: hand this a close-up of a brick wall and the honest answer is a map of nothing, and a stretch turns that into a map of noise. Left as the probability the network emitted, "nothing here" stays sayable — and when it is said, the old reading of the box (its border as a sample of the background) runs instead.
  • The boundary is settled against the picture. The map arrives soft and at 320 pixels square, so its edge is several pixels of ramp wherever the box was bigger than that. The two sides of a real edge are two colours, and each uncertain pixel goes to the one it is nearer.

Verified in the running application: the ants trace a boxer's ears, muzzle and collar chain, not a colour threshold.

Content-Aware Fill

Three stages, each doing the part the next one cannot.

  1. A network — 723k parameters, trained here, shipped in the binary — predicts the region back. What it is good at is layout: a horizon that continues, a wall that stays a wall, a shadow that keeps its direction.
  2. Patch synthesis (Criminisi et al.) fills from the edge inwards, taking the piece of boundary with the most to go on, finding the patch of the real picture that best continues it, and copying that patch in whole. Structure gets filled before texture, so a line across a hole comes out a line. Copying rather than averaging is the whole difference from a blur: every pixel that comes out was somewhere in this photograph. The network's answer is what the search is scored against inside the hole, so it decides what gets copied where.
  3. Seam relaxation. Elsewhere in the photograph was lit slightly differently, so the fill can be the right texture and still arrive as a visible rectangle a shade off. The mismatch is measured round the boundary and relaxed across the hole — a smooth field, so the texture survives and only the tone moves. Which is the one thing diffusion was always good at.

Diffusion is still there: as the seed when the model cannot run, and as the whole answer when there is nothing to copy from either.

The fill network trained on 17,746 CC BY photographs from Open Images and about four hundred thousand holes — half rectangles, half free-form strokes, because those are the shapes people draw round signage and wires. Held-out error in the hole 0.074, and the held-out detail measure is tracked alongside it, because a network that has given up and is filling holes with the average of their edges scores a respectable error and a gradient of nearly nothing.

Also

  • Model reads its input's channel count from the ONNX graph rather than assuming three. An inpainting model needs a fourth plane for the mask, because "black" and "gone" are otherwise the same pixel and a network cannot be expected to guess which.
  • run_framed's output resampling is now shared with the inpainting path.
  • The Manage Models dialog now holds two models that belong to tools rather than filters. One list of every model in the build beats a tidy division by which menu asked for it.

Performance

A 600×500 hole in a 1400×1400 picture fills in 3 s (it was 28 s before the hole boundary was tracked incrementally rather than rescanned per patch, and the candidate search parallelised). Ordinary fills are a few hundred milliseconds. Object Selection is about 1.5 s per drag, which is the network.

Checks

cargo fmt, cargo clippy --workspace --all-targets and cargo test --workspace are clean. Six new tests, including two that need no photograph in the repository: the segmentation model has to cut round a disc on a field, and it has to find nothing in fine grain — the second is what the fallback depends on. Content-Aware Fill is pinned by measuring the roughness inside a filled hole against the roughness of the real pixels round it, which is the assertion the old diffusion fill could never have passed.

Both were verified in the running application, headlessly.

🤖 Generated with Claude Code

…tent-Aware Fill

The two tools that were still heuristics run models now, and neither
model is Adobe's.

Object Selection runs U^2-Net over a crop around the box you drew,
because a box round an object is a picture whose subject is that
object, which is the one question that network answers. The map comes
back soft and at the resolution it thinks in, so the boundary is then
settled against the picture's own colours: the two sides of a real edge
are two colours, and each uncertain pixel belongs to the one it is
nearer. The map is deliberately *not* stretched over its own range the
way the reference implementation stretches it -- a selection tool has
to be able to come back empty, and a stretch turns a picture of nothing
into a map of noise. When it does come back empty the old reading of
the box, its border as a sample of the background, runs instead.

Content-Aware Fill is three stages, each doing what the next cannot. A
723k-parameter network trained here predicts the region back and gets
the layout -- a horizon that continues, a wall that stays a wall.
Exemplar-based patch synthesis (Criminisi) then rebuilds the hole out
of patches of the photograph itself, scored against the network's
answer where there is no evidence, so the texture in a filled hole is
real texture from this picture rather than a network's average of every
picture. Finally the mismatch round the seam is measured and relaxed
across the hole, which is the one thing the old diffusion was always
good at. Diffusion is still there as the seed when the model cannot
run, and as the whole answer when there is nothing to copy from either.

The fill network trained on 17,746 CC BY photographs from Open Images
and four hundred thousand holes -- rectangles and free-form strokes,
because those are the shapes people draw. Held-out error in the hole
0.074.

Also: `Model` reads its input's channel count from the graph rather
than assuming three, because an inpainting model needs a fourth plane
for the mask -- "black" and "gone" are otherwise the same pixel.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@IAmJSD
IAmJSD force-pushed the t3code/segment-and-fill branch from 32c47d5 to e3d74c1 Compare August 29, 2026 23:05
@IAmJSD
IAmJSD merged commit aa3d741 into main Aug 29, 2026
3 checks passed
@IAmJSD
IAmJSD deleted the t3code/segment-and-fill branch August 29, 2026 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant