YOLOv5 series 1. Make your own data set

YOLOv5 series 1. Make your own data set
YOLOv5 series 2. Use yolov5 to identify your own data



foreword

The Yolov5 used in this article is version 6.1, and the GPU version is used (pro-test CPU can also run, but the speed will be much slower), and the environment used is torch1.7.1+cuda10.1.


1. Download Labelme

The installation of Labelme is very simple, if you want to install it directly on the command line:

pip install labelme

If you want to install labelme in Anaconda's virtual environment virtual environment, you need to enter the virtual environment first, and then install:

activate 自己的想安装的环境名称
pip install labelme

If you want to use Anaconda to install a virtual environment for Labelme separately:

conda create -n labelme python=3.6
activate labelme
pip install labelme

2. Labelme usage steps

1. Open Labelme

According to the installation location in the first step, use the command line to directly enter labelme in the corresponding location to open:

labelme

insert image description here
Because I installed it directly, I didn't enter the virtual environment. After opening, the following interface will automatically pop up:
insert image description here
the above mark is the main function we mark, first use Open dir to open the folder where our image data is located:
insert image description here

2.Labelme label data set

Here is a data set for detecting masks downloaded from kaggle, and then click Create Polygons, because it is made with Yolov5, so right-click on the image, select create Rectangle, and then frame the mask.
insert image description here
Then click ok and continue to mark the next mask. After all the masks are marked, click the next one or click save:
insert image description here

3. Save in json format

insert image description here
Just save it directly in .json format (the saved name is the same as the name of the photo), and mark it all the way.

3. Convert json format to txt format

Because yolov5 needs photos and txt format, the json file we get cannot be used directly. We need to convert json to txt format first. The following code is to convert json to txt :

import json
import os

name2id = {
    
    'Mask': 0,}  #具体自己数据集类别
               
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的标签路径
    txt_name = 'E:\\BaiduNetdiskDownload\\PyTorch-YOLOv3\\data\\custom\\labels\\' + json_name[0:-5] + '.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'))
 
    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标签路径
    json_floder_path = 'E:\\BaiduNetdiskDownload\\PyTorch-YOLOv3\\data\\custom\\json\\'
    json_names = os.listdir(json_floder_path)
    for json_name in json_names:
        decode_json(json_floder_path,json_name)

Here we have obtained the label file in txt format required by yolov5.


4. Create your own Yolov5 data set

The location of the dataset folder I created here is as follows:

--MaskDataSet
	--train
		--images
		--labels
	--test
		--images
		--labels
	--valid
		--images
		--labels
	--data.yaml
--yolov5源码位置

Among them, images are put into the picture, and labels are put into the corresponding txt label file; the content of the data.yaml file is as follows:

train: ../MaskDataSet/train/images
test: ../MaskDataSet/test/images
val: ../MaskDataSet/valid/images	
#如果按照上述方式建立文件夹,则上面train、test和val地址可以不变

nc: 1   							#标签类别个数
names: ['Mask',]  					#标签名
#上面nc和names可以根据自己的数据集进行修改

Finally, the dataset is created successfully! Next, we can start training our labeled dataset~

Guess you like

Origin blog.csdn.net/fjlaym/article/details/123992962