如何還原經過Normalize的PyTorch Tensor?
Jun 16, 2021
參考資料
如果我們想要UnNormalize,可以怎麼做?
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)