Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion about default unnormalize method for pytorch tensor #14

Open
genghisun opened this issue May 19, 2023 · 3 comments
Open

Discussion about default unnormalize method for pytorch tensor #14

genghisun opened this issue May 19, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@genghisun
Copy link
Contributor

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

def auto_unnormalize_image(x):
all_int = isinteger(np.unique(x)).all()
range_0_1 = within_0_1(x)
range_0_255 = within_0_255(x)
has_negative = (x.min() < 0)
if has_negative:
logger.debug('Detects input has negative values, auto rescaling input to 0-1.')
return rescale_0_1(x)
if range_0_255 and all_int and (not range_0_1): # if image is all integer and between 0 - 255. Normalize it to 0-1.
logger.debug('Detects input are all integers within range 0-255. Divided all values by 255.')
return x / 255.
if range_0_1:
logger.debug('Inputs already within 0-1, no unnormalization is performed.')
return x
logger.debug('Auto rescaling input to 0-1.')
return rescale_0_1(x)
def rescale_0_1(x):
"""
Rescaling tensor to 0-1 using min-max normalization
"""
return (x - x.min()) / (x.max() - x.min())

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
Copy link
Owner

xwying commented May 27, 2023

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?

@xwying xwying added the enhancement New feature or request label May 27, 2023
@genghisun
Copy link
Contributor Author

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
Copy link
Owner

xwying commented May 28, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants