运行 使用 PyTorch 的单个预测问题

Issue with running a single prediction with PyTorch

我有一个使用 PyTorch 训练的模型,现在我想在一个例子中简化 运行 它

>>> model
nn.Sequential {
  [input -> (0) -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> output]
  (0): nn.SpatialConvolutionMap
  (1): nn.Tanh
  (2): nn.SpatialMaxPooling(2x2, 2, 2)
  (3): nn.SpatialConvolutionMap
  (4): nn.Tanh
  (5): nn.SpatialMaxPooling(2x2, 2, 2)
  (6): nn.Reshape(6400)
  (7): nn.Linear(6400 -> 128)
  (8): nn.Tanh
  (9): nn.Linear(128 -> 5)
  (10): nn.LogSoftMax
}

然后我从我的测试集中加载一张图像:

image = cv2.imread('image.png',cv2.IMREAD_GRAYSCALE)
transformation = transforms.Compose([transforms.ToTensor()]) 
image_tensor = transformation(image).float()
inp = Variable(image_tensor)

最后尝试 运行 网络

output = model(inp) 

但是我收到错误 TypeError: 'Sequential' object is not callable

您的模型似乎不是 nn.Sequential (pytorch Sequential), but rather torch.legacy.nn.Sequential(传统的 lua 手电筒模型)。
尝试明确使用此模型 forward()

output = model.forward(inp[None, ...])  # don't forget to add "batch" dimension