xwying / torchshow

Visualize PyTorch tensors with a single line of code.
MIT License
629 stars 9 forks source link

Discussion about default unnormalize method for pytorch tensor #14

Open genghisun opened 1 year ago

genghisun commented 1 year ago

Currently, it seems that for input pytorch float tensor between -1 and 1, a min-max normalization is taken:

https://github.com/xwying/torchshow/blob/3008c23ebac2811f07c314119b14ba1725b215f7/torchshow/visualization.py#L264-L287

However, usually people use transforms.Normalize((0.5,), (0.5,)) to normalize image. So I think when x is between -1 and 1, it should be x * 0.5 + 0.5 to keep the image color consistent.

I know it can be achieved by setting set_image_mean([0.5, 0.5, 0.5]) and set_image_std([0.5, 0.5, 0.5]), but this may be used for other special cases. For the most common cases, i think we should adopt the above method.

Any idea?

xwying commented 1 year ago

Good suggestion! I think we could do it if this is a common normalization preset. In fact, what you described can be extended to all the zero-centralize normalizations as well. The tricky part that I can think of is how to determine if the data is zero-centralized rather than just happen to fall between that range. In other word, is simply checking the [-1, 1] range (or [-M, M] for more general cases) sufficient to determine these kind of inputs?

genghisun commented 1 year ago

I think simply checking [-1, 1] is enough. Is there any other possible situation? For normal images it seems that these are the only cases. For other special cases, they can use set_image_mean and set_image_std.

xwying commented 1 year ago

Right, maybe we can try to handle the [-1, 1] case for now. Would you be able to create an PR for this feature and some testing cases so we can review and discuss further?