yolov5 中的非标准化标签

Asi*_*sim 2 opencv normalization python-3.x 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")
Run Code Online (Sandbox Code Playgroud)

但是当我在文本文件中写入这些标签并运行 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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

cv2.rectangle(temp_img,(int(x), int(y)),(int(x+w), int(y+h)),color=(0, 255, 0),thickness=2)
Run Code Online (Sandbox Code Playgroud)

在图像上正确创建的边界框

我试图在网上找到解决方案,例如GitHub 上提出的这个问题,但还没有找到任何东西。谁能告诉我我在这里做错了什么?我认为将原始标签转换为 0-1 标准化标签时存在问题,因为断言指出它已找到非标准化标签。任何帮助将不胜感激!

小智 6

YOLOv5要求数据集为darknet格式。Here\xe2\x80\x99s 是它的概要:

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

例子:

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

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

\n
    \n
  • $bbox_{宽度} = x_{最大}/宽度 - x_{最小}/宽度 = (1122-1032)/1156 = 0.07785467128027679$
  • \n
  • $bbox_{高度} = y_{最大}/高度 - y_{最小}/高度 = (54-20)/1144 = 0.029720279720279717$
  • \n
  • $x_{中心}=x_{最小值}/宽度+bbox_{宽度}/2 = 0.9316608996539792$
  • \n
  • $y_{center}=y_{min}/高度 + bbox_{高度}/2 = 0.032342657342657344$
  • \n
  • 类别idx=2
  • \n
  • 最终结果:2 0.9316608996539792 0.032342657342657344 0.07785467128027679 0.029720279720279717
  • \n
\n