There are a few issues in the example code:
- Even though you are defining your own
Compose
class, it seems you are still using thetorchvision.transforms.Compose
one, so you might want to remove theT
namespace. - Your custom
Compose
object takes two inputs. However, the underlyingtorchvision.transforms
don’t, so you would have to call the transformation on the image and target separately. - Since you are using (one) random transformation, the image and target will not be transformed using the same random number, which might be wrong (e.g. for a segmentation use case). If you want to apply the same random transform on both inputs, have a look at this post
-
RandomHorizontalFlip
andResize
work onPIL.Images
, soToTensor
should be applied last.
Here is a small dummy example:
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, image, target):
for t in self.transforms:
image = t(image)
target = t(target)
return image, target
import torchvision.transforms as transforms
transform = []
transform.append(transforms.RandomHorizontalFlip(0.5))
transform.append(transforms.Resize(10))
transform.append(transforms.ToTensor())
transform = Compose(transform)
to_image = transforms.ToPILImage()
x = to_image(torch.randn(3, 24, 24))
y = to_image(torch.randn(3, 24, 24))
transform(x, y)