【问题标题】:Getting 'tensor is not a torch image' for data type <class 'torch.Tensor'>获取数据类型的“张量不是火炬图像”<class 'torch.Tensor'>
【发布时间】:2020-06-22 21:27:31
【问题描述】:

正如问题所暗示的,我正在尝试将images 转换为张量。

X, y = train_sequence[idx]  

images = Variable(torch.from_numpy(X)).to(device) # [batch, channel, H, W]
masks = Variable(torch.from_numpy(y)).to(device) 
print(type(images)) ## Output: <class 'torch.Tensor'>


images = transforms.Normalize((0.5, 0.5, 0.5, 0.5, 0.5), (0.5, 0.5, 0.5,0.5, 0.5))(images)
masks =  transforms.Normalize((0.5), (0.5))(masks)

但我得到了错误 ---&gt; 19 images = transforms.Normalize((0.5, 0.5, 0.5, 0.5, 0.5), (0.5, 0.5, 0.5,0.5, 0.5))(images)

TypeError: tensor is not a torch image.

【问题讨论】:

    标签: numpy machine-learning deep-learning pytorch tensor


    【解决方案1】:

    这是因为到目前为止,torchvison.transforms.Normalize 仅支持具有 2 或 3 个通道的图像,没有批量维度,即。 (C, H, W)。因此,与其传入 4D 张量,不如使用类似的方法:

    image = torch.ones(3, 64, 64)
    image = transforms.Noramalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(image)
    

    另外,因为0.5 值代表图像通道的均值和标准差,所以通常应该只有 3 个通道(您不会“标准化”批量维度,只有空间维度),所以不是使用长度为 5 的 tuple,执行 (0.5, 0.5, 0.5)

    【讨论】:

    • 知道了。谢谢!
    猜你喜欢
    • 2019-01-19
    • 2017-04-04
    • 2017-02-23
    • 2022-10-23
    • 2022-07-20
    • 1970-01-01
    • 2021-07-19
    • 2021-05-21
    • 1970-01-01
    相关资源
    最近更新 更多