f
gcw_pCMO8DfP/RMBG-1.4
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

RMBG-1.4 — 背景移除 / 前景抠图模型 (昇腾 NPU 版)

RMBG-1.4 是 BRIA AI 推出的背景移除模型,基于 U2-Net 风格 (BriaRMBG) 架构:conv_in(3→64, stride=2) → RSU7/6/5/4/4F 编码器 → 对称解码器 → 6 路 side 输出,输入 1024×1024 RGB,输出 1 通道前景 mask(平均 6 个 side 输出)。模型约 44.0M 参数,纯 Conv2d/BatchNorm2d/ReLU/MaxPool2d/F.interpolate,无私有 GPU 算子依赖。本仓库为 昇腾 Ascend NPU (torch_npu) 适配版,全流程运行于 npu:0。

环境要求

  • Python 3.11
  • 昇腾 NPU (Ascend910) + torch_npu 2.9 + CANN
  • 依赖: torch torch_npu transformers fastapi uvicorn python-multipart safetensors pillow numpy torchvision

快速开始

# 命令行单图推理 (输入图片 -> 前景/背景 mask + 指标 JSON)
python3 inference.py --img sample_input.png

# 启动 FastAPI 服务 (根路径为上传表单, POST /predict 返回推理 JSON)
python3 inference.py --server --port 8010

命令行参数

参数说明
--img输入图片路径 (必需)
--server启动 FastAPI Web 服务
--portFastAPI 服务端口 (默认 8000)

NPU 适配说明

  • 设备:torch.npu.is_available()=True,set_device(0),模型与输入 Tensor 全部 .to("npu:0"),全域无 cuda 调用。
  • 建模代码:从 HF auto_map vendored 于 ./briarmbg/(briarmbg.py + MyConfig.py + MyPipe.py),并在 MyPipe.py 中移除 skimage 依赖、设备改为 npu:0。
  • 前向输出:BriaRMBG.forward 返回 [sigmoid(d1..d6), [hx1d..hx6]],最终 mask = 6 个 side 输出逐像素平均,再 bilinear 上采样回原图尺寸。
  • 预处理:与官方 MyPipe.preprocess_image 一致 —— bilinear resize 到 1024×1024、/255、normalize mean=[0.5,0.5,0.5]、std=[1,1,1](见 preprocessor_config.json)。
  • 权重:./model_weights/briaai/RMBG-1.4/model.safetensors(798 张量,strict missing=0/unexpected=0)。

输出格式

{
  "model": "briaai/RMBG-1.4",
  "device": "npu:0",
  "dtype": "torch.float32",
  "image_size": {"width": 800, "height": 600},
  "mask_size": {"width": 800, "height": 600, "channels": 1},
  "inference_time_s": 0.2,
  "foreground_pixel_ratio": 0.0622,
  "foreground_bbox": {"x_min": 311, "y_min": 42, "x_max": 718, "y_max": 479, "w": 408, "h": 438},
  "contains_subject": true,
  "mask_base64_png": "..."  // FastAPI 模式返回前景 mask 的 base64 PNG
}

验证截图

  • assets/agent_workflow.png —— 昇腾 NPU 环境部署 + 任务启动日志
  • assets/npu_device_call.png —— CLI python inference.py --img sample_input.png 推理成功 (npu:0, 0.2s)
  • assets/model_result.png —— FastAPI POST /predict 上传图片返回推理 JSON

原始日志存于 assets/_logs/。

免责声明

本仓库仅用于技术验证与学习。模型权重版权归 BRIA AI 所有,License 请以官方仓库为准。