跳至内容

Sony IMX500 Export for Ultralytics YOLO11

This guide covers exporting and deploying Ultralytics YOLO11 models to Raspberry Pi AI Cameras that feature the Sony IMX500 sensor.

在计算能力有限的设备(如Raspberry Pi AI Camera)上部署计算机视觉模型可能很棘手。使用为提高性能而优化的模型格式会带来巨大的不同。

The IMX500 model format is designed to use minimal power while delivering fast performance for neural networks. It allows you to optimize your Ultralytics YOLO11 models for high-speed and low-power inferencing. In this guide, we'll walk you through exporting and deploying your models to the IMX500 format while making it easier for your models to perform well on the Raspberry Pi AI Camera.

树莓派人工智能摄像头

为什么要导出到 IMX500

索尼的IMX500 智能视觉传感器是边缘人工智能处理领域一款改变游戏规则的硬件。它是全球首款具有片上人工智能功能的智能视觉传感器。该传感器有助于克服边缘人工智能领域的诸多挑战,包括数据处理瓶颈、隐私问题和性能限制。
其他传感器只是传递图像和帧,而 IMX500 却能讲述一个完整的故事。它直接在传感器上处理数据,让设备能够实时产生洞察力。

Sony's IMX500 Export for YOLO11 Models

IMX500 旨在改变设备直接在传感器上处理数据的方式,而无需将数据发送到云端进行处理。

IMX500 可使用量化模型。量化可使模型更小、更快,而不会损失太多精度。它是边缘计算有限资源的理想选择,通过减少延迟和在本地快速处理数据,使应用程序能够快速响应,而无需依赖云。本地处理还能保证用户数据的私密性和安全性,因为数据不会发送到远程服务器。

IMX500 主要功能

  • 元数据输出:IMX500 可以同时输出图像和元数据(推理结果),而不是只传输图像,也可以只输出元数据,以尽量减小数据量、减少带宽和降低成本。
  • 解决隐私问题:通过在设备上处理数据,IMX500 解决了隐私问题,是人员计数和占用跟踪等以人为本的应用的理想之选。
  • 实时处理:快速的传感器处理支持实时决策,非常适合自主系统等边缘人工智能应用。

Before You Begin: For best results, ensure your YOLO11 model is well-prepared for export by following our Model Training Guide, Data Preparation Guide, and Hyperparameter Tuning Guide.

使用示例

Export an Ultralytics YOLO11 model to IMX500 format and run inference with the exported model.

备注

在这里,我们进行推理只是为了确保模型能按预期运行。不过,要在树莓派人工智能相机上进行部署和推理,请跳转到 "部署"部分的 "使用 IMX500 导出"。

示例

from ultralytics import YOLO

# Load a YOLO11n PyTorch model
model = YOLO("yolo11n.pt")

# Export the model
model.export(format="imx", data="coco8.yaml")  # exports with PTQ quantization by default

# Load the exported model
imx_model = YOLO("yolo11n_imx_model")

# Run inference
results = imx_model("https://ultralytics.com/images/bus.jpg")
# Export a YOLO11n PyTorch model to imx format with Post-Training Quantization (PTQ)
yolo export model=yolo11n.pt format=imx data=coco8.yaml

# Run inference with the exported model
yolo predict model=yolo11n_imx_model source='https://ultralytics.com/images/bus.jpg'

警告

The Ultralytics package installs additional export dependencies at runtime. The first time you run the export command, you may need to restart your console to ensure it works correctly.

出口参数

论据 类型 默认值 说明
format str 'imx' 导出模型的目标格式,定义与各种部署环境的兼容性。
imgsz inttuple 640 模型输入所需的图像尺寸。对于正方形图像,可以是一个整数,或者是一个元组 (height, width) 了解具体尺寸。
int8 bool True 激活 INT8 量化,进一步压缩模型并加快推理速度,同时将精度损失降至最低,主要用于边缘设备。
data str 'coco8.yaml' 通往 数据集 配置文件(默认: coco8.yaml),对量化至关重要。
fraction float 1.0 指定用于 INT8 量化校准的数据集分数。允许在完整数据集的子集上进行校准,这对实验或资源有限的情况非常有用。如果在启用 INT8 时没有指定,则将使用整个数据集。
device str None 指定导出设备:GPU (device=0)、CPU (device=cpu).

提示

如果在支持CUDA 的GPU 上导出,请通过参数 device=0 以便更快地导出。

有关导出过程的更多详情,请访问Ultralytics 有关导出的文档页面

导出过程将创建一个ONNX 模型用于量化验证,同时创建一个名为 <model-name>_imx_model.该目录将包括 packerOut.zip 文件,这对打包模型以便在 IMX500 硬件上部署至关重要。此外,该 <model-name>_imx_model 文件夹将包含一个文本文件 (labels.txt) 列出了与模型相关的所有标签。

yolo11n_imx_model
├── dnnParams.xml
├── labels.txt
├── packerOut.zip
├── yolo11n_imx.onnx
├── yolo11n_imx500_model_MemoryReport.json
└── yolo11n_imx500_model.pbtxt

在部署中使用 IMX500 导出

After exporting Ultralytics YOLO11n model to IMX500 format, it can be deployed to Raspberry Pi AI Camera for inference.

硬件先决条件

确保有以下硬件:

  1. Raspberry Pi 5 或 Raspberry Pi 4 Model B
  2. 树莓派人工智能摄像头

将 Raspberry Pi AI 摄像头连接到 Raspberry Pi 上的 15 针 MIPI CSI 接口,并打开 Raspberry Pi 电源

软件先决条件

备注

本指南使用 Raspberry Pi 5 上运行的 Raspberry Pi OS Bookworm 进行测试。

步骤 1:打开终端窗口,执行以下命令将 Raspberry Pi 软件更新到最新版本。

sudo apt update && sudo apt full-upgrade

第 2 步: 安装运行 IMX500 传感器所需的 IMX500 固件和打包工具。

sudo apt install imx500-all imx500-tools

第 3 步:安装运行的先决条件 picamera2 应用程序。我们稍后将在部署过程中使用该应用程序。

sudo apt install python3-opencv python3-munkres

步骤 4:重启 Raspberry Pi 使更改生效

sudo reboot

打包模型并部署到人工智能相机

在获得 packerOut.zip 在 IMX500 转换过程中,您可以将此文件传入打包工具,以获得 RPK 文件。然后可使用以下工具将此文件直接部署到 AI 摄像机上 picamera2.

第 1 步:将模型打包成 RPK 文件

imx500-package -i path/to/packerOut.zip -o path/to/output/folder

上述操作将生成一个 network.rpk 指定输出文件夹内的文件。

步骤 2:克隆 picamera2 安装,并导航至 imx500 示例

git clone https://github.com/raspberrypi/picamera2
cd picamera2
pip install -e . --break-system-packages
cd examples/imx500

Step 3: Run YOLO11 object detection, using the labels.txt file that has been generated during the IMX500 export.

python imx500_object_detection_demo.py --model path/to/network.rpk --fps 17 --bbox-normalization --ignore-dash-labels --bbox-order xy --labels path/to/labels.txt

然后,您就可以看到实时推理输出,如下所示

树莓派人工智能摄像头推理

基准

YOLOv8 and YOLO11n benchmarks below were run by the Ultralytics team on Raspberry Pi AI Camera with imx 模型格式测量速度和精度。

模型 格式 现状 Size of RPK (MB) mAP50-95(B) 推理时间(毫秒/分钟)
YOLOv8n imx 3.1 0.602 58.82
YOLO11n imx 3.2 0.644 62.50

备注

Validation for the above benchmark was done using coco8 dataset on a Raspberry Pi 5

引擎盖下有什么?

IMX500 部署

索尼模型压缩工具包(MCT)

索尼的模型压缩工具包(MCT)是通过量化和剪枝优化深度学习模型的强大工具。它支持各种量化方法,并提供先进的算法来减少模型大小和计算复杂度,同时不会明显牺牲准确性。MCT 特别适用于在资源受限的设备上部署模型,确保高效推理和减少延迟。

MCT 的支持功能

索尼的 MCT 具有一系列旨在优化神经网络模型的功能:

  1. 图形优化:通过将批量归一化等层折叠到前面的层中,将模型转化为更高效的版本。
  2. 量化参数搜索:利用均方误差、无削边和均方误差等指标将量化噪声最小化。
  3. 高级量化算法:
    • 移位负校正:解决对称激活量化带来的性能问题。
    • 异常值过滤:使用 z 分数检测并清除异常值。
    • 聚类:利用非均匀量化网格实现更好的分布匹配。
    • 混合精度搜索:根据灵敏度为每层分配不同的量化位宽。
  4. 可视化:使用 TensorBoard 观察模型性能、量化阶段和位宽配置。

量化

MCT 支持多种量化方法,以减小模型大小并提高推理速度:

  1. 训练后量化 (PTQ):
    • 可通过 Keras 和PyTorch API 使用。
    • 复杂性:低
    • 计算成本:低 (CPU 分钟)
  2. 基于梯度的后训练量化(GPTQ):
    • 可通过 Keras 和PyTorch API 使用。
    • 复杂度中等
    • 计算成本:中等(2-3GPU 小时)
  3. 量化感知训练(QAT):
    • 复杂性:高
    • 计算成本:高(12-36GPU 小时)

MCT 还支持各种权重和激活的量化方案:

  1. 二合一电源(硬件友好型)
  2. 对称
  3. 制服

结构化修剪

MCT 引入了针对特定硬件架构设计的结构化硬件感知模型剪枝技术。该技术通过剪枝 SIMD 组,充分利用目标平台的单指令多数据(SIMD)功能。这样可以减少模型大小和复杂性,同时优化通道利用率,并与 SIMD 架构保持一致,以便有针对性地利用加权内存空间。通过 Keras 和PyTorch API 提供。

IMX500 转换器工具(编译器)

The IMX500 Converter Tool is integral to the IMX500 toolset, allowing the compilation of models for deployment on Sony's IMX500 sensor (for instance, Raspberry Pi AI Cameras). This tool facilitates the transition of Ultralytics YOLO11 models processed through Ultralytics software, ensuring they are compatible and perform efficiently on the specified hardware. The export procedure following model quantization involves the generation of binary files that encapsulate essential data and device-specific configurations, streamlining the deployment process on the Raspberry Pi AI Camera.

真实世界使用案例

导出为 IMX500 格式在各行各业都有广泛的适用性。下面是一些例子:

  • 边缘人工智能和物联网:在无人机或安防摄像头上实现物体检测,在这种情况下,必须在低功耗设备上进行实时处理。
  • 可穿戴设备:在健康监测可穿戴设备上部署为小规模人工智能处理而优化的模型。
  • Smart Cities: Use IMX500-exported YOLO11 models for traffic monitoring and safety analysis with faster processing and minimal latency.
  • 零售分析:通过在销售点系统或智能货架上部署优化模型,加强店内监控。

结论

Exporting Ultralytics YOLO11 models to Sony's IMX500 format allows you to deploy your models for efficient inference on IMX500-based cameras. By leveraging advanced quantization techniques, you can reduce model size and improve inference speed without significantly compromising accuracy.

更多信息和详细指南,请参阅索尼IMX500 网站

常见问题

How do I export a YOLO11 model to IMX500 format for Raspberry Pi AI Camera?

To export a YOLO11 model to IMX500 format, use either the Python API or CLI command:

from ultralytics import YOLO

model = YOLO("yolo11n.pt")
model.export(format="imx")  # Exports with PTQ quantization by default

导出过程将创建一个目录,其中包含部署所需的文件,包括 packerOut.zip 可与 Raspberry Pi 上的 IMX500 打包工具一起使用。

使用 IMX500 格式进行边缘人工智能部署的主要优势是什么?

IMX500 格式为边缘部署提供了几个重要优势:

  • 片上人工智能处理减少了延迟和功耗
  • 同时输出图像和元数据(推理结果),而不是只输出图像
  • 无需依赖云,在本地处理数据,提高隐私性
  • 实时处理功能是时间敏感型应用的理想选择
  • 优化量化,在资源有限的设备上高效部署模型

部署 IMX500 需要哪些硬件和软件前提条件?

部署 IMX500 型号时,您需要

硬件

  • Raspberry Pi 5 或 Raspberry Pi 4 Model B
  • 配备 IMX500 传感器的树莓派人工智能相机

软件

  • 树莓派操作系统书虫
  • IMX500 固件和工具 (sudo apt install imx500-all imx500-tools)
  • Python 套餐为 picamera2 (sudo apt install python3-opencv python3-munkres)

What performance can I expect from YOLO11 models on the IMX500?

基于Ultralytics Raspberry Pi AI 摄像头基准测试:

  • YOLO11n achieves 58.82ms inference time per image
  • COCO8 数据集的 mAP50-95 为 0.522
  • 量化后的模型大小仅为 2.9MB

这表明,IMX500 格式可为边缘人工智能应用提供高效的实时推理,同时保持良好的精度。

如何将导出的模型打包并部署到 Raspberry Pi AI 摄像机?

导出为 IMX500 格式后:

  1. 使用打包工具创建 RPK 文件:

    imx500-package -i path/to/packerOut.zip -o path/to/output/folder
    
  2. 克隆并安装 picamera2:

    git clone https://github.com/raspberrypi/picamera2
    cd picamera2 && pip install -e . --break-system-packages
    
  3. 使用生成的 RPK 文件运行推理:

    python imx500_object_detection_demo.py --model path/to/network.rpk --fps 17 --bbox-normalization --labels path/to/labels.txt
    


📅创建于 5 个月前 ✏️已更新 9 天前

评论