Default reuse-as-seg OFF; default expandable_segments on Linux/WSL

Two default flips for server-edition:

1. cross_check.reuse_as_seg now defaults to false (settings + default.yaml + CLI help): the dedicated segmenter keeps its own forward and the cross-check veto stays an independent second signal. The reuse remains available via --cross-check-as-seg / config; measured cost of off vs on: ~20 s/image on CPU, +0.1-0.25 s and +0.9-1.3 GB VRAM on GPU (13.3 vs 12.0 GB reserved under expandable segments -- still fits a 16 GB card).

2. bgfilter/__init__.py defaults PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True on non-Windows platforms, before torch loads (setdefault: an explicit env value wins; native Windows is excluded because torch warns and ignores it there). Measured: reserved 15.3 -> 12.0 GB and ~10% faster on the RTX 5070 Ti; no effect on CPU-only runs.

README/DEPLOY(_ZH) synced: reuse documented as opt-in, GPU profile numbers updated for both reuse states, perf table labeled with the config it was measured under.

Verified: defaults resolve off/set as intended on Windows and WSL; explicit PYTORCH_CUDA_ALLOC_CONF override wins.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-07 20:40:19 +08:00
parent 6bf648cd64
commit 9b9d71cf82
7 changed files with 74 additions and 57 deletions
+18 -16
View File
@@ -38,15 +38,15 @@ download (~0.9 GB) and one inference pass per image; disable with
`--no-cross-check` (see the `cross_check` config section, and
`docs/hair_gap_artifacts.md` for the analysis behind it).
When the cross-check is on, that same HR-matting forward is **reused as the
segmentation mask by default**, skipping the primary seg model entirely (one
less model to load, ~20 s faster per image on CPU). Pilot-validated
(TestImage3 / FixImage1): trimap 99.8% identical, no structural change to
fingers, hair wisps or thin lines. Disable with `--no-cross-check-as-seg` to
run the dedicated seg model instead. The reuse is a same-family swap, so it
When the cross-check is on, that same HR-matting forward can optionally be
**reused as the segmentation mask** (`--cross-check-as-seg`), skipping the
primary seg model entirely (one less model to load, ~20 s faster per image on
CPU, ~1 GB less VRAM on GPU). Pilot-validated (TestImage3 / FixImage1): trimap
99.8% identical, no structural change to fingers, hair wisps or thin lines.
**Off by default**: the dedicated segmenter keeps its own forward and the veto
stays an independent second signal. The reuse is a same-family swap, so it
applies to the `birefnet` backend only: with `--seg-backend anime-seg` the
anime segmenter keeps its own forward and the cross-check veto still runs
independently on top.
anime segmenter always keeps its own forward.
The segmentation trimap defaults to `directional` mode (chroma + seg + a hue-direction
split: it keeps a background-coloured garment such as a white shirt while dropping a
@@ -119,8 +119,10 @@ D:\MiniConda\envs\lightML\python.exe -m bgfilter.cli `
## Performance (CPU reference numbers)
Measured on a Ryzen 9700X (Zen 5, native bf16), 32 GB RAM, default settings
(cross-check on, reused as seg; chunked global attention on):
Measured on a Ryzen 9700X (Zen 5, native bf16), 32 GB RAM, with cross-check on,
**`--cross-check-as-seg` enabled** and chunked global attention on. (Reuse is
off by default on this branch; without it, add the primary BiRefNet@1024
forward — roughly +20 s/image at these sizes.)
| input | precision | warm / image | peak memory |
|---|---|---|---|
@@ -218,12 +220,12 @@ caps input size (default ~4MP → `413`); `BGFILTER_PRELOAD=1` loads the default
startup. Run a single worker (`--workers 1`) — each worker loads its own copy of the models.
Like the CLI, the service runs the **cross-model veto by default** (config
default: on). With reuse-as-seg it replaces the primary seg forward rather than
adding to it, so it is not the latency hit it once was; its HR-matting model is
preloaded with the rest of the warm set (and, on the birefnet backend, the now-
redundant primary segmenter is skipped at startup). Set `BGFILTER_CROSS_CHECK=0`
to turn it off service-wide, or use the per-request `cross_check` form field to
override either way; `/healthz` reports the default as `crossCheck`.
default: on); its HR-matting model is preloaded with the rest of the warm set.
Set `BGFILTER_CROSS_CHECK=0` to turn it off service-wide, or use the per-request
`cross_check` form field to override either way; `/healthz` reports the default
as `crossCheck`. With `cross_check.reuse_as_seg: true` in the config (off by
default) the HR-matting forward replaces the primary seg forward instead of
adding to it, and the then-redundant primary segmenter is skipped at startup.
## Quality Check