albumentations-team / albumentations

Fast and flexible image augmentation library. Paper about the library: https://www.mdpi.com/2078-2489/11/2/125
https://albumentations.ai
MIT License
14.24k stars 1.65k forks source link

ValueError: Expected x_min for bbox (-0.02666666666666667, 0.3022222222222222, 0.10666666666666667, 0.48444444444444446, tensor(1)) to be in the range [0.0, 1.0], got -0.02666666666666667. #679

Closed SlowMonk closed 3 years ago

SlowMonk commented 4 years ago

šŸ› Bug

To Reproduce

Steps to reproduce the behavior:

1. 1. 1.

Expected behavior

Environment

Additional context

I'm getting ValueError: Expected x_min for bbox (-0.02666666666666667, 0.3022222222222222, 0.10666666666666667, 0.48444444444444446, tensor(1)) to be in the range [0.0, 1.0], got -0.02666666666666667. message

my original box is

new_boxes->[[442. 79. 972. 564.]]

its x_min, y_min, x_max, y_max format then it only give this error message when


        if random.random() < 0.2:
            print('horizontalFlip')
            print(f'new_boxes->{new_boxes}')
            augment = albumentations.HorizontalFlip(p=0.5)
            aug = albumentations.Compose([augment],bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})
            aug_result = aug(image=np.array(new_image), bboxes=new_boxes, labels=labels)
            new_image = aug_result['image']
            new_boxes = aug_result['bboxes']
            new_image = FT.to_pil_image(new_image)
ternaus commented 4 years ago

What is the shape of the image?

marcocaccin commented 4 years ago

This unfortunately is regularly happening for me as well. Here's my env along with a reproducible script.

It seems to me that all of these errors seem to deal with numerical errors when doing geometric operations on totally legit bounding boxes right at the image boundary. One way to deal with the issue would be to allow an "unsafe" transformation by letting the user control the check_validity flag from BBoxParams and deal with truncations manually afterwards: that's still a better option than having a training job fail because of uncontrollable exceptions.

CC: @BeckerFelix

Environment

import numpy as np
import albumentations as A

np.random.seed(123)
HEIGHT, WIDTH = 720, 1280

def random_bbox():
    x1 = np.random.randint(low=0, high=WIDTH)
    y1 = np.random.randint(low=0, high=HEIGHT)
    x2 = np.random.randint(low=x1 + 1, high=WIDTH + 1)
    y2 = np.random.randint(low=y1 + 1, high=HEIGHT + 1)
    bbox_albu = A.convert_bbox_to_albumentations([x1, y1, x2, y2], source_format='pascal_voc', rows=HEIGHT, cols=WIDTH)
    bbox_yolo = A.convert_bbox_from_albumentations(bbox_albu, target_format='yolo', rows=HEIGHT, cols=WIDTH, check_validity=True)
    # NOTE: at this point the bounding box has been checked to be valid.

    return bbox_yolo

transform = A.Compose(
    [A.HorizontalFlip(), A.RandomBrightnessContrast()],
    bbox_params=A.BboxParams(format='yolo', label_fields=["class_labels"])
)
img = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8)

for i in range(1000):
    bboxes = [random_bbox()]
    try:
        transform(image=img, bboxes=bboxes, class_labels=[1])
    except:
        print(f"[{i}] Invalid transformation of box: {str(bboxes[0])}")

>>> [327] Invalid transformation of box: (0.755859375, 0.5944444444444444, 0.48671875, 0.3611111111111111)
>>> [363] Invalid transformation of box: (0.373046875, 0.9409722222222222, 0.68828125, 0.11527777777777778)
>>> [683] Invalid transformation of box: (0.465625, 0.9881944444444445, 0.5765625, 0.020833333333333332)
Dipet commented 4 years ago

Problem with floating point arithmetic. Example:

HEIGHT = 720
WIDTH = 1280

pascal_bbox = [1122, 17, 1280, 720]
albu_bbox = (0.8765625, 0.02361111111111111, 1.0, 1.0)
yolo = (0.9375, 0.5104166666666666, 0.1234375, 0.9763888888888889)

reverse_pascal = (1122.0, 17.5, 1280.0, 720.5) # <- 0.5 error is here. Because height 703 does not divisible by 2,

I think cliping using conversion to int for x_min and x_max will fix this error. Also I will check rounding

marcocaccin commented 4 years ago

Thank you @Dipet , that was really fast. I wanted to ask if I could be of any help after a long weekend but I see it's already implemented and approved. Awesome work! I'll keep an eye on the release notes for the next albu versions, but let me know if you need any testing from my side

Dipet commented 3 years ago

Should be fixed by #924

alejopaullier96 commented 1 year ago

This problem is still present in version 1.3.0:

ValueError(f"Expected {name} for bbox {bbox} to be in the range [0.0, 1.0], got {value}.")
ValueError: Expected y_max for bbox (0.14792899408284024, 0.9671641791044776, 0.28205128205128205, 1.0029850746268656, tensor(2)) to be in the range [0.0, 1.0], got 1.0029850746268656.

I am using the following augmentations for an object detection problem:

def get_train_transforms():
    return A.Compose(
        [
            A.Resize(height=config.RESOLUTION, width=config.RESOLUTION, p=1),
            A.Normalize(p=1),
            ToTensorV2(p=1.0),
        ], 
        p=1.0, 
        bbox_params=A.BboxParams(
            format='pascal_voc',
            min_area=0, 
            min_visibility=0,
            label_fields=['labels']
        )
    )

def get_valid_transforms():
    return A.Compose(
        [
            A.Resize(height=config.RESOLUTION, width=config.RESOLUTION, p=1.0),
            A.Normalize(p=1),
            ToTensorV2(p=1.0),
        ], 
        p=1.0, 
        bbox_params=A.BboxParams(
            format='pascal_voc',
            min_area=0, 
            min_visibility=0,
            label_fields=['labels']
        )
    )
vladyskai commented 2 months ago

This was still an issue for me. It worked with the clip parameter set to true.