遇见 YOLO26: 下一代视觉 AI。
Ultralytics
集成

在 CPU 上部署使用 Neural Magic 的 DeepSparse 的 Ultralytics YOLOv5 以实现 GPU 级性能

利用 Neural Magic 的 DeepSparse 增强 Ultralytics YOLOv5 模型训练与部署,在 CPU 上实现 GPU 级的性能。实现更快、可扩展的 YOLOv5 部署。

ULUltralytics 团队
5 min read
在 CPU 上部署使用 Neural Magic 的 DeepSparse 的 Ultralytics YOLOv5

想要加速你 YOLOv5 模型的训练和部署吗?我们为你提供了解决方案!向你介绍我们的最新合作伙伴 Neural Magic。由于 Neural Magic 提供的软件工具强调极致的模型性能和简化的工作流程,因此我们携手合作,共同提供一种能进一步优化 YOLOv5 部署过程 的解决方案,这再自然不过了。

DeepSparse 是 Neural Magic 的 CPU 推理运行时,它利用神经网络中的稀疏性和低精度算术,在普通硬件上提供卓越的性能。例如,与 ONNX Runtime 基准相比,DeepSparse 在同一台机器上运行 YOLOv5s 时实现了 5.8 倍的加速

YOLOv5 通过 DeepSparse 实现的吞吐量对比

你的深度学习工作负载首次能够满足生产环境的性能需求,而无需复杂且昂贵的硬件加速器。简单来说,DeepSparse 为你提供 GPU 的性能和软件的简洁性:

  1. 灵活部署:在云端、数据中心和边缘侧使用任何硬件提供商,都能保持一致的运行效果
  2. 无限扩展性:通过标准 Kubernetes 进行横向扩展,垂直扩展至数百个核心,或通过无服务器架构实现完全抽象
  3. 轻松集成:使用整洁的 API 将你的模型集成到应用程序中,并在生产环境中进行监控

Link to this section在普通 CPU 上实现 GPU 级的性能#

DeepSparse 利用模型稀疏性来获得性能加速。

通过剪枝和量化实现的稀疏化,可以在保持高准确率的同时,大幅减少执行网络所需的存储空间和计算量。DeepSparse 具备稀疏感知能力,可跳过零值的乘加运算,并在前向传播过程中缩减计算量。由于稀疏计算受限于内存,DeepSparse 会以深度优先的方式执行网络,将问题分解为张量列(Tensor Columns),这些垂直计算条目可以更好地适应缓存。

DeepSparse 和 Ultralytics YOLOv5 网络

具有压缩计算且按深度在缓存中执行的稀疏网络,使 DeepSparse 能够在 CPU 上提供 GPU 级的性能!

Link to this section创建在自定义数据上训练的 YOLOv5 稀疏版本#

Neural Magic 的开源模型库 SparseZoo 包含每个 YOLOv5 模型的预稀疏化检查点。使用与 Ultralytics 集成的 SparseML,你可以通过单个 CLI 命令将稀疏检查点微调到你的数据上。

Link to this section使用 DeepSparse 部署 YOLOv5#

Link to this section安装 DeepSparse#

运行以下命令来安装 DeepSparse。我们建议你使用带有 Python 的虚拟环境。

pip install deepsparse[server,yolo,onnxruntime]

Link to this section获取 ONNX 文件#

DeepSparse 接受 ONNX 格式的模型,可以通过以下方式传入:

  • ONNX 模型的本地路径
  • 识别 SparseZoo 中模型的 SparseZoo 存根(stub)

我们将比较标准稠密 YOLOv5s 与通过以下 SparseZoo 存根识别的剪枝量化 YOLOv5s:

zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none
zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none

Link to this section部署模型#

DeepSparse 提供便捷的 API,用于将你的模型集成到应用程序中。

要尝试下面的部署示例,请拉取示例图像并使用以下命令保存为 basilica.jpg:

wget -O basilica.jpg https://raw.githubusercontent.com/neuralmagic/deepsparse/main/src/deepsparse/yolo/sample_images/basilica.jpg

Link to this sectionPython API#

管道(Pipelines)在运行时周围封装了预处理和输出后处理功能,为将 DeepSparse 添加到应用程序提供了整洁的接口。DeepSparse-Ultralytics 集成包含一个开箱即用的 Pipeline,它接受原始图像并输出边界框。

创建 Pipeline 并运行推理:

from deepsparse import Pipeline

# list of images in local filesystem
images = ["basilica.jpg"]

# create Pipeline
model_stub = "zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none"
yolo_pipeline = Pipeline.create(
    task="yolo",
    model_path=model_stub,
)

# run inference on images, receive bounding boxes + classes
pipeline_outputs = yolo_pipeline(images=images, iou_thres=0.6, conf_thres=0.001)
print(pipeline_outputs)

如果你在云端运行,可能会遇到 open-cv 找不到 libGL.so.1 的错误。在 Ubuntu 上运行以下命令即可安装:

apt-get install libgl1-mesa-glx

Link to this sectionHTTP 服务器#

DeepSparse Server 在流行的 FastAPI Web 框架和 Uvicorn Web 服务器之上运行。只需单个 CLI 命令,你就可以轻松建立一个带有 DeepSparse 的模型服务端点。服务器支持来自 DeepSparse 的任何 Pipeline,包括使用 YOLOv5 进行目标检测,使你能够将原始图像发送到端点并接收边界框。

启动带有剪枝量化 YOLOv5s 的服务器:

deepsparse.server \
  --task yolo \
  --model_path zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none

一个使用 Python requests 包的示例请求:

import requests, json

# list of images for inference (local files on client side)
path = ['basilica.jpg']
files = [('request', open(img, 'rb')) for img in path]

# send request over HTTP to /predict/from_files endpoint
url = 'http://0.0.0.0:5543/predict/from_files'
resp = requests.post(url=url, files=files)

# response is returned in JSON
annotations = json.loads(resp.text)  # dictionary of annotation results
bounding_boxes = annotations["boxes"]
labels = annotations["labels"]

Link to this sectionAnnotate CLI#

你还可以使用 annotate 命令让引擎在磁盘上保存带标注的照片。尝试 --source 0 来标注你的实时网络摄像头画面!

deepsparse.object_detection.annotate --model_filepath zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none --source basilica.jpg

运行上述命令将创建一个 annotation-results 文件夹,并将标注后的图像保存在其中。

使用 YOLOv5 进行标注的巴西利卡

Link to this section基准测试性能#

使用 DeepSparse 的基准测试脚本,我们将比较 DeepSparse 与 ONNX Runtime 在 YOLOv5s 上的吞吐量。

基准测试是在 AWS c6i.8xlarge 实例(16 核)上运行的。

Link to this sectionBatch 32 性能对比#

Link to this sectionONNX Runtime 基准#

在 Batch 32 时,ONNX Runtime 使用标准稠密 YOLOv5s 可达到 42 帧/秒:

deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none -s sync -b 32 -nstreams 1 -e onnxruntime

原始模型路径: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none Batch 大小: 32 场景: sync 吞吐量 (项/秒): 41.9025

Link to this sectionDeepSparse 稠密模型性能#

虽然 DeepSparse 在使用优化的稀疏模型时表现最佳,但在使用标准稠密 YOLOv5s 时表现也很好。

在 Batch 32 时,DeepSparse 使用标准稠密 YOLOv5s 可达到 70 帧/秒——比 ORT 性能提升 1.7 倍!

deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none -s sync -b 32 -nstreams 1

原始模型路径: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none Batch 大小: 32 场景: sync 吞吐量 (项/秒): 69.5546

Link to this sectionDeepSparse 稀疏模型性能#

当模型应用稀疏性时,DeepSparse 相对于 ONNX Runtime 的性能优势更加显著。

在 Batch 32 时,DeepSparse 使用剪枝量化 YOLOv5s 可达到 241 帧/秒——比 ORT 性能提升 5.8 倍!

deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none -s sync -b 32 -nstreams 1

原始模型路径: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none Batch 大小: 32 场景: sync 吞吐量 (项/秒): 241.2452

Link to this sectionBatch 1 性能对比#

DeepSparse 在对延迟敏感的 Batch 1 场景中,也能获得比 ONNX Runtime 更快的速度。

Link to this sectionONNX Runtime 基准#

在 Batch 1 时,ONNX Runtime 使用标准稠密 YOLOv5s 可达到 48 帧/秒。

deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none -s sync -b 1 -nstreams 1 -e onnxruntime

原始模型路径: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none Batch 大小: 1 场景: sync 吞吐量 (项/秒): 48.0921

Link to this sectionDeepSparse 稀疏模型性能#

当模型应用稀疏性时,DeepSparse 相对于 ONNX Runtime 的性能优势更加显著。

在 Batch 1 时,DeepSparse 使用剪枝量化 YOLOv5s 可达到 135 帧/秒——比 ONNX Runtime 性能提升 2.8 倍!

deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none -s sync -b 1 -nstreams 1

原始模型路径: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none Batch 大小: 1 场景: sync 吞吐量 (项/秒): 134.9468

由于 c6i.8xlarge 实例具有 VNNI 指令,如果权重按 4 个一组进行剪枝,DeepSparse 的吞吐量可以进一步提升。

在 Batch 1 时,DeepSparse 使用 4-block 剪枝量化 YOLOv5s 可达到 180 项/秒——比 ONNX Runtime 性能提升 3.7 倍!

deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned35_quant-none-vnni -s sync -b 1 -nstreams 1

原始模型路径: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned35_quant-none-vnni Batch 大小: 1 场景: sync 吞吐量 (项/秒): 179.7375

大功告成!你已经准备好 使用 DeepSparse 优化你的 YOLOv5 部署。

Link to this section开始使用 YOLOv5 和 DeepSparse#

想与我们联系,请 加入我们的社区 并留下你的问题和评论。查看 Ultralytics YOLOv5 仓库 以及完整的 Neural Magic 部署 YOLOv5 文档

在 Ultralytics,我们与其他初创公司进行商业合作,以资助我们研发像 YOLOv5 这样出色的开源工具,从而让每个人都能免费使用。本文可能包含指向这些合作伙伴的联盟链接。

Explore solutions

Real-time AI that works with your team

机器人技术中的 AI

使用 Ultralytics YOLO 模型为智能机器赋能。机器人技术中的视觉 AI 可推动自主导航、感知、物体跟踪和实时控制。
了解更多
Real-time AI that works with your team

物流中的 AI

使用 Ultralytics YOLO 模型简化物流。视觉 AI 可实现包裹检查、分拣、车辆跟踪和实时仓库安全监控。
了解更多
Real-time AI that works with your team

零售业 AI

使用 Ultralytics YOLO 模型重塑零售业。视觉 AI 推动库存跟踪、货架监控、队列管理和更智能的客户洞察。
了解更多
Real-time AI that works with your team

医疗保健中的 AI

利用 Ultralytics YOLO 模型构建医疗保健解决方案。医疗保健中的视觉 AI 可助力更快速的医学影像分析、更智能的诊断和患者监测。
了解更多
Real-time AI that works with your team

制造业中的 AI

使用 Ultralytics YOLO 模型优化制造业。视觉 AI 推动质量控制、缺陷检测、PPE 合规性和装配线自动化。
了解更多
Real-time AI that works with your operation

汽车中的 AI

将计算机视觉应用于汽车行业,并配合 Ultralytics YOLO 模型。汽车视觉 AI 可提升道路安全、辅助驾驶和车辆自动化,打造更智能的道路。
了解更多
Real-time AI tailored to your operation

农业中的 AI

借助 Ultralytics YOLO 模型,将视觉 AI 引入智慧农业。赋能作物监测、牲畜追踪和精准农业,实现更高、更智能的产量。
了解更多
Real-time AI that works with your team

机器人技术中的 AI

使用 Ultralytics YOLO 模型为智能机器赋能。机器人技术中的视觉 AI 可推动自主导航、感知、物体跟踪和实时控制。
了解更多
Real-time AI that works with your team

物流中的 AI

使用 Ultralytics YOLO 模型简化物流。视觉 AI 可实现包裹检查、分拣、车辆跟踪和实时仓库安全监控。
了解更多
Real-time AI that works with your team

零售业 AI

使用 Ultralytics YOLO 模型重塑零售业。视觉 AI 推动库存跟踪、货架监控、队列管理和更智能的客户洞察。
了解更多
Real-time AI that works with your team

医疗保健中的 AI

利用 Ultralytics YOLO 模型构建医疗保健解决方案。医疗保健中的视觉 AI 可助力更快速的医学影像分析、更智能的诊断和患者监测。
了解更多
Real-time AI that works with your team

制造业中的 AI

使用 Ultralytics YOLO 模型优化制造业。视觉 AI 推动质量控制、缺陷检测、PPE 合规性和装配线自动化。
了解更多
Real-time AI that works with your operation

汽车中的 AI

将计算机视觉应用于汽车行业,并配合 Ultralytics YOLO 模型。汽车视觉 AI 可提升道路安全、辅助驾驶和车辆自动化,打造更智能的道路。
了解更多
Real-time AI tailored to your operation

农业中的 AI

借助 Ultralytics YOLO 模型,将视觉 AI 引入智慧农业。赋能作物监测、牲畜追踪和精准农业,实现更高、更智能的产量。
了解更多
Real-time AI that works with your team

机器人技术中的 AI

使用 Ultralytics YOLO 模型为智能机器赋能。机器人技术中的视觉 AI 可推动自主导航、感知、物体跟踪和实时控制。
了解更多
Real-time AI that works with your team

物流中的 AI

使用 Ultralytics YOLO 模型简化物流。视觉 AI 可实现包裹检查、分拣、车辆跟踪和实时仓库安全监控。
了解更多
Real-time AI that works with your team

零售业 AI

使用 Ultralytics YOLO 模型重塑零售业。视觉 AI 推动库存跟踪、货架监控、队列管理和更智能的客户洞察。
了解更多
Real-time AI that works with your team

医疗保健中的 AI

利用 Ultralytics YOLO 模型构建医疗保健解决方案。医疗保健中的视觉 AI 可助力更快速的医学影像分析、更智能的诊断和患者监测。
了解更多
Real-time AI that works with your team

制造业中的 AI

使用 Ultralytics YOLO 模型优化制造业。视觉 AI 推动质量控制、缺陷检测、PPE 合规性和装配线自动化。
了解更多
Real-time AI that works with your operation

汽车中的 AI

将计算机视觉应用于汽车行业,并配合 Ultralytics YOLO 模型。汽车视觉 AI 可提升道路安全、辅助驾驶和车辆自动化,打造更智能的道路。
了解更多
Real-time AI tailored to your operation

农业中的 AI

借助 Ultralytics YOLO 模型,将视觉 AI 引入智慧农业。赋能作物监测、牲畜追踪和精准农业,实现更高、更智能的产量。
了解更多

让我们一起构建 AI 的未来!

开启你的机器学习未来之旅