pull/17463/head^2
UltralyticsAssistant 4 months ago
parent c5ad60213c
commit 49df6cc426
  1. 154
      docs/en/integrations/imx500.md
  2. 6
      tests/test_exports.py
  3. 3
      ultralytics/engine/exporter.py

@ -1,7 +1,7 @@
---
comments: true
description: Learn to export Ultralytics YOLOv8 models to Sony's IMX500 format to optimize your models for efficient deployment.
keywords: YOLOv8, IMX500, MCT, model export, quantization, pruning, deep learning optimization, Raspberry Pi AI Camera, edge AI, PyTorch
---
comments: true
description: Learn to export Ultralytics YOLOv8 models to Sony's IMX500 format to optimize your models for efficient deployment.
keywords: YOLOv8, IMX500, MCT, model export, quantization, pruning, deep learning optimization, Raspberry Pi AI Camera, edge AI, PyTorch
---
# Learn to Export to IMX500 Format From YOLOv8 Model
@ -26,13 +26,13 @@ The IMX500 works with quantized models. Quantization makes models smaller and fa
**IMX500 Key Features:**
* **Metadata Output:** Instead of transmitting full images, the IMX500 outputs only metadata, minimizing data size, reducing bandwidth, and lowering costs.
* **Addresses Privacy Concerns:** By processing data on the device, the IMX500 addresses privacy concerns, ideal for human-centric applications like person counting and occupancy tracking.
* **Real-time Processing:** Fast, on-sensor processing supports real-time decisions, perfect for edge AI applications such as autonomous systems.
- **Metadata Output:** Instead of transmitting full images, the IMX500 outputs only metadata, minimizing data size, reducing bandwidth, and lowering costs.
- **Addresses Privacy Concerns:** By processing data on the device, the IMX500 addresses privacy concerns, ideal for human-centric applications like person counting and occupancy tracking.
- **Real-time Processing:** Fast, on-sensor processing supports real-time decisions, perfect for edge AI applications such as autonomous systems.
**Before You Begin:** For best results, ensure your YOLOv8 model is well-prepared for export by following our [Model Training Guide](https://docs.ultralytics.com/modes/train/), [Data Preparation Guide](https://docs.ultralytics.com/datasets/), and [Hyperparameter Tuning Guide](https://docs.ultralytics.com/guides/hyperparameter-tuning/).
## What’s under the hood?
## What’s under the hood?
<p align="center">
<img width="640" src="https://github.com/ultralytics/assets/releases/download/v8.3.0/imx500-deploy.avif" alt="IMX500 deployment">
@ -52,55 +52,55 @@ Export an Ultralytics YOLOv8 model to IMX500 format.
!!! example
=== "Python"
```python
=== "Python"
```python
from ultralytics import YOLO
# Load the YOLOv8n model
model = YOLO("yolov8n.pt")
# Export the model to imx500 format with Post-Training Quantization (PTQ)
model.export(format="imx500") # exports with PTQ quantization by default
# Alternatively, export with Gradient-based Post-Training Quantization (GPTQ)
# model.export(format="imx500", gptq=True)
# Validate the exported imx500 ONNX model
imx500_model = YOLO("yolov8n_imx500_model.onnx")
# Run inference
# Load the YOLOv8n model
model = YOLO("yolov8n.pt")
# Export the model to imx500 format with Post-Training Quantization (PTQ)
model.export(format="imx500") # exports with PTQ quantization by default
# Alternatively, export with Gradient-based Post-Training Quantization (GPTQ)
# model.export(format="imx500", gptq=True)
# Validate the exported imx500 ONNX model
imx500_model = YOLO("yolov8n_imx500_model.onnx")
# Run inference
imx500_model.val(data=”coco128.yaml”)
```
=== "CLI"
```bash
# Export the model to imx500 format with Post-Training Quantization (PTQ)
yolo export model=yolov8n.pt format=imx500
# Alternatively, export with GPTQ quantization
# yolo export model=yolov8n.pt format=imx500 gptq=True
# Validate Exported model
yolo val model=yolov8n_imx500_model.onnx data=coco128.yaml
```
=== "CLI"
```bash
# Export the model to imx500 format with Post-Training Quantization (PTQ)
yolo export model=yolov8n.pt format=imx500
# Alternatively, export with GPTQ quantization
# yolo export model=yolov8n.pt format=imx500 gptq=True
# Validate Exported model
yolo val model=yolov8n_imx500_model.onnx data=coco128.yaml
```
!!! note
It might take longer to export with GPTQ quantization based on the hardware specifications.
The export process will create an ONNX model for quantization validation, along with a directory named imx500\_model. This directory will include the packerOut.zip file, which is essential for packaging the model for deployment on the IMX500 hardware. Additionally, the imx500\_model folder will contain a text file listing all the labels associated with the model and other relevant metadata.
The export process will create an ONNX model for quantization validation, along with a directory named imx500_model. This directory will include the packerOut.zip file, which is essential for packaging the model for deployment on the IMX500 hardware. Additionally, the imx500_model folder will contain a text file listing all the labels associated with the model and other relevant metadata.
```bash
├── yolov8n_imx500_model
│ ├── dnnParams.xml
│ ├── labels.txt
│ ├── packerOut.zip
│ ├── yolov8n_imx500_model_MemoryReport.json
│ └── yolov8n_imx500_model.pbtxt
└── yolov8n_imx500_model.onnx
├── yolov8n_imx500_model
│ ├── dnnParams.xml
│ ├── labels.txt
│ ├── packerOut.zip
│ ├── yolov8n_imx500_model_MemoryReport.json
│ └── yolov8n_imx500_model.pbtxt
└── yolov8n_imx500_model.onnx
```
## Arguments
When exporting a model to IMX500 format , you can specify various arguments:
| Key | Value | Description |
| --------- | ----------- | ------------------------------------------------------------------- |
| `format` | `'imx500'` | Format to export to (imx500) |
| `gptq` | `False` | Use Gradient-based Post-Training Quantization (GPTQ) instead of PTQ |
| Key | Value | Description |
| -------- | ---------- | ------------------------------------------------------------------- |
| `format` | `'imx500'` | Format to export to (imx500) |
| `gptq` | `False` | Use Gradient-based Post-Training Quantization (GPTQ) instead of PTQ |
## Using IMX500 Export in Deployment
@ -110,7 +110,7 @@ After exporting YOLOv8 models to Sony IMX500 format, they can be deployed to Ras
Make sure you have the below hardware:
1. Raspberry Pi 5 or Raspberry Pi 4 Model B
1. Raspberry Pi 5 or Raspberry Pi 4 Model B
2. Raspberry Pi AI Camera
Connect the Raspberry Pi AI camera to the 15-pin MIPI CSI connector on the Raspberry Pi and power on the Raspberry Pi
@ -123,56 +123,56 @@ Connect the Raspberry Pi AI camera to the 15-pin MIPI CSI connector on the Raspb
Step 1: Open a terminal window and execute the following commands to update the Raspberry Pi software to the latest version.
```bash
sudo apt update && sudo apt full-upgrade
```bash
sudo apt update && sudo apt full-upgrade
```
Step 2: Install IMX500 firmware which is required to operate the IMX500 sensor along with a packager tool.
```bash
sudo apt install imx500-all imx500-tools
```bash
sudo apt install imx500-all imx500-tools
```
Step 3: Install prerequisites to run \`picamera2\` application. We will use this applicaiton later for the deployment process.
```bash
sudo apt install python3-opencv python3-munkres
```bash
sudo apt install python3-opencv python3-munkres
```
Step 4: Reboot Raspberry Pi for the changes to take into effect
```bash
sudo reboot
```bash
sudo reboot
```
### Package Model and Deploy to AI Camera
### Package Model and Deploy to AI Camera
After obtaining `packerOut.zip` from the IMX500 conversion process, you can pass this file into the packager tool to obtain an RPK file. This file can then be deployed directly to the AI Camera using `picamera2`.
Step 1: Package the model into RPK file
Step 1: Package the model into RPK file
```bash
imx500-package -i <path to packerOut.zip> -o <output folder>
```bash
imx500-package -i <path to packerOut.zip> -o <output folder>
```
The above will generate a `network.rpk` file inside the specified output folder.
Step 2: Clone `picamera2` repository and navigate to the imx500 examples
```bash
git clone [https://github.com/raspberrypi/picamera2](https://github.com/raspberrypi/picamera2)
cd picamera2/examples/imx500
```bash
git clone [https://github.com/raspberrypi/picamera2](https://github.com/raspberrypi/picamera2)
cd picamera2/examples/imx500
```
Step 3: Run YOLOv8 object detection, using the labels.txt file that has been generated during the imx500 export.
Step 3: Run YOLOv8 object detection, using the labels.txt file that has been generated during the imx500 export.
```bash
python imx500_object_detection_demo.py --model <path to network.rpk> --bbox-normalization --ignore-dash-labels --bbox-order xy –labels <path to labels.txt>
```
```bash
python imx500_object_detection_demo.py --model <path to network.rpk> --bbox-normalization --ignore-dash-labels --bbox-order xy –labels <path to labels.txt>
```
Finally you will be able to see live inference output as follows
*(Update with inference output image)*
_(Update with inference output image)_
## Benchmarks
@ -189,31 +189,31 @@ MCT supports several quantization methods to reduce model size and improve infer
1. **Post-Training Quantization (PTQ)**:
Available via Keras and PyTorch APIs.
Complexity: Low
Computational Cost: Low (CPU minutes)
Computational Cost: Low (CPU minutes)
2. **Gradient-based Post-Training Quantization (GPTQ)**:
Available via Keras and PyTorch APIs.
Complexity: Medium
Computational Cost: Moderate (2-3 GPU hours)
Computational Cost: Moderate (2-3 GPU hours)
3. **Quantization-Aware Training (QAT)**:
Complexity: High
Computational Cost: High (12-36 GPU hours)
MCT also supports various quantization schemes for weights and activations:
1. Power-of-Two (hardware-friendly)
1. Power-of-Two (hardware-friendly)
2. Symmetric
3. Uniform
### Main Features
1. **Graph Optimizations**: Transforms models into more efficient versions by folding layers like batch normalization into preceding layers.
2. **Quantization Parameter Search**: Minimizes quantization noise using metrics like Mean-Square-Error, No-Clipping, and Mean-Average-Error.
1. **Graph Optimizations**: Transforms models into more efficient versions by folding layers like batch normalization into preceding layers.
2. **Quantization Parameter Search**: Minimizes quantization noise using metrics like Mean-Square-Error, No-Clipping, and Mean-Average-Error.
3. **Advanced Quantization Algorithms**:
**Shift Negative Correction**: Addresses performance issues from symmetric activation quantization.
**Outliers Filtering**: Uses z-score to detect and remove outliers.
**Clustering**: Utilizes non-uniform quantization grids for better distribution matching.
**Mixed-Precision Search**: Assigns different quantization bit-widths per layer based on sensitivity.
4. Visualization**: Use TensorBoard to observe model performance insights, quantization phases, and bit-width configurations.
**Mixed-Precision Search**: Assigns different quantization bit-widths per layer based on sensitivity.
4. Visualization\*\*: Use TensorBoard to observe model performance insights, quantization phases, and bit-width configurations.
#### Enhanced Post-Training Quantization (EPTQ)
@ -225,22 +225,22 @@ MCT introduces structured, hardware-aware model pruning designed for specific ha
## Benefits of Using MCT
1. **Model Size Reduction**: Significantly reduces the model size through quantization and pruning.
2. **Inference Speedup**: Improves inference speed by optimizing computations.
3. **Hardware Compatibility**: Generates models optimized for specific hardware architectures.
4. **Advanced Algorithms**: Utilizes state-of-the-art quantization and pruning algorithms.
1. **Model Size Reduction**: Significantly reduces the model size through quantization and pruning.
2. **Inference Speedup**: Improves inference speed by optimizing computations.
3. **Hardware Compatibility**: Generates models optimized for specific hardware architectures.
4. **Advanced Algorithms**: Utilizes state-of-the-art quantization and pruning algorithms.
5. **Ease of Integration**: Seamlessly integrates with PyTorch models.
### **Real-World Use Cases**
**Export to** IMX500 format has wide applicability across industries. Here are some examples:
* **Edge AI and IoT**: Enable object detection on drones or security cameras, where real-time processing on low-power devices is essential.
* **Wearable Devices**: Deploy models optimized for small-scale AI processing on health-monitoring wearables.
* **Smart Cities**: Use IMX500-exported YOLOv8 models for traffic monitoring and safety analysis with faster processing and minimal latency.
* **Retail Analytics**: Enhance in-store monitoring by deploying optimized models in point-of-sale systems or smart shelves.
- **Edge AI and IoT**: Enable object detection on drones or security cameras, where real-time processing on low-power devices is essential.
- **Wearable Devices**: Deploy models optimized for small-scale AI processing on health-monitoring wearables.
- **Smart Cities**: Use IMX500-exported YOLOv8 models for traffic monitoring and safety analysis with faster processing and minimal latency.
- **Retail Analytics**: Enhance in-store monitoring by deploying optimized models in point-of-sale systems or smart shelves.
## Conclusion
Exporting Ultralytics YOLOv8 models to Sony's IMX500 format allows you to deploy your models for efficient inference on IMX500-based cameras. By leveraging advanced quantization and pruning techniques, you can reduce model size and improve inference speed without significantly compromising accuracy.
Exporting Ultralytics YOLOv8 models to Sony's IMX500 format allows you to deploy your models for efficient inference on IMX500-based cameras. By leveraging advanced quantization and pruning techniques, you can reduce model size and improve inference speed without significantly compromising accuracy.
For more information and detailed guidelines, refer to Sony's [IMX500 website](https://developer.aitrios.sony-semicon.com/en/raspberrypi-ai-camera).

@ -31,6 +31,7 @@ def test_export_onnx():
file = YOLO(MODEL).export(format="onnx", dynamic=True, imgsz=32)
YOLO(file)(SOURCE, imgsz=32) # exported model inference
@pytest.mark.skipif(not LINUX or MACOS, reason="Skipping test on Windows and Macos")
def test_export_imx500_ptq():
"""Test YOLOv8n exports to imx500 format."""
@ -38,6 +39,7 @@ def test_export_imx500_ptq():
file = model.export(format="imx500", imgsz=32, gptq=False)
YOLO(file)(SOURCE, imgsz=32)
@pytest.mark.slow
@pytest.mark.skipif(IS_RASPBERRYPI or not LINUX or MACOS, reason="Skipping test on Raspberry Pi and Windows")
def test_export_imx500_gptq():
@ -46,6 +48,7 @@ def test_export_imx500_gptq():
file = model.export(format="imx500", imgsz=32, gptq=True)
YOLO(file)(SOURCE, imgsz=32)
@pytest.mark.skipif(not TORCH_1_13, reason="OpenVINO requires torch>=1.13")
def test_export_openvino():
"""Test YOLO exports to OpenVINO format for model inference compatibility."""
@ -219,6 +222,3 @@ def test_export_ncnn():
"""Test YOLO exports to NCNN format."""
file = YOLO(MODEL).export(format="ncnn", imgsz=32)
YOLO(file)(SOURCE, imgsz=32) # exported model inference

@ -1114,9 +1114,10 @@ class Exporter:
if "C2f" not in self.model.__str__():
raise ValueError("IMX500 export is only supported for YOLOv8 detection models")
check_requirements(("model-compression-toolkit==2.1.1", "sony-custom-layers==0.2.0", "tensorflow==2.12.0"))
check_requirements(("imx500-converter[pt]==3.14.3", "keras<3.0")) # Separate requirements for imx500-converter
check_requirements(("imx500-converter[pt]==3.14.3", "keras<3.0")) # Separate requirements for imx500-converter
import subprocess
import model_compression_toolkit as mct
import onnx
from sony_custom_layers.pytorch.object_detection.nms import multiclass_nms

Loading…
Cancel
Save