YOLOv5 tobacco disease detection system optimization tutorial

The code is placed at the end of the text

1, Environment configuration

Please refer to station B for pycharm installation

You can also refer to my other article: (1 message) AI recognition tutorial yolov5 (cross the fire line, csgo and other FPS game recognition)_ SYBH. Blog - CSDN blog_ yolo real-time detection of game screen

2, Dataset annotations

(1) Install labelme

pip install labelme in Anaconda Prompt

(2) Use labelme

Enter labelme in Anaconda Prompt, and a window will pop up.

Then open the folder where the picture is located

Click rectangle to mark the object you want to identify.


After marking, save it to a new folder. The saved file format is. json

Convert format (json to txt)

import json
import os
name2id =  {'Global Risk':0,'Black List':1}    #Label name:
def convert(img_size, box):
    dw = 1. / (img_size[0])
    dh = 1. / (img_size[1])
    x = (box[0] + box[2]) / 2.0 - 1
    y = (box[1] + box[3]) / 2.0 - 1
    w = box[2] - box[0]
    h = box[3] - box[1]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)
def decode_json(json_floder_path, json_name):
    txt_name = 'C:\\img \\' + json_name[0:-5] + '.txt'
    #Absolute path to store txt
    txt_file = open(txt_name, 'w')
    json_path = os.path.join(json_floder_path, json_name)
    data = json.load(open(json_path, 'r', encoding='gb2312',errors='ignore'))
    img_w = data['imageWidth']
    img_h = data['imageHeight']
    for i in data['shapes']:
        label_name = i['label']
        if (i['shape_type'] == 'rectangle'):
            x1 = int(i['points'][0][0])
            y1 = int(i['points'][0][1])
            x2 = int(i['points'][1][0])
            y2 = int(i['points'][1][1])
            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write(str(name2id[label_name]) + " " + " ".join([str(a) for a in bbox]) + '\n')
if __name__ == "__main__":
    json_floder_path = 'G:\\img\\'
    #The absolute path of the folder where the json is stored
    json_names = os.listdir(json_floder_path)
    for json_name in json_names:
        decode_json(json_floder_path, json_name)

3, Training

reference resources: (1 message) AI recognition tutorial yolov5 (cross the fire line, csgo and other FPS game recognition)_ SYBH. Blog - CSDN blog_ yolo real-time detection of game screen

4, Optimize

Improvement of YOLOv5 network

Since the disease target is very small and the receptive field of the smallest detection head of the original YOLOv5 is still larger than the size of the target, it is necessary to improve the original network
Generally, the structure of target detection includes the following modules:


We can start with each of the above modules and propose improvement points. For example, we start with the optimization algorithm. The author of Yolov5 provides two optimization functions Adam and SGD (default), and preset matching training super parameters.
I suggest that if you need to train smaller custom datasets, Adam is the more appropriate choice. If a larger dataset is trained, SGD is better for Yolov5 than Adam.
In fact, the academic community has not reached a unified conclusion on which is better between SGD and Adam, which depends on the actual project situation.
At the same time, we can improve the activation function. The authors of Yolov5 use leaky relu and sigmoid activation functions. In Yolov5, the leak relu activation function is used in the middle / hidden layer, and the S-shaped activation function is used in the last detection layer. Yolov4 uses the mish activation function.

We can improve the loss function. The loss calculation of Yolo series is based on the target score, class probability score and boundary box area score
Yolo V5 uses GIOU loss as the loss of the bounding box.
Yolo V4 uses the CIO loss as the loss of the bounding box. Compared with other methods mentioned above, CIOU loss brings faster convergence speed and better performance.

How to modify a model through code
(1) First of all, you need to open the model / common The bottleneck and PW of implementing mobilenetv2 in PY_ Conv.

(2) Then, you need to change the code (parse_model function of models/yolo.py) to read the model configuration file in yolo V5 and call the above module.

(3) And PW_ The conv bottleneck MOB needs to be added at the import reference,

(4) Then, we built the model configuration file, and I modified it on the basis of yolov5s.yaml

Disease detection training & recognition effect

system integration
Finally, we integrated the tobacco leaf detection and disease detection systems, so that the operator can know the health status of all tobacco leaves with one glance. At the same time, the disease type, location and quantity of each leaf are clearly marked on the leaves. Good robustness, high recognition rate and high accuracy.



Code link + video tutorial: 🍞 We are shipping your work details

Tags: Python programming language

Posted by gijs25 on Sat, 27 Aug 2022 17:55:21 +0530