当前位置:网站首页>[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
边栏推荐
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
- [depth first search] Ji suanke: a joke of replacement
- map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
- PMP practice once a day | don't get lost in the exam -7.6
- Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
- tensorflow和torch代码验证cuda是否安装成功
- 应用使用Druid连接池经常性断链问题分析
- 中缀表达式转后缀表达式详细思路及代码实现
- ACTF 2022圆满落幕,0ops战队二连冠!!
- R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
猜你喜欢
Php+redis realizes the function of canceling orders over time
Oracle advanced (IV) table connection explanation
能源行业的数字化“新”运维
PMP每日一练 | 考试不迷路-7.6
关于静态类型、动态类型、id、instancetype
Based on butterfly species recognition
Helm deploy etcd cluster
业务与应用同步发展:应用现代化的策略建议
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
随机推荐
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
If you have any problems, you can contact me. A rookie ~
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
Cereals Mall - Distributed Advanced p129~p339 (end)
通俗的讲解,带你入门协程
Interview assault 63: how to remove duplication in MySQL?
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
Detailed idea and code implementation of infix expression to suffix expression
2022.2.12
It's super detailed in history. It's too late for you to read this information if you want to find a job
[paper notes] transunet: transformers make strongencoders for medical image segmentation
tensorflow和torch代码验证cuda是否安装成功
Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]
C#/VB. Net to add text / image watermarks to PDF documents
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
Looting iii[post sequence traversal and backtracking + dynamic planning]
Tensorflow and torch code verify whether CUDA is successfully installed
Swagger2 reports an error illegal DefaultValue null for parameter type integer
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
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services