Search code examples
pythonpandascsvannotationsyolo

Converting CSV dataset to yolo format


I was trying to train a dataset in yolov4 but I had some errors coming up while training about my annotations being in the wrong format.

The dataset had its annotations in a CSV with the format

(x_min, x_max, y_min, y_max)

I checked the properties of the image and the size of each image was 1280x720 so I made two more columns with width and height.

         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 

My code to change to yolo format is:

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'])

I am not sure if my math above is correct but I would get the results and put them into .txt seperately with the results shown for example the first img_id in the table:

0 0.377734375 0.09166666666666666 0.07109375 0.18333333333333332

This is in the format yolov4 states which is

<object_class> <x_center> <y_center> <width> <height> 

But when training I get errors for a lot of images and annotation files for example:

data/obj/da5d62ac-db28-11ea-95b0-8fa5e97cd019.txt Wrong annotation: x = 0 or y = 0

This is what is contained inside that text file

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

The CSV data for that img id is below

                       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.


Solution

  • 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