TypeError:张量不是火炬图像

时间:2018-08-12 08:21:58

标签: python pytorch

在Udacity学习AI课程时,我在“转移学习”部分遇到了此错误。这是似乎引起麻烦的代码:

import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models

data_dir = 'filename'

# TODO: Define transforms for the training data and testing data
train_transforms= transforms.Compose([transforms.Resize((224,224)), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), transforms.ToTensor()])
test_transforms= transforms.Compose([transforms.Resize((224,224)), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), transforms.ToTensor()])

# Pass transforms in here, then run the next cell to see how the transforms look
train_data = datasets.ImageFolder(data_dir + '/train', transform=train_transforms)
test_data = datasets.ImageFolder(data_dir + '/test', transform=test_transforms)

trainloader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)
testloader = torch.utils.data.DataLoader(test_data, batch_size=32)

4 个答案:

答案 0 :(得分:11)

问题在于变换的顺序。 ToTensor变换应该先于Normalize变换,因为后者期望张量,但是Resize变换会返回图像。更改错误行的正确代码:

train_transforms = transforms.Compose([
    transforms.Resize((224,224)), 
    transforms.ToTensor(), 
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
test_transforms = transforms.Compose([
    transforms.Resize((224,224)), 
    transforms.ToTensor(), 
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])

答案 1 :(得分:1)

另一种不太优雅的解决方案(假设该图像已加载opencv,因此是BGR):

t_ = transforms.Compose([transforms.ToPILImage(),
                         transforms.Resize((224,224)),
                         transforms.ToTensor()])

norm_ = transforms.Normalize([103.939, 116.779, 123.68],[1,1,1])
img = 255*t_(img)
img = norm_(img)

答案 2 :(得分:0)

此错误的一个原因是privateLookupIn仅接受3d数据(3、224、224)。这是示例代码:

transforms.Normalize

由于输入数据具有4d形状,因此将引发错误。如果将代码更改为此,则错误将消失。

# imagenet normalize

from torchvision.transforms import Normalize

mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize = Normalize(mean, std)


img = np.random.choice(255, (10, 224, 224, 3))
img = img/255 # [0, 1]
img = torch.tensor(img, device=device).float().permute(0, 3, 1, 2)
img = normalize(img)

答案 3 :(得分:0)

就我而言,我遇到了 the comment of shark deng 中描述的问题:normalize 在批处理中不起作用。

我的一个解决方案是改用 {{1}},它适用于批处理。