【基于YOLO-v3训练自己的数据与检测任务】

文前白话

本文以YOLO-v3为例,介绍训练自己的检测任务过程,包含数据集的制作与转化、训练参数调整等。

1、安装标注工具:labelme 进行数据标注

# python3
conda create --name=labelme python=3.6
conda activate labelme
pip install labelme

2、 准备需要的网络配置文件

在这里插入图片描述

  • 通过安装 git 可以在Windows系统中,在该文件夹下执行bash create_custom_model.sh 3 命令(所在文件夹右键 Git bash Here) ,其中, 数字 3 表示实际的分类数,会自动生成 yolov3-custom.cfg 配置文件:
    在这里插入图片描述
  • 自动生成的yolov3-custom.cfg 相比于coco数据集的训练网络模型参数都没变,只是分类数目改变:
    在这里插入图片描述- 具体的先验框大小等参数也可以自己修改。
  • 特别注意:create_custom_model.sh不能重复执行,每次执行要先把之前生成的 yolov3-custom.cfg 文件 删除掉才可以。

3、进行标签格式转换

  • (这个要看使用的源码类型,yolo-v3 和 v4/v5 源码中的数据标签有所不同)

  • labelme中的标签框信息格式为 :x1,y1,x2,y2
    在这里插入图片描述

  • YOLO-V3中使用的格式是:Cx,Cy,W,H 相对位置(取值范围0-1)
    在这里插入图片描述

  • 可以使用 json2yolo.py 脚本代码 进行格式转换:

# 	json2yolo.py 用它来把标签转换成对的格式
 
import json
import os

name3id = {'person':0,'car':1,'moto_car':2,} # 自己的类别
# 注意: 这里的类别顺序要和 classes.names 里的顺序一致
               
def convert(img_size, box): # 拿到json文件中的x1,y1,x2,y2坐标进行转换--》转换为相对位置【最好画坐标草图理解逻辑】
    dw = 1./(img_size[0]) #  dw = 1 / img_w
    dh = 1./(img_size[1]) #  dh = 1 / img_h
    x = (box[0] + box[2])/2.0 - 1   # 计算 x 的中心点
    y = (box[1] + box[3])/2.0 - 1   # 计算 y 的中心点
    w = box[2] - box[0]   # 通过坐标得到框的宽 w = x2 - x1
    h = box[3] - box[1]   # 通过坐标得到框的高 h = y2 - y1
    x = x*dw    #  将 w,h 转换到(0,1)之间的数据 (按比例缩小)
    w = w*dw
    y = y*dh  
    h = h*dh
    return (x,y,w,h)  # 得到转化后框的相对位置中心坐标以及 w,h
 
 
def decode_json(json_floder_path,json_name):
 
    txt_name = 'E:\\002_GIT\PyTorch-YOLOv3\\data\\custom\\labels\\' + json_name[0:-5] + '.txt' # 转换好的标签存放路径及名称

    txt_file = open(txt_name, 'w') # 写入
    # json_floder_path:labelme标注生成json文件的路径
    json_path = os.path.join(json_floder_path, json_name)  # 读入json文件
    data = json.load(open(json_path, 'r', encoding='gb2312'))
    # 提取数据
    img_w = data['imageWidth']  # 获取原图的  W
    img_h = data['imageHeight'] # 获取原图的  H
 
    for i in data['shapes']: # 遍历文件中的每一个 sharps
        
        label_name = i['label']  # 获取类别名
        if (i['shape_type'] == 'rectangle'):
 
            x1 = int(i['points'][0][0])  # 取到的坐标json文件中的x1,y1,x2,y2 各自坐标值
            y1 = int(i['points'][0][1])  # y1
            x2 = int(i['points'][1][0])  # x2
            y2 = int(i['points'][1][1])  # y2
 
            bb = (x1,y1,x2,y2) # 取出的bbox数值
            bbox = convert((img_w,img_h),bb) # 拿到标注框的坐标以后导入convert函数进行转换
            txt_file.write(str(name3id[label_name]) + " " + " ".join([str(a) for a in bbox]) + '\n')
    
if __name__ == "__main__":
    
    json_floder_path = 'E:\\002_GIT\\PyTorch-YOLOv3\\labels_uav'    # 读入 labelme标注生成json文件夹
    json_names = os.listdir(json_floder_path)
    for json_name in json_names: # 遍历文件夹内的json文件
        decode_json(json_floder_path,json_name) # 对每一个json文件,做decode_json的函数解析,进行格式转换

  • 转化后前后的标签格式对比:

在这里插入图片描述
在这里插入图片描述

4、设置图片数据与标签的路径

  • 标注图片存放路径
    在这里插入图片描述
  • 更改 classes.names 文件:改成自己训练任务里对应的类别名
    在这里插入图片描述
    在这里插入图片描述
  • 训练集与验证集分配,路径写入txt文件中:
  • 在训练时,直接从 txt 文件中的路径地址读取相应的图像数据

在这里插入图片描述脚本遍历文件夹名称,并写入记事本中。
具体脚本参见: 【yolo训练时,遍历文件中的图片路径,并写入到指定的txt文件中】.

  • 然后修改 custom.data 中的数据:(设置读取数据的路径地址)

在这里插入图片描述
在这里插入图片描述

注意:不同系统下路径的表述: / or \

  • 可以参考源码中coco数据集的格式安排。

5、训练代码参数更改

  • 1.train.py需要设置的参数
    –model_def config/yolov3-custom.cfg
    –data_config config/custom.data
    –pretrained_weights weights/darknet53.conv.74 #在别人训练基础上去做
  • train.py中:

    • epoch、batch_size 依据实际的硬件配置设置
      在这里插入图片描述
  • datasets.py 中修改数据读取路径
    在这里插入图片描述

  • 改好就可以开始训练。

  • 训练过程打印出的信息:

在这里插入图片描述

6、预测时的代码参数更改

–image_folder data/samples/ #把需要预测的数据放到这里
–checkpoint_model checkpoints/yolov3_ckpt_100.pth #换成训练好模型的路径
–class_path data/custom/classes.names #画图时候要把框上显示出来自己的类别名

版权声明:本文为CSDN博主「Wupke」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Kefenggewu_/article/details/122336631

Wupke

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

TensorRTX 推演

前言 在github上下载到yolov5的第5版及其对应的tensorRTX版本,在目标硬件平台进行部署推演。 GitHub上tensorRTX步骤 (1)下载好tensorrtx中yolov5文

【原理篇】一文读懂Mask RCNN

Mask RCNN 何凯明大神的经典论文之一,是一个实例分割算法,正如文中所说,Mask RCNN是一个简单、灵活、通用的框架,该框架主要作用是实例分割,目标检测&#xff0