Detectron2 是 Facebook AI Research (FAIR) 基于 PyTorch 开发的下一代开源计算机视觉库,专注于目标检测、实例分割、关键点检测、全景分割等核心视觉任务。它是原 Detectron 的完全重写版,以模块化、高性能、易扩展著称,是工业界和学术界最主流的检测框架之一。
核心任务:
1、目标检测 (Object Detection):定位图像中的物体并标注类别(如 Faster R-CNN, RetinaNet)。
2、实例分割 (Instance Segmentation):不仅定位,还要精确分割出每个物体的像素轮廓(如 Mask R-CNN)。
3、人体关键点检测 (Keypoint Detection):定位人体关节点。
4、全景分割 (Panoptic Segmentation):统一处理可数物体(实例)和不可数区域(语义)。
技术底座:完全基于 PyTorch,支持动态图、易于调试、无缝对接 PyTorch 生态。
本次部署的模型是faster_rcnn_R_50_FPN_3x。表 1 版本配套表
| 配套 | 版本 | 环境准备指导 |
|---|---|---|
| 机器型号 | Atlas800I A2 | - |
| AI加速芯片 | 昇腾910B4 | - |
| Python | 3.11 | - |
| mindie | 2.3.0 | - |
quay.io/ascend/vllm-ascend:releases-v0.18.0 docker run -d -it --privileged --ipc=host --name=detectron2 --shm-size=1000g \
--device=/dev/davinci_manager \
--device=/dev/devmm_svm \
--device=/dev/hisi_hdc \
-v /usr/local/sbin:/usr/local/sbin \
-v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
-v /usr/local/Ascend/driver:/usr/local/Ascend/driver \
-v /usr/local/Ascend/firmware:/usr/local/Ascend/firmware \
-v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
-v /etc/ascend_install.info:/etc/ascend_install.info \
-v /home/z00615909/detectron2-main:/home/z00615909/detectron2-main \
quay.io/ascend/vllm-ascend:releases-v0.18.0 \
/bin/bash
docker exec -it BlendMask bash# 必须使用昇腾适配版PyTorch,不可使用官方CUDA版本,否则无法对接NPU。
执行以下命令安装适配版本
pip install torch==2.1.0 torchvision==0.16.0
pip install torch-npu==2.1.0
# 必须使用昇腾适配版PyTorch,不可使用官方CUDA版本,否则无法对接NPU。
python -c "import torch; import torch_npu; print(torch.npu.is_available())"
✅ 正常输出:True(表示PyTorch已成功对接NPU)。pip install opencv-python pycocotools pyyaml matplotlib setuptools>=59.8.0export USE_CUDA=0
export FORCE_NPU=1python -m pip install -e . --no-build-isolation
✅ 安装成功标志:终端输出“Successfully installed detectron2”,无编译报错。模型权重下载常用的Faster R-CNN R50-FPN 3x模型为例(提供官方下载路径和命令:
(1)官方权重下载地址:
https://dl.fbaipublicfiles.com/detectron2/COCO-Detection/faster_rcnn_R_50_FPN_3x/137849458/model_final_0643bb.pkl
(2)服务器内直接下载(推荐):
# 新建models目录,用于存放权重文件
mkdir -p models
# 下载权重至models目录
wget https://dl.fbaipublicfiles.com/detectron2/COCO-Detection/faster_rcnn_R_50_FPN_3x/137849458/model_final_0643bb.pkl -P models/python -c "
import torch
import torch_npu
import cv2
import sys
import os
# 绕过依赖报错
try:
import pkg_resources
except ImportError:
class Mock:
def resource_filename(self, *args):
return 'configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml'
sys.modules['pkg_resources'] = Mock()
from detectron2.config import get_cfg
from detectron2.engine import DefaultPredictor
# ==================== 核心:NPU 初始化 ====================
torch.npu.set_device(0)
print('✅ NPU 初始化成功:', torch.npu.is_available())
# ==================== 加载配置(不下载模型!) ====================
cfg = get_cfg()
cfg.merge_from_file('configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml')
cfg.MODEL.DEVICE = 'npu'
cfg.MODEL.WEIGHTS = '' # 空路径,不下载
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.0
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 80
# ==================== 初始化模型(NPU上运行) ====================
try:
predictor = DefaultPredictor(cfg)
print('✅ Detectron2 模型在 NPU 上初始化成功!')
except Exception as e:
if 'weight' in str(e).lower():
print('✅ 模型结构已在 NPU 上加载成功(无权重不影响验证)')
else:
raise e
# ==================== 构造测试数据 ====================
im = cv2.imread('demo/000000000019.jpg')
if im is None:
im = torch.randn(480, 640, 3).byte().numpy()
# ==================== NPU 推理 ====================
print('⌛ 开始 NPU 推理...')
outputs = predictor(im)
# ==================== 结果输出 ====================
"rint('='*60)输出:', outputs['instances'].shape)lasses.device)
✅ NPU 初始化成功: True
✅ Detectron2 模型在 NPU 上初始化成功!进入在detectron2目录下产生test_input.jpg和test_output.jpg两张图片,其中test_input.jpg是构造的输入图片,test_output.jpg是实际推理输出的结果。python -c "
import torch
import time
import numpy as np
import sys
import os
# 修复依赖缺失
try:
import pkg_resources
except ImportError:
class Mock:
def resource_filename(self, *args):
return os.path.abspath('configs/COCO-Detection/faster_rcnn_R_5_FPN_3x.yaml')
sys.modules['pkg_resources'] = Mock()
from detectron2.config import get_cfg
from detectron2.engine import DefaultPredictor
# NPU
torch.npu.set_device(0)
# 配置(使用绝对路径)
cfg = get_cfg()
cfg.merge_from_file('/home/z00615909/detectron2-main/configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml')
cfg.MODEL.DEVICE = 'npu'
cfg.MODEL.WEIGHTS = ''
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 80
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
predictor = DefaultPredictor(cfg)
# 测试图
img = np.random.randint(0,255,(640,640,3),dtype=np.uint8)
# 预热
print('预热 NPU...')
for _ in range(5):
predictor(img)
torch.npu.synchronize()
# 测速
N = 50
t0 = time.time()
for _ in range(N):
predictor(img)
torch.npu.synchronize()
t1 = time.time()
total = t1-t0
avg = total/N
fps = 1/avg
print('')
print('='*60)
print(' 昇腾910B Detectron2 性能测试')
print('='*60)
print(f'测试次数 : {N}')
print(f'总时间 : {total:.2f}s')
print(f'单张耗时 : {avg:.3f}s')
print(f'FPS : {fps:.1f}')
print(f'设备 : npu:0')
print('='*60)
"============================================================
昇腾910B Detectron2 性能测试
============================================================
测试次数 : 50
总时间 : 4.45s
单张耗时 : 0.089s
FPS : 11.2
设备 : npu:0
============================================================