当前位置:   article > 正文

基于 Paddle 和 YOLOv5 实现 猫狗检测_yolov5 paddle

yolov5 paddle

1. YOLOv5训练数据集准备

解压数据集并移动到工作目录

In [1]

  1. #解压数据集
  2. !ls /home/aistudio/data
  3. !unzip -q /home/aistudio/data/data249593/datasets.zip
  4. !mv datasets VOCdata
  5. # 将数据集移动到工作区
  6. !mv /home/aistudio/VOCdata /home/aistudio/work/
  7. %cd /home/aistudio/work/VOCdata/
  8. # 重命名图片文件夹
  9. !mv JPEGImages images
data249593
/home/aistudio/work/VOCdata

In [ ]

!unzip test.zip
执行下面两段代码来将VOC格式数据集转换成YOLOV5标准格式

In [2]

  1. %cd /home/aistudio/work/VOCdata/
  2. import os
  3. import random
  4. trainval_percent = 0.1
  5. train_percent = 0.9
  6. xmlfilepath = '/home/aistudio/work/VOCdata/Annotations' #xml文件存放地址
  7. if not os.path.exists('ImageSets/'):
  8. os.makedirs('ImageSets/')
  9. total_xml = os.listdir(xmlfilepath)
  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('ImageSets/trainval.txt', 'w')
  17. ftest = open('ImageSets/test.txt', 'w')
  18. ftrain = open('ImageSets/train.txt', 'w')
  19. fval = open('ImageSets/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. ftest.write(name)
  26. else:
  27. fval.write(name)
  28. else:
  29. ftrain.write(name)
  30. ftrainval.close()
  31. ftrain.close()
  32. fval.close()
  33. ftest.close()
/home/aistudio/work/VOCdata

In [3]

  1. %cd /home/aistudio/work/VOCdata/
  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. sets = ['train', 'test','val']
  8. Imgpath = '/home/aistudio/work/VOCdata/images' #图片文件夹
  9. xmlfilepath = '/home/aistudio/work/VOCdata/Annotations/' #xml文件存放地址
  10. ImageSets_path='ImageSets/'
  11. classes = ['cat','dog'] # 识别目标种类
  12. def convert(size, box):
  13. dw = 1. / size[0]
  14. dh = 1. / size[1]
  15. x = (box[0] + box[1]) / 2.0
  16. y = (box[2] + box[3]) / 2.0
  17. w = box[1] - box[0]
  18. h = box[3] - box[2]
  19. x = x * dw
  20. w = w * dw
  21. y = y * dh
  22. h = h * dh
  23. return (x, y, w, h)
  24. def convert_annotation(image_id):
  25. in_file = open(xmlfilepath+'%s.xml' % (image_id))
  26. out_file = open('labels/%s.txt' % (image_id), 'w')
  27. tree = ET.parse(in_file)
  28. root = tree.getroot()
  29. size = root.find('size')
  30. w = int(size.find('width').text)
  31. h = int(size.find('height').text)
  32. for obj in root.iter('object'):
  33. difficult = obj.find('difficult').text
  34. cls &#
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号