当前位置:网站首页>[pytorch] yolov5 train your own data set
[pytorch] yolov5 train your own data set
2022-07-06 19:20:00 【. Yun Zhe】
1.labelme Generate tags
2.labelme Label conversion yolo label
# -*- 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)) # Absolute path
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 Data file format
── 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
边栏推荐
- PMP每日一练 | 考试不迷路-7.6
- Computer network: sorting out common network interview questions (I)
- 快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
- The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
- Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
- Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
- ROS custom message publishing subscription example
- IC设计流程中需要使用到的文件
- Reptiles have a good time. Are you full? These three bottom lines must not be touched!
- 今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
猜你喜欢

LeetCode-1279. 红绿灯路口

Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers

Pytorch common loss function

About static type, dynamic type, ID, instancetype

Detailed idea and code implementation of infix expression to suffix expression
Interview assault 63: how to remove duplication in MySQL?
Benefit a lot, Android interview questions

Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview

Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry

Wx applet learning notes day01
随机推荐
openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
黑马--Redis篇
pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
QPushButton绑定快捷键的注意事项
IC设计流程中需要使用到的文件
三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理
First day of rhcsa study
如何提高网站权重
ModuleNotFoundError: No module named ‘PIL‘解决方法
QLabel 跑马灯文字显示
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
Modulenotfounderror: no module named 'PIL' solution
Reptiles have a good time. Are you full? These three bottom lines must not be touched!
Oracle advanced (IV) table connection explanation
R language ggplot2 visualization: use ggviolin function of ggpubr package to visualize violin diagram
Digital "new" operation and maintenance of energy industry
ROS custom message publishing subscription example
The list of people who passed the fifth phase of personal ability certification assessment was published
Detailed idea and code implementation of infix expression to suffix expression
helm部署etcd集群