Display image in PIL format from torch.Tensor

Hello, I’m quite new to Pytorch. I was wondering how I could convert my tensor of size torch.Size([1, 3, 224, 224]) to display in an image format on a Jupyter notebook. A PIL format or a CV2 format should be fine.

I tried using transforms.ToPILImage(x) but it resulted in a different format like this: ToPILImage(mode=ToPILImage(mode=tensor([[[[1.3034e-16, 1.3034e-16, 1.3034e-16, ..., 1.4475e-16,. Maybe I’m doing something wrong :no_mouth:

ToPILImage() should work.
I’m not sure what you are passing as the mode argument, but this small code snippet works fine:

x = torch.randn(1, 3, 224, 224)
trans = torchvision.transforms.ToPILImage()
out = trans(x[0])
out.show()
3 Likes

Thanks a lot @ptrblck