Split trimap and dropout-fill segmentation backends

Add SegmentationSettings.fill_backend / fill_model_name so the dropout-fill can
use a different (higher-recall) segmenter than the trimap. Default is now the
combination the investigation converged on: BiRefNet builds the trimap (reliable
topology -- finger gap removed, strands kept as sure_fg), anime-seg drives the
fill (more, more-coherent anime wisps). Because the fill only adds (max), a
higher-recall-but-less-reliable mask there is harmless.

On TestImage2 this eliminates the strand breakage a full anime-seg pipeline
caused (lost 699 -> 0) while keeping ~91% of its extra wisp recall (gained
1133 -> 1031). Strands stay continuous; the recovered wisps are anime-seg's
coherent curves rather than BiRefNet's grainy fill.

Also adds an optional unsharp-mask knob (segmentation.sharpen, default off).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-01 11:17:46 +08:00
parent 08378e57f8
commit 9805b51b99
4 changed files with 45 additions and 2 deletions
+26 -2
View File
@@ -28,13 +28,36 @@ class MattingPipeline:
self.settings = settings
self._runner: ViTMatteRunner | None = None
self._segmenter = None
self._fill_segmenter = None
def _segment(self, rgb: np.ndarray) -> np.ndarray:
if self._segmenter is None:
from .segmentation import make_segmenter
self._segmenter = make_segmenter(self.settings.segmentation)
return self._segmenter.mask(rgb)
mask = self._segmenter.mask(rgb)
seg = self.settings.segmentation
if seg.sharpen > 0:
from .segmentation import unsharp_mask
mask = unsharp_mask(mask, seg.sharpen, seg.sharpen_radius)
return mask
def _segment_fill(self, rgb: np.ndarray, default_mask: np.ndarray) -> np.ndarray:
"""Mask for the dropout-fill; may use a different (higher-recall) backend
than the trimap, since the fill only adds and cannot break anything."""
seg = self.settings.segmentation
if not seg.fill_backend or seg.fill_backend == seg.backend:
return default_mask
if self._fill_segmenter is None:
from dataclasses import replace
from .segmentation import make_segmenter
self._fill_segmenter = make_segmenter(
replace(seg, backend=seg.fill_backend, model_name=seg.fill_model_name)
)
return self._fill_segmenter.mask(rgb)
def _predict_alpha(self, rgb: np.ndarray, trimap: np.ndarray, bg_confidence: np.ndarray) -> tuple[np.ndarray, str]:
if self.settings.model.matting_method == "chroma":
@@ -93,7 +116,8 @@ def _run_image(
alpha = suppress_alpha_by_chroma(alpha, bg_confidence, trimap, settings.alpha_post)
alpha = clean_alpha(alpha, trimap, settings.alpha_post)
if seg_mask is not None:
alpha = fill_seg_dropouts(alpha, seg_mask, settings.alpha_post)
fill_mask = pipeline._segment_fill(rgb, seg_mask)
alpha = fill_seg_dropouts(alpha, fill_mask, settings.alpha_post)
foreground = estimate_foreground_rgb(
rgb, alpha, bg_confidence, model, settings.foreground
)