YOLOv5-6.1 teaches you how to reproduce and realize your own data

1.YOLOv5 directory structure

├──.github: Contains USSUE_TEMPLATE and workflows. It stores some configuration files. It is not too important. You can leave it alone.

├── data: Configuration files (such as .yaml files) that mainly store some hyperparameters are used to configure the paths of training sets, test sets, and validation sets, which also include the number of target detection types and the names of the types) ; There are also some official pictures provided for testing. If you are training your own data set, you need to modify the yaml file. However, it is not recommended to place your own data set under this path. Instead, it is recommended to place the data set under the same level directory of the yolov5 project . You can also test your own files by placing the video/picture that needs to be tested under images and running detect.py

 

               hyps folder    # stores hyperparameter configuration files in yaml format

               images   # Stores two official test pictures

               scripts   # Store multi-category data sets and weight download shell scripts

├── models: It mainly contains some configuration files and functions for network construction, which contains four different versions of the project, namely s, m, l, and x. As you can tell from the name, the size of these versions. Their detection measures are from fast to slow, but their accuracy is from low to high. This is the so-called inability to have your cake and eat it too. If you train your own data set, you need to modify the corresponding yaml file to train your own model.

├── Utils: It stores tool functions, including loss functions, metrics functions, plots functions, etc., as well as tools for connecting and interrupting related platforms.

Other directory files at the same level

  • .dockerignore    # docker’s ignore file
  • .gitattributes    # Used to exclude files with .ipynb suffix from GitHub language statistics
  • .gitignore    # docker’s ignore file
  • CONTRIBUTING.md   # markdown format documentation
  • detect.py    #Use the trained weight parameters for target detection, which can detect images, videos and cameras.
  • export.py   # Model export
  • hubconf.py   # pytorch hub related
  • LICENSE     # Certificate
  • README.md     # markdown format documentation
  • requirements.txt   # You can download dependent environments through pip install requirement
  • setup.cfg   # project packaging file
  • train.py    # Function to train your own data set.
  • tutorial.ipynb   # Tutorial on getting started with target detection
  • val.py   # Target detection verification script

2. The folder runs appears after training.

The folder train stores the data process recorded during training data.
The folder detect stores the data for each prediction using the trained model.

3. Train your own model

3.1 Modify the data configuration file.
         Once the pre-training model type and data set are ready, you can start training your own yolov5 target detection model. Training the target detection model requires modifying the parameters in two yaml files. One is the corresponding yaml file in the data directory, and the other is the corresponding yaml file in the model directory.

       Modify the corresponding yaml file in the data directory. Find the coco.yaml file in the directory, make a copy of the file, and rename the copied file, preferably related to the project, so as to facilitate subsequent operations. I changed it here to test1.yaml

       Open this folder and modify the parameters in it.

After change

3.2 Modify the model configuration file.
       Since this project uses the pre-training weight yolov5s.pt, the corresponding parameters in the yolov5s.yaml file in the models directory must be used (because different pre-training weights correspond to different numbers of network layers, so Using the wrong pre-training weights will result in an error). Just like modifying the yaml file in the data directory above, we'd better copy the yolov5s.yaml file and then rename it. I renamed it to yolov5_games.yaml.

        To open the yolov5_games.yaml file, you only need to modify the numbers in the picture. Here is the identification of 5 categories.

       At this point, the corresponding configuration parameters have been modified.

3.3 Train your own model and enable tensorbord to view parameters.
       If the parameters of the above data set and the two yaml files have been modified, you can start yolov5 training. First we find the py file train.py.

         Then find the entrance to the main function, which contains the main parameters of the model. The analysis of the main parameters of the model is as follows.

if __name__ == '__main__':
"""
    Opt model main parameter analysis:
    --weights: the path address of the initialized weight file
    --cfg: the path address of the model yaml file
    --data: the path address of the data yaml file
    -- hyp: hyperparameter file path address
    --epochs: training rounds
    --batch-size: how many batch files to feed
    --img-size: input image size
    --rect: whether to use rectangular training, default False
    --resume :Then interrupt the training and continue the training with the last result
    --nosave: do not save the model, the default is False
    --notest: do not perform the test, the default is False
    --noautoanchor: do not automatically adjust the anchor, the default is False
    --evolve: whether to perform hyperparameters Evolution, default False
    --bucket: Google cloud disk bucket, generally not used
    --cache-images: whether to cache images into memory in advance to speed up training, default False
    --image-weights: use weighted image selection for training
    --device: training device, cpu; 0 (indicates one gpu device cuda:0); 0, 1, 2, 3 (multiple gpu devices)
    --multi-scale: whether to perform multi-scale training, the default is False
    --single-cls: whether the data set has only one category, the default is False
    --adam: whether to use the adam optimizer
    --sync-bn: whether to use cross-card synchronization BN , use in DDP mode
    --local_rank: DDP parameters, do not modify
    them --workers: maximum number of working cores
    --project: storage location of the training model
    --name: directory name where the model is saved
    --exist-ok: whether the model directory is If it exists, create it if it does not exist     . To train your own model, you need to modify the following parameters before training. First fill in the path of weights into the corresponding parameters, and then add the path to the yolov5s.yaml file of the repaired models model
.
Fill in the corresponding parameters, and finally fill in the text1.yaml file path of the data data into the corresponding parameters. These parameters must be modified.

 
         There are several parameters that need to be changed according to your own needs:

        The first is the training rounds of the model, here are the 300 rounds of training.

  parser.add_argument('--epochs', type=int, default=300)
         The second step is the number of input images and the number of working cores. Everyone's computer here is different, so the performance of everyone here depends on their own computer. , here we need to adjust these two parameters. Everyone’s computer configuration is different, so you can modify the parameters according to your own computer configuration. The official one is 16, but due to computer limitations, the batch-size can be changed to 8

parser.add_argument('--batch-size', type=int, default=8, help='total batch size for all GPUs, -1 for autobatch')
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')


       Once the above is set up, you can start training. However, pycharm users may encounter the following errors. This means there is not enough virtual memory.  

     

It can be modified according to the following operations. Find the datasets.py file in the utils path and change the search num_workers parameter nw to 0.

 

 

        At this point, you can run the train.py function to train your own model. 

 3.4 Enable tensorbord to view parameters.
         There is a tensorbord function written in yolov5. You can run the command to call tensorbord, and then view tensorbord. First open the command control terminal of pycharm, enter the following command, and a URL address will appear. Copy that line of URL and open it in the browser to see the training process.

 

 
        As shown in the figure below, this has been trained for 100 rounds.

        If the model has been trained, but we still want to use tensorbord to view the training process of this model, we need to enter the following command. You can see the training results of the model.

tensorboard --logdir=runs
4 inference test

        After the data is trained, a run folder will be generated in the main directory, and two weight files will be generated in the run/train/exp/weights directory, one is the weight file of the last round, and the other is the best Weight file, we will use this best weight file for inference testing later. In addition, some files such as pictures of verification files will also be generated.

         Find the detect.py file in the home directory and open the file.

        Then find the entrance to the main function, which contains the main parameters of the model. The analysis of the main parameters of the model is as follows. 

f __name__ == '__main__':
"""
--weights: path address of weights
--source: test data, which can be a picture/video path, or '0' (the computer comes with a camera), or rtsp Waiting for video stream
--output: Save path of pictures/videos after network prediction
--img-size: Network input picture size
--conf-thres: Confidence threshold
--iou-thres: Iou threshold for nms
--device :Whether to use GPU or CPU for inference
--view-img: Whether to display the predicted pictures/videos, the default is False
--save-txt: Whether to save the predicted frame coordinates in a txt file, the default is False
--classes: Setting Only keep a certain part of the category, in the form of 0 or 0 2 3
--agnostic-nms: Whether nms also removes the boxes between different categories, the default is False
--augment: Perform multi-scale, flip and other operations (TTA) during inference Inference
--update: If True, strip_optimizer operation is performed on all models to remove optimizer and other information in the pt file. The default is False
--project: The results of inference are saved in the runs/detect directory
--name: The results are saved folder name

Modify to the weight of the relevant path
""

         Here you need to pass the best weights that have just been trained into the inference function. Inference can then be performed on the image video.


        To test inference on the picture, change the following parameters to the path of the picture, and then run detect.py to test.
        After the inference test is completed, a detect directory will be generated under run, and the inference results will be saved in the exp directory. as the picture shows.

         The inference results for the picture are shown below. The effect is still very good.

 

        Testing the video is the same as testing the picture above, except that the path of the picture is changed to the path of the video.

        To test using the camera, just rewrite the path to 0. But it seems that an error will still be reported, which has stuck with me for a long time. The error is reported as follows.

         Solution: First find the py file datasets.py.  

        Open the file, find the 279th line of code, and add str to the two url parameters. As shown in the picture, you can run the computer's camera perfectly. 

        At this point, yolov5 has completely completed training its own model.
 

reference

YOLOv5 source code line-by-line ultra-detailed annotation and interpretation (1) - Project directory structure analysis_yolov5 source code analysis_Luren Jia'ω''s blog-CSDN blog target detection---teach you to use yolov5 to train your own target detection model_yolov5 Training model_The blog that Brother Pao takes you to learn-CSDN blog

Guess you like

Origin blog.csdn.net/weixin_61076411/article/details/129519786