首先是pascal voc标签数据格式转yolo标签数据格式,这两种有什么区别,欢迎翻看之前的博客 PASCAL VOC格式的标注格式和YOLO格式的txt标记文件

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join# 要生成的文件夹
sets = [('2012', 'train'), ('2012', 'val')]# 类别
classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus","car", "cat", "chair", "cow", "diningtable", "dog", "horse","motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]def convert(size, box):dw = 1. / size[0]dh = 1. / size[1]x = (box[0] + box[1]) / 2.0y = (box[2] + box[3]) / 2.0w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)def convert_annotation(year, image_id,image_set):# 读取Annntation文件中的xml文件in_file = open('F:/data/VOCdevkit/VOC%s/Annotations/%s.xml' % (year, image_id))out_file = open('VOC2012/labels/%s/%s.txt' % (image_set,image_id ), 'w')tree = ET.parse(in_file)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)for obj in root.iter('object'):difficult = obj.find('difficult').textcls = obj.find('name').textif cls not in classes or int(difficult) == 1:continuecls_id = classes.index(cls)xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text),float(xmlbox.find('xmax').text),float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text))bb = convert((w, h), b)out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')# wd = getcwd()# 遍历sets = [('2012', 'train'), ('2012', 'val')]
for year, image_set in sets:# 在当前路径创建文件if not os.path.exists('VOC2012/labels/''%s' % (image_set)):os.makedirs('VOC2012/labels/%s' % (image_set))image_ids = open('F:/data/VOCdevkit/VOC%s/ImageSets/Main/%s.txt'% (year, image_set)).read().strip().split()if not os.path.exists('VOC2012/images/%s' % (image_set)):os.makedirs('VOC2012/images/%s' % (image_set))list_file = open('VOC2012/images/%s.txt' % (image_set), 'w')for image_id in image_ids:# 在images中创建一个txt写入每一个图片数据的绝对路径list_file.write('F:/data/VOCdevkit/VOC2012/JPEGImages/%s.jpg\n' % (image_id))convert_annotation(year, image_id,image_set)list_file.close()

VOC 数据集文件夹

建立一个文件夹VOC2012

其中train.txt存放的是图片的绝对路径

labels中存放的是每个图片的标签信息,yolo格式

创建一个yaml文件voc2012.yaml

看似是把一切弄好了是吧,结果疯狂报错,找不到label信息?WTF
好的,debug看看吧,最后找到了原因:
好吧,发现之前训练coco的时候就注意到这个函数过,然而一点印象没有,这个函数要做什么事情,如果你的images中的train和val亦或者还有test,你在里面存放的是图片,那向上面那么做,train一点事情没有,然而如果你的images中放的是train.txt和val.text那么,这个函数会把路径中的’JPEGImages’ 转换成’labels’那么我们必须去绝对路径中的那个文件夹中放置一个labels的文件夹,程序是实时读取这个文件夹中的txt文件

注意这里有我创建的一个labels文件夹,注意这里不区分为文件夹,将所有图片的label的标注信息txt文件都放在文件夹中即可

所以不需要在当前项目目录中创建labels文件夹了,照样可以train,当然可以不用这个函数,直接读取我们想要读取的地址应该也可以,但是这里就不做更改了,如果有人做了,并且有读者朋友看到了,麻烦评论区指路一下, 我去观摩一下,自己太懒不想改了,但是这样就比较麻烦的是如果用服务器读取的话,还是相对路径比较简单的,这里还是更改一下比较好,个人认为,以上就是全部内容。


补:使用服务器训练yolov5

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import joinsets = ['train', 'val']classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus","car", "cat", "chair", "cow", "diningtable", "dog", "horse","motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]def convert(size, box):dw = 1. / size[0]dh = 1. / size[1]x = (box[0] + box[1]) / 2.0y = (box[2] + box[3]) / 2.0w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)def convert_annotation(image_id,image_set):in_file = open('//root/data/public/PASCAL-VOC2012/Annotations/%s.xml' % (image_id))out_file = open('//root/data/public/PASCAL-VOC2012/labels/%s.txt' % (image_id), 'w')tree = ET.parse(in_file)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)for obj in root.iter('object'):difficult = obj.find('difficult').textcls = obj.find('name').textif cls not in classes or int(difficult) == 1:continuecls_id = classes.index(cls)xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text),float(xmlbox.find('xmax').text),float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text))bb = convert((w, h), b)out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')# wd = getcwd()for image_set in sets:if not os.path.exists('//root/data/public/PASCAL-VOC2012/labels/'):os.makedirs('//root/data/public/PASCAL-VOC2012/labels/')image_ids = open('//root/data/public/PASCAL-VOC2012/%s.txt'% (image_set)).read().strip().split()if not os.path.exists('VOC2012/images/'):os.makedirs('VOC2012/images/')list_file = open('VOC2012/images/%s.txt' % (image_set), 'w')for image_id in image_ids:list_file.write('//root/data/public/PASCAL-VOC2012/JPEGImages/%s.jpg\n' % (image_id))convert_annotation(image_id,image_set)list_file.close()

根据所用的gpu显存来决定batch size大小:
BATCH_SIZE设置越大,那么所有训练样本数据完成一次训练(完成一个epoch)要进行的参数更新次数会更少,那么训练耗时更短,BATCH_SIZE设置越小,一个epoch训练完所有样本数据要进行的参数更新次数会更多,因此训练耗时更长。

使用yolov5训练PASCAL VOC2012数据集以及遇到的坑相关推荐

  1. PspNet在MMsegmentation框架下成功训练Pascal VOC2012数据集及踩坑实录

    时间:2021/08/29/05:45:28 一晚上了,难以置信,我竟然还不困,还有着整理博客的冲动.也就在这周,熬夜能力突然就在几个晚上的不归宿中突破了,一夜过后天壤之别,一晚比一晚能熬,之前还会担 ...

  2. PASCAL VOC2012数据集

    PASCAL VOC2012数据集 下载的官方网址:http://host.robots.ox.ac.uk/pascal/voc/voc2012/index.html#devkit 一.介绍与下载 P ...

  3. Yolov5训练自己的数据集(windows10)

    Yolov5训练自己的数据集(windows10) 环境配置+训练数据集 一.Anaconda 的安装教程(图文) Anaconda下载 下载地址:https://www.anaconda.com/d ...

  4. Yolo实用指南(step by step)之三yolov5训练自己的数据集

    关于yolov5训练自己的数据集的文章网上已经有一大把,自己之前也摸索了数据标注有一阵子,可一直迟迟未动手,感觉对原理不熟悉,很多东西浮光掠影有些肤浅,思来想去还是花了整整一天实践一下吧,毕竟这是基础 ...

  5. yolov5训练自己的数据集

    How to Train YOLOv5 On a Custom Dataset   根据这篇文章下的数据集 YOLOv5训练自己的数据集   根据这篇文章,输入训练命令行 Hyperparameter ...

  6. 手把手教你使用YOLOV5训练自己的数据集并用TensorRT加速

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 前言 本文主要介绍目标检测YOLOV5算法来训练自己的数据集,并且使用TensorRT来对训练好的模型 ...

  7. Yolov5训练自己的数据集+TensorRT加速+Qt部署

    本人由于项目要求,需要利用Yolov5网络训练自己的目标检测与分类模型,并利用TensorRT加速将其部署到Qt界面上.目前已经实现了整个流程,写下这篇博客供需要的各位参考.(本文描述的重点主要是在后 ...

  8. YOLOv5训练自己的数据集实现视频的识别

    写在前面 我本来是使用这个模型进行手写签名的定位,但是因为比赛的主办方原因,数据不允许公开,所以我使用动物世界的一段开头视屏来制作我的数据集.这整个模型跑通的过程中,我参考了很多不错的博客,写这篇博客 ...

  9. yolov5训练自己的数据集,OpenCV DNN推理

    学更好的别人, 做更好的自己. --<微卡智享> 本文长度为4238字,预计阅读9分钟 前言 上一篇<OpenCV--自学笔记>搭建好了yolov5的环境,作为目标检测在应用中 ...

  10. Windows10+YOLOv5训练自己的数据集

    Windows10+YOLOv5训练自己的数据集 一.环境和配置 1.1 安装anaconda 1.2 在anaconda中安装pytorch虚拟环境 1.3 安装CUDA和cudnn 1.4 安装p ...

最新文章

  1. ZooKeeper 定位:能解决什么问题?不能解决什么问题?
  2. 鉴机识变,面向未来|RocketMQ Summit 2022 即将来袭
  3. esp32-wrover流水灯c语言程序,ESP-WROVER-KIT V4.1 入门指南
  4. easyui datagrid
  5. JDBC学习笔记02【ResultSet类详解、JDBC登录案例练习、PreparedStatement类详解】
  6. Java Web专题(一)
  7. bzoj2442codevs4654 单调队列优化dp
  8. QuerWrapper常用方法
  9. rssi室内定位算法原理_室内定位方案常用的4种定位算法
  10. python变量的赋值操作_Python中关于变量赋值操作的实例分享
  11. linux命令中选项分为,Linux 考试试题
  12. matlab循环取出矩阵的某一行并标示上A1 A2 A3
  13. 我的第一篇博客-缓存显示图片
  14. 凭实力蝉联第一!Flink 又双叒叕上榜啦
  15. chrome无法打开某些网页,但safari可以
  16. JAVA之day3对象
  17. python 实例化_python中如何实例化一个对象-问答-阿里云开发者社区-阿里云
  18. 多种群粒子群 MATLAB,多种群粒子群算法怎么编码啊,楼主小白求大神指导
  19. 433M超再生无线模块编码-解码
  20. 圆桌实录 | 为什么不约而同选择了大 Kernel

热门文章

  1. Mysql存储过程和函数区别介绍
  2. AC9刷梅林安装entware usb挂载 u盘分区使用swap脚本
  3. 关于连接PostgreSQL时提示 FATAL: password authentication failed for user 连接用户名 的解决方法...
  4. Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you‘re try
  5. 深入理解JVM专题目录
  6. 合并排序时间复杂度推导
  7. golang笔记14--go 语言爬虫实战项目介绍
  8. 通过抓包工具抓包APP就连不上网的解决方案
  9. 优化算法学习(LM算法)
  10. 供应链SCOR模型搭建/改进