Tensor To Pil Image

Hello all,

Could you please help me with a doubt? I have a 2D NumPy array that represents an image in black and white (shape: 1x224x224). After converting the NumPy array to a PyTorch tensor, I want to apply transformations to the image (using ToPILImage). Nevertheless, I got a noisy image as a result.

The NumPy array in matplotlib looks like this:

But the ToPILImage() figures looks noisy:

My question is:

  1. How can I convert the PyTorch tensor (in black and white) to the PIL object, so it looks more like the first matplotlib image? I am using transforms.ToPILImage() at the moment.

Thank you so much,

Could you check the dtype and shape of the numpy array before transforming it to a PIL.Image and compare it to the result of the transformation? Based on the output the transformation might cause overflows e.g. if the dtype is also changed.
If you get stuck, please post an executable code snippet to reproduce this issue so that we could help in debugging.