以下示例来自
https://github.com/hugo1840/Pytorch_tutorial/blob/master/classification_cifar10.py
此代码成功训练。我运行了这段代码并保存了模型。
现在,我要预测自己的形象。我使用了以下代码
import torch
from mod1 import Net
from PIL import Image
import numpy
image = Image.open("plane.png")
pix = numpy.array(image) #convert image to numpy array
image.show()
net = Net()
net.eval()
img = torch.Tensor(pix) #convert numpy array to tensor
net = torch.load('pytorch_Network.h5')
print(net(img))
但是我得到了这个错误:
Traceback (most recent call last):
File "/home/ihor/Tasks/try1/pytorch_load_model.py", line 14, in <module>
print(net(img))
File "/home/ihor/anaconda3/envs/tensorflow/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "/home/ihor/Tasks/try1/pytorch.py", line 51, in forward
x = self.pool(F.relu(self.conv1(x)))
File "/home/ihor/anaconda3/envs/tensorflow/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "/home/ihor/anaconda3/envs/tensorflow/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 301, in forward
self.padding, self.dilation, self.groups)
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [6, 3, 5, 5], but got input of size [368, 860] instead
很抱歉,如果这是一个基本问题,但由于某种原因,我在网上找不到太多的方法来指导我。我已经在Google上搜索了很多,阅读了不同的文章,但是没有任何帮助。
谢谢