当前位置:网站首页>【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
边栏推荐
- 第五期个人能力认证考核通过名单公布
- [matlab] Simulink the input and output variables of the same module cannot have the same name
- Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
- Precautions for binding shortcut keys of QPushButton
- 涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
- Leetcode topic [array] - 119 Yang Hui triangle II
- Solve DoS attack production cases
- R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
- LeetCode-1279. 红绿灯路口
- If you have any problems, you can contact me. A rookie ~
猜你喜欢
JDBC详解
Implementation of AVL tree
Computer network: sorting out common network interview questions (I)
ACTF 2022圆满落幕,0ops战队二连冠!!
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
How to type multiple spaces when editing CSDN articles
业务与应用同步发展:应用现代化的策略建议
AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】
AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
随机推荐
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
The second day of rhcsa study
Qlabel marquee text display
Pytorch common loss function
打家劫舍III[后序遍历与回溯+动态规划]
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
中缀表达式转后缀表达式详细思路及代码实现
Interface test tool - postman
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
[matlab] Simulink the input and output variables of the same module cannot have the same name
R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
The list of people who passed the fifth phase of personal ability certification assessment was published
ROS custom message publishing subscription example
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
受益匪浅,安卓面试问题
Certains marchés de l'emploi de Shanghai refusent d'embaucher des personnes qui se rétablissent positives à Xinguan
helm部署etcd集群
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up