当前位置:网站首页>【pytorch】yolov5 训练自己的数据集
【pytorch】yolov5 训练自己的数据集
2022-07-06 11:23:00 【.云哲.】
1.labelme生成标签
2.labelme标签转成yolo标签
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import shutil
classes = ["cellphone", "person"]
def convert(size, box):
dw = 1./(size[0])
dh = 1./(size[1])
x = (box[0] + box[1])/2.0 - 1
y = (box[2] + box[3])/2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
def convert_annotation(path, save, image_id):
in_file = open("{}/{}.xml".format(path, image_id), "r", encoding="utf-8")
out_file = open("{}/{}.txt".format(save, image_id), "w", encoding="utf-8")
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").text
cls = obj.find("name").text
if cls not in classes or int(difficult) == 1:
continue
cls_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')
img_path = os.path.join(path, "{}.jpg".format(image_id))
save_path = os.path.join(save, "{}.jpg".format(image_id))
shutil.copy(img_path, save_path)
wd = getcwd()
if __name__ == '__main__':
# get image_ids
path = "val_voc"
save = "val"
txt_yolo = "val.txt"
image_ids = []
for file in listdir(path):
if file.endswith(".xml"):
image_id = file.replace(".xml", "")
image_ids.append(image_id)
# generate jpg and txt labels
for image_id in image_ids:
convert_annotation(path, save, image_id)
# write yolo
list_file = open(txt_yolo, "w", encoding="utf-8")
for image_id in image_ids:
txt_path = os.path.join(save, "{}.txt".format(image_id))
img_path = os.path.join(wd, save, "{}.jpg".format(image_id)) # 绝对路径
lines = open(txt_path, "r", encoding="utf-8")
write_line = "{} {}\n".format(img_path, " ".join([line.strip() for line in lines]))
list_file.write(write_line)
list_file.close()
3.yolov5数据文件格式
── coco128
├── images
│ ├── train
│ └── val
└── labels
├── train
└── val
$ cat 00009.txt
45 0.479492 0.688771 0.955609 0.5955
50 0.637063 0.732938 0.494125 0.510583
边栏推荐
- Unlock 2 live broadcast themes in advance! Today, I will teach you how to complete software package integration Issues 29-30
- 业务与应用同步发展:应用现代化的策略建议
- 中缀表达式转后缀表达式详细思路及代码实现
- Pytorch common loss function
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
- CCNP Part 11 BGP (III) (essence)
- Multithreading Basics: basic concepts of threads and creation of threads
- Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
- Method of accessing mobile phone storage location permission under non root condition
- R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
猜你喜欢
黑马--Redis篇
Php+redis realizes the function of canceling orders over time
业务与应用同步发展:应用现代化的策略建议
How to improve website weight
How to type multiple spaces when editing CSDN articles
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
PMP practice once a day | don't get lost in the exam -7.6
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
[translation] a GPU approach to particle physics
Simple understanding of MySQL database
随机推荐
short i =1; i=i+1与short i=1; i+=1的区别
C#/VB. Net to add text / image watermarks to PDF documents
AcWing 3537.树查找 完全二叉树
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
Some recruitment markets in Shanghai refuse to recruit patients with covid-19 positive
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
[translation] a GPU approach to particle physics
谷粒商城--分布式高级篇P129~P339(完结)
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
Wx applet learning notes day01
Benefit a lot, Android interview questions
裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
Precautions for binding shortcut keys of QPushButton
基于蝴蝶种类识别
史上超级详细,想找工作的你还不看这份资料就晚了
LeetCode-1279. 红绿灯路口
Interview assault 63: how to remove duplication in MySQL?
中缀表达式转后缀表达式详细思路及代码实现