Support loading model weights from local project folders

Add bgfilter/weights.py: resolve_model_source() maps a HuggingFace repo id
to a local folder under the weights dir (models/ by default, override with
BGFILTER_WEIGHTS_DIR) when one named after the repo basename exists; otherwise
the repo id is returned unchanged. Opt-in and backward compatible.

- vitmatte_infer.py / segmentation.py resolve model_name through it; anime-seg
  reads <dir>/isnetis.onnx directly instead of hf_hub_download when local.
- .gitignore: models/, model-cache/
- README: document bundling weights as plain project folders.

Verified: anime-seg loads from a local models/anime-seg/ folder offline.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-03 02:50:19 +00:00
parent 7a87a00ca1
commit da4164e95a
5 changed files with 51 additions and 4 deletions
+7
View File
@@ -205,6 +205,13 @@ D:\MiniConda\envs\lightML\python.exe scripts\smoke_samples.py `
`FileMetadataError`; (2) with `hf-xet` installed the Xet download path fails instantly.
Once the weights are cached, run offline with `HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1`
(as the service does in production).
- Bundling weights in the project — instead of the HF cache, drop each model into a
plain folder named after the repo basename under `models/`:
`models/vitmatte-base-composition-1k`, `models/BiRefNet`, `models/anime-seg`. The loader
prefers a matching local folder and falls back to the HF repo id / cache when absent, so
it is opt-in. Populate them with e.g. `hf download ZhengPeng7/BiRefNet --local-dir
models/BiRefNet`. Override the base directory with `BGFILTER_WEIGHTS_DIR`. `models/` is
gitignored.
- Foreground colour estimation uses pymatting's `estimate_foreground_ml` to propagate
clean foreground colour into semi-transparent edges before de-spill. Set
`foreground.method: unmix` to fall back to the legacy heuristic.