I’m trying to create a transform that pads a PIL image to be square. (I wish this was one of the included transforms. Anybody know why it isn’t?)
def square_pad(image):
w, h = image.size
if w==h:
return image
max_wh = np.max([w, h])
hp = int((max_wh - w) // 2)
vp = int((max_wh - h) // 2)
hp2 = max_wh-w-hp
vp2 = max_wh-h-vp
padding = (hp, vp, hp2, vp2)
return F.pad(image, padding, 255, 'constant')
How can I add this to a list of transforms for transforms.Compose() and put it into a DataLoader with multiple workers?
I always get the following error:
AttributeError: Can't pickle local object 'main_worker.<locals>.square_pad'
I’ve tried adding it to the list directly:
train_transforms = [...,square_pad,...]
I’ve tried using Lambda:
train_transforms = [...,transforms.Lambda(square_pad),...]
I’ve tried making it a class with a call method,
I always get the “Can’t pickle local object” error.
BTW, it does work when I use num_workers=0, but that will not work for my application.
Any suggestions? It seems like there really is no way to use a custom transform, and there is also no way to do it with built in transforms.