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

pytorch ImageFolder transform bug #879

Closed
dglubokov opened this issue Apr 18, 2021 · 1 comment
Closed

pytorch ImageFolder transform bug #879

dglubokov opened this issue Apr 18, 2021 · 1 comment

Comments

@dglubokov
Copy link

🐛 Bug

While using the torchvision.datasets.ImageFolder I get an error:

You have to pass data to augmentations as named arguments, for example: aug(image=image)

This is because the ImageFolder uses:

...
if self.transform is not None:
    sample = self.transform(sample)
...

from torchvision/datasets/folder.py.

Is is correct to create an issue here?

Traceback

KeyError                                  Traceback (most recent call last)
<ipython-input-50-60c394f66b5b> in <module>
----> 1 model_ft = train_model(mobilenet_v3_large, criterion, optimizer_ft, exp_lr_scheduler,
      2                        num_epochs=25)

<ipython-input-38-1a5d4f162548> in train_model(model, criterion, optimizer, scheduler, num_epochs)
     20 
     21             # Iterate over data.
---> 22             for inputs, labels in dataloaders[phase]:
     23                 inputs = inputs.to(device)
     24                 labels = labels.to(device)

~/.local/lib/python3.9/site-packages/torchvision/datasets/folder.py in __getitem__(self, index)
    178         sample = self.loader(path)
    179         if self.transform is not None:
    180             sample = self.transform(sample)
    181         if self.target_transform is not None:
    182             target = self.target_transform(target)

~/.local/lib/python3.9/site-packages/albumentations/core/composition.py in __call__(self, force_apply, *args, **data)
    163     def __call__(self, *args, force_apply=False, **data):
    164         if args:
    165             raise KeyError("You have to pass data to augmentations as named arguments, for example: aug(image=image)")
    166         self._check_args(**data)
    167         assert isinstance(force_apply, (bool, int)), "force_apply must have bool or int type"

KeyError: 'You have to pass data to augmentations as named arguments, for example: aug(image=image)'

Expected behavior

Should work while training.

Environment

  • Albumentations version: 0.5.2
  • Python version: 3.9
  • OS: Linux
  • How you installed albumentations (conda, pip, source): pip
@Dipet
Copy link
Collaborator

Dipet commented Apr 22, 2021

Library works only with named arguments and numpy array. For this reason, it is incompatible with torchvision.
Try to wrap transforms using this class

class Transforms:
    def __init__(self, transforms: A.Compose):
        self.transforms = transforms

    def __call__(self, img, *args, **kwargs):
        return self.transforms(image=np.array(img))

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

2 participants