Hi everybody,
I want to predict different images using my trained network.
For some reasons this code works only with one image, if I want to use different others images this doesn’t work.
I got the error:
RuntimeError: invalid argument 2: size '[-1 x 400]' is invalid for input with 880 elements at /opt/conda/conda-bld/pytorch_1532579805626/work/aten/src/TH/THStorage.cpp:80
So why this works only with one picture?
This is full code here:
import torch
from pytorch import Net
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
transform = transforms.Compose([
transforms.Resize(32),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
image = Image.open("plane.png").convert('RGB')
x = transform(image)
x = x.unsqueeze(0)
pix = np.array(x) #convert image to numpy array
print((image))
image.show()
net = Net()
net.eval()
img = torch.from_numpy(pix)
net = torch.load('pytorch_Network2.h5')
idx_to_class = {
0: 'airplane',
1: 'automobile',
2: 'bird',
3: 'cat',
4: 'deer',
5: 'dog',
6: 'frog',
7: 'horse',
8: 'ship',
9: 'truck'
}
output = net(img)
pred = torch.argmax(output, 1)
for p in pred:
cls = idx_to_class[p.item()]
print(cls)