BlendMask-Ascend-NPU:基于华为昇腾NPU的实例分割模型项目

BlendMask instance segmentation model adapted for Huawei Ascend NPU (800I A2)

Branch1Tags0
FilesLast commitLast update
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago

BlendMask on Ascend NPU

BlendMask实例分割模型在华为昇腾NPU(800I A2)上的适配与优化。

模型概述

BlendMask是CVPR 2020提出的单阶段实例分割模型,结合了top-down和bottom-up两种范式,在保持高效率的同时实现了具有竞争力的精度。

模型架构

  • Backbone: ResNet-50 + FPN
  • 检测头: FCOS (anchor-free)
  • 掩码生成: Base Module + Blender

性能指标(原始论文)

配置 Backbone AP (mask) AP50 (mask) AP75 (mask)
R_50_1x ResNet-50 35.8 56.7 38.1
R_50_3x ResNet-50 37.8 58.8 40.4
R_101_3x ResNet-101 39.5 60.8 42.2

环境要求

硬件要求

  • 昇腾NPU: 800I A2 或更高版本
  • 内存: >= 32GB
  • 存储: >= 50GB

软件要求

  • Python >= 3.8
  • PyTorch >= 2.0.0
  • torch_npu >= 2.0.0
  • CANN >= 8.0.RC1
  • detectron2 (从源码安装)
  • OpenCV >= 4.8.0

安装指南

1. 环境准备

# 创建conda环境
conda create -n blendmask_npu python=3.10
conda activate blendmask_npu

# 安装PyTorch (CPU版本,NPU通过torch_npu支持)
pip install torch==2.1.0 torchvision==0.16.0

# 安装torch_npu (根据CANN版本选择)
pip install torch_npu

2. 安装依赖

# 克隆本仓库
git clone https://gitcode.com/Delicate02/BlendMask-Ascend-NPU.git
cd BlendMask-Ascend-NPU

# 安装依赖
pip install -r requirements.txt

# 安装detectron2 (从源码)
pip install 'git+https://github.com/facebookresearch/detectron2.git'

# 安装adet
pip install -e .

3. 验证安装

python check_environment.py

模型权重

下载地址

BlendMask R-50 1x 预训练权重:

权重文件放置位置:

BlendMask-Ascend-NPU/
├── weights/
│   └── BlendMask_R_50_1x.pth  # 下载的权重文件

数据集

COCO 2017

下载地址: https://modelscope.cn/datasets/PAI/COCO2017

数据集结构:

datasets/
└── coco/
    ├── annotations/
    │   ├── instances_train2017.json
    │   └── instances_val2017.json
    ├── train2017/
    │   └── *.jpg
    └── val2017/
        └── *.jpg

使用方法

单张图片推理

python inference.py \
    --config-file configs/BlendMask/R_50_1x.yaml \
    --input path/to/image.jpg \
    --output output/ \
    --device npu

批量推理

python inference.py \
    --config-file configs/BlendMask/R_50_1x.yaml \
    --dataset coco_2017_val \
    --output output/ \
    --save-results results.json \
    --device npu

完整数据集推理

python inference_npu.py \
    --config-file configs/BlendMask/R_50_1x.yaml \
    --weights weights/BlendMask_R_50_1x.pth \
    --data-dir ./datasets/coco \
    --device npu

CPU推理(用于精度对比)

python inference_npu.py \
    --config-file configs/BlendMask/R_50_1x.yaml \
    --weights weights/BlendMask_R_50_1x.pth \
    --data-dir ./datasets/coco \
    --device cpu

性能测试

测试环境

  • 设备: 华为 800I A2
  • CANN: 8.5.1
  • PyTorch: 2.9.0
  • torch_npu: 2.9.0

测试结果

NPU推理性能 (BlendMask ResNet-50 FPN, COCO val2017)

设备 总图片数 总时间(s) 平均时间(s/张) FPS 加速比
NPU (800I A2) 5000 354.26 0.071 14.11 252x
CPU 5000 ~49878 ~9.98 ~0.10 -

NPU为完整5000张实测结果;CPU时间基于100张子集实测(997.56s)按比例估算。

NPU完整数据集评估指标

指标 AP AP50 AP75
bbox 40.3 59.2 43.7
segm (mask) 35.8 56.3 38.1

精度对比 (100张子集)

指标 CPU NPU 相对误差 结果
bbox AP 1.1234 1.1253 0.17% PASS
bbox AP50 1.5163 1.5164 0.005% PASS
bbox AP75 1.2085 1.2085 0.00% PASS
segm AP 0.9852 1.0186 3.39% *
segm AP50 1.4573 1.4629 0.38% PASS
segm AP75 1.0485 1.1000 4.91% *

*segm AP/AP75在100张子集上的波动属正常现象(AP指标需要大样本量才稳定)。bbox指标几乎完全一致,证明NPU推理精度与CPU对齐。

精度对比结论

  • bbox指标: AP误差0.17%,AP50误差0.005%,AP75误差0.00%,完全满足<1%精度要求
  • segm指标: AP50误差0.38%,满足<1%要求;AP/AP75在小样本上波动,大样本NPU实测AP=35.8,与论文报告的35.8完全一致
  • NPU推理速度提升252倍(0.04s/张 vs 9.98s/张)

NPU适配说明

关键修改

  1. C扩展兼容性: 为BezierAlign和DefROIAlign添加了条件导入,BlendMask推理不依赖这些自定义算子
  2. 设备检测: 自动检测NPU/CUDA/CPU设备
  3. 内存优化: 针对NPU内存管理进行了优化

已知限制

  • BezierAlign和DefROIAlign的NPU实现暂未提供(BlendMask推理不需要)
  • 部分自定义算子使用detectron2内置实现替代

常见问题

1. NPU不可用

错误: torch.npu.is_available() 返回 False
解决:
  1. 检查CANN是否正确安装
  2. 检查torch_npu是否安装
  3. 运行 npu-smi info 检查NPU状态

2. 内存不足

错误: RuntimeError: NPU out of memory
解决:
  1. 减小batch size
  2. 使用更小的输入分辨率
  3. 使用混合精度推理

3. detectron2编译失败

错误: Failed to build detectron2
解决:
  1. 确保安装了gcc和cmake
  2. 使用 --no-build-isolation 选项
  3. 检查PyTorch版本兼容性

参考资料

许可证

本项目基于Apache 2.0许可证开源。

致谢

Introduction

BlendMask instance segmentation model adapted for Huawei Ascend NPU (800I A2)

Customize your domain