PyTorch Dataloader-枚举时列表不可调用错误

时间:2019-01-30 00:28:12

标签: pytorch

在PyTorch数据加载器上进行迭代时,例如

# define dataset, dataloader
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=64)

# define model, optimizer, loss
# not included - irrelevant to the question

for ii, (inputs, labels) in enumerate(trainloader):

    # Move input and label tensors to the GPU
    inputs, labels = inputs.to(device), labels.to(device)

    start = time.time()

    outputs = model.forward(inputs)
    loss = criterion(outputs, labels)
    loss.backward()

我在这行上得到一个TypeError: 'list' object is not callable

for ii, (inputs, labels) in enumerate(trainloader):

我忘记了什么愚蠢的事情?

1 个答案:

答案 0 :(得分:0)

您还记得在转换列表上调用过transforms.Compose吗?

在这一行

train_data = datasets.ImageFolder(data_dir + '/train', transform=train_transforms)

transform参数需要可调用的对象,而不是列表。

例如,这是错误的:

train_transforms = [
    transforms.RandomResizedCrop(224),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]

应该看起来像这样

train_transforms = transforms.Compose([
    transforms.RandomResizedCrop(224),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])