Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 opened this issue Aug 11, 2020 · 6 comments

Comments

@SlowMonk
Copy link

🐛 Bug

To Reproduce

Steps to reproduce the behavior:

Expected behavior

Environment

  • Albumentations version (e.g., 0.1.8):
  • Python version (e.g., 3.7): 3.7
  • OS (e.g., Linux): ubuntu
  • How you installed albumentations (conda, pip, source): pip
  • Any other relevant information:

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
Copy link
Collaborator

ternaus commented Aug 11, 2020

What is the shape of the image?

@marcocaccin
Copy link
Contributor

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

  • Albumentations version: 0.4.6, 0.3.3
  • Python version: 3.6.12
  • OS (e.g., Linux): ubuntu 20.04
  • How you installed albumentations (conda, pip, source): pip
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
Copy link
Collaborator

Dipet commented Aug 28, 2020

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
Copy link
Contributor

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
Copy link
Collaborator

Dipet commented Jul 7, 2021

Should be fixed by #924

@Dipet Dipet closed this as completed Jul 7, 2021
@alejopaullier96
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants