当前位置:网站首页>【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
边栏推荐
- R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the palette parameter, and set the colors of data points and box graphs of dot plots at differ
- 渲大师携手向日葵,远控赋能云渲染及GPU算力服务
- 驼峰式与下划线命名规则(Camel case With hungarian notation)
- pytorch常见损失函数
- 五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
- Human bone point detection: top-down (part of the theory)
- Qlabel marquee text display
- Pytorch common loss function
- Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
- test about BinaryTree
猜你喜欢

Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million

CCNP Part 11 BGP (III) (essence)

About static type, dynamic type, ID, instancetype

How to improve website weight

Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion

谷粒商城--分布式高级篇P129~P339(完结)

数学知识——高斯消元(初等行变换解方程组)代码实现

Interface test tool - postman
三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理

助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
随机推荐
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
Wx applet learning notes day01
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
Precautions for binding shortcut keys of QPushButton
Binary search tree
PMP每日一练 | 考试不迷路-7.6
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
Optical blood pressure estimation based on PPG and FFT neural network [translation]
中缀表达式转后缀表达式详细思路及代码实现
Abstract classes and abstract methods
Detailed idea and code implementation of infix expression to suffix expression
CCNP Part 11 BGP (III) (essence)
In 50W, what have I done right?
How to type multiple spaces when editing CSDN articles
Druid 数据库连接池 详解
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip
test about BinaryTree
Test technology stack arrangement -- self cultivation of test development engineers
Qlabel marquee text display