Skip to content

Commit

Permalink
Merge pull request #1519 from LJ-Hao/docusaurus-version
Browse files Browse the repository at this point in the history
update:tutorial_of_ai_kit_with_raspberrypi5_about_yolov8n_object_dete…
  • Loading branch information
grilliiiii authored Aug 9, 2024
2 parents 235c8d0 + 1b2e865 commit 8cfa8a9
Showing 1 changed file with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ no_comments: false # for Disqus
# Tutorial of AI Kit with Raspberry Pi 5 about YOLOv8n object detection
## Introduction

[YOLOv8](https://github.com/ultralytics/ultralytics) (You Only Look Once version 8) is the popular most YOLO series of real-time pose estimation and object de tection models. It builds upon the strengths of its predecessors by introducing several advancements in speed, accuracy, and flexibility. The [Raspberry-pi-AI-kit](https://www.seeedstudio.com/Raspberry-Pi-AI-Kit-p-5900.html) is used to accelerate inference speed, featuring a 13 tera-operations per second (TOPS) neural network inference accelerator built around the Hailo-8L chip.
[YOLOv8](https://github.com/ultralytics/ultralytics) (You Only Look Once version 8) is the popular most YOLO series of real-time pose estimation and object detection models. It builds upon the strengths of its predecessors by introducing several advancements in speed, accuracy, and flexibility. The [Raspberry-pi-AI-kit](https://www.seeedstudio.com/Raspberry-Pi-AI-Kit-p-5900.html) is used to accelerate inference speed, featuring a 13 tera-operations per second (TOPS) neural network inference accelerator built around the Hailo-8L chip.

This wiki we will show you how to use yolov8n to do object detection with ai kit on raspberry pi5 from traing to deploying.
This wiki will guide you on how to use YOLOv8n for object detection with AI Kit on Raspberry Pi 5, from training to deployment.

## Prepare Hardware

Expand Down Expand Up @@ -55,7 +55,7 @@ This wiki we will show you how to use yolov8n to do object detection with ai kit

Please refer to [this](https://www.raspberrypi.com/documentation/accessories/ai-kit.html)

## Host computer
## On Host Computer

:::note
We will install hailo software, make sure you have a hailo account.
Expand All @@ -68,7 +68,7 @@ Install python3.11
sudo apt install python3.11
```

Creat yolo_env as your virtual environment
Create yolo_env as your virtual environment

```
python3.11 -m venv yolo_env
Expand All @@ -86,7 +86,7 @@ Install ultralytics
pip install ultralytics
```

Train yolov8n, here we use coco as dateset, if you want train your own dataset, you can use [this](https://docs.ultralytics.com) to train your own dataset.
Train YOLOv8n using the COCO dataset. If you want to train your own dataset, you can refer to [this](https://docs.ultralytics.com) for instructions on how to do so.

```
mkdir yolomodel && cd yolomodel
Expand All @@ -95,15 +95,15 @@ yolo detect train data=coco128.yaml model=yolov8n.pt name=retrain_yolov8n epochs

<p style={{textAlign: 'center'}}><img src="https://files.seeedstudio.com/wiki/reComputer-R1000/hailo-tutorial/train.png" alt="pir" width={1000} height="auto"/></p>

You will get best.pt model after your training like below:
You will get the `best.pt` model after your training, as shown below:

```
cd ./runs/detect/retrain_yolov8n/weights/
ls
```
<p style={{textAlign: 'center'}}><img src="https://files.seeedstudio.com/wiki/reComputer-R1000/hailo-tutorial/train_result.png" alt="pir" width={1000} height="auto"/></p>

Convert pt model to onnx
Convert the `.pt` model to `.onnx`.

```
yolo export model=./best.pt imgsz=640 format=onnx opset=11
Expand Down Expand Up @@ -148,15 +148,15 @@ Install [Model zoo](https://hailo.ai/developer-zone/software-downloads/), here y
pip install hailo_model_zoo-2.11.0-py3-none-any.whl
```

Test hailo_model_zoo weather it can work
Test whether `hailo_model_zoo` is functioning correctly.

```
hailomz -h
```
<p style={{textAlign: 'center'}}><img src="https://files.seeedstudio.com/wiki/reComputer-R1000/hailo-tutorial/model_zoo_test.png" alt="pir" width={1000} height="auto"/></p>


Install hailo_model_zoo github file
Install `hailo_model_zoo` github file

```
cd yolomodel/runs/detect/retrain_yolov8n/weights
Expand All @@ -170,20 +170,19 @@ Install coco dataset for evaluate/optimize/compile the yolov8n model
python hailo_model_zoo/datasets/create_coco_tfrecord.py val2017
python hailo_model_zoo/datasets/create_coco_tfrecord.py calib2017
```
### Use hailomz to parse the model:

Use hailomz to parse the model
### Use `hailo_model_zoo` to parse the model:

```
hailomz parse --hw-arch hailo8l --ckpt ./best.onnx yolov8n
```

<p style={{textAlign: 'center'}}><img src="https://files.seeedstudio.com/wiki/reComputer-R1000/hailo-tutorial/parse_model.png" alt="pir" width={1000} height="auto"/></p>

### Use hailomz to optimize the model:
### Use `hailo_model_zoo` to optimize the model:

:::note
If you excute the following command, you may get some errors, but you can copy ```./hailo_model_zoo/hailo_model_zoo``` to your local library. And if the error is about not find dateset, you can enter ```~/.hailomz``` and find the dataset, and copy it to your local dataset.
If you execute the following command, you may encounter some errors. However, you can copy `./hailo_model_zoo/hailo_model_zoo` to your local library. If the error indicates that the dataset cannot be found, you can navigate to `~/.hailomz`, locate the dataset, and copy it to your local dataset directory.
:::

```
Expand All @@ -192,22 +191,23 @@ hailomz optimize --hw-arch hailo8l --har ./yolov8n.har yolov8n

<p style={{textAlign: 'center'}}><img src="https://files.seeedstudio.com/wiki/reComputer-R1000/hailo-tutorial/optimize_model.png" alt="pir" width={1000} height="auto"/></p>

### Use hailomz to compile the model:
### Use `hailo_model_zoo` to compile the model:

```
hailomz compile yolov8n --hw-arch hailo8l --har ./yolov8n.har
```

<p style={{textAlign: 'center'}}><img src="https://files.seeedstudio.com/wiki/reComputer-R1000/hailo-tutorial/model_compile.png" alt="pir" width={1000} height="auto"/></p>

After all you will get a hef format model, you can use it to deploy on reComputer r1000.
After all you will get a `hef` model, you can use it to deploy on raspberry pi5 with AI kit

```
ls
```

<p style={{textAlign: 'center'}}><img src="https://files.seeedstudio.com/wiki/reComputer-R1000/hailo-tutorial/compile_result.png" alt="pir" width={1000} height="auto"/></p>

## raspberry pi5
## On Raspberry Pi5

### update the system:

Expand All @@ -231,12 +231,12 @@ dtparam=pciex1_gen=3
```
:::note
If you want to use gen2,please comment dtparam=pciex1_gen=3
If you want to use `gen2`, please comment `dtparam=pciex1_gen=3`
:::

### Install hailo-all and reboot:

Open terminal on the Raspberry Pi5, and input command as follows to install Hailo software.
Open the terminal on the Raspberry Pi 5 and enter the following command to install the Hailo software

```
sudo apt install hailo-all
Expand Down Expand Up @@ -272,28 +272,29 @@ cd Benchmarking-YOLOv8-on-Raspberry-PI-reComputer-r1000-and-AIkit-Hailo-8L

### Copy your model to the raspberry pi5:

make a directory named ```hailomodel```
Make a directory named `hailomodel`

```
mkdir hailomodel
```

:::note
The command below you should run on your host computer, not your raspberry pi5, and make sure your host computer and raspberry pi5 at the same net.
The command below should be run on your host computer, not your Raspberry Pi 5. Ensure that both your host computer and Raspberry Pi 5 are connected to the same network.
:::

```
scp -r ./yolomodel/runs/detect/retrain_yolov8n/weights/yolov8n.hef name@:ip /home/pi/Benchmarking-YOLOv8-on-Raspberry-PI-reComputer-r1000-and-AIkit-Hailo-8L/hailomodel/
scp -r ./yolomodel/runs/detect/retrain_yolov8n/weights/yolov8n.hef username@ip /home/pi/Benchmarking-YOLOv8-on-Raspberry-PI-reComputer-r1000-and-AIkit-Hailo-8L/hailomodel/
```

### Change code

Find line 105 and 106 in ```object-detection-hailo.py```, and change the code like below:
Find line 105 and 106 in `object-detection-hailo.py`, and change the code like below:
```
elif args.network == "yolov8n":
self.hef_path = os.path.join(self.current_path, './hailomodel/yolov8n.hef')
self.hef_path = os.path.join(self.current_path, './hailomodel/yolov8n.hef')
```

Find line 172 in ```object-detection-hailo.py```, and change the code like below:
Find line 172 in `object-detection-hailo.py`, and change the code like below:
```
parser.add_argument("--network", default="yolov8n", choices=['yolov6n', 'yolov8s', 'yolox_s_leaky'], help="Which Network to use, defult is yolov6n")
```
Expand All @@ -306,6 +307,7 @@ bash run.sh object-detection-hailo

## Result

Here is the video in which we demonstrate the process of training the YOLOv8n model and deploying it on the Raspberry Pi 5. We set the batch size to 8, the input size to 640x640, and the input video frame rate to 240 fps. The inference speed achieved is 136.7 fps, which is exceptionally fast for most application scenarios.

<div align="center">
<iframe width="800" height="400" src="https://www.youtube.com/embed/mVNrEVpvvuc" title="YOLOv8n Object Detection on Raspberry Pi 5 with AI Kit" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
Expand Down

0 comments on commit 8cfa8a9

Please sign in to comment.