获取数据类型 <class 'torch.Tensor'> 的 'tensor is not a torch image'
Getting 'tensor is not a torch image' for data type <class 'torch.Tensor'>
如问题所示,我正在尝试将 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)
但是我在
---> 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.
这是因为截至目前,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 个通道(您没有 "normalise" 批次维度,只有空间维度ones),所以不要使用长度为 5 的 tuple
,而是 (0.5, 0.5, 0.5)
.
如问题所示,我正在尝试将 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)
但是我在
---> 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.
这是因为截至目前,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 个通道(您没有 "normalise" 批次维度,只有空间维度ones),所以不要使用长度为 5 的 tuple
,而是 (0.5, 0.5, 0.5)
.