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

Targets that should be ignored #1771

Open
daitakahashi opened this issue Jun 4, 2024 · 10 comments
Open

Targets that should be ignored #1771

daitakahashi opened this issue Jun 4, 2024 · 10 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@daitakahashi
Copy link

daitakahashi commented Jun 4, 2024

Feature description

We need a way to declare a target that will be ignored by Compose-wrapped transforms.

Motivation and context

Let say, we have a following data and Compose-wrapped transforms.

data = {'image': image, 'mask': mask, 'text_description': 'image description'}
transform = A.Compose([A.Blur()])  # note: A.Blur() is image only

What we want to do is applying the transformation on image and mask but text_description without manipulating the data.

transformed = transform(**data)
transformed['text_description'] == 'image description'  # unchanged

Before #1706, Compose simply ignored all unknown keys. As a result, the above code worked "as expected" (as a byproduct of an issue solved by the pr).

However, current transforms check args by default.

>>> transformed = transform(**data)
Traceback (most recent call last):
...
ValueError: Key text_description is not in available keys.

I expected that we have a way to declare the text_description being ignored, e.g., transform.add_targets({'text_description': 'ignore'}). But in the current albumentations, this kind of declaration does not work and raises ValueError when it is called.

>>> transform.add_targets({'text_description': 'ignore'})  # or anything other than 'image'
>>> transformed = transform(**data)
Traceback (most recent call last):
...
ValueError: Key text_description is not in available keys.

Alternatives

A currently available workaround is setting is_check_args of the Compose object to be False. The workaround skips all argument checks added by #1706.

@daitakahashi daitakahashi added the enhancement New feature or request label Jun 4, 2024
@daitakahashi daitakahashi changed the title Targets to be ignored Targets that should be ignored Jun 4, 2024
@ternaus
Copy link
Collaborator

ternaus commented Jun 4, 2024

Thanks.

Applying transforms, without wrapping with Compose, typically leads to enexpected bugs => better to always wrap with compose.

At the same time adding is_check_args parameter to transforms looks quite doable, and if user knows that they are doing - why not have such functionality?

Will work on this.

@ayasyrev
Copy link
Contributor

ayasyrev commented Jun 4, 2024

Do you use single transform or inside Compose?
Single transform don`t check keys - just ignores unused keys.

@daitakahashi
Copy link
Author

Ayasyrev is right. I found that I have always wrapped transforms with Compose. So the situation I described in this issue is limited to transforms that are wrapped with Compose. I will update this issue to make sure it.

@daitakahashi
Copy link
Author

daitakahashi commented Jun 4, 2024

By the way, this is a bit unintuitive for me (albumentations 1.4.8).

>>> tr = A.Compose([A.Blur()])
>>> tr.add_targets({'mask2': 'mask'})
>>> out = tr(image=np.zeros((2, 2, 3)), mask2=np.zeros((2, 2, 3)))
Traceback (most recent call last):
...
ValueError: Key mask2 is not in available keys.
>>> tr = A.Compose([A.Flip()])
>>> tr.add_targets({'mask2': 'mask'})
>>> out = tr(image=np.zeros((2, 2, 3)), mask2=np.zeros((2, 2, 3)))
>>>

I encountered it when I compiling an example case of this issue.

@ternaus
Copy link
Collaborator

ternaus commented Jun 4, 2024

It does not look right to me eigher.

@ternaus ternaus added the bug Something isn't working label Jun 4, 2024
@daitakahashi
Copy link
Author

I feel that checking against _available_keys seems to be too restrictive as _available_keys depends on transforms inside and key-permissive nature of those transforms.

@ayasyrev
Copy link
Contributor

ayasyrev commented Jun 5, 2024

I encountered it when I compiling an example case of this issue.

This is not OK. Will fix it,

@ayasyrev
Copy link
Contributor

ayasyrev commented Jun 5, 2024

By the way, this is a bit unintuitive for me (albumentations 1.4.8).

>>> tr = A.Compose([A.Blur()])
>>> tr.add_targets({'mask2': 'mask'})
>>> out = tr(image=np.zeros((2, 2, 3)), mask2=np.zeros((2, 2, 3)))
Traceback (most recent call last):
...
ValueError: Key mask2 is not in available keys.
>>> tr = A.Compose([A.Flip()])
>>> tr.add_targets({'mask2': 'mask'})
>>> out = tr(image=np.zeros((2, 2, 3)), mask2=np.zeros((2, 2, 3)))
>>>

I encountered it when I compiling an example case of this issue.

The reason why you get error here - Blur is ImageOnly transform, so masks not processed here and add some additional key for mask make not many sense. But as mask is special case, I'll add this possibility.

@daitakahashi
Copy link
Author

daitakahashi commented Jun 5, 2024

@ayasyrev Yes, Blur() is imageonly, I understand it. I'm concerning a case of trying out several combinations of transformations to a predefined dataset (I often do such experiments, so my concern may be biased). Anyway, I want the transforms not to crash regardless of the data format and algorithms inside.
A.Compose([A.Flip(p=0), A.Blur()]) will work for most of data if we declare key-target pairs by add_targets(), because Flip knows mask, bboxes, and keypoints. But this doesn't look nice.

@daitakahashi
Copy link
Author

daitakahashi commented Jun 5, 2024

I mean the example in the documentation may not work if we use an ImageOnlyTransform, e.g., GaussianBlur, instead of the HorizontalFlip (note: I haven't tried yet).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants