I am trying to convert a real valued tensor to a PIL image to save on disk. When going through the documentation of
to_pil_image, I have a question in the following line in the pytorch/vision repo -
- I do not understand why the image is being multiplied by 255, I understand that the 255 represents the standard 8 bit pixel values, but it does not make sense to multiply the image by 255 unless the input tensor values are between [0,1], since there will be overflows above the [0,255] range and in this case I believe PIL wraps the overflowed values like so -
val%255. I observed this issue in practice when I am saving a real valued tensor to PIL image using the
ToPILImage()utility. Hence if the wrapping happens then, the visualization of images will not be a correct one.
- The documentation of
TensorToPIL()states that Converts a torch.Tensor of shape C x H x W or a numpy ndarray of shape H x W x C to a PIL Image while preserving the value range. - I am not sure what they mean by value range, since from the GitHub code it seems like the value range is not preserved due to some operations on the pixel values. Or am I missing something in my understanding of what preserving value ranges mean? I think the documentation - ToPILImage — Torchvision main documentation - needs to be clearer regarding this utility.
- Even the
save_imageutility in pytorch internally seems to be multiplying the image by 255 here - vision/utils.py at 87cde716b7f108f3db7b86047596ebfad1b88380 · pytorch/vision · GitHub - again under the assumption that the input pixel values are between [0,1], which need not be true. I could not find this assumption stated anywhere explicitly. Please do let me know if I am missing something.
Thanks for your help and please let me know if I am missing something.