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。
torch_npu 2.9 + CANNtorch 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 服务 |
--port | FastAPI 服务端口 (默认 8000) |
torch.npu.is_available()=True,set_device(0),模型与输入 Tensor 全部 .to("npu:0"),全域无 cuda 调用。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 请以官方仓库为准。