首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 25-02-18 05:20
  • 2771
  • 5880
blog.csdn.net

一、准备深度学习环境

下载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下可查看自己模型文件测试效果即可

到此全部完成

注:本文转载自blog.csdn.net的山山而川_R的文章"https://blog.csdn.net/m0_60657960/article/details/134539663"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top