Skip to content

YOLOv5 Training and Deployment Guide

Document Information

This tutorial explains how to train a YOLOv5 object detection model and deploy it to a CSUN RV1126B development board.

ItemDescription
Document nameYOLOv5 Training and Deployment Guide
CompanyNissho Technology Co., Ltd.
URLhttp://www.dragonwake.com
E-mailinfo@dragonwake.com
Created2026/06/04
VersionVer1.0
RevisionInitial release

1. YOLOv5 Overview

YOLOv5 is an object detection model released by Ultralytics on June 9, 2020. It is improved based on YOLOv3 and provides four model variants: YOLOv5s, YOLOv5m, YOLOv5l, and YOLOv5x.

Compared with YOLOv4, YOLOv5 keeps detection accuracy at a similar level while reducing the average weight file size, training time, and inference time. The YOLOv5 network consists of four main parts: input, Backbone, Neck, and Head.

This tutorial describes how to train the YOLOv5 object detection algorithm and deploy it to a CSUN RV1126B board. For data annotation methods, refer to the separately published annotation guide.

YOLOv5 training and deployment workflow

Figure 1-1 YOLOv5 training and deployment workflow

2. Download Resources

Download the resources and source code required for this manual from the following link.

https://dl.dragonwake.com/download/rv1126b/AI/demo/yolov5/AIDemo_yolov5_All.zip

After extraction, the directory structure is as follows.

|-- 01-data Dataset
|-- 02-training Training source code
|-- 03-model_convert AI model conversion source code
|-- 04-AI_deploy AI model deployment source code

AIDemo YOLOv5 directory structure after extraction

Figure 2-1 Directory structure after extracting AIDemo_yolov5_All.zip

3. Prepare the Dataset

3.1. Download the Dataset

After downloading the package from “2. Download Resources”, run the following command in the 01-data directory to extract the dataset.

Terminal window
unzip mask.zip

After extraction, the following files or folders are available.

  • images: image files used for training and validation
  • labels: YOLO-format label files corresponding to each image
  • list_dataset_file.py: script for generating training and validation image path lists

YOLOv5 dataset files after extraction

Figure 3-1 Dataset files after extraction

3.2. Generate Path Lists

Enter the RV1126B Docker development environment, then run list_dataset_file.py in the dataset directory.

Terminal window
cd ~/linuxshare/work/rv1126b/jp/embedded/images/develop_environment
./run.sh 2204
cd /opt/linuxshare/work/rv1126b/jp/AI/demo/Tutorial/yolov5/01-data/
python list_dataset_file.py

After execution, the training sample list train.txt and validation sample list valid.txt are generated.

Terminal output when generating path lists

Figure 3-2 Output of list_dataset_file.py

These list files are referenced by the training configuration file in later steps.

Generated train.txt and valid.txt files

Figure 3-3 Generated train.txt and valid.txt

4. Train the YOLOv5 Object Detection Algorithm

4.1. Download the Training Source Code

Use Git on the PC to clone the company GitHub repository. Depending on the network, cloning may take some time.

Terminal window
git clone https://github.com/csunltd/rv1126b-yolov5.git

Terminal output for cloning the YOLOv5 repository

Figure 4-1 Cloning the YOLOv5 repository

After cloning, the source directory is shown below.

YOLOv5 training source directory

Figure 4-2 YOLOv5 training source directory

4.2. Train the Model

Go to the YOLOv5 working directory. The following example trains a mask detection model. Before training, update the train.txt and valid.txt paths in data/mask.yaml according to the actual dataset location.

# Train/val/test sets
path: ../../01-data
train: train.txt
val: valid.txt
test: valid.txt
# Classes
nc: 2
names: ['head', 'mask']

mask.yaml configuration example

Figure 4-3 mask.yaml configuration example

Install the required Python modules.

Terminal window
pip install PyYAML tqdm ultralytics pandas seaborn

Start training with the following command.

Terminal window
python train.py --data mask.yaml --cfg yolov5s.yaml --weights "" --batch-size 64

When training starts, the terminal displays information such as epoch, loss, and accuracy. The training result is saved under ./runs/train/exp*/, and accuracy metrics can be checked in results.csv.

YOLOv5 training terminal output

Figure 4-4 YOLOv5 training terminal output

4.3. Run Model Inference on the PC

After training, the model with the best validation result is usually generated as ./runs/train/exp*/weights/best.pt. Run the following command to perform inference and quickly evaluate detection results.

Terminal window
python detect.py --source data/images --weights ./runs/train/exp8/weights/best.pt --conf 0.5

Inference results are saved under ./runs/detect/exp*/.

YOLOv5 mask detection result on the PC

Figure 4-5 YOLOv5 mask detection result on the PC

4.4. Convert the pt Model to an ONNX Model

To deploy the algorithm to the CSUN RV1126B board, the model must eventually be converted to RKNN format. Before RKNN conversion, convert the PyTorch pt model to ONNX. The file best.anchors.txt is generated at the same time.

Terminal window
python export.py --include onnx --rknpu RV1126 --weights ./runs/train/exp8/weights/best.pt

For better compatibility with RV1126B and RKNN conversion tools, explicitly specifying --opset 12 is recommended.

Terminal window
python export.py --include onnx --rknpu RV1126 --opset 12 --weights ./runs/train/exp8/weights/best.pt

The ONNX model file is generated in the same directory as best.pt.

best.onnx and best.anchors.txt generated after ONNX export

Figure 4-6 Files generated after ONNX export

5. rknn-toolkit Model Conversion

5.1. Build the rknn-toolkit Conversion Environment

The ONNX model must be converted to an RKNN model before it can run on the CSUN RV1126B board. Therefore, prepare the rknn-toolkit model conversion environment first. TensorFlow, TensorFlow Lite, Caffe, Darknet, and other models can be converted in a similar way, but this tutorial uses an ONNX model as the example.

For environment setup, refer to the “AI Model Conversion Environment Setup Guide”.

5.2. Convert the ONNX Model to an RKNN Model

Models with the .rknn extension can be evaluated and executed on the CSUN RV1126B board. Common trained models from TensorFlow, TensorFlow Lite, Caffe, Darknet, ONNX, PyTorch, and other frameworks can be converted to RKNN with RKNN-Toolkit2. Models trained by other frameworks can also be converted to ONNX first and then converted to RKNN.

For detailed conversion procedures, refer to the “RKNN Model Conversion Tutorial Example”.

5.2.1. Extract the Model Conversion Demo

Download and extract the model conversion demo from “2. Download Resources”.

Terminal window
cd /data/project/sales/csun/rv1126b/jp/AI/demo/Tutorial/yolov5/03-model_convert
unzip quant_dataset.zip
tar -xJf yolov5_model_convert.tar.xz

YOLOv5 model conversion demo directory after extraction

Figure 5-1 Model conversion demo directory after extraction

5.2.2. Enter the Model Conversion Docker Environment

Use the following command to mount the working directory into the Docker container. /data/project/sales/csun/rv1126b/jp/AI/demo/Tutorial is used as the working area and is mapped to /test inside the container. USB devices are mounted with /dev/bus/usb:/dev/bus/usb.

Terminal window
docker run -t -i --privileged \
-v /dev/bus/usb:/dev/bus/usb \
-v /data/project/sales/csun/rv1126b/jp/AI/demo/Tutorial:/test \
rknn-toolkit2:2.3.2-cp38 /bin/bash

Terminal output when entering the rknn-toolkit2 Docker environment

Figure 5-2 Entering the rknn-toolkit2 Docker environment

5.2.3. Generate the Quantization Image List

In the Docker environment, go to the model conversion working directory.

Terminal window
cd /test/yolov5/03-model_convert/yolov5_model_convert

Run gen_list.py to generate the quantization image list.

Terminal window
python gen_list.py

Terminal output for generating the quantization image list

Figure 5-3 Terminal output for generating the quantization image list

The generated quantization image list is saved as pic_path.txt.

Generated pic_path.txt

Figure 5-4 Generated pic_path.txt

5.2.4. Convert the ONNX Model to an RKNN Model

rknn_convert.py performs int8 quantization by default. The main script content is shown below.

import os
import urllib
import traceback
import time
import sys
import numpy as np
import cv2
from rknn.api import RKNN
ONNX_MODEL = 'best.onnx'
RKNN_MODEL = './yolov5_mask_rv1126b.rknn'
DATASET = './pic_path.txt'
QUANTIZE_ON = True
if __name__ == '__main__':
rknn = RKNN(verbose=True)
if not os.path.exists(ONNX_MODEL):
print('model not exist')
exit(-1)
print('--> Config model')
rknn.config(
mean_values=[[0, 0, 0]],
std_values=[[255, 255, 255]],
target_platform='rv1126b'
)
print('done')
print('--> Loading model')
ret = rknn.load_onnx(model=ONNX_MODEL)
if ret != 0:
print('Load yolov5 failed!')
exit(ret)
print('done')
print('--> Building model')
ret = rknn.build(do_quantization=QUANTIZE_ON, dataset=DATASET)
if ret != 0:
print('Build yolov5 failed!')
exit(ret)
print('done')
print('--> Export RKNN model')
ret = rknn.export_rknn(RKNN_MODEL)
if ret != 0:
print('Export yolov5 rknn failed!')
exit(ret)
print('done')

Place best.onnx in the yolov5_model_convert directory and run the following command to start conversion.

Terminal window
python rknn_convert.py

RKNN model conversion terminal output

Figure 5-5 RKNN model conversion terminal output

After successful conversion, an RKNN model that can run on the CSUN RV1126B board is generated.

Generated RKNN model file

Figure 5-6 Generated RKNN model file

6. YOLOv5 Model Deployment

6.1. Deployment Example Overview

This section describes how to deploy the YOLOv5 model to the RV1126B board. This model is only a sample trained with a simple training process, and its accuracy is not guaranteed.

6.2. Preparation

6.2.1. Hardware Preparation

Prepare an RV1126B board, a Type-C data cable, and a LAN cable. You can use tools such as MobaXterm to log in to the RV1126B board over SSH. For details, refer to the Getting Started Guide.

Connect with a LAN cable.

RV1126B board LAN connection example

Figure 6-1 RV1126B board LAN connection example

Connect with a Type-C serial cable.

RV1126B board Type-C serial connection example

Figure 6-2 RV1126B board Type-C serial connection example

6.2.2. Prepare the Development Environment

If this is your first time reading this document, refer to the Getting Started Guide and follow the described steps to prepare the compile environment.

On the PC-side Ubuntu system, run run.sh to enter the RV1126B compile environment.

Terminal window
cd ~/develop_environment
./run.sh 2204

Terminal output when entering the RV1126B Docker development environment

Figure 6-3 Entering the RV1126B Docker development environment

6.3. Build the Sample Program

After moving the downloaded package into the RV1126B Docker development environment, run the following commands to extract it.

Terminal window
cd /opt/linuxshare/work/rv1126b/jp/AI/demo/Tutorial/yolov5/04-AI_deploy
tar -xvf yolov5_detect_C_demo.tar.bz2

YOLOv5 detection demo directory after extraction

Figure 6-4 YOLOv5 detection demo directory after extraction

In the RV1126B Docker development environment, enter the sample program directory and build the demo.

Terminal window
sudo mount -t nfs -o vers=3,proto=tcp,mountproto=tcp,nolock,retrans=5,timeo=5 192.168.11.85:/ /mnt
cd /opt/linuxshare/work/rv1126b/jp/AI/demo/Tutorial/yolov5/04-AI_deploy/yolov5_detect_C_demo/
./build.sh

YOLOv5 detection demo build result

Figure 6-5 YOLOv5 detection demo build result

After successful compilation, copy the executable program directory yolov5_detect_demo_release/ to the /userdata directory on the RV1126B board.

Terminal window
cp yolov5_detect_demo_release/ /mnt/userdata/ -rf

Copy the YOLOv5 detection demo to the development board

Figure 6-6 Copy the YOLOv5 detection demo to the development board

6.4. Run the YOLOv5 Model on the Development Board

Enter the board shell via serial debugging or SSH, then go to the sample program deployment directory.

Terminal window
cd /userdata/yolov5_detect_demo_release/

Run the sample program.

Terminal window
chmod 777 yolov5_detect_demo
sudo ./yolov5_detect_demo

The execution result is shown below. The algorithm execution time is about 48 ms.

Run the YOLOv5 detection demo on the RV1126B board

Figure 6-7 Run the YOLOv5 detection demo on the RV1126B board

From the RV1126B compile environment, retrieve the test result image with the following command.

Terminal window
cp /mnt/userdata/yolov5_detect_demo_release/result.jpg .

Copy the detection result image from the board

Figure 6-8 Copy the detection result image from the board

The test result is shown below.

YOLOv5 mask detection result on the RV1126B board

Figure 6-9 YOLOv5 mask detection result on the RV1126B board

The YOLOv5 object detection sample has now been successfully executed on the board.