当前位置:网站首页>【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 uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
- 同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
- Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
- How word displays modification traces
- Method of accessing mobile phone storage location permission under non root condition
- How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
- Qlabel marquee text display
- short i =1; i=i+1与short i=1; i+=1的区别
- English topic assignment (25)
- Characteristic colleges and universities, jointly build Netease Industrial College
猜你喜欢
CCNP Part 11 BGP (III) (essence)
Analysis of frequent chain breaks in applications using Druid connection pools
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
RT-Thread 组件 FinSH 使用时遇到的问题
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿
Don't miss this underestimated movie because of controversy!
LeetCode-1279. 红绿灯路口
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
随机推荐
Some recruitment markets in Shanghai refuse to recruit patients with covid-19 positive
业务与应用同步发展:应用现代化的策略建议
PMP practice once a day | don't get lost in the exam -7.6
Test technology stack arrangement -- self cultivation of test development engineers
When visual studio code starts, it prompts "the code installation seems to be corrupt. Please reinstall." Solution to displaying "unsupported" information in the title bar
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
[translation] a GPU approach to particle physics
ROS custom message publishing subscription example
打家劫舍III[后序遍历与回溯+动态规划]
Use map function and split function to type multiple elements in one line
【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
驼峰式与下划线命名规则(Camel case With hungarian notation)
test about BinaryTree
LeetCode-1279. 红绿灯路口
R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
How to improve website weight
Digital "new" operation and maintenance of energy industry
安装Mysql报错:Could not create or access the registry key needed for the...
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
MATLAB中deg2rad和rad2deg函数的使用