PistonY / torch-toolbox

🛠 Toolbox to extend PyTorch functionalities
BSD 3-Clause "New" or "Revised" License
416 stars 56 forks source link

Cutout throws 'Image' object has no attribute 'shape' #6

Closed ArthDh closed 4 years ago

ArthDh commented 4 years ago

pytorch-lightning==0.7.6 torchtoolbox==0.1.4.1 torchvision==0.6.0a0+82fd1c8

Loading Images using: img = Image.open(str(IMAGE_DIR ))) # import PIL.Image as Image

Composing Transforms using:

self.transform_train = transforms.Compose([transforms.Resize((224, 224)), 
                                    transforms.RandomHorizontalFlip(0.5),
                                    transforms.RandomVerticalFlip(0.5),      
                                    transforms.ColorJitter(0.4, 0.4, 0.4),
                                    Cutout(),
                                    transforms.ToTensor(),
                                    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                                                   std=[0.229, 0.224, 0.225])])

Trace: AttributeError: Caught AttributeError in DataLoader worker process 0. Original Traceback (most recent call last): File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop data = fetcher.fetch(index) File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch data = [self.dataset[idx] for idx in possibly_batched_index] File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in data = [self.dataset[idx] for idx in possibly_batched_index] File "", line 30, in getitem img = self.transform(img) File "/opt/conda/lib/python3.7/site-packages/torchvision/transforms/transforms.py", line 61, in call img = t(img) File "/opt/conda/lib/python3.7/site-packages/torchtoolbox/transform/transforms.py", line 1450, in call left, top, h, w, ch = self.get_params(img, self.scale, self.ratio) File "/opt/conda/lib/python3.7/site-packages/torchtoolbox/transform/transforms.py", line 1433, in get_params img_h, img_w, img_c = img.shape AttributeError: 'Image' object has no attribute 'shape'

PistonY commented 4 years ago

Hi @ArthDh

If you want to use Cutout with PIL you could use torchtoolbox.cutout.Cutout_PIL.

Best, Devin.

ArthDh commented 4 years ago

It would be nice if we could have just one Cutout function that could handle both PIL and CV images!

PistonY commented 4 years ago

It's a good idea but need more if else and convert code, which could make code run slower. Also I have plans to Integrate with albumentations, it would be better when this finished.

ArthDh commented 4 years ago

We could just do a type check and call the function as needed there would be no need to convert the image in that case. Integration with albumentations sounds good!

PistonY commented 4 years ago

If you have good idea please make a pr to me.