当前位置:   article > 正文

【矩池云】YOLOv3~YOLOv5训练红外小目标数据集

红外小目标数据集

一、数据集准备

数据集下载地址:https://github.com/YimianDai/sirst

1. 需要将数据集转换为YOLO所需要的txt格式

参考链接:https://github.com/pprp/voc2007_for_yolo_torch

1.1 检测图片及其xml文件

  1. import os, shutil
  2. def checkPngXml(dir1, dir2, dir3, is_move=True):
  3. """
  4. dir1 是图片所在文件夹
  5. dir2 是标注文件所在文件夹
  6. dir3 是创建的,如果图片没有对应的xml文件,那就将图片放入dir3
  7. is_move 是确认是否进行移动,否则只进行打印
  8. """
  9. if not os.path.exists(dir3):
  10. os.mkdir(dir3)
  11. cnt = 0
  12. for file in os.listdir(dir1):
  13. f_name,f_ext = file.split(".")
  14. if not os.path.exists(os.path.join(dir2, f_name+".xml")):
  15. print(f_name)
  16. if is_move:
  17. cnt += 1
  18. shutil.move(os.path.join(dir1,file), os.path.join(dir3, file))
  19. if cnt > 0:
  20. print("有%d个文件不符合要求,已打印。"%(cnt))
  21. else:
  22. print("所有图片和对应的xml文件都是一一对应的。")
  23. if __name__ == "__main__":
  24. dir1 = r"dataset/images/images" # 修改为自己的图片路径
  25. dir2 = r"dataset/masks/masks" # 修改为自己的图片路径
  26. dir3 = r"dataset/Allempty" # 修改为自己的图片路径
  27. checkPngXml(dir1, dir2, dir3, False)

1.2 划分训练集

  1. import os
  2. import random
  3. import os, fnmatch
  4. trainval_percent = 0.8
  5. train_percent = 0.8
  6. xmlfilepath = r"dataset/masks/masks"
  7. txtsavepath = r"dataset"
  8. total_xml = fnmatch.filter(os.listdir(xmlfilepath), '*.xml')
  9. print(total_xml)
  10. num=len(total_xml)
  11. list=range(num)
  12. tv=int(num*trainval_percent)
  13. tr=int(tv*train_percent)
  14. trainval= random.sample(list,tv)
  15. train=random.sample(trainval,tr)
  16. ftrainval = open('dataset/trainval.txt', 'w')
  17. ftest = open('dataset/test.txt', 'w')
  18. ftrain = open('dataset/train.txt', 'w')
  19. fval = open('dataset/val.txt', 'w')
  20. for i in list:
  21. name=total_xml[i][:-4]+'\n'
  22. if i in trainval:
  23. ftrainval.write(name)
  24. if i in train:
  25. ftrain.write(name)
  26. else:
  27. fval.write(name)
  28. else:
  29. ftest.write(name)
  30. ftrainval.close()
  31. ftrain.close()
  32. fval.close()
  33. ftest.close()

1.3 转为txt标签

  1. # -*- coding: utf-8 -*-
  2. """
  3. 需要修改的地方:
  4. 1. sets中替换为自己的数据集
  5. 2. classes中替换为自己的类别
  6. 3. 将本文件放到VOC2007目录下
  7. 4. 直接开始运行
  8. """
  9. import xml.etree.ElementTree as ET
  10. import pickle
  11. import os
  12. from os import listdir, getcwd
  13. from os.path import join
  14. sets=[('2007', 'train'), ('2007', 'val'), ('2007', 'test')] #替换为自己的数据集
  15. classes = ["Target"] #修改为自己的类别
  16. def convert(size, box):
  17. dw = 1./(size[0])
  18. dh = 1./(size[1])
  19. x = (box[0] + box[1])/2.0
  20. y = (box[2] + box[3])/2.0
  21. w = box[1] - box[0]
  22. h = box[3] - box[2]
  23. x = x*dw
  24. w = w*dw
  25. y = y*dh
  26. h = h*dh
  27. return (x,y,w,h)
  28. def convert_annotation(year, image_id):
  29. in_file = open('dataset/masks/masks/%s.xml'%(image_id)) #将数据集放于当前目录下
  30. out_file = open('dataset/labels/%s.txt'%(image_id), 'w')
  31. tree=ET.parse(in_file)
  32. root = tree.getroot()
  33. size = root.find('size')
  34. w = int(size.find('width').text)
  35. h = int(size.find('height').text)
  36. print(w,h)
  37. for obj in root.iter('object'):
  38. difficult = obj.find('difficult').text
  39. cls = obj.find('name').text
  40. if cls not in classes or int(difficult)==1:
  41. continue
  42. cls_id = classes.index(cls)
  43. print(cls_id)
  44. xmlbox = obj.find('bndbox')
  45. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
  46. bb = convert((w,h), b)
  47. print(bb)
  48. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
  49. wd = getcwd()
  50. for year, image_set in sets:
  51. if not os.path.exists('dataset/labels/'):
  52. os.makedirs('dataset/labels/')
  53. image_ids = open('dataset/%s.txt'%(image_set)).read().strip().split()
  54. list_file = open('%s_%s.txt'%(year, image_set), 'w')
  55. for image_id in image_ids:
  56. list_file.write('dataset/images/images/%s.png\n'%(image_id))
  57. convert_annotation(year, image_id)
  58. list_file.close()
  59. # os.system("cat 2007_train.txt 2007_val.txt > train.txt") #修改为自己的数据集用作训练

1.4 构造数据集

  1. import os, shutil
  2. """
  3. 需要满足以下条件:
  4. 1. 在JPEGImages中准备好图片
  5. 2. 在labels中准备好labels
  6. 3. 创建好如下所示的文件目录:
  7. - images
  8. - train2014
  9. - val2014
  10. - labels(由于voc格式中有labels文件夹,所以重命名为label)
  11. - train2014
  12. - val2014
  13. """
  14. def make_for_torch_yolov3(dir_image,
  15. dir_label,
  16. dir1_train,
  17. dir1_val,
  18. dir2_train,
  19. dir2_val,
  20. main_trainval,
  21. main_test):
  22. if not os.path.exists(dir1_train):
  23. os.mkdir(dir1_train)
  24. if not os.path.exists(dir1_val):
  25. os.mkdir(dir1_val)
  26. if not os.path.exists(dir2_train):
  27. os.mkdir(dir2_train)
  28. if not os.path.exists(dir2_val):
  29. os.mkdir(dir2_val)
  30. with open(main_trainval, "r") as f1:
  31. for line in f1:
  32. print(line[:-1])
  33. # print(os.path.join(dir_image, line[:-1]+".png"), os.path.join(dir1_train, line[:-1]+".png"))
  34. shutil.copy(os.path.join(dir_image, line[:-1]+".png"),
  35. os.path.join(dir1_train, line[:-1]+".png"))
  36. shutil.copy(os.path.join(dir_label, line[:-1]+".txt"),
  37. os.path.join(dir2_train, line[:-1]+".txt"))
  38. with open(main_test, "r") as f2:
  39. for line in f2:
  40. print(line[:-1])
  41. shutil.copy(os.path.join(dir_image, line[:-1]+".png"),
  42. os.path.join(dir1_val, line[:-1]+".png"))
  43. shutil.copy(os.path.join(dir_label, line[:-1]+".txt"),
  44. os.path.join(dir2_val, line[:-1]+".txt"))
  45. if __name__ == "__main__":
  46. '''
  47. https://github.com/ultralytics/yolov3
  48. 这个pytorch版本的数据集组织
  49. - images
  50. - train2014 # dir1_train
  51. - val2014 # dir1_val
  52. - labels
  53. - train2014 # dir2_train
  54. - val2014 # dir2_val
  55. trainval.txt, test.txt 是由create_main.py构建的
  56. '''
  57. dir_image = r"dataset/images/images"
  58. dir_label = r"dataset/labels"
  59. dir1_train = r"dataset/image/train2014"
  60. dir1_val = r"dataset/image/val2014"
  61. dir2_train = r"dataset/label/train2014"
  62. dir2_val = r"dataset/label/val2014"
  63. main_trainval = r"dataset/trainval.txt"
  64. main_test = r"dataset/test.txt"
  65. make_for_torch_yolov3(dir_image,
  66. dir_label,
  67. dir1_train,
  68. dir1_val,
  69. dir2_train,
  70. dir2_val,
  71. main_trainval,
  72. main_test)

最终数据集格式如下:

037b886f0f004da1b6e0ed1ae8cd45c6.png

 2. 构造训练所需要的数据集

根据以上数据集 需要单独构建一个datasets文件夹,存放标签和图像,具体格式如下:

36edb51c6b4c4003ac8723e253c6b8dd.png

可以参考该链接:https://github.com/ultralytics/yolov5/issues/7389

3. 构建数据配置文档,需要注意 YOLOv5目录需要和datasets目录同级。

命名为hongwai.yaml

  1. # YOLOv3
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/361349
    推荐阅读
    相关标签