10000 [Feature Request] Un-Normalize Image Tensor · Issue #528 · pytorch/vision · GitHub
[go: up one dir, main page]

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

[Feature Request] Un-Normalize Image Tensor #528

Closed
varagrawal opened this issue Jun 5, 2018 · 7 comments
Closed

[Feature Request] Un-Normalize Image Tensor #528

varagrawal opened this issue Jun 5, 2018 · 7 comments

Comments

@varagrawal
Copy link

Basically the inverse of transforms.Normalize as this will allow us to visualize tensors during training more easily.

@karandwivedi42
Copy link
Contributor

#281

@fmassa
Copy link
Member
fmassa commented Jun 6, 2018

I agree with @karandwivedi42 and the comment that he linked to.

If you want to reverse the normalization, all you need to do is to use a new normalization, with slight modifications:

mean = torch.tensor([1, 2, 3], dtype=torch.float32)
std = torch.tensor([2, 2, 2], dtype=torch.float32)

normalize = T.Normalize(mean.tolist(), std.tolist())

unnormalize = T.Normalize((-mean / std).tolist(), (1.0 / std).tolist())

EDIT: fixed thanks to @karandwivedi42 comment

@fmassa fmassa closed this as completed Jun 6, 2018
@fmassa
Copy link
Member
fmassa commented Jun 7, 2018

Thanks @karandwivedi42 , my bad!
I'll fix my comment!

@danieltudosiu
Copy link

Hey,

This does not seem to work for me at all. I am using the following code:

import numpy as np
import matplotlib.pyplot as plt
from albumentations.augmentations import Normalize

mean = [-0.485 / 0.229, -0.456 / 0.224, -0.406 / 0.225]
std = [1.0 / 0.229, 1.0 / 0.224, 1.0 / 0.225]
a = np.load("/path/to/your/image)
plt.imshow(a)
plt.show()

norm = Normalize(always_apply=True)
a = norm(image=a)["image"]
plt.imshow(a)
plt.show()

denorm = Normalize(mean=mean, std=std, always_apply=True)
a = denorm(image=a)["image"]
plt.imshow(a)
plt.show()

Could you please tell me what am I doing wrong :(

@danieltudosiu
Copy link

For future reference this is the correct answer:

mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225)
a = np.load(
    "/home/danieltudosiu/storage/datasets/green_roof_retrofit/debugging/train/img_rgb/roofs_image_0002.npy"
)
plt.imshow(a)
plt.show()

norm = Normalize(mean=mean, std=std, always_apply=True)
a = norm(image=a)["image"]
plt.imshow(a)
plt.show()

denorm = Normalize(
    mean=[-m / s for m, s in zip(mean, std)],
    std=[1.0 / s for s in std],
    always_apply=True,
    max_pixel_value=1.0
)
a = (denorm(image=a)["image"]*255).astype(np.uint8)
plt.imshow(a)
plt.show()

@varunagrawal
Copy link
Contributor

Your solution is pretty much what @fmassa laid out. If you look at his unnormalize method, he is computing the negative mean divided by the standard deviation.

@tkupek
Copy link
tkupek commented Jun 20, 2023

I also had issues with the solution above. Seems like max_pixel_value=1.0 and separately multiply with 255 from @danieltudosiu does the trick. You can also combine with in a simple Compose pipeline:

Compose(
        [
            Normalize(
                mean=tuple(-m / s for m, s in zip(mean, std)),
                std=tuple(1.0 / s for s in std),
                max_pixel_value=1.0,
            ),
            FromFloat(max_value=255, dtype="uint8"),
        ]
    )

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

No branches or pull requests

6 participants
0