yolov5 training coco128 data set and testing and detection

yolov5 training coco128 data set and testing and detection

reference link

Build a COCO dataset training, verification and testing environment for YOLOv5
CoCo dataset download

YOLOv5 training coco128

YOLOv5 environment construction, coco128 training examples, detailed records [understand in one article]

1. coco data set

1 Introduction

The full name of MS COCO is Microsoft Common Objects in Context, which originated from Microsoft's 2014 Microsoft COCO dataset. It is mainly used for target detection, image segmentation, pose estimation, etc. There are 80 classes in total. MSCOCO is a large-scale dataset with 80 categories
insert image description here
. , the data is divided into three parts: training, verification and testing, each part contains 118287, 5000 and 40670 pictures respectively, and the total size is about 25g. The test data set has no label information, so the annotation part only has training and verification.
About COCO's test set: The 2017 COCO test set contains ~40K test images. The test set is split into two roughly equal-sized splits of about 20K images: test-dev and test-challenge.
insert image description here

Test-Dev: The test-dev split is the default test data for testing in general. The results of papers should usually be reported in the test-dev set for fair and open comparisons.
Test-Challenge: The test-challenge split is used for the annual hosted COCO challenge

2. Download

(1) Official website
The first method must be the official website. Go to the download page: Images on the official website
insert image description here
are datasets, Annotations indicate that annotation information is stored in JSON format ( annotations ), and COCO API is used to access and operate all "annotations" for preprocessing
insert image description here
. Year data sets are used in different tasks, commonly used are 2017 train/val/test images

(2) Other download methods
CoCo dataset Download Dataset
's COCO dataset: Introduction to COCO dataset, download, and detailed guide on how to use it

3. Decompressed data

After downloading, decompress the image to coco/images/ according to the requirements of https://github.com/cocodataset/cocoapi, as shown in the figure below.
insert image description here
Download and decompress the image according to the path requirements
, then download the annotations_trainval2017.zip file, and decompress the annotations file to coco/annotations/
insert image description here
download and decompress the annotations according to the path requirements according to the requirements of cocoapi. The ones used for target detection are: instances_val2017.json and instances_train2017.json

4. Convert COCO dataset (.json) training format to YOLO format (.txt)

detailed! correct! COCO data set (.json) training format converted to YOLO format (.txt)
yolov5 data set format: COCO data set converted to yolov5 training data set format code
Put the original image of the downloaded coco data in the images directory, the standard file Placed under annotations, in the coco data set, the position of the target (category) marked in the coco2017train or coco2017val data set is represented by (x, y, width, height) in the annotations file, x, y represent the position of the upper left corner of the bbox, width , height indicates the width and height of the bbox. When YOLO is training or verifying, the label format read is represented by (xmin, ymin, xmax, ymax), where xmin, ymin represent the position of the upper left corner of the bbox, xmax, ymax represent the position of the lower right corner of the bbox, and are required to be saved It is in .txt file format (the name corresponds to the image)
insert image description here

The implementation code is as follows

#-*-coding:gb2312-*-


#COCO 格式的数据集转化为 YOLO 格式的数据集
#--json_path 输入的json文件路径
#--save_path 保存的文件夹名字,默认为当前目录下的labels。

import os
import json
from tqdm import tqdm
import argparse

parser = argparse.ArgumentParser()
#这里根据自己的json文件位置,换成自己的就行
parser.add_argument('--json_path', default='/home/kandi/datasets/coco/annotations/instances_train2017.json',type=str, help="input: coco format(json)")
#这里设置.txt文件保存位置
parser.add_argument('--save_path', default='/home/kandi/datasets/coco/labels/train2017', type=str, help="specify where to save the output dir of labels")
arg = parser.parse_args()

def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = box[0] + box[2] / 2.0
    y = box[1] + box[3] / 2.0
    w = box[2]
    h = box[3]
#round函数确定(xmin, ymin, xmax, ymax)的小数位数
    x = round(x * dw, 6)
    w = round(w * dw, 6)
    y = round(y * dh, 6)
    h = round(h * dh, 6)
    return (x, y, w, h)

if __name__ == '__main__':
    json_file =   arg.json_path # COCO Object Instance 类型的标注
    ana_txt_save_path = arg.save_path  # 保存的路径

    data = json.load(open(json_file, 'r'))
    if not os.path.exists(ana_txt_save_path):
        os.makedirs(ana_txt_save_path)

    id_map = {} # coco数据集的id不连续!重新映射一下再输出!
    with open(os.path.join(ana_txt_save_path, 'classes.txt'), 'w') as f:
        # 写入classes.txt
        for i, category in enumerate(data['categories']):
            f.write(f"{category['name']}\n")
            id_map[category['id']] = i
    # print(id_map)
    #这里需要根据自己的需要,更改写入图像相对路径的文件位置。
    list_file = open(os.path.join(ana_txt_save_path, 'train2017.txt'), 'w')
    for img in tqdm(data['images']):
        filename = img["file_name"]
        img_width = img["width"]
        img_height = img["height"]
        img_id = img["id"]
        head, tail = os.path.splitext(filename)
        ana_txt_name = head + ".txt"  # 对应的txt名字,与jpg一致
        f_txt = open(os.path.join(ana_txt_save_path, ana_txt_name), 'w')
        for ann in data['annotations']:
            if ann['image_id'] == img_id:
                box = convert((img_width, img_height), ann["bbox"])
                f_txt.write("%s %s %s %s %s\n" % (id_map[ann["category_id"]], box[0], box[1], box[2], box[3]))
        f_txt.close()
        #将图片的相对路径写入train2017或val2017的路径
        list_file.write('./images/train2017/%s.jpg\n' %(head))
    list_file.close()

YOLOv5 parses the official json format annotation file into a txt file, which can also be downloaded from the link , and then decompressed to the .../datasets folder

2. Training train.py

yolov training custom data

  • train.py: data loader designed for both speed and accuracy
  • val.py: Validation (Validation) is usually used to test the accuracy of the model after the model training is completed, mAP
  • detect.py: Aims to achieve the best inference results in the real world.

Here I choose to train the YOLOv5s model on COCO128. Pretrained weights are automatically downloaded from the latest YOLOv5 release.

python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --weights yolov5s.pt

  • img: Specifies the pixels of the training photo
  • batch: the number of training pictures in a single iteration, the larger the memory required, the larger
  • epochs: training rounds
  • data: need to be modified according to your own data set and its location
  • weights: weight file

The log information of the input part of the terminal during training is as follows:

                 Class     Images  Instances          P          R      mAP50   mAP50-95:  50%|     | 2/4 00:00
                 Class     Images  Instances          P          R      mAP50   mAP50-95:  75%|  | 3/4 00:00
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100%|| 4/4 00:00
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100%|| 4/4 00:00
                   all        128        929      0.748      0.651      0.737      0.487

3 epochs completed in 0.003 hours.
Optimizer stripped from runs/train/exp/weights/last.pt, 14.8MB
Optimizer stripped from runs/train/exp/weights/best.pt, 14.8MB

Validating runs/train/exp/weights/best.pt...
Fusing layers... 
Model summary: 157 layers, 7225885 parameters, 0 gradients, 16.4 GFLOPs

                 Class     Images  Instances          P          R      mAP50   mAP50-95:   0%|          | 0/4 00:00
                 Class     Images  Instances          P          R      mAP50   mAP50-95:  25%|       | 1/4 00:00
                 Class     Images  Instances          P          R      mAP50   mAP50-95:  50%|     | 2/4 00:01
                 Class     Images  Instances          P          R      mAP50   mAP50-95:  75%|  | 3/4 00:02
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100%|| 4/4 00:03
                 Class     Images  Instances          P          R      mAP50   mAP50-95: 100%|| 4/4 00:03
                   all        128        929      0.752      0.651      0.738      0.487
                person        128        254      0.871      0.718      0.808      0.528

Among them, you can see Validating runs/train/exp/weights/best.pt..., indicating that after each batch training in the training phase, the val script will be called once to verify the model once. And when the entire model training is over, the val script will be called again.

All training results are saved in the run directory of runs/train/incremental, namely runs/train/exp2 and other runs/train/exp3. After the training is completed, find last under the runs->train->exp->weights folder. pt and best.pt files, where best.pt is the weight file with the best training effect, and last.pt is the weight file for the last round of training

3. Verify val.py

  • test.py has been changed to val.py
  • train.py: data loader designed for both speed and accuracy
  • val.py: Validation (Validation) is usually used to test the accuracy of the model after the model training is completed, mAP
  • detect.py: Aims to achieve the best inference results in the real world.

After the training is complete, find the last.pt and best.pt files in the runs->train->exp->weights folder and open val.py to run the model verification.

python val.py --weights runs/train/exp6/weights/best.pt --data coco128.yaml --img 640

4. Reasoning prediction detect.py

  • train.py: data loader designed for both speed and accuracy
  • val.py: Validation (Validation) is usually used to test the accuracy of the model after the model training is completed, mAP
  • detect.py: Aims to achieve the best inference results in the real world.

1. Use the model best.pt trained by coco128 to detect

The small weight file best.pt we trained above is about 14.1M. The detection command is as follows:

python detect.py --weights runs/train/exp6/weights/best.pt --source data/images/zidane.jpg

The test results are as follows:
[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-Pi7ruYTd-1670036355059) (yolov5 training coco data set.assets/image-20221203091642062.png)]

2. Use yolov5 pre-trained small model yolov5s.pt

yolov5s.pt is about 14.1M, and the detection command is as follows:
python detect.py --weights weights/yolov5s.pt --source data/images/zidane.jpg
The detection results are as follows:
insert image description here

3. The large model yolov5x6.pt pre-trained with yolov5

yolov5x6.pt is about 269M, and the detection command is as follows. The
python detect.py --weights weights/yolov5x6.pt --source data/images/zidane.jpg
detection results are as follows. From
insert image description here
the confidence values ​​(values ​​on the red and green boxes) detected by the above three situations, the higher the confidence value obtained from the large weight file.

Guess you like

Origin blog.csdn.net/LoongEmbedded/article/details/128044021