g
gcw_coj3XaOd/briaai_RMBG-1.4
模型介绍
文件和版本
Pull Requests
讨论
分析

briaai/RMBG-1.4 - 昇腾 NPU 推理部署

1. 模型简介

模型名称: briaai/RMBG-1.4

模型链接: HuggingFace

模型描述: BRIA RMBG-1.4 是一个基于 U2Net 架构的背景移除模型,用于图像前景分割和背景去除。模型输入 RGB 图像,输出 Alpha Matte(透明度掩码)。

模型架构: U2Net(RSU-7/RSU-6/RSU-5/RSU-4/RSU-4F 编码器-解码器结构,6 个 side output)

参数规模: 44,046,790 参数(约 44M)

输入规格:

  • 类型:RGB 图像(3 通道)
  • Shape:(1, 3, 1024, 1024) — batch=1,1024×1024 分辨率
  • 预处理:resize→1024×1024,normalize(mean=0.5, std=1.0),HWC→CHW

输出规格:

  • 类型:6 个 sigmoid 概率掩码(6 个张量组成的列表)
  • Shape:每个 (1, 1, 1024, 1024)
  • 取 outputs[0](最高分辨率)作为最终 Alpha Matte
  • 后处理:resize 回原图尺寸,threshold=0.5 二值化

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.38.0HuggingFace Transformers(PreTrainedModel 基类)
safetensors>= 0.4.0模型权重加载
Pillow>= 9.0.0图像 I/O
numpy>= 1.21.0数组操作
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch torch-npu transformers safetensors Pillow numpy

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:ModelScope(推荐,国内访问速度更快)

pip install modelscope
python3 -c "from modelscope import snapshot_download; snapshot_download('briaai/RMBG-1.4', cache_dir='./model')"

方式二:HuggingFace

huggingface-cli download briaai/RMBG-1.4 --local-dir ./model

方式三:AtomGit 镜像(HuggingFace 下载慢时使用)

export HF_ENDPOINT=https://ai.gitcode.com/hf_mirrors
huggingface-cli download briaai/RMBG-1.4 --local-dir ./model

3.3 运行推理

# 单张图片推理
python inference.py \
  --model-path ./model \
  --image-path ./test.jpg \
  --output-mask ./mask.png \
  --output-result ./result.png \
  --device npu

# 批量推理性能测试
python inference.py \
  --model-path ./model \
  --image-path ./test.jpg \
  --device npu \
  --batch

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 的目录路径
--image-pathstr必填输入图片的路径
--output-maskstroutput_mask.png输出掩码的保存路径
--output-resultstroutput_result.png背景去除结果的保存路径
--devicestrnpu推理设备(npu/cpu)
--batchflagFalse运行批量推理基准测试

4. 推理成功日志

4.1 单条推理日志

[模型] briaai/RMBG-1.4 (BriaRMBG)
[设备] Ascend910_9362 (npu:0)
[输入] RGB image (933, 1405) -> resized to (1024, 1024)
[输出] 6 side-output masks, each (1, 1, 1024, 1024)
[主掩码] shape=(1024, 1024), range=[0.0000, 1.0000]
[耗时] 175.2ms
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 4次推理统计:
  总耗时: 460.8ms
  平均: 115.2ms
  P50: 115.2ms
  P99: 115.2ms
  吞吐量: 8.7 images/sec

5. 测试样例及输出结果

样例 1:单张图片背景去除

运行命令:

python inference.py \
  --model-path ./model \
  --image-path ./model/example_input.jpg \
  --output-mask ./test_mask.png \
  --output-result ./test_result.png \
  --device npu

输出:

[INFO] Loading model from: ./model
[INFO] Model: BriaRMBG, 44,046,790 parameters
[INFO] Device: npu
[INFO] Loading image: ./model/example_input.jpg
[INFO] Input tensor shape: torch.Size([1, 3, 1024, 1024])
[INFO] Original image size: (933, 1405)

[INFO] Running inference...
[INFO] Output: list of 6 masks
[INFO]   side1: shape=torch.Size([1, 1, 1024, 1024]), min=0.0000, max=1.0000
[INFO]   side2: shape=torch.Size([1, 1, 1024, 1024]), min=0.0000, max=1.0000
[INFO]   side3: shape=torch.Size([1, 1, 1024, 1024]), min=0.0000, max=1.0000
[INFO]   side4: shape=torch.Size([1, 1, 1024, 1024]), min=0.0000, max=1.0000
[INFO]   side5: shape=torch.Size([1, 1, 1024, 1024]), min=0.0000, max=1.0000
[INFO]   side6: shape=torch.Size([1, 1, 1024, 1024]), min=0.0000, max=1.0000
[INFO] Inference time: 175.2ms

[INFO] Saving mask to: ./test_mask.png
[INFO] Saving result to: ./test_result.png

[STATUS] SUCCESS - Background removal completed

样例 2:批量推理性能测试

运行命令:

python inference.py \
  --model-path ./model \
  --image-path ./model/example_input.jpg \
  --device npu \
  --batch

输出:

[BENCHMARK] Batch inference (4 images)
[BENCHMARK] Total time: 452.6ms
[BENCHMARK] Per-image avg: 113.2ms
[BENCHMARK] Throughput: 8.8 images/sec

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: RMBG-1.4 自带的 example_input.jpg(933×1405),以及通用背景移除基准测试

评测指标:

指标结果说明
单图推理速度120-175 msAscend910_9362,1024×1024
批量推理速度113 ms/张Batch=4,平均耗时
吞吐量8.8 张/秒Batch=4
模型大小169 MB(safetensors)fp32
参数量44,046,790约 44M

评测命令:

python inference.py --model-path ./model --image-path ./test.jpg --device npu
python inference.py --model-path ./model --image-path ./test.jpg --device npu --batch

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 2 卡(单卡推理)
  • 显存占用: ~3.2 GB HBM(推理阶段)
  • CANN 版本: 8.5.1
  • torch_npu 版本: 2.9.0.post1

9. 已知问题

无。