YOLOv5 version 5.0 + OpenCV 4.5.2 deployment

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


Preface

The version must be consistent with mine, otherwise it is not guaranteed to run! ! !


1. YOLOv5 5.0

We choose YOLOv5 5.0 version for this task. In https://github.com/ultralytics/yolov5 , you can choose the corresponding version to download. Since most of the C++ code on GitHub is v5 5.0, v5 5.0 was chosen. (If you are proficient in the later process and deployment code, you can try the deployment of v5 7.0 version)

1. Download the code

Opening the above link directly here should be a page like this, which is the code of v5 7.0
Insert image description here

We want to choose the v5 5.0 version, then we have to select Tags in the master, which contains the corresponding versions.
Insert image description here
This is the code of v5 5.0, as shown in the figure below.
Insert image description here
Then, we download the zip package of the code (you can also use git clone method (copy the code directly to the local)
Insert image description here
and extract it to your own folder
Insert image description here

2. Debugging training code

2.1. Configuration environment

Open the folder in PyCharm.
First, configure the required environment. The requirements.txt contains the minimum versions required for various packages. Copy pip install -r requirement.txt in the Terminal of PyCharm to start configuring the environment. (It is recommended to create a new virtual environment for yolov5 5.0 version to avoid the need to update the environment in other codes in the same environment.)
Insert image description here
Various packages in our environment can be viewed in Anaconda Prompt. I remember that the numpy and onnx versions cannot be too high. If there is any problem, just follow my version. (If you encounter other version problems, please search on Baidu and most of them can be solved)
Insert image description here

2.2. Training

Open the train.py file, and in if name == ' main ':, add the default value models/yolov5s.yaml to "-cfg". This is the yaml file of the yolov5s model. It will follow the settings in this yaml file later. Go build the network structure and download the yolov5s.pt file. (You can set up other models, just write the path name correctly.)
Insert image description here
Right-click train, you can see that the yolov5s.pt file is downloading below, but if you look closely, you can see that it is downloading version v7.0. What we want is version 5.0. of. And we will see an error after the download is completed. The reason for the error is that our .pt file was downloaded incorrectly, so we need to find the corresponding file from the official website.
Insert image description here
Insert image description here
Go back to our official website address on GitHub. There is a release on the right side.
Insert image description here
Scroll down and you will find the link to the 5.0 version. Click to enter.
Insert image description here
At the bottom there will be the yolov5 5.0 version of yolov5s.pt. Download it and put it in the folder just now. . Insert image description here
At this time, when we click train, an error will still be reported. You can see that it is because of a path problem and the downloaded coco128.zip is not decompressed (it is not clear whether the code is written incorrectly or it is my problem).
Insert image description here
Since the default value of "--data" in train.py is data/coco128.yaml, let's open coco128.yaml and take a look. It is found that train and val point to the coco128 folder in the upper-level directory, then we manually decompress it and change the path to an absolute path.
Insert image description here
Click train again, and an error will still be reported.
Insert image description here
Modify this line in loss
Insert image description here
. At the same time, delete the .cache cache file in the labels folder in the data set.
Insert image description here
Then train can run! ! !
Insert image description here
After training, best.pt and last.pt files will be generated
Insert image description here

3. Export .onnx file

Open the export.py file in the models folder, modify the default value of "-weights" to the best.pt path after training, and then export it. The corresponding .onnx file will be generated in the best.pt folder.
Insert image description here

2. C++ deployment

Here I found the C++ code directly on GitHub, and directly posted the link https://github.com/xiaojunjun65/YOLOv5-onnx . The author’s tutorial is at https://blog.csdn.net/xiaojunjun200211/article/details/121784214 , you can refer to
the C++ code by yourself, mainly modify the value of class_name, .onnx file path and image path, and then you can run it.


Summarize

It is recommended that students who have no deployment experience can first deploy version 5.0 according to this process, or when looking for code on GitHub, be sure to deploy according to the YOLOv5 version required by the author, and then deploy other versions after becoming proficient. The main problem is that if the version is inconsistent with the author, the detection box will not be displayed, the .onnx node will report errors, etc. I am a beginner and I don’t know the reason yet. If you want to learn more later, you can try the deployment of a higher version.
Insert image description here
Friends are welcome to communicate and give advice, thank you! ! !
Thanks: https://blog.csdn.net/xiaojunjun200211/article/details/121784214

Guess you like

Origin blog.csdn.net/m0_37591982/article/details/131239786