update at 2026-03-28 16:46:40

This commit is contained in:
douboer
2026-03-28 16:46:40 +08:00
parent 57cd9a1f39
commit a1b0d6731c
13 changed files with 921 additions and 58 deletions

103
README.md
View File

@@ -14,7 +14,9 @@ python remove_background.py
# 处理单个文件
python remove_background.py input.jpg output.png
# 查看所有参数-m, --model)
# 查看所有参数
python remove_background.py -h
```
不同模型适用于不同场景:
@@ -39,7 +41,19 @@ python remove_background.py input.jpg output.png -m birefnet-portrait
# 使用快速模型
python remove_background.py input.jpg output.png -m u2netp
``
# 书画类文字偏浅的温和参数示例
python remove_background.py images output \
--remove-subject --black_subject --gray_subject --save_mask \
--black-threshold 30\
--gray-saturation-threshold 30 --gray-value-threshold 30 \
--edge-grow 2 \
--feather --feather-radius 4 \
--aot-pretrain experiments/G0000000.pt \
--aot-max-size 1000
```
查看已下载模型:
```bash
ls -lh ~/.u2net/
```
@@ -75,6 +89,40 @@ rm -rf ~/.u2net/
rm ~/.u2net/u2net.onnx
```
## AOT-GAN 修补后端
`--remove-subject` 默认使用 AOT-GAN 修补。
AOT-GAN 依赖 PyTorch官方仓库测试 Python 3.8 / torch 1.8.1)。建议使用独立虚拟环境或确保兼容版本。
```bash
# 安装依赖(示例)
pip install torch torchvision
```
下载预训练权重后,运行示例:
```bash
python remove_background.py "images/IMG_9259 2.JPG" \
--remove-subject --black-subject --gray-subject --save-mask \
--aot-pretrain experiments/places2.pth
```
CPU 无 GPU 时的加速建议(只裁剪主体区域并限制最大边):
```bash
python remove_background.py "images/IMG_9259 2.JPG" \
--remove-subject --black-subject --gray-subject --save-mask \
--aot-pretrain experiments/places2.pth \
--aot-crop --aot-crop-pad 24 --aot-max-size 1400
```
减少“补脸”倾向:启用随机噪声预填充
```bash
python remove_background.py "images/IMG_9259 2.JPG" \
--remove-subject --black-subject --gray-subject --save-mask \
--aot-pretrain experiments/places2.pth \
--aot-crop --aot-crop-pad 64 --aot-max-size 900 \
--aot-noise-prefill --aot-noise-strength 1.0
```
## 可调整参数说明
### 1. 模型选择 (model_name)
@@ -97,10 +145,11 @@ rm ~/.u2net/u2net.onnx
Alpha Matting 是后处理步骤,可以显著改善边缘质量,特别是头发、毛发等细节。
#### alpha_matting (True/False)
- **作用**: 是否启用alpha matting
- **默认**: False
- **建议**: 如果边缘不自然,启用此选项
#### alpha_matting(开关)
- **作用**: 是否启用 alpha matting,提升边缘质量
- **默认**: 关闭(不传 `-a/--alpha-matting`
- **启用方式**: 传入 `-a``--alpha-matting`
- **效果**: 有利于细节边缘(毛发/细线),但速度稍慢
#### alpha_matting_foreground_threshold (0-255)
- **作用**: 前景阈值,控制哪些区域被认为是前景
@@ -128,9 +177,45 @@ Alpha Matting 是后处理步骤,可以显著改善边缘质量,特别是头
### 3. Mask后处理 (post_process_mask)
- **作用**: 对mask进行额外后处理
- **默认**: False
- **建议**: 可以尝试启用看效果是否改善
- **作用**: 对 mask 进行额外后处理
- **默认**: 关闭(不传 `-p/--post-process`
- **启用方式**: 传入 `-p``--post-process`
- **效果**: 有助于减少毛边,但可能略损失细节
### 4. 去主体补背景 (remove_subject)
用于“去掉主体并补全背景”。当前仅使用 AOT-GAN 修补。
- **remove_subject开关**: 启用去主体补背景(默认关闭,传 `--remove-subject` 开启)
- **aot_root**: AOT-GAN 目录(默认: `AOT-GAN-for-Inpainting`
- **aot_pretrain**: AOT-GAN 权重文件路径(必填)
- **aot_device**: AOT-GAN 设备(默认: `cpu`
- **aot_block_num**: AOTBlock 数量(默认: 8
- **aot_rates**: AOTBlock 膨胀率(默认: `1+2+4+8`
- **aot_crop开关**: 仅对 mask 覆盖区域裁剪修补(默认关闭,传 `--aot-crop` 开启)
- **aot_crop_pad (像素)**: 裁剪边缘留白像素(默认: 0
- **aot_max_size (像素)**: AOT 输入最大边限制(默认: 0 表示不限制)
- **aot_noise_prefill开关**: AOT使用随机噪声预填充默认关闭
- **aot_noise_strength (系数)**: 噪声强度(默认: 1.0
- **mask_dilate (像素)**: mask 膨胀大小(默认: 3。越大去除范围越大风险更高
- **mask_blur (像素)**: mask 模糊大小(默认: 3。越大边缘越柔和但易过度
- **mask_threshold (0-255)**: alpha 阈值(默认: 10。越大保留越多主体
- **edge_grow (像素)**: 主体边缘额外扩张(默认: 0。用于清理残留边缘
- **save_mask开关**: 保存 mask 方便检查(默认关闭,传 `--save-mask` 开启)
- **black_subject开关**: 将黑色内容也视为主体(默认关闭,传 `--black-subject` 开启)
- **black_threshold (0-255)**: 黑色阈值(默认: 50。越大越容易把浅灰当黑
- **gray_subject开关**: 将灰阶内容也视为主体(默认关闭,传 `--gray-subject` 开启)
- **gray_saturation_threshold (0-255)**: 灰阶饱和度阈值(默认: 30。越大越容易把彩色当灰
- **gray_value_threshold (0-255)**: 灰阶亮度阈值(默认: 200。越大越容易把浅灰当灰
- **feather开关**: 启用边缘过渡(默认关闭,传 `--feather` 开启)
- **feather_radius (像素)**: 过渡半径(默认: 5。越大过渡越柔和但可能变糊
- **说明**: 过渡仅在 mask 外侧进行,避免把主体边缘带回
### 5. 参数调优建议(针对书画/字迹)
- 先开启 `--remove-subject`,仅看主体遮罩是否覆盖到字迹
- 文字残留:提高 `--black-threshold``--gray-*` 阈值
- 过度修补:降低 `--black-threshold``--gray-value-threshold`,并减小 `--mask-dilate/--mask-blur`
- 边缘不自然:尝试开启 `--feather` 并使用较小的 `--feather-radius`
## 常见问题解决