• yolov7训练数据集详细流程bike-car-person


    一、准备深度学习环境

    下载yolov7代码

    下载完成解压放在自己的主目录

    命名yolov7-4

    二、 准备自己的数据集

    1.进入主目录

    2.进入data目录下把你的xml文件夹命名为Annotations,把你的存放图片文件夹命名为images

    3.分别新建ImageSets、imagtest(里面存放测试图片)、labels(里面存放转换之后的yolo格式文件)

    三、 1.2.在data目录下新建split_train_val.py文件

    里面内容如下

    1. # coding:utf-8
    2. import os
    3. import random
    4. import argparse
    5. parser = argparse.ArgumentParser()
    6. #xml文件的地址,根据自己的数据进行修改 xml一般存放在Annotations下
    7. parser.add_argument('--xml_path', default='Annotations', type=str, help='input xml label path')
    8. #数据集的划分,地址选择自己数据下的ImageSets/Main
    9. parser.add_argument('--txt_path', default='ImageSets/Main', type=str, help='output txt label path')
    10. opt = parser.parse_args()
    11. trainval_percent = 1.0
    12. train_percent = 0.9
    13. xmlfilepath = opt.xml_path
    14. txtsavepath = opt.txt_path
    15. total_xml = os.listdir(xmlfilepath)
    16. if not os.path.exists(txtsavepath):
    17. os.makedirs(txtsavepath)
    18. num = len(total_xml)
    19. list_index = range(num)
    20. tv = int(num * trainval_percent)
    21. tr = int(tv * train_percent)
    22. trainval = random.sample(list_index, tv)
    23. train = random.sample(trainval, tr)
    24. file_trainval = open(txtsavepath + '/trainval.txt', 'w')
    25. file_test = open(txtsavepath + '/test.txt', 'w')
    26. file_train = open(txtsavepath + '/train.txt', 'w')
    27. file_val = open(txtsavepath + '/val.txt', 'w')
    28. for i in list_index:
    29. name = total_xml[i][:-4] + '\n'
    30. if i in trainval:
    31. file_trainval.write(name)
    32. if i in train:
    33. file_train.write(name)
    34. else:
    35. file_val.write(name)
    36. else:
    37. file_test.write(name)
    38. file_trainval.close()
    39. file_train.close()
    40. file_val.close()
    41. file_test.close()

    运行之后会在ImageSets/Main下生成四个.txt文件

    2.在data目录下新建voc_label.py文件,里面存放代码,里面classes需要改成自己的类别

    1. # -*- coding: utf-8 -*-
    2. import xml.etree.ElementTree as ET
    3. import os
    4. from os import getcwd
    5. sets = ['train', 'val', 'test']
    6. classes = ['bike','carsgraz','person'] # 改成自己的类别
    7. abs_path = os.getcwd()
    8. print(abs_path)
    9. def convert(size, box):
    10. dw = 1. / (size[0])
    11. dh = 1. / (size[1])
    12. x = (box[0] + box[1]) / 2.0 - 1
    13. y = (box[2] + box[3]) / 2.0 - 1
    14. w = box[1] - box[0]
    15. h = box[3] - box[2]
    16. x = x * dw
    17. w = w * dw
    18. y = y * dh
    19. h = h * dh
    20. return x, y, w, h
    21. def convert_annotation(image_id):
    22. in_file = open('./Annotations/%s.xml' % (image_id), encoding='UTF-8')
    23. out_file = open('./labels/%s.txt' % (image_id), 'w')
    24. tree = ET.parse(in_file)
    25. root = tree.getroot()
    26. size = root.find('size')
    27. w = int(size.find('width').text)
    28. h = int(size.find('height').text)
    29. for obj in root.iter('object'):
    30. difficult = obj.find('difficult').text
    31. cls = obj.find('name').text
    32. if cls not in classes or int(difficult) == 1:
    33. continue
    34. cls_id = classes.index(cls)
    35. xmlbox = obj.find('bndbox')
    36. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
    37. float(xmlbox.find('ymax').text))
    38. b1, b2, b3, b4 = b
    39. # 标注越界修正
    40. if b2 > w:
    41. b2 = w
    42. if b4 > h:
    43. b4 = h
    44. b = (b1, b2, b3, b4)
    45. bb = convert((w, h), b)
    46. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
    47. wd = getcwd()
    48. for image_set in sets:
    49. if not os.path.exists('./labels/'):
    50. os.makedirs('./labels/')
    51. image_ids = open('./ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
    52. list_file = open('./%s.txt' % (image_set), 'w')
    53. for image_id in image_ids:
    54. list_file.write(abs_path + '/images/%s.png\n' % (image_id)) # 注意你的图片格式,如果是.jpg记得修改
    55. convert_annotation(image_id)
    56. list_file.close()

    3.拷贝一份coco.yaml文件里面改成自己的类别和data目录下三个txt文件路径

    代码如下

    1. # COCO 2017 dataset http://cocodataset.org
    2. # download command/URL (optional)
    3. # download: bash ./scripts/get_coco.sh
    4. # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
    5. train: /home/sxj/yolov7-4/data/train.txt # 118287 images
    6. val: /home/sxj/yolov7-4/data/val.txt # 5000 images
    7. test: /home/sxj/yolov7-4/data/test.txt # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794
    8. # number of classes
    9. nc: 3
    10. # class names
    11. names: ['bike','carsgraz','person']

    4.修改cfg目录下/home/sxj/yolov7-4/cfg/deploy/yolov7.yaml,yolov7.yaml文件里面改成自己类别数

    四、返回yolov7主目录修改train.py文件

    其中 --weights', type=str, default='yolov7.pt', help='initial weights path'改成yolov7.pt文件路径

    '--cfg', type=str, default='/home/sxj/yolov7-4/cfg/deploy/yolov7.yaml', help='model.yaml path')改成yolov7.yaml路径

    '--data', type=str, default='data/car.yaml', help='data.yaml path'把data目录下的coco.yaml文件改成自己的路径

    里面'--epochs', type=int, default=50

    '--batch-size', type=int, default=1, help='total batch size for all GPUs'

    '--device', default='0', help='cuda device, i.e. 0 or 0,1,2,3 or cpu or mps'

    参数根据需要调整

    五、完成之后运行python train.py

    出现如下报错及解决方法:YOLO7报错:indices should be either on cpu or on the same device as the indexed tensor (cpu)

    YOLO7报错:indices should be either on cpu or on the same device as the indexed tensor (cpu)

    运行之后在runs里面找到best.pt权重文件

    拷贝一份放在主目录下,打开detect.py改成自己best.pt权重文件和测试图片路径

    在运行

    python detect.py

    在 runs/detect/exp下可查看自己模型文件测试效果即可

    到此全部完成

  • 相关阅读:
    在RVIZ中显示深度数据
    RabbitMQ 如何保证消息不丢失
    6、Linux:一起玩转vi/vim编辑命令
    2022最新版-李宏毅机器学习深度学习课程-P50 BERT的预训练和微调
    【UniApp】-uni-app-传递数据
    Python-自动化测试面试
    kafka在linux上集群部署说明
    线性回归实现原理
    固定资产管理的得力助手——易点易动固定资产管理系统的强大功能
    【攻破css系列——附加篇】vscode自动格式化
  • 原文地址:https://blog.csdn.net/m0_60657960/article/details/134539663