I can't predict new (single) instances with your PyTorch model

import torch
from torchvision.transforms import transforms
from PIL import Image
from pathlib import Path

trans = transforms.Compose([
    transforms.RandomHorizontalFlip(),
    transforms.Resize(32),
    transforms.CenterCrop(32),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
    ])

image = Image.open(Path(r'C:\Users\yasar\Desktop\as\RedBull07841.jpg'))
model.eval()

input = trans(image)

input = input.view(1, 3, 32,32)

output = model(input)

prediction = int(torch.max(output.data, 1)[1].numpy())
print(prediction)

if (prediction == 0):
    print ('ferrari')
if (prediction == 1):
    print ('mclaren')
if (prediction == 2):
    print ('mercedes')
if (prediction == 3):
    print ('redbull')

reesul : RuntimeError: shape ‘[1, 519840]’ is invalid for input of size 32

Could you post more information about the model and where the error is raised as it’s currently a bit unclear what exactly is failing?

class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        
        self.conv1=nn.Conv2d(in_channels=3,out_channels=4,kernel_size=(5,5))
        self.conv2=nn.Conv2d(in_channels=4,out_channels=8,kernel_size=(3,3))
        self.conv3=nn.Conv2d(in_channels=8,out_channels=16,kernel_size=(2,2))#22
        self.conv4=nn.Conv2d(in_channels=16,out_channels=32,kernel_size=(2,2))#10
        
        self.max=nn.MaxPool2d(kernel_size=(2,2))
        self.func=nn.ELU()
        
        self.fc1=nn.Linear(in_features=519840,out_features=50)
        #self.fc1=nn.Linear(in_features=32*100*100,out_features=50)
        self.fc2=nn.Linear(in_features=50,out_features=50)
        self.fc3=nn.Linear(in_features=50,out_features=100)
        self.fc4=nn.Linear(in_features=100,out_features=4)
        
    def forward(self,x):
        
        x=self.conv1(x)
        x=self.func(x)
        
        x=self.max(x)
        
        x=self.conv2(x)
        x=self.func(x)
        
        x=self.max(x)
        
        x=self.conv3(x)
        x=self.func(x)
        
        x=self.max(x)
        
        x=self.conv4(x)
        x=self.func(x)

        #x=x.view(x.size(0), -1)
        x = x.view(x.size(0),519840) 
        
        x=self.fc1(x)
        x=self.func(x)
        x=self.fc2(x)
        x=self.func(x)
        x=self.fc3(x)
        x=self.func(x)
        x=self.fc4(x)
        return x  

i did model training and i want to test it on a single image and i found this code but it doesn’t work what should i do. error pops up when testing

Thanks for the code.
Based on the previously posted input shape of [1, 3, 32, 32] you will run into a shape mismatch error in the view operation and then in self.fc1.
Replace them with x = x.view(x.size(0), -1) and self.fc1 = nn.Linear(in_features=32,out_features=50) and it will work:

model = Net()
x = torch.randn(1, 3, 32, 32)
out = model(x)
model.eval()
out = model(x)

I did as you said, but this time I got mat1 and mat2 shape errors. I got this error before and you said I need to change the input value for fc1 with the number in the error and it worked