python-3.x - yolov5 中的非标准化标签

标签 python-3.x opencv normalization yolov5

我在我的自定义数据集上训练 yolov5,但出现非标准化标签错误。注释有 x,y 和 w,h,这意味着边界框存在于 (x,y) 到 (x+w,y+h) 之间。我正在使用 cv2 矩形函数在图像上显示边界框,它正在创建完美的边界框。我知道我必须将我的原始标签转换为标准化的中心 x、中心 y、宽度和高度值。我在下面这样做:

x2=x+w # x,y, w and h are given
y2=y1+h

xc=x+w/2
yc=y+h/2
xc=xc/width # normalize from 0-1. Width and height are image's width and height
yc=yc/height
  
wn=w/width # normalize the width from 0-1
hn=h/height
 
label_file.write(f"{category_idx} {xc} {yc} {wn} {hn}\n")

但是当我在文本文件中写入这些标签并运行 yolov5 训练时,它给出了以下断言错误:

assert (l[:, 1:] <= 1).all(), 'non-normalized or out of bounds coordinate labels: %s' % file # throws assertion error
AssertionError: non-normalized or out of bounds coordinate labels: /Raja/Desktop/yolov5/data/roi/labels/train/10.txt

10.txt 文件如下:

1 0.7504960317460317 0.3599537037037037 0.16765873015873023 0.059193121693121686
4 0.21664186507936506 0.3316798941798942 0.19122023809523808 0.0443121693121693
5 0.47879464285714285 0.2931547619047619 0.32663690476190477 0.04728835978835977
0 0.265625 0.47701719576719576 0.3045634920634921 0.0889550264550264
1 0.17671130952380953 0.5830026455026455 0.13120039682539683 0.07275132275132279
2 0.5212053571428572 0.7986111111111112 0.15550595238095244 0.07407407407407407
2 0.7638888888888888 0.8009259259259259 0.16121031746031755 0.07275132275132279

我正在使用 cv2 矩形函数在图像上显示边界框,它正在创建完美的边界框,如下图所示:

cv2.rectangle(temp_img,(int(x), int(y)),(int(x+w), int(y+h)),color=(0, 255, 0),thickness=2)

bounding boxes created properly on the image

我尝试从 GitHub 上提出的 this 问题等在线查找解决方案,但尚未找到任何解决方案。 谁能告诉我我在这里做错了什么?我认为问题存在于将原始标签转换为 0-1 规范化标签时,因为断言表明它已找到非规范化标签。任何帮助将不胜感激!

最佳答案

YOLOv5 要求数据集为暗网格式。这是它的外观概述:

  • 每张图片一个带有标签文件的 txt
  • 每个对象一行
  • 每一行都是类 x_center y_center width height 格式。
  • 框坐标必须采用规范化的 xywh 格式(从 0 - 1)。如果您的框以像素为单位,请将 x_centerwidth 除以图像宽度,将 y_centerheight 除以图像高度.
  • 类号是从零开始索引的(从 0 开始)。

示例:

  • 图像属性:宽度=1156 像素,高度=1144 像素。
  • 边界框属性:xmin=1032、ymin=20、xmax=1122、ymax=54、object_name="Ring"。
  • 让objects_list="手链","耳环","戒指","项链"

YOLOv5 格式: f"{category_idx} {x1 + bbox_width/2} {y1 + bbox_height/2} {bbox_width} {bbox_height}\n"

  • $bbox_{width} = x_{max}/width - x_{min}/width = (1122-1032)/1156 = 0.07785467128027679$
  • $bbox_{height} = y_{max}/height - y_{min}/height = (54-20)/1144 = 0.029720279720279717$
  • $x_{center}=x_{min}/width+bbox_{width}/2 = 0.9316608996539792$
  • $y_{center}=y_{min}/height + bbox_{height}/2 = 0.032342657342657344$
  • category_idx=2
  • 最终结果:2 0.9316608996539792 0.032342657342657344 0.07785467128027679 0.029720279720279717

关于python-3.x - yolov5 中的非标准化标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71595111/

相关文章:

opencv - 连接字符的分割

sql - 您如何知道 SQL 数据库何时需要更多规范化?

python - 有没有办法使用 cv2.approxPolyDP 来近似开放曲线?

python - 在 NumPy 数组中存储大于 64 位的整数

python - 使用 Pandas 删除 Python 3.4.2 中的标题行

visual-studio-2008 - Visual Studio 2008 中包含文件 core 和 highgui 时出错

python - 在 0.074 秒内以代码 = 0 退出 - 输出窗口在 Visual Studio 中没有输出

matlab - 相机标定后,棋盘格是否需要保持不变?

database-design - 数据库设计::2 个参与者事件中的规范化::加入表或 2 列?

php - MySQL/PHP 数据库规范化