概述
YOLOv7是yolo系列目标检测网络,在5 FPS到160 FPS范围内的速度和精度达到了新的高度,并在GPU V100上具有30 FPS或更高的所有已知实时目标检测器中具有最高的精度56.8%AP。
环境
华为云ai1s
CPU:Intel® Xeon® Gold 6278C CPU @ 2.60GHz
内存:8G
NPU:Ascend 310
操作系统:Ubuntu 18.04.4 LTS
代码仓地址
https://gitee.com/ascend/modelzoo-GPL/tree/master/built-in/ACL_Pytorch/Yolov8_for_PyTorch
实验记录
下载源码
获取源码
git clone https://gitee.com/ascend/modelzoo-GPL
cd modelzoo-GPL
cp -r built-in/ACL_Pytorch/Yolov7_for_Pytorch ../
cd Yolov7_for_Pytorch
git clone https://github.com/WongKinYiu/yolov7.git
cd yolov7
mkdir output # 新建output文件夹,作为模型结果的默认保存路径
安装依赖
pip3.7.5 install -r requirements.txt
获取OM推理代码
将推理部署代码放到Pytorch源码相应目录下。
YOLOv7_for_PyTorch
├── aipp.cfg 放到yolov7下
├── atc.sh 放到yolov7下
└── om_nms_acc.py 放到yolov7下
cp ../aipp.cfg ./
cp ../atc.sh ./
cp ../om_nms_acc.py ./
准备数据集
模型使用coco2017 val数据集进行精度评估,在Pytorch源码根目录下新建coco文件夹,数据集放到coco里
mkdir coco
cd coco
wget http://images.cocodataset.org/zips/val2017.zip
wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip
unzip val2017.zip
mkdir image
mv val2017 image/
unzip annotations_trainval2017.zip
模型推理
获取权重
wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7x.pt
导出ONNX模型
python3.7.5 export.py --weights=yolov7x.pt --grid --img-size=640 --dynamic-batch --simplify
_bz2报错解决:
sudo cp /usr/lib/python3.7/lib-dynload/_bz2.cpython-37m-x86_64-linux-gnu.so /usr/local/python3.7.5/lib/python3.7/lib-dynload/
_lzma报错
sudo apt-get install liblzma-dev -y
pip3.7.5 install backports.lzma
修改lzma.py文件
#修改前
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
#修改后
try:
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
except ImportError:
from backports.lzma import *
from backports.lzma import _encode_filter_properties, _decode_filter_properties
安装依赖
sudo apt-get install libprotobuf-dev protobuf-compiler
依赖安装完成后继续执行export.py
使用ATC工具将ONNX模型转OM模型
source /usr/local/Ascend/ascend-toolkit/set_env.sh
执行atc命令
bash atc.sh yolov7x.onnx yolov7x_bs8 8 Ascend310P3
推理验证
安装工具
pip3.7.5 install -v 'git+https://gitee.com/ascend/tools.git#egg=aclruntime&subdirectory=ais-bench_workload/tool/ais_bench/backend'
pip3.7.5 install -v 'git+https://gitee.com/ascend/tools.git#egg=ais_bench&subdirectory=ais-bench_workload/tool/ais_bench'
执行推理
python3.7.5 om_nms_acc.py --model=yolov7x_bs8.om --output=output --batch=8 --conf-thres=0.001 --iou-thres=0.65 --device=0 --eval
性能验证
python3.7.5 -m ais_bench --model=yolov7x_bs8.om --output=output --batchsize=8 --device=0 --loop=1000