显示火炬PIL格式的图像.张量



我对Pytorch很陌生。我想知道如何将torch.Size([1, 3, 224, 224])大小的张量转换为Jupyter笔记本上的图像格式。PIL格式或CV2格式应该可以。

我尝试使用transforms.ToPILImage(x),但它产生了一种不同的格式,如:ToPILImage(mode=ToPILImage(mode=tensor([[[[1.3034e-16, 1.3034e-16, 1.3034e-16, ..., 1.4475e-16,.

也许我做错了什么:no_mouth:

由于您的图像是标准化的,因此需要取消标准化。您必须执行与标准化过程相反的操作。一种方法是

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

要使用它,您需要平均值和标准偏差(用于规范图像(。然后,

unorm = UnNormalize(mean = [0.35675976, 0.37380189, 0.3764753], std = [0.32064945, 0.32098866, 0.32325324])
image = unorm(normalized_image)