vnassets/background: add bg removal via rembg + isnet-anime

rembg with ONNX Runtime on CPU was chosen over BiRefNet: the latter
requires deform_conv2d which crashes on ROCm bfloat16 and runs at 40s
in float32. rembg delivers 0.23s per 1024x1024 image, no GPU deps,
and isnet-anime is trained specifically on anime images — exactly the
target domain.

CLI and session API both support single-image and batch (plural-first)
modes, reusing one model session across files.
This commit is contained in:
Michele Rossi
2026-07-08 11:20:36 +02:00
parent d33f3a4e34
commit 8a11325b2f
5 changed files with 236 additions and 3 deletions

View File

@@ -139,6 +139,43 @@ Either model can be omitted (`None`) for single-model sessions. Properties:
The standalone `vnasset generate` and `vnasset edit` CLI commands are thin
wrappers around a one-shot session — same API, backwards compatible.
### Background Removal
Remove backgrounds from character sprites (output is RGBA PNG):
```bash
# Single file
vnasset remove-bg --input character_base.png --output character_transparent.png
# Batch (reuses model across files)
vnasset remove-bg --input base.png --input happy.png --input sad.png --output-dir transparent/
```
Or via the session API:
```python
with VnAssetsSession() as vna:
vna.remove_background("base.png", output="base_transparent.png")
# Batch
vna.remove_backgrounds(
["base.png", "happy.png", "sad.png"],
output_dir="transparent/",
)
```
| Option | Default | Description |
|--------|---------|-------------|
| `--input` | (required, repeatable) | Input image path(s) |
| `--output` | (auto) | Output path (single mode) |
| `--output-dir` | (none) | Output directory (batch mode) |
| `--model` | `isnet-anime` | Model: `isnet-anime`, `u2net`, `u2netp`, `u2net_human_seg`, `isnet-general-use`, `sam` |
The default `isnet-anime` model is trained on anime images — ideal for the
novaAnimeXL art style. Inference runs on CPU via onnxruntime (~0.25s per
1024×1024 image on Strix Halo). The model is ~176 MB, downloaded on first use
to `~/.u2net/`.
## Architecture
```
@@ -350,7 +387,8 @@ Use `--raw` to bypass weighting and fall back to plain diffusers encoding.
| Compel prompt weighting + BREAK | ✅ Working |
| Lightning LoRA fuse-at-load | ✅ Working |
| Flash attention (experimental) | ✅ Working |
| Metadata JSON output | ✅ Working |
| `vnasset remove-bg` | ✅ Working |
| Session background removal | ✅ Working |
| `vnasset pipeline` (batch YAML config) | 🚧 Planned |
| `vnasset serve` (daemon/HTTP API) | 🚧 Planned |
| `torch.compile` on UNet | 🚧 Planned |
@@ -371,5 +409,3 @@ Use `--raw` to bypass weighting and fall back to plain diffusers encoding.
availability or a ComfyUI installation.
- **`vnasset serve`** — lightweight daemon with Unix socket or HTTP API for
integrating VNAsset into external tools.
- **Background removal** — green-screen trim and transparency pass for
post-processing sprites.