RuntimeError:张量a(224)的大小必须与非单一尺寸0时张量b(3)的大小匹配

时间:2020-02-12 02:04:44

标签: deep-learning pytorch

尝试对pytorch进行标准化

from torchvision.transforms import Normalize

class Unnormalize:
    """Converts an image tensor that was previously Normalize'd
    back to an image with pixels in the range [0, 1]."""
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std

    def __call__(self, tensor):
        mean = torch.as_tensor(self.mean, dtype=tensor.dtype, device=tensor.device).view(3, 1, 1)
        std = torch.as_tensor(self.std, dtype=tensor.dtype, device=tensor.device).view(3, 1, 1)
        return torch.clamp(tensor*std + mean, 0., 1.)


mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std)
unnormalize_transform = Unnormalize(mean, std)

那我的图像大小是img = torch.Size([3,224,224])

img_temp=img.permute((1, 2, 0)).cpu().detach()
normalize_transform(img_temp)

--------------------------------------------------- ---------------------------- RuntimeError Traceback(最近一次调用 最后) ----> 1 normalize_transform(img_temp)

〜/ venv / lib / python3.7 / site-packages / torchvision / transforms / transforms.py 在通话中(自身,张量) 173张量:标准化张量图像。 174“”“ -> 175 return F.normalize(张量,self.mean,self.std,self.inplace) 176 177 def 代表(自己):

〜/ venv / lib / python3.7 / site-packages / torchvision / transforms / functional.py 归一化(张量,均值,标准,就地) 215平均值= torch.as_tensor(平均值,dtype = dtype,设备= tensor.device) 216 std = torch.as_tensor(std,dtype = dtype,device = tensor.device) -> 217张量.sub_(平均值[:,无,无])。div_(std [:,无,无]) 218返回张量 219

RuntimeError:张量a(224)的大小必须与张量的大小匹配 b(3)在非单维度0上

0 个答案:

没有答案
相关问题