When i try to make predictions after i have trained my network RuntimeError: shape '[-1, 12544]' is invalid for input of size 6400

class SimpleCNN(torch.nn.Module):
  def __init__(self, input_ch, output):
    super(SimpleCNN, self).__init__()
    self.cnn1 = torch.nn.Conv2d(in_channels=input_ch, out_channels=16, kernel_size=[3,3],stride=1, padding=1)
    self.cnn2 = torch.nn.Conv2d(in_channels=16, out_channels=32, kernel_size=[3,3],stride=1, padding=1)
    self.cnn3 = torch.nn.Conv2d(in_channels=32, out_channels=32, kernel_size=[3,3], padding=1)
    self.cnn4 = torch.nn.Conv2d(in_channels=32, out_channels=64, kernel_size=[3,3], padding=1)
    self.cnn5 = torch.nn.Conv2d(in_channels=64, out_channels=128, kernel_size=[3,3], padding=1)
    self.cnn6 = torch.nn.Conv2d(in_channels=128, out_channels=256, kernel_size=[3,3], padding=1)
    self.pool = torch.nn.MaxPool2d(kernel_size=[3,3])
    self.Bnorm = torch.nn.BatchNorm2d(num_features=256)
    self.drop = torch.nn.Dropout2d(p=0.3)
    self.lin1 = torch.nn.Linear(256*7*7, out_features=512)
    self.lin2 = torch.nn.Linear(512, output)

  def forward(self, x):
    x = F.relu(self.cnn1(x))
    x = self.pool(F.relu(self.cnn2(x)))
    x = F.relu(self.cnn3(x))
    x = self.pool(F.relu(self.cnn4(x)))
    x = F.relu(self.cnn5(x))
    x = self.pool(F.relu(self.cnn6(x)))
    x = self.Bnorm(x)
    x = self.drop(x)
    x = x.view(-1, 256*7*7)
    x = F.relu(self.lin1(x))
    x = self.lin2(x)
    return x
    
model = SimpleCNN(3, 29)

My prediction (does this problem only for single image prediction)

#validation mode on torch, also there is a mode to turn the model into val() mode
correct = 0
total = 0
with torch.no_grad():
    for data in test_dataLoader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print('Accuracy of the network on the 20 test images: %d %%' % (
    100 * correct / total))

Could you replace this line:

x = x.view(-1, 256*7*7)

with

x = x.view(x.size(0), -1)
print(x.shape)

I guess you might not be using the same preprocessing pipeline and thus your images might have a different spatial shape during evaluation.
Let me know, if that’s the case or if you still see an error.

Hello, thanks for the reply, i solved the issue that same day, the problem was i changed my input image size and i had to reconfigure my linear laryer again. Thank you again