Augmentor icon indicating copy to clipboard operation
Augmentor copied to clipboard

Incompatibility to pytorch: Some transformations return lists for singe Image

Open lukas-schott opened this issue 7 years ago • 5 comments

The pipeline is not compatible with pytorch because the operations return lists [ <PIL.Image.Image image mode=L size=28x28 at 0x7F4C6F113A90>] but expect as input: <PIL.Image.Image image mode=L size=28x28 at 0x7F4C6F113A90>

Here a minimalistic example, where I show what the MNIST dataloader does internally and why it fails..

import Augmentor
from torchvision import datasets, transforms
from torch.utils import data

loader = data.DataLoader(datasets.MNIST('./../data', download=True, transform=transforms.Compose([transforms.ToTensor()])))

p1 = Augmentor.Pipeline()
p1.zoom(probability=1, min_factor=1.1, max_factor=1.5)
p1 = p1.torch_transform()

p2 = Augmentor.Pipeline()
p2.shear(probability=1, max_shear_left=5, max_shear_right=5)
p2 = p2.torch_transform()
# test it
for i, (batch, label) in enumerate(loader):
    batch = transforms.ToPILImage()(batch[0])
    batch = p1(batch)  
    print(batch)           # prints [<PIL.Image.Image image mode=L size=28x28 at 0x7F4BE1BF9470>]
    batch = p2(batch)      # expects list --> fails, works if I do p2(batch[0])
    print('batch', batch)
    break

lukas-schott avatar Apr 25 '18 15:04 lukas-schott

workaround:

def from_list(x):
    if isinstance(x, list):
        return x[0]
    else:
        return x


loader = data.DataLoader(datasets.MNIST('./../data', download=True, transform=transforms.Compose([
                p1, from_list, p2, from_list, transforms.ToTensor()])))

lukas-schott avatar Apr 25 '18 16:04 lukas-schott

This bug still exists.

p.torch_transform() expects an image array but gets a list.

I dont really understand how the workaround works, Im using the pytorch implementation as mentioned in the docs:

 p = Augmentor.Pipeline()
p.flip_left_right(probability=0.5)
p.flip_top_bottom(probability=0.5)
p.random_distortion(probability=0.5, grid_width=8, grid_height=8, magnitude=8)


transforms_ = torchvision.transforms.Compose([
    p.torch_transform(),
    torchvision.transforms.ToTensor()
])

In Operations.py this line expects the size of an image:

w, h = images[0].size but since image[0] is a list it returns the number of elements in the array =/

StuckinPhD avatar Jan 04 '19 21:01 StuckinPhD

This bug still exists.

p.torch_transform() expects an image array but gets a list.

I dont really understand how the workaround works, Im using the pytorch implementation as mentioned in the docs:

 p = Augmentor.Pipeline()
p.flip_left_right(probability=0.5)
p.flip_top_bottom(probability=0.5)
p.random_distortion(probability=0.5, grid_width=8, grid_height=8, magnitude=8)


transforms_ = torchvision.transforms.Compose([
    p.torch_transform(),
    torchvision.transforms.ToTensor()
])

In Operations.py this line expects the size of an image:

w, h = images[0].size but since image[0] is a list it returns the number of elements in the array =/

Hi did you solved this ?

Samyssmile avatar Jan 12 '19 04:01 Samyssmile

This bug still exists. p.torch_transform() expects an image array but gets a list. I dont really understand how the workaround works, Im using the pytorch implementation as mentioned in the docs:

 p = Augmentor.Pipeline()
p.flip_left_right(probability=0.5)
p.flip_top_bottom(probability=0.5)
p.random_distortion(probability=0.5, grid_width=8, grid_height=8, magnitude=8)


transforms_ = torchvision.transforms.Compose([
    p.torch_transform(),
    torchvision.transforms.ToTensor()
])

In Operations.py this line expects the size of an image: w, h = images[0].size but since image[0] is a list it returns the number of elements in the array =/

Hi did you solved this ?

I just stopped using p.torch_transform() and went the normal route of saving the generated images to a directory and then loading them in my Dataset class

StuckinPhD avatar Jan 12 '19 12:01 StuckinPhD

Hi~ Is this problem fixed?

genghisun avatar Aug 06 '20 02:08 genghisun