Machine-Learning-Collection icon indicating copy to clipboard operation
Machine-Learning-Collection copied to clipboard

ValueError: Expected y_max for bbox...

Open huansu opened this issue 3 years ago • 4 comments

I try to use Pascal Voc 2007 dataset to run Dataset.py. After I converted the bounding_box data to (0, 1] and rerun the Dataset.py, I got the ValueError like this: ValueError: Expected y_max for bbox (0.30930930930930933, 0.5045045045045045, 1.0, 1.1951951951951951, 14.0) to be in the range [0.0, 1.0], got 1.1951951951951951. by the way, I write this function to do the job:

def Convert(width, heigth, node_xmin, node_ymin, node_xmax, node_ymax):
    x = ((node_xmin + node_xmax)/2.0) * (1./width)
    y = ((node_ymin + node_ymax)/2.0) * (1./heigth)
    W = (node_xmax - node_xmin) * (1./width)
    H = (node_xmax - node_xmin) * (1./heigth)
    return x, y, W, H

I read the Issue at here when I try to solve this problem. But I still got the similar error. Occasionally Dataset.py could be able to display images, but the bounding_box looks out of position. I read the code at config.test_transforms, I guess the image size was changed by test_transforms, but bounding_box not. If my guess is right, how could I solve this problem?

huansu avatar Feb 22 '22 05:02 huansu

My guess is right, if you don't use the dataset which is provided by author, you need to change the labels(from xml to yolo x, y,w,h). Because the author have changed the size of the image by padding it in the code, but not the labels. I think I have solved this error by writing this function:

def Convert(width, heigth, xmin, ymin, xmax, ymax):
    if width >= heigth:
        coefficient = 416 / width
        dif  = int((416 - (heigth * coefficient)) / 2)
        width, heigth, xmin, ymin, xmax, ymax = (int(float(width) * coefficient), int(float(heigth) * coefficient),
                                                 int(float(xmin) * coefficient), int(float(ymin)) * coefficient,
                                                 int(float(xmax) * coefficient), int(float(ymax) * coefficient))
        x = ((xmin + xmax) / 2.0) * (1. / width)
        y = (((ymin+dif) + (ymax+dif)) / 2.0) * (1. / (heigth+2*dif))
        W = (xmax - xmin) * (1. / width)
        H = (ymax+dif - ymin+dif) * (1. / (heigth+2*dif))
    elif width < heigth:
        coefficient = 416 / heigth
        dif = (416 - (width * coefficient)) / 2
        width, heigth, xmin, ymin, xmax, ymax = (int(float(width) * coefficient), int(float(heigth) * coefficient),
                                                 int(float(xmin) * coefficient), int(float(ymin)) * coefficient,
                                                 int(float(xmax) * coefficient), int(float(ymax) * coefficient))
        x = ((xmin+dif + xmax+dif) / 2.0) * (1. / (width+2*dif))
        y = ((ymin + ymax) / 2.0) * (1. / heigth)
        W = (xmax - xmin) * (1. / (width+2*dif))
        H = (ymax - ymin) * (1. / heigth)

    return float(x), float(y), float(W), float(H)

huansu avatar Feb 23 '22 12:02 huansu

Doesn't the albumentations part perform the same thing and change the labels and bounding box size in proportion to the size of the image?

cyborgdn avatar Mar 03 '22 10:03 cyborgdn

Doesn't the albumentations part perform the same thing and change the labels and bounding box size in proportion to the size of the image?

Yes, I think it should works for labels as you said, but not. I get error, so I closed the function of changing labels. Therefore, I only use it to pad my image(416 * 416).

huansu avatar Mar 03 '22 10:03 huansu

Doesn't the albumentations part perform the same thing and change the labels and bounding box size in proportion to the size of the image?

Yes, I think it should works for labels as you said, but not. I get error, so I closed the function of changing labels. Therefore, I only use it to pad my image(416 * 416).

What is the error you are getting?

cyborgdn avatar Mar 03 '22 11:03 cyborgdn