Solvedalbumentations 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.

🐛 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 :
16 Answers

✔️Accepted Answer

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

Other Answers:

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

More Issues: