Local deployment of yolo training environment

Local deployment of yolo training environmentEnvironment ConfigurationDownload and install Yolo v5 environment dependenciesPreparing training dataUse YOLOv5 to train a fruit classification modelConvert the trained model to a kmodel that can be recognized by K230Download the conversion toolOther considerations

 

Environment Configuration

In this section, we will demonstrate how to deploy the Yolo v5 environment locally and train a classification (recognition) model.

The operating environment we demonstrate is as follows:

System: Ubuntu 22.04

GPU: 4090D*1

CUDA version: 12.4

The system has pre-installed Miniconda3_3.10

image-20250407170307028

Download and install Yolo v5 environment dependencies

Enter the command

image-20250407170524619

implement

This part of the file contains a lot of content, please be patient

image-20250407172727019

Preparing training data

Here we use the training set officially provided by Canaan Technology as sample data

Please download the sample dataset provided. The sample dataset contains classification, detection and segmentation datasets for three types of fruits (apple, banana and orange). Unzip the dataset to yolov5the directory and use it fruits_clsas the dataset for the fruit classification task.

We execute the following code

image-20250407173125149

image-20250407173145274

Use YOLOv5 to train a fruit classification model

yolov5Execute the command in the directory and use yolov5to train three types of fruit classification models:

We enter the yolov5 directory (~/yolov5 in the example)

Execute the following code to start training

If an interface similar to the one below appears, it means that training has started.

image-20250407173506203

After the training is successfully completed, the output is as follows

image-20250407174312591

Convert the trained model to a kmodel that can be recognized by K230

The model we trained with yolov5 is best.pt with the suffix pt

In order to use it on K230, we need to convert it into a model in kmodel format

Download the conversion tool

Execute the following code to install the required tools

image-20250407174846393

Download the script tool and test_yolov5.zipunzip the model conversion script tool to yolov5the directory;

image-20250407174945839

Start model format conversion

Step 1: Convert pt format to onnx format

Note that "runs/train-cls/exp/weights/best.pt" here is the path of the pt model we trained.

If the previous steps are done exactly as the tutorial, then the path here does not need to be modified

Step 2: Enter the test_yolov5/classify directory

image-20250407175320091

Execute model conversion instructions

Note: An error may occur at this time: RuntimeError: /root/miniconda3/bin/../lib/libstdc++.so.6: version `GLIBCXX_3.4.30' not found (required by /usr/lib/dotnet/host/fxr/7.0.19/libhostfxr.so): Success

Solution:

Check if GLIBCXX_3.4.30 exists

If there is one in the output, it means that the file is not really missing, but the version of libstdc++ in the virtual environment is lower than the version of the libstdc++ library in the system.

image-20250407175635681

At this point we only need to execute the following command

Delete the libstdc++.so.6 file in the virtual environment (or rename it to another name, here we rename it to libstdc++.so.6.old)

Then create a soft link and let the libstdc++.so.6 called by the virtual environment point to the libstdc++.so.6 that comes with the system.

After the conversion command is completed, the output is as follows

image-20250407181403685

WARNING can be ignored here

The generated model is in the directory ~/yolov5/runs/train-cls/exp/weights

image-20250407182035920

The name is best.kmodel

We copy this best.kmodel to the data directory of our K230

image-20250407182129333

Then open CanMV IDE, copy the following code and click Run

Let's test the effect

Identify Apples

image-20250407182347568

Identify bananas

image-20250407182436788

Identify oranges

image-20250407182541251

You can see that this routine can run successfully.

Other considerations

  1. Environment configuration, Yolov5 environment dependencies, and model conversion tools only need to be downloaded once, and no re-download is required for subsequent training
  2. If the speed of pip download is much lower than the actual network bandwidth, please check whether the pip mirror source is configured.
  3. The model trained with the current dataset may identify non-banana objects other than oranges/apples as bananas. This is normal. If a more accurate model is needed, a training set with a larger amount of data can be prepared for training.