accimage icon indicating copy to clipboard operation
accimage copied to clipboard

in place operations only breaks FiveCrop.

Open xvdp opened this issue 6 years ago • 1 comments

Hi, I was writing some augmentation transforms and I noticed that all operations are done in place. I forked and added a .copy() method for my use, but I wonder if this is the correct way of doing it. I like accimage quite as it is 4 to 6 times faster in my profiling than either going thru opencv or PIL to tensor. I like, as well, that accimage operates in place by default, but as it stands, accimage breaks transformations that rely on data copies, such as FiveCrop. PIL default behaviour always returns a copy. On my augmentation library Im changing the FiveCrop to bypass accimage by running Image.copy() if accimage.

img = ...
import PIL
import accimage
import torchvision.transforms as ttrans
fcrop = ttrans.Compose([ttrans.FiveCrop(100),
         ttrans.Lambda(lambda crops: torch.stack([ttrans.ToTensor()(crop) for crop in crops]))])
aim = PIL.Image.open(img)
f = fcrop(aim)
# OK

fcrop = ttrans.Compose([ttrans.FiveCrop(100),
         ttrans.Lambda(lambda crops: torch.stack([ttrans.ToTensor()(crop) for crop in crops]))])
aim = accimage.Image(img)
f = fcrop(aim)
# Fails 

If you are interested you can look at my forked solution. BUT it is a half way solution since it requires a fix to torvision.transforms.

xvdp avatar Mar 28 '19 18:03 xvdp

Writing the transform with the .copy() op I realized that it makes no sense to have torchvision transformations see accimage and PIL as the same when their behaviour is different, so I added a kwarg, in_place=[False] so that default behaviour is the same in PIL and accimage, but accimage can be run leaner with the in_place key on. This fixes the disparity whilst keeping the ability to go lean. If you want, I do a pull request.

xvdp avatar Mar 28 '19 23:03 xvdp