Skip to content

Instantly share code, notes, and snippets.

@EckoTan0804
Created March 12, 2023 10:25
Show Gist options
  • Save EckoTan0804/47a6565fa637af598e833a385615b312 to your computer and use it in GitHub Desktop.
Save EckoTan0804/47a6565fa637af598e833a385615b312 to your computer and use it in GitHub Desktop.
Unnormalize normalized PyTorch tensor
class UnNormalize(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
"""
Args:
tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
Returns:
Tensor: Normalized image.
"""
for t, m, s in zip(tensor, self.mean, self.std):
t.mul_(s).add_(m)
# The normalize code -> t.sub_(m).div_(s)
return tensor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment