如何還原經過Normalize的PyTorch Tensor?

class UnNormalize(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
for t, m, s in zip(tensor, self.mean, self.std):
t.mul_(s).add_(m)
return tensor
#假設transforms.Normalize的mean和std長這樣:
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])
mean=[0.485, 0.456, 0.406]
std=[0.229, 0.224, 0.225]
unorm = UnNormalize(mean=mean, std=std)#所得到的image就是被還原後的tensor
image = unorm(normalized_image)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store