我有一个使用PyTorch训练有素的模型,现在我想简单地在一个示例上运行它
FooBar fb = FooBar.combine(p::fooA, p::barA);
fb = FooBar.combine(p::fooA, p::barB);
// and so on
然后我从测试集中加载图像:
>>> 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)
但是出现错误 TypeError:“顺序”对象不可调用
答案 0 :(得分:0)
您的模型似乎不是nn.Sequential
( py 火炬Sequential
),而是torch.legacy.nn.Sequential
(传统的lua火炬模型)。
尝试明确使用此模型forward()
:
output = model.forward(inp[None, ...]) # don't forget to add "batch" dimension