I’ve trained my own model in pytorch and I want to test it with an unused image for my own training and testing, but it doesn’t work.
from torchvision import transforms
img=(Image.open(r"C:\Users\yasar\Desktop\Pytorch\ferrari.jpg"))
transform = transforms.Compose([ # Defining a variable transforms
transforms.Resize(256), # Resize the image to 256×256 pixels
transforms.CenterCrop(224), # Crop the image to 224×224 pixels about the center
transforms.ToTensor(), # Convert the image to PyTorch Tensor data type
transforms.Normalize( # Normalize the image
mean=[0.485, 0.456, 0.406], # Mean and std of image as also used when training the network
std=[0.229, 0.224, 0.225]
)])
img_t = transform(img)
img_t.shape
batch_t = torch.unsqueeze(img_t, 0)
model.eval()
out = model(batch_t)
out.shape
RuntimeError: shape ‘[1, 519840]’ is invalid for input of size 20000
What were the input shapes during training for this model?
Based on the error message you’ve posted, I assume you are running into a shape mismatch inside the model’s forward
(maybe in a linear layer)?
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 ```
28*28 size