Reputation: 1480
I am training yolov5 on my custom dataset and am getting the non-normalized labels' error. The annotations have x,y, and w,h which means that the bounding box is present from (x,y) to (x+w,y+h). I am using the cv2 rectangle function to display the bounding boxes on the image and it is creating the perfect bounding boxes. I understand that I have to convert my raw labels to normalized center x, center y, width, and height values. I am doing that below:
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")
But when I write these labels in the text file and run the yolov5 training, it gives the following assertion error:
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
The 10.txt file is given below:
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
I am using the cv2 rectangle function to display the bounding boxes on the image and it is creating the perfect bounding boxes as displayed in the picture below:
cv2.rectangle(temp_img,(int(x), int(y)),(int(x+w), int(y+h)),color=(0, 255, 0),thickness=2)
I have tried to find the solution online like from this issue raised on GitHub but haven't found anything yet. Can anyone please tell me what am I doing wrong here? I believe that the issue exists in converting raw labels to 0-1 normalized labels as the assertion states that it has found non-normalized labels. Any help will be highly appreciated!
Upvotes: 2
Views: 2850
Reputation: 321
x_center
and width
by image width, and y_center
and height
by image height.Example:
YOLOv5 format: f"{category_idx} {x1 + bbox_width / 2} {y1 + bbox_height / 2} {bbox_width} {bbox_height}\n"
Upvotes: 6