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

Training fails if no bounding boxes remain in patch after affine transformation #844

Open
GitMup opened this issue Dec 4, 2024 · 1 comment
Labels
API This tag is used for small improvements to the readability and usability of the python API. Feature Request New feature or request

Comments

@GitMup
Copy link

GitMup commented Dec 4, 2024

Using A.Affine like so:

def get_transform(augment) -> A.Compose or ToTensorV2:
    """This is the new transform"""
    if augment:
        transform = A.Compose(
            [
                A.Affine(rotate=(-180, 180), rotate_method="ellipse", p=0.60, mode=cv2.BORDER_REFLECT_101),
                ToTensorV2(),
            ],
            bbox_params=A.BboxParams(format="pascal_voc", label_fields=["category_ids"]),
        )

    else:
        transform = ToTensorV2()

    return transform

When this transformation rotates all bounding boxes outside of the patch, leaving no boxes inside, training will result in an index error:

IndexError: tensors used as indices must be long, int, byte or bool tensors

I've fixed this one at the dataset level by having a child dataset class repeat the transform until there is an image with a bounding box:

        name, image, targets = super().__getitem__(idx)

        while targets["boxes"].size()[0] == 0:
            name, image, targets = super().__getitem__(idx)
        return name, image, targets

This could also be fixed by setting the target tensor dtype to int, but having negative samples severely degredates the perfomance on my dataset so I've done it this way.

@bw4sz
Copy link
Collaborator

bw4sz commented Jan 16, 2025

This is interesting and i'm trying to decide if there is a global fix. I know albumentations has clip=True in the bbox params, but as you said that would introduce negative samples by accident. Adding a while loop for transforms feels like it could get you lost in a infinite situation in the conditions were different. Do you have a suggestion for something the API could do to anticipate this.

@bw4sz bw4sz added Feature Request New feature or request API This tag is used for small improvements to the readability and usability of the python API. labels Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API This tag is used for small improvements to the readability and usability of the python API. Feature Request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants