• yolov7训练


    一、制作数据集

    使用LabelImage进行标注,LabelImage的下载安装方法,百度上用的比较多,这里就不赘述了。简单介绍一下LabelImage的标注方法,制作自己的数据集。

    1、下载LabelImage

    下载LabelImage源码,地址:LabelImage地址
    下载LabelImage exe Release Binary v1.8.1 · heartexlabs/labelImg · GitHub

     

     2.安装及快捷键

    Ctrl + u选择要标注的文件目录;
    Ctrl + r选择标注好的标签存放的目录;
    Ctrl + s保存标注好的标签(自动保存模式下会自动保存);
    Ctrl + d复制当前标签和矩形框;
    Ctrl + Shift + d删除当前图片;
    Space将当前图像标记为已验证;
    w开始创建矩形框;
    d切换到下一张图;
    a切换到上一张图;
    del删除选中的标注矩形框;
    Ctrl++放大图片;
    Ctrl--缩小图片;
    ↑→↓←移动选中的矩形框的位置;

    每标注完一张图片要保存标注的数据,否则切换图片的时候标注的结果会消失!!!!点击快捷方式(CTRL+S)直接保存标注的结果。

    反复多张标注完成后,基础数据集已经标注完毕。

    3. 制作VOC2007数据集

    构建结构目录,我的结构目录如下所示:

    介绍一下各个文件夹的用途:

    Annotations:里面是标注图片对应的标注信息,是xml格式的(标注你的xml,可以自行进去看一下结构,里面主要的就是类别和标注的坐标点,其他不重要)。

    ImageSets:在后面里面生成Main文件夹,里面包含train和test,主要记录训练集的文件名称和测试集的文件名称。

    JPEImages:原始的图片数据。

    labels:该文件夹和ImageSets的Main文件夹在后面共同生成,用于生成VOC2007格式的数据集。

    4.生成VOC2007数据集的文件

    该步骤会将上一步的ImageSets和labels缺少的文件补齐,并生成2007_Train和2007_test的txt文件。下面的代码自动制作VOC2007的数据集,

    1)将代码拷贝到ubuntu下yolov7/data目录下新建为main.py

    2)在yolov7/data目录下新建文件夹Annotations和JPEImages。并将标注的xml和照片考入进去

    3)修改下面代码的路径为自己电脑的文件对应路径

    4)直接运行即可 

    1. #缺少依赖包的同学自行下载一下,很好下
    2. import xml.etree.ElementTree as ET
    3. import pickle
    4. import os
    5. from os import listdir, getcwd
    6. from os.path import join
    7. import random
    8. #类别根据你的数据集类别进行定义
    9. classes=["mosquitto"]
    10. def clear_hidden_files(path):
    11. dir_list = os.listdir(path)
    12. for i in dir_list:
    13. abspath = os.path.join(os.path.abspath(path), i)
    14. if os.path.isfile(abspath):
    15. if i.startswith("._"):
    16. os.remove(abspath)
    17. else:
    18. clear_hidden_files(abspath)
    19. def convert(size, box):
    20. dw = 1./size[0]
    21. dh = 1./size[1]
    22. x = (box[0] + box[1])/2.0
    23. y = (box[2] + box[3])/2.0
    24. w = box[1] - box[0]
    25. h = box[3] - box[2]
    26. x = x*dw
    27. w = w*dw
    28. y = y*dh
    29. h = h*dh
    30. return (x,y,w,h)
    31. #下面的文件夹和文件的名称根据你的喜好自定定义,也可以按照我这里的代码直接运行
    32. def convert_annotation(image_id):
    33. in_file = open('./Annotations/%s.xml' %image_id)
    34. out_file = open('./labels/%s.txt' %image_id, 'w')
    35. tree=ET.parse(in_file)
    36. root = tree.getroot()
    37. size = root.find('size')
    38. w = int(size.find('width').text)
    39. h = int(size.find('height').text)
    40. for obj in root.iter('object'):
    41. difficult = obj.find('difficult').text
    42. cls = obj.find('name').text
    43. if cls not in classes or int(difficult) == 1:
    44. continue
    45. cls_id = classes.index(cls)
    46. xmlbox = obj.find('bndbox')
    47. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
    48. bb = convert((w,h), b)
    49. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
    50. in_file.close()
    51. out_file.close()
    52. wd = os.getcwd()
    53. work_sapce_dir = os.path.join(wd, "./")
    54. if not os.path.isdir(work_sapce_dir):
    55. os.mkdir(work_sapce_dir)
    56. annotation_dir = os.path.join(work_sapce_dir, "Annotations/")
    57. if not os.path.isdir(annotation_dir):
    58. os.mkdir(annotation_dir)
    59. clear_hidden_files(annotation_dir)
    60. image_dir = os.path.join(work_sapce_dir, "JPEGImages/")
    61. if not os.path.isdir(image_dir):
    62. os.mkdir(image_dir)
    63. clear_hidden_files(image_dir)
    64. VOC_file_dir = os.path.join(work_sapce_dir, "ImageSets/")
    65. if not os.path.isdir(VOC_file_dir):
    66. os.mkdir(VOC_file_dir)
    67. VOC_file_dir = os.path.join(VOC_file_dir, "Main/")
    68. if not os.path.isdir(VOC_file_dir):
    69. os.mkdir(VOC_file_dir)
    70. train_file = open(os.path.join(wd, "2007_train.txt"), 'w')
    71. test_file = open(os.path.join(wd, "2007_test.txt"), 'w')
    72. train_file.close()
    73. test_file.close()
    74. VOC_train_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/train.txt"), 'w')
    75. VOC_test_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/test.txt"), 'w')
    76. VOC_train_file.close()
    77. VOC_test_file.close()
    78. if not os.path.exists('./labels'):
    79. os.makedirs('./labels')
    80. train_file = open(os.path.join(wd, "2007_train.txt"), 'a')
    81. test_file = open(os.path.join(wd, "2007_test.txt"), 'a')
    82. VOC_train_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/train.txt"), 'a')
    83. VOC_test_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/test.txt"), 'a')
    84. list = os.listdir(image_dir) # list image files
    85. probo = random.randint(1, 100)
    86. print("Probobility: %d" % probo)
    87. for i in range(0,len(list)):
    88. path = os.path.join(image_dir,list[i])
    89. if os.path.isfile(path):
    90. image_path = image_dir + list[i]
    91. voc_path = list[i]
    92. (nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))
    93. (voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))
    94. annotation_name = nameWithoutExtention + '.xml'
    95. annotation_path = os.path.join(annotation_dir, annotation_name)
    96. probo = random.randint(1, 100)
    97. print("Probobility: %d" % probo)
    98. if(probo < 75):
    99. if os.path.exists(annotation_path):
    100. train_file.write(image_path + '\n')
    101. VOC_train_file.write(voc_nameWithoutExtention + '\n')
    102. convert_annotation(nameWithoutExtention)
    103. else:
    104. if os.path.exists(annotation_path):
    105. test_file.write(image_path + '\n')
    106. VOC_test_file.write(voc_nameWithoutExtention + '\n')
    107. convert_annotation(nameWithoutExtention)
    108. train_file.close()
    109. test_file.close()
    110. VOC_train_file.close()
    111. VOC_test_file.close()

    5.训练模型

    1.修改yolov7/data/coco.yaml代码需要修改的地方为5处。

    1):把代码自动下载COCO数据集的命令注释掉,以防代码自动下载数据集占用内存;

    2):修改train的位置为train_list.txt的路径;

    3):修改val的位置为val_list.txt的路径;

    4):修改nc为数据集目标总数;

    5):修改names为数据集所有目标的名称。然后保存。

     2.修改yolov7/utils/datasets.py代码

    如下图所示 将images修改为JPEGImages

     3.将yolov7/cfg/training/yolov7.yaml 复制一共重新命名,修改nc为数据集目标总数;

     至此就可以开始训练了

    python train.py --weights weights/yolov7.pt --device '0' --cfg cfg/training/yolov7_mos.yaml --data data/coco.yaml --batch-size 16 --epoch 300
    

    下面简单介绍一下相关的参数。

    1. --weights weights/yolov7.pt # 接收预训练模型路径的参数
    2. --cfg cfg/training/yolov7_mos.yaml # 接收模型配置文件的参数
    3. --data data/coco.yaml # 接收数据配置文件的参数
    4. --device "0" # GPU/CPU训练,若1块,则"0";若2块,为"0","1";若CPU,则cpu
    5. --batch-size 16 # 按照自己GPU内存大小大致确定
    6. --epoch 300 # 迭代代数

     更多参数在yolov7/train.py里查看

    6. 训练结果与推理

    1. 训练结果

    训练结束后,终端会打印出最好的模型和最后一个epoch的模型结果保存在哪里,如下图所示,

  • 相关阅读:
    【MATLAB第73期】# 源码分享 | 基于MATLAB的不同类型数据排列方式合集
    11.18-创建视图 11.19-SELECT语法结构 11.20-简单查询 11.21-连接查询 11.22-聚集函数 11.23-授权语句
    嵌入式分享合集91
    ubuntu 20.04 passwd 指令不能使用
    puppeteer在mac和linux上表现不一致的问题记录
    Axure RP 9 for Mac(原型设计软件)中文正式版
    Apache Doris 系列: 基础篇-Routine Load
    行人检测综述 之 精华提取——图表与挑战
    【C++】类和对象(上)
    使用docker搭建webtop
  • 原文地址:https://blog.csdn.net/weixin_41012767/article/details/126758244