| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch / torch_npu | 2.1.0+ | NPU 上需安装 torch_npu |
| transformers | >=4.39.1, <5 | trust_remote_code 加载 BriaRMBG(5.x 移除了 PreTrainedModel 旧接口,不兼容) |
| huggingface_hub | <1.0 | 与 transformers 4.x 配套 |
| pillow / numpy | 最新版 | 图像处理与可视化 |
| 昇腾驱动 | CANN 8.0.RC2+ | 推荐最新版 |
安装命令:
pip install --target /mnt/workspace/RMBG-1.4/libs -i https://pypi.tuna.tsinghua.edu.cn/simple "transformers>=4.39.1" pillow numpy huggingface_hub safetensors# 检查 NPU 设备
npu-smi info
# 权重已就位
ls /home/developer/models/RMBG-1.4/ # config.json briarmbg.py MyConfig.py model.safetensors ...PYTHONPATH=/mnt/workspace/RMBG-1.4/libs python3 inference.py --model_path /home/developer/models/RMBG-1.4| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model_path | str | /home/developer/models/RMBG-1.4 | 本地权重目录 |
| --device | str | 自动(npu > cuda > cpu) | 推理设备 |
| --image_size | int | 1024 | 模型输入分辨率(官方默认 1024x1024) |
| --output | str | assets/model_result.png | 结果图保存路径 |
输入:
python3 inference.py --model_path /home/developer/models/RMBG-1.4输出:
[INFO] device = npu
[INFO] 模型加载完成 load_time=xx.xs
[INFO] 输入张量: (1, 3, 1024, 1024) dtype=torch.float32
[INFO] 推理完成 耗时/elapsed=x.xxxs (size=1024, device=npu)
[INFO] alpha 抠图形状: (1024, 1024), 均值=0.xxx, 前景占比(>128)=0.xxx
[INFO] 结果图已保存: assets/model_result.png
[RESULT] status=ok device=npu 耗时/elapsed=x.xxxs输入:
python3 inference.py --model_path /home/developer/models/RMBG-1.4 --image_size 512输出:
[INFO] 输入张量: (1, 3, 512, 512) dtype=torch.float32
[INFO] 推理完成 耗时/elapsed=x.xxxs ...
[RESULT] status=ok device=npu 耗时/elapsed=x.xxxs


AutoModelForImageSegmentation.from_pretrained(..., trust_remote_code=True, local_files_only=True) 从本地目录加载,推理全程离线;result[0][0] 上采样回原图尺寸并 min-max 归一化得到 alpha 抠图;