How can convert 1 channel tensor to PIL Image?

Hello all, I am new to pytorch.
I made a model that returns shape (1, 128, 128) tensor and wanted to resize it to (1, 101, 101). To use transforms.Resize(), I converted the tensor to PIL image using transforms.ToPILImage(mode=‘L’) function. Then, the system said ‘ValueError: Only modes [‘RGB’, ‘YCbCr’, ‘HSV’] are supported for 3D inputs’ !!
But, I think my tensor has 1 channel not 3D inputs.
my question is

  • how to convert 1 channel tensor to PIL Image ?
  • is there better way to resize tensor than i did ??

Thanks in advance! :smile:

That’s strange, as this code seems to work:

image = transforms.ToPILImage(mode='L')(torch.randn(1, 96, 96))

However, you could just simply slice your image and pass it to your transformation:

transform(image[0])

Thanks for reply!
I found some errors in my code - I put numpy type data as input format,
and it works well now :sweat_smile:

I have kind of the same problem but I’m trying to call to_pil_image on a tensor that consists of sequences of grayscale images. [seq_len, width, height]. How can I call this function on all images? Because now it thinks that my image has 10 (10-seq_len) channels.