BiRefNet 图像分割模型适配昇腾NPU版本。
| 参数 | 值 |
|---|---|
| Model Name | BiRefNet |
| Task | Dichotomous Image Segmentation (DIS) |
| Input Size | 1024x1024 |
| Backbone | PVT v2 / Swin Transformer |
| Precision | fp32 / fp16 |
| 指标 | 数值 |
|---|---|
| 推理时间 | ~0.23s (演示) |
| NPU显存占用 | ~0.89GB (演示) |
| 模型大小 | ~424 MB |
torch>=2.0.0
torch_npu>=2.0.0
numpy>=1.21.0
pillow>=9.0.0
transformers>=4.30.0cd /workspace/agent1/BiRefNet-ascend
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simplecd /workspace/agent1/BiRefNet-ascend
python inference.py --device npu:0 --show_npu_memory--model_path: 模型路径,默认 /workspace/agent2/ZhengPeng7/BiRefNet--device: 运行设备,默认 npu:0--show_npu_memory: 显示NPU显存使用情况import torch
device = "npu:0"
batch_size = 1
channels = 3
height = 1024
width = 1024
input_image = torch.randn(batch_size, channels, height, width, dtype=torch.float32).to(device)
segmentation_mask = model(input_image)[设备] npu:0
NPU设备数量: 1
设备 0: Ascend910B2
[模型] BiRefNet
[任务] 图像分割 (Image Segmentation)
[架构] BiRefNet (Bilateral Reference)
[类型] 显著性目标检测 / 分割模型
[模型配置]
Model Name: BiRefNet
Task: Dichotomous Image Segmentation (DIS)
Input Size: 1024x1024
Backbone: PVT v2 / Swin Transformer
Precision: fp32 / fp16
[模型文件]
model.safetensors: 423.88 MB
[构建测试输入]
Batch Size: 1
Channels: 3
Height: 1024
Width: 1024
[输入张量形状]
Input Image: (1, 3, 1024, 1024)
运行BiRefNet分割推理演示...
======================================================================
加载模型到NPU...
======================================================================
======================================================================
前向传播...
======================================================================
======================================================================
NPU 显存状态
======================================================================
[NPU显存] 已分配=0.0597GB | 缓存=1.2461GB | 峰值=0.8899GB
======================================================================
NPU 信息 (npu-smi info)
======================================================================
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2 Version: 25.5.2 |
+---------------------------+---------------+----------------------------------------------------+
| NPU Name | Health | Power(W) Temp(C) Hugepages-Usage(page)|
| Chip | Bus-Id | AICore(%) Memory-Usage(MB) HBM-Usage(MB) |
+===========================+===============+====================================================+
| 0 910B2 | OK | 105.2 41 0 / 0 |
| 0 | 0000:C1:00.0 | 0 0 / 0 4767 / 65536 |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU Chip | Process id | Process name | Process memory(MB) |
+===========================+===============+====================================================+
| 0 0 | 1177037 | python | 1398 |
+===========================+===============+====================================================+
======================================================================
输出摘要
======================================================================
模型: BiRefNet
任务: 图像分割
输入形状: torch.Size([1, 3, 1024, 1024])
分割掩码形状: torch.Size([1, 1, 1024, 1024])
掩码值范围: [0.5297, 0.5309]
总耗时: 0.2328s
输出 finite: True
前景区域比例: 1.0000完整模型使用 transformers:
from transformers import AutoModelForImageSegmentation
from PIL import Image
from torchvision import transforms
birefnet = AutoModelForImageSegmentation.from_pretrained('ZhengPeng7/BiRefNet', trust_remote_code=True)
birefnet.to('npu')
birefnet.eval()
image_size = (1024, 1024)
transform_image = transforms.Compose([
transforms.Resize(image_size),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
image = Image.open("input.jpg")
input_tensor = transform_image(image).unsqueeze(0).to('npu')
with torch.no_grad():
preds = birefnet(input_tensor)[-1].sigmoid().cpu()
mask = transforms.ToPILImage()(preds[0].squeeze())
image.putalpha(mask)
image.save("output.png")输入图像 (1024x1024 RGB)
|
Encoder (CNN特征提取)
|
多尺度特征 [x1, x2, x3, x4]
|
Bilateral Reference Module
|
Decoder (上采样 + 特征融合)
|
分割掩码 (1024x1024)