YOLOv7 推理加速实战:OpenVINO 与 TorchORT 两种流程对比
这篇教程根据我复现 YOLOv7 推理加速流程时整理,重点演示环境安装、数据准备、普通模型推理、TorchORT 推理和结果可视化。
本文整理自我的学习和项目复现过程,尽量按实操顺序保留 notebook 的关键步骤,同时把数据集获取方式调整为适合中文教程发布的写法。
本文会重点跑通以下流程:
- 安装 YOLOv7 环境
- 从数据集后台获取测试数据
- 挂载训练权重目录
- 运行普通推理流程
- 安装 TorchORT 并运行加速推理
如果你正在系统学习目标检测、实例分割、OCR、多目标跟踪或视觉大模型,建议收藏本文;配套 notebook、示例图片和运行环境说明后续会继续整理。如果环境配置卡住,可以在评论区说明具体报错。
📚 文章目录
⚙️ 安装 YOLOv7
克隆 YOLOv7 仓库并安装依赖。
# 下载 YOLOv7 仓库并安装依赖
!git clone https://github.com/WongKinYiu/yolov7.git
%cd yolov7
!pip install -r requirements.txt
🧩 安装 PyTorch
安装 notebook 指定的 PyTorch 版本。
!pip install torch==1.12.1
📦 准备测试数据
从数据集后台获取 YOLOv7 格式数据,并配置本地路径。
from types import SimpleNamespace
# 从数据集后台下载 YOLOv7 检测 格式数据集后,修改 DATASET_DIR 指向解压目录。
DATASET_DIR = "/content/dataset" # 修改为数据集后台导出的数据集目录
dataset = SimpleNamespace(location=DATASET_DIR, version="1", name="custom-dataset")
💾 挂载权重目录
挂载云盘或设置本地权重路径。
# 挂载云盘或设置本地权重路径。
from google.colab import drive
drive.mount('/content/gdrive')
TRAINED_MODEL_PATH = "/content/gdrive/MyDrive/TrainedModel/best.pt"
🧪 普通权重推理
使用普通 YOLOv7 推理脚本运行样例图片。
# If you DO NOT have a retrained model, use this command to run evaluation with the provided trained model:
!python detect_without_jit.py --weights /content/yolov7/runs/best.pt --conf 0.25 --img-size 640 --source UWH-6/test/images/DJI_0021_mp4-32_jpg.rf.0d9b746d8896d042b55a14c8303b4f36.jpg
# Run evaluation on a sample image with a retrained model
!python detect_without_jit.py --weights /content/gdrive/MyDrive/TrainedModel/best.pt --conf 0.25 --img-size 640 --source UWH-6/test/images/DJI_0021_mp4-32_jpg.rf.0d9b746d8896d042b55a14c8303b4f36.jpg
🖼️ 普通推理结果
展示普通推理结果图片。
# 展示样例图片推理结果
import glob
from IPython.display import Image, display
i = 0
limit = 10000 # max images to print
# Change the path below according to the location where result image is saved.
for imageName in glob.glob('/content/yolov7/runs/detect/exp/*.jpg'): #assuming JPG.
if i < limit:
display(Image(filename=imageName))
print("\n")
i = i + 1

🚀 安装 TorchORT
安装 TorchORT 推理依赖。
# Install torch-ort-infer
!pip install torch-ort-infer
⚡ TorchORT 推理
使用 TorchORT 推理脚本运行样例图片。
# If you DO NOT have a retrained model, use this command to run evaluation using the provided trained model:
!python detect_ort.py --weights /content/yolov7/runs/best.pt --conf 0.25 --img-size 640 --source UWH-6/test/images/DJI_0021_mp4-32_jpg.rf.0d9b746d8896d042b55a14c8303b4f36.jpg
# Run evaluation on a sample image with a retrained model
!python detect_ort.py --weights /content/gdrive/MyDrive/TrainedModel/best.pt --conf 0.25 --img-size 640 --source UWH-6/test/images/DJI_0021_mp4-32_jpg.rf.0d9b746d8896d042b55a14c8303b4f36.jpg
🖼️ TorchORT 结果
展示 TorchORT 推理结果图片。
# 展示样例图片推理结果
import glob
from IPython.display import Image, display
i = 0
limit = 10000 # max images to print
for imageName in glob.glob('/content/yolov7/runs/detect/exp2/*.jpg'): #assuming JPG
if i < limit:
display(Image(filename=imageName))
print("\n")
i = i + 1

📌 小结
这篇教程完整整理了 YOLOv7 OpenVINO 与 TorchORT 推理 的核心复现流程。实际操作时,建议先确认 GPU、依赖版本、数据集路径和模型权重路径,再逐段运行 notebook。
后续我会继续按源项目顺序整理同系列中的目标检测、实例分割、OCR、多目标跟踪和视觉大模型教程。
📚 同系列教程汇总
-
YOLOv7 推理加速实战:OpenVINO 与 TorchORT 两种流程对比
432

被折叠的 条评论
为什么被折叠?



