python - 将 CSV 数据集转换为 yolo 格式
问题描述
我试图在 yolov4 中训练一个数据集,但在训练我的注释格式错误时出现了一些错误。
数据集在 CSV 中有注释,格式为
(x_min, x_max, y_min, y_max)
我检查了图像的属性,每个图像的大小是 1280x720,所以我又制作了两列宽度和高度。
img_id x_min x_max y_min y_max
0 94a69b66-23f0-11e9-a78e-2f2b7983ac0d 438 529 0 132
1 94a6a3a4-23f0-11e9-a78f-ebd9c88ef3e8 433 529 0 131
2 94a6a430-23f0-11e9-a790-2b5f72f1667a 440 529 0 132
3 94a6a48a-23f0-11e9-a791-fb958b6ab6b3 452 550 0 154
4 94a6a4da-23f0-11e9-a792-f320b734bd9b 462 550 0 153
我更改为 yolo 格式的代码是:
convert_dict = {'x_min': float,
'x_max': float,
'y_min': float,
'y_max': float
}
df["width"] = 1280
df["height"] = 720
df = df.astype(convert_dict)
xcen = ((df.x_min + df.x_max)) / 2 / df['width']
ycen = ((df.y_min + df.y_max)) / 2 / df['height']
df['width'] = ((df.x_max - df.x_min)) / df['width']
df['height'] = ((df.y_max - df.y_min)) / df['height']
df['xcen'] = xcen
df['ycen'] = ycen
df = df.drop(columns=['x_min', 'x_max','y_min','y_max'])
我不确定我上面的数学是否正确,但我会得到结果并将它们单独放入 .txt 中,结果显示例如表中的第一个 img_id:
0 0.377734375 0.09166666666666666 0.07109375 0.18333333333333332
这是 yolov4 状态的格式,即
<object_class> <x_center> <y_center> <width> <height>
但是在训练时,我会遇到很多图像和注释文件的错误,例如:
data/obj/da5d62ac-db28-11ea-95b0-8fa5e97cd019.txt Wrong annotation: x = 0 or y = 0
这是该文本文件中包含的内容
0 0.256640625 1.0763888888888888 0.35859375 0.12222222222222222
2 0.560546875 0.6451388888888889 0.22578125 0.24305555555555555
2 0.6125 0.7430555555555556 0.2015625 0.18333333333333332
0 0.755859375 0.8152777777777778 0.33671875 0.6138888888888889
0 0.91640625 0.4423611111111111 0.1640625 0.44305555555555554
该 img id 的 CSV 数据如下
img_id x_min x_max y_min y_max label_l1 width height
219661 da5d62ac-db28-11ea-95b0-8fa5e97cd019 99 558 731 819 0 1280 720
219662 da5d62ac-db28-11ea-95b0-8fa5e97cd019 573 862 377 552 2 1280 720
219663 da5d62ac-db28-11ea-95b0-8fa5e97cd019 655 913 469 601 2 1280 720
219664 da5d62ac-db28-11ea-95b0-8fa5e97cd019 752 1183 366 808 0 1280 720
219665 da5d62ac-db28-11ea-95b0-8fa5e97cd019 1068 1278 159 478 0 1280 720
Is my code for converting to yolo format wrong? Or is this an issue with the images from the dataset or something to do with the path?
I will try to run this in the google collab to see if I get the same issue.
解决方案
I think you messed up calculating x and y:
YOLO usses x_center position and y_center position (normalised, <1), which is the centerof your bounding box. Plus the distance of the box along the x axes (w) and the y axes (h).
I think that with x being the mean at our code
(xcen = ((df.x_min + df.x_max)) / 2 / df['width'])
xcen+w can be higher than one and might give errors
and that what happens exactly in your first line of data
0 0.256640625 ***1.0763888888888888*** 0.35859375 0.12222222222222222
Can you try this :
x = ((xmin + xmax)/2)-1 / width
y = ((ymin+ ymax)/2)-1 / height
w = (xmax - xmin) / width
h = (ymax - ymin) / height
from here How can I convert form [xmin ymin xmax ymax] to [x y width height] normalized in image?
Let me know if it helps
推荐阅读
- c# - 大摇大摆地为通用控制器添加文档和操作
- php - 如何从数据库中获取某个部分?
- c# - Image.RotateFlip 似乎没有旋转位图
- wordpress - 如何在 Wordpress 中使用自定义随机头像?
- django - 如何在 django-rest-framework-social-oauth2 中使用 csrf (csrftoken) cookie 会话
- php - str_split() 有几个长度
- java - Spring Boot中同一实体的多种表示形式
- bash - 谷歌大查询命令行执行复杂查询给出错误意外'('
- spring - 制作与 Jboss Wildfly 兼容的 Spring 5 瘦身战
- ios - Xcode 中如何有两种类型的环境