albumentations-team/albumentations

ValueError: Expected x_max for bbox (0.94375, 0.5775173611111111, 1.003125, 0.6372395833333333, 0) to be in the range [0.0, 1.0], got 1.003125.

adelkaiarullin opened this issue ยท 24 comments

๐Ÿ› Bug

I tried to use any of transforms like VerticalFlip, RandomSizedBBoxSafeCrop and others box coordinate transformations but always i got the error "Expected x_max for bbox (0.9515625, 0.5316840277777778, 1.003125, 0.6955729166666667, 0) to be in the range [0.0, 1.0], got 1.003125".
if i replace lines x_min, x_max = x_min / cols, x_max / cols, y_min, y_max = y_min / rows, y_max / rows in bbox_utils.py in normalize_bbox method by x_min, x_max = min(x_min / cols, 1.0), min(x_max / cols, 1.0), y_min, y_max = min(y_min / rows, 1.0), min(y_max / rows, 1.0) . it works correctly.

To Reproduce

Steps to reproduce the behavior:

  1. transforms = [
    VerticalFlip(),
    RandomBrightnessContrast(),
    RandomShadow(p=0.5),
    RandomSnow(p=0.5),
    RandomFog(),
    JpegCompression()]
    augmentor = Compose(transforms, bbox_params=BboxParams(format='yolo', label_fields=['category_id']))
  2. Input bboxes
    [[0.492578125, 0.5118055555555555, 0.01328125, 0.02638888888888889], [0.501171875, 0.5013888888888889, 0.01171875, 0.019444444444444445], [0.509765625, 0.5020833333333333, 0.01328125, 0.020833333333333332], [0.51640625, 0.51875, 0.0265625, 0.034722222222222224], [0.581640625, 0.5131944444444444, 0.02265625, 0.029166666666666667], [0.613671875, 0.5145833333333333, 0.02734375, 0.034722222222222224], [0.7546875, 0.5319444444444444, 0.0859375, 0.08055555555555556], [0.46796875, 0.5423611111111111, 0.065625, 0.10138888888888889], [0.9734375, 0.6097222222222223, 0.0515625, 0.1638888888888889]]

Traceback (most recent call last):
File "/home/robo/Code/Python/ONNX/mobilenetv2.py", line 655, in
for batch_data, boxes in det_dataset.get_batchGPU(batch_size):
File "/home/robo/Code/Python/ONNX/mobilenetv2.py", line 609, in get_batchGPU
max_length_box = self.get_image(start_index, batch_size, batch, labels)
File "/home/robo/Code/Python/ONNX/mobilenetv2.py", line 579, in get_image
sample = self.getItemGPURandomGreed(start_index)
File "/home/robo/Code/Python/ONNX/mobilenetv2.py", line 569, in getItemGPURandomGreed
return self.getItemGPUVariableGreed(indx, np.random.randint(1, 3), np.random.randint(1, 3))
File "/home/robo/Code/Python/ONNX/mobilenetv2.py", line 564, in getItemGPUVariableGreed
aug = augmentor(**annotation)
File "/home/robo/.local/lib/python3.6/site-packages/albumentations/core/composition.py", line 174, in call
p.preprocess(data)
File "/home/robo/.local/lib/python3.6/site-packages/albumentations/core/utils.py", line 63, in preprocess
data[data_name] = self.check_and_convert(data[data_name], rows, cols, direction="to")
File "/home/robo/.local/lib/python3.6/site-packages/albumentations/core/utils.py", line 71, in check_and_convert
return self.convert_to_albumentations(data, rows, cols)
File "/home/robo/.local/lib/python3.6/site-packages/albumentations/augmentations/bbox_utils.py", line 51, in convert_to_albumentations
return convert_bboxes_to_albumentations(data, self.params.format, rows, cols, check_validity=True)
File "/home/robo/.local/lib/python3.6/site-packages/albumentations/augmentations/bbox_utils.py", line 305, in convert_bboxes_to_albumentations
return [convert_bbox_to_albumentations(bbox, source_format, rows, cols, check_validity) for bbox in bboxes]
File "/home/robo/.local/lib/python3.6/site-packages/albumentations/augmentations/bbox_utils.py", line 305, in
return [convert_bbox_to_albumentations(bbox, source_format, rows, cols, check_validity) for bbox in bboxes]
File "/home/robo/.local/lib/python3.6/site-packages/albumentations/augmentations/bbox_utils.py", line 253, in convert_bbox_to_albumentations
check_bbox(bbox)
File "/home/robo/.local/lib/python3.6/site-packages/albumentations/augmentations/bbox_utils.py", line 332, in check_bbox
"to be in the range [0.0, 1.0], got {value}.".format(bbox=bbox, name=name, value=value)
ValueError: Expected x_max for bbox (0.9515625, 0.5316840277777778, 1.003125, 0.6955729166666667, 0) to be in the range [0.0, 1.0], got 1.003125.

Environment

  • Albumentations version 0.4.2:
  • Python version 3.6.8:
  • OS Ubuntu 18.04:
  • pip :
Dipet commented

Please, use Markdown formatting for your messages. Without it it is really hard to understand anything in your message.

I tried to use any of transforms like VerticalFlip, RandomSizedBBoxSafeCrop and others box coordinate transformations but always i got the error Expected x_max for bbox (0.9515625, 0.5316840277777778, 1.003125, 0.6955729166666667, 0) to be in the range [0.0, 1.0], got 1.003125.
if i replace lines x_min, x_max = x_min / cols, x_max / cols, y_min, y_max = y_min / rows, y_max / rows in bbox_utils.py in normalize_bbox method by x_min, x_max = min(x_min / cols, 1.0), min(x_max / cols, 1.0), y_min, y_max = min(y_min / rows, 1.0), min(y_max / rows, 1.0) . it works correctly.

To Reproduce

Steps to reproduce the behavior:
1.

transforms = [
    VerticalFlip(),
    RandomBrightnessContrast(),
    RandomShadow(p=0.5),
    RandomSnow(p=0.5),
    RandomFog(),
    JpegCompression()]
    augmentor = Compose(transforms, bbox_params=BboxParams(format='yolo', label_fields=['category_id']))
  1. Input bboxes
[[0.492578125, 0.5118055555555555, 0.01328125, 0.02638888888888889], [0.501171875, 0.5013888888888889, 0.01171875, 0.019444444444444445], [0.509765625, 0.5020833333333333, 0.01328125, 0.020833333333333332], [0.51640625, 0.51875, 0.0265625, 0.034722222222222224], [0.581640625, 0.5131944444444444, 0.02265625, 0.029166666666666667], [0.613671875, 0.5145833333333333, 0.02734375, 0.034722222222222224], [0.7546875, 0.5319444444444444, 0.0859375, 0.08055555555555556], [0.46796875, 0.5423611111111111, 0.065625, 0.10138888888888889], [0.9734375, 0.6097222222222223, 0.0515625, 0.1638888888888889]]
Traceback (most recent call last):
  File "/home/robo/Code/Python/ONNX/mobilenetv2.py", line 655, in <module>
    for batch_data, boxes in det_dataset.get_batchGPU(batch_size):
  File "/home/robo/Code/Python/ONNX/mobilenetv2.py", line 609, in get_batchGPU
    max_length_box = self.get_image(start_index, batch_size, batch, labels)
  File "/home/robo/Code/Python/ONNX/mobilenetv2.py", line 579, in get_image
    sample = self.getItemGPURandomGreed(start_index)
  File "/home/robo/Code/Python/ONNX/mobilenetv2.py", line 569, in getItemGPURandomGreed
    return self.getItemGPUVariableGreed(indx, np.random.randint(1, 3), np.random.randint(1, 3))
  File "/home/robo/Code/Python/ONNX/mobilenetv2.py", line 564, in getItemGPUVariableGreed
    aug = augmentor(**annotation)
  File "/home/robo/.local/lib/python3.6/site-packages/albumentations/core/composition.py", line 174, in __call__
    p.preprocess(data)
  File "/home/robo/.local/lib/python3.6/site-packages/albumentations/core/utils.py", line 63, in preprocess
    data[data_name] = self.check_and_convert(data[data_name], rows, cols, direction="to")
  File "/home/robo/.local/lib/python3.6/site-packages/albumentations/core/utils.py", line 71, in check_and_convert
    return self.convert_to_albumentations(data, rows, cols)
  File "/home/robo/.local/lib/python3.6/site-packages/albumentations/augmentations/bbox_utils.py", line 51, in convert_to_albumentations
    return convert_bboxes_to_albumentations(data, self.params.format, rows, cols, check_validity=True)
  File "/home/robo/.local/lib/python3.6/site-packages/albumentations/augmentations/bbox_utils.py", line 305, in convert_bboxes_to_albumentations
    return [convert_bbox_to_albumentations(bbox, source_format, rows, cols, check_validity) for bbox in bboxes]
  File "/home/robo/.local/lib/python3.6/site-packages/albumentations/augmentations/bbox_utils.py", line 305, in <listcomp>
    return [convert_bbox_to_albumentations(bbox, source_format, rows, cols, check_validity) for bbox in bboxes]
  File "/home/robo/.local/lib/python3.6/site-packages/albumentations/augmentations/bbox_utils.py", line 253, in convert_bbox_to_albumentations
    check_bbox(bbox)
  File "/home/robo/.local/lib/python3.6/site-packages/albumentations/augmentations/bbox_utils.py", line 332, in check_bbox
    "to be in the range [0.0, 1.0], got {value}.".format(bbox=bbox, name=name, value=value)
ValueError: Expected x_max for bbox (0.9515625, 0.5316840277777778, 1.003125, 0.6955729166666667, 0) to be in the range [0.0, 1.0], got 1.003125.

Environment

  • Albumentations version 0.4.2:
  • Python version 3.6.8:
  • OS Ubuntu 18.04:
  • pip :
Dipet commented

You catch this error, because some of your bboxes greater than image size.

Dipet commented

I think, bboxes greater than image to 1 pixel. Maybe you forget reduce bbox coords by 1 pixel, when produce bboxes. Original bbox converter from VOC to YOLO format looks like this:

(x_min, y_min, x_max, y_max), tail = bbox[:4], bbox[4:]
x = (x_min + x_max) / 2 - 1
y = (y_min + y_max) / 2 - 1
width = x_max - x_min
height = y_max - y_min
bbox = normalize_bbox((x, y, width, height) + tail, rows, cols)

https://github.com/pjreddie/darknet/blob/f6d861736038da22c9eb0739dca84003c5a5e275/scripts/voc_label.py#L12

Dipet commented

Also it is possibly due to rounding error when your ymax coord in bbox equal to image shape.

Adding my solution (might help someone else )
If you are sure there is no problem in your bounding boxes ,only floating point precision issue then you can add this block to the function

def check_bbox(bbox):
    """Check if bbox boundaries are in range 0, 1 and minimums are lesser then maximums"""
   #my added block 
    bbox=list(bbox)
    for i in range(4):
      if (bbox[i]<0) :
        bbox[i]=0
      elif (bbox[i]>1) :
        bbox[i]=1
    bbox=tuple(bbox)
   #end of block
   #rest of the code as it is

the directory

/usr/local/lib/python3.6/dist-packages/albumentations/augmentations/bbox_utils.py

N.B : also check the indentation when add the code segment

I checked for my dataset that I was using, that the bounding boxes were not an issue. I tried @abidKiller's solution and it worked for me. I wonder where the floating point precision issue resides, be great to fix it!

I'm disappointed with albumentations. I see until today jun-2021 this bug was not corrected.

Albumentations version 0.5.2:
Python version 3.8.10:
OS windows 10

Thank you @abidKiller , your solution helped me.

I am having the same problem. Shouldn't albumentations round down or up the values instead of throwing an error?

Even Im cought up with the same issue If the team is interested in a PR I am willing to do

Inside the latest check_bbox procedure in boxutils
one simply has to do ,that is add round upto 4 decimal places. this will resolve the issue
if not 0 <= round(value,4) <=1

i suggest @Dipet @coldfir3 @jyauri @narain1 above solution to fix this issue.

In the pipeline, use bbox_params=A.BboxParams(format='pascal_voc'))

In the pipeline, use bbox_params=A.BboxParams(format='pascal_voc'))

I had bboxes with cocojson formats but format="pascal_voc" worked fine for me. Thanks

In the pipeline, use bbox_params=A.BboxParams(format='pascal_voc'))
Works for me too! I have annotations in coco json format but format="pascal_voc" worked fine for me.

I'm also getting this error using Albumentations in Tensorflow OD API. I've checked all the boxes and they are OK (none of them is outside the image boundaries).

I'm using A.BboxParams(format='albumentations') and everything looks good until on one example the training breaks with:

Expected x_max for bbox (A, B, C, D, 0) 
to be in the range [0.0, 1.0], got C.

I've tried using pascal_voc but I still get the same error. How can I debug this problem?

P.S. I've solved it using a hacky solution by bypassing check_box in bbox_utils.py

I'm also getting this error using Albumentations in Tensorflow OD API. I've checked all the boxes and they are OK (none of them is outside the image boundaries).

I'm using A.BboxParams(format='albumentations') and everything looks good until on one example the training breaks with:

Expected x_max for bbox (A, B, C, D, 0) 
to be in the range [0.0, 1.0], got C.

I've tried using pascal_voc but I still get the same error. How can I debug this problem?

P.S. I've solved it using a hacky solution by bypassing check_box in bbox_utils.py

how can you do that? can you share with me the way to bypass it?

@buiqhung127 see this comment #459 (comment)

@buiqhung127 see this comment #459 (comment)

Thank you, I saw it. However, I currently work on Kaggle, which make the albumentations source interference more complicated, so I am looking for a "legal" solution. I've also tried the other way but nothing works in my case.

You should change the lines in "normalize_bbox" to the following.

x_min, x_max = max(0, x_min / cols), min(1, x_max / cols)
y_min, y_max = max(0, y_min / rows), min(1, y_max / rows)

For the next one that comes here, these errors are generally caused by mismatched bbox sizes that could've been the result of incorrect labelling. Please verify your data or the specific image that's causing the error before blaming the developers of not correcting the issue.

Adding my solution (might help someone else ) If you are sure there is no problem in your bounding boxes ,only floating point precision issue then you can add this block to the function

def check_bbox(bbox):
    """Check if bbox boundaries are in range 0, 1 and minimums are lesser then maximums"""
   #my added block 
    bbox=list(bbox)
    for i in range(4):
      if (bbox[i]<0) :
        bbox[i]=0
      elif (bbox[i]>1) :
        bbox[i]=1
    bbox=tuple(bbox)
   #end of block
   #rest of the code as it is

the directory

/usr/local/lib/python3.6/dist-packages/albumentations/augmentations/bbox_utils.py

N.B : also check the indentation when add the code segment

For those of you of you running into this issue when using the Cut and Paste Augmentation Technique, make the change to check_bbox() in the following script:

anaconda/envs/your_environment/lib/python3.8/site-packages/albumentations/core/bbox_utils.py

In addition, if you're performing this CopyPaste augmentation, it might be advantageous to have it perform first, followed by other augmentations that change the image (resize, rotate)

augmentation_list = [
                     CopyPaste(always_apply=True, p=1.0, blend=True, sigma=15, pct_objects_paste=1.0, max_paste_objects=5),
                     A.Resize(800, 1333, p=1.0),
                     A.HorizontalFlip(p=0.5),
                     A.Rotate(limit=(-15, 15), p=0.5),
                     A.OneOf([A.Blur(), A.MotionBlur(), A.GaussNoise()], p=0.1),
                    ]

I am getting the error as mentioned, i am trying to build a deep face recognition system using open cv and tensor flow, this error is occuring while data augmentation
line of code :
augmented = augmentor(image=img, bboxes=[coords], class_labels=['Saumya'])

Screenshot 2023-04-11 223952

This is still an Issue today which is absolutely strange!

I'm also facing this issue. Has there been any updates?