Different result from different saving image method

When I save the same value torch tensor or ndarray, save_image() from torchvision and save() from matplotlib respectively give me different image visualization, I know they both do some normalization method but can’t tell the detail algorithms they use.
Does anyone know which causes the difference? Thanks

You could check both implementations and compare the used paths in them.
matplotlib.pyplot.imsave seems to deploy to different methods based on the specified format etc. while the torchvision.utils.save_image method normalizes the image to [0, 255].

1 Like

@ptrblck Thanks for answering, however I am wondering which function should I choose in what situation each?
I saw these two function are used frequently in deep learning, but there must be different in output looking.

I think it depends on the tensor/array you are passing to these methods and what its values would mean.
A normalization is often applied, if you provide data in a “non-image” format.
E.g. floating point values in the range [-1, 1] wouldn’t directly represent pixel data (color values) and would thus be mapped to uint8 data in the range [0, 255], which is a standard image format.
The best approach would be for you to create this mapping and pass the array in an expected format, so that neither torchvision nor matplotlib would change any values, as they are most likely trying to use their best guess on what the array might represent.

1 Like