BlendMask 实例分割模型(ResNet-101 + FPN + FCOS + BezierAlign),在华为昇腾 Ascend 910B NPU 上的推理部署与性能优化。
| 组件 | 版本 |
|---|---|
| CANN | 8.5.1 |
| Python | 3.11.x |
| PyTorch | 2.9.0 |
| torch_npu | 2.9.0.post1 |
| Ascend NPU | 910B (64GB HBM) |
# 1. 克隆仓库
git clone https://atomgit.com/weixin_72661020/BlendMask-NPU-Ascend910-Optimized.git
cd BlendMask-NPU-Ascend910-Optimized
# 2. 下载模型权重
# 从 HuggingFace 下载 R_101_3x.pth (211MB)
export HF_ENDPOINT=https://hf-mirror.com
hfd.sh ZjuCv/AdelaiDet --tool aria2c -d ./models/blendmask/
# 或使用 aria2c 直接下载
aria2c -x 16 -s 16 https://hf-mirror.com/ZjuCv/AdelaiDet/resolve/main/R_101_3x.pth \
-o models/blendmask/R_101_3x.pth
# 3. 安装依赖框架
git clone --depth 1 https://github.com/facebookresearch/detectron2.git
git clone --depth 1 https://github.com/aim-uofa/AdelaiDet.git adelai
# 编译 AdelaiDet C++ 扩展(需要修复 BezierAlign_cpu.cpp 的 PyTorch 2.9 API 兼容性)
sed -i 's/input\.type()/input.scalar_type()/g; s/grad\.type()/grad.scalar_type()/g' \
adelai/adet/layers/csrc/BezierAlign/BezierAlign_cpu.cpp
FORCE_CUDA=0 pip install --no-build-isolation -e ./detectron2
FORCE_CUDA=0 pip install --no-build-isolation -e ./adelai
# 4. 下载 COCO2017 验证集
# 从 ModelScope 下载
export MODELSCOPE_API_TOKEN="your_token"
modelscope download --dataset PAI/COCO2017 --local_dir ./datasets/coco \
--include "val2017.zip" --include "annotations_trainval2017.zip"
cd ./datasets/coco && unzip -q val2017.zip && unzip -q annotations_trainval2017.zip && cd ../..
# 5. 安装其他依赖
pip install scikit-image# 单张图片推理
python inference.py --image test.jpg --weights models/blendmask/R_101_3x.pth
# COCO 验证集评估(优化配置)
bash scripts/eval_coco.sh optimized
# 性能基准测试
python inference.py --benchmark --benchmark-iters 200 --batch-size 2通过以下手段实现端到端加速:
| 优化手段 | 说明 |
|---|---|
| AMP FP16 | 自动混合精度推理(torch.npu.amp.autocast) |
| CPU_AFFINITY_CONF=2 | NPU 线程绑核,减少调度漂移 |
| TASK_QUEUE_ENABLE=2 | 启用一级流水线优化 |
| RSE 调整 | ResizeShortestEdge 从 800 → 755 |
| INFERENCE_TH 调优 | 推理阈值 0.082,减少冗余检测 |
USE_AMP=1 ASCEND_VISIBLE_DEVICES=1 TASK_QUEUE_ENABLE=2 CPU_AFFINITY_CONF=2 \
D2_INFERENCE_TH=0.082 D2_TEST_RSE_MIN=755 D2_POST_NMS_TOPK=90 \
python3 eval/eval_npu.pyCOCO 2017 val (5000 images, batch_size=2, NPU:1):
| 配置 | E2E 时间 (s) | 加速比 | 说明 |
|---|---|---|---|
| 原始基线 (RSE800, INFERENCE_TH=0.05) | 393 | - | 默认配置 |
| 此前优化 (RSE755, INFERENCE_TH=0.082) | 327 | 16.8% | 参数调优 |
| AMP FP16 + 绑核 + TASK_QUEUE | 268 | 31.8% | 本仓库最优 |
| 配置 | mask AP | AP50 | AP75 | APs | APm | APl | 误差 |
|---|---|---|---|---|---|---|---|
| 论文基线 | 39.44 | 61.2 | 42.4 | 20.9 | 42.8 | 56.8 | - |
| AMP FP16 优化 | 39.24 | 60.8 | 42.2 | 20.1 | 42.5 | 56.7 | 0.5% |
精度误差 0.5%,远小于 1% 的容忍范围。
Segmentation Results:
AP (IoU=0.50:0.95): 39.239
AP50: 60.755
AP75: 42.238
APs: 20.067
APm: 42.545
APl: 56.710
=== RSE 755: mask AP = 39.24 ===
=== E2E_TIME=268.33s ===| 文件 | 说明 |
|---|---|
inference.py | 单张图片推理 + 性能基准测试脚本 |
eval/eval_npu.py | NPU 优化版 COCO 评估脚本(AMP + 环境变量调优) |
eval/eval_rse.py | 原始 COCO 评估脚本 |
scripts/eval_coco.sh | COCO 评估启动脚本(baseline/optimized 配置) |
scripts/benchmark.sh | 性能基准测试脚本 |
optimization_results.tsv | 完整优化迭代记录(40+ 轮) |
logs/ | 评估运行日志 |
eval_baseline.log | 基线配置评估日志 |
eval_optimized.log | 优化配置评估日志 |
详见 optimization_results.tsv,共记录了 40+ 轮优化迭代,包括:
torchvision_npu 适配器(已安装,待 PyTorch 2.9 兼容版本)