Input size and target size is not matching

batch size = 4

image size = 128

loss = MSELoss

I am trying to generate 6 numeric values from an image of size 128x128 using the below CNN model.
I am getting a warning

Using a target size (torch.Size([4, 6])) that is different to the input size (torch.Size([1, 6])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.

I read many discussions here but sorry I am not getting it. please help me.

class MultiLabelNN(nn.Module):

    def __init__(self):

        super(MultiLabelNN, self).__init__()

        self.conv1 = nn.Conv2d(3,64, 5)

        self.pool = nn.MaxPool2d(2,2)

        self.relu = nn.ReLU()

        self.sigmoid = nn.Sigmoid()

        self.conv2 = nn.Conv2d(64, 128, 5)        

        self.conv3 = nn.Conv2d(128, 256, 5)

        self.conv4 = nn.Conv2d(256,320,5)

        self.fc1 = nn.Linear(20480,2048)

        self.fc2 = nn.Linear(2048, 1024)

        self.fc3 = nn.Linear(1024, 512)

        self.fc4 = nn.Linear(512, 256)

        self.fc5 = nn.Linear(256, 6)

    def forward(self, x):

        x = self.conv1(x)

        x = self.relu(x)    

        x = self.pool(x)  

        x = self.conv2(x)

        x = self.relu(x)

        x = self.pool(x)

        x = self.conv3(x)

        x = self.relu(x)

        x = self.pool(x)

        x = self.conv4(x)

        x = self.relu(x)

        x = self.pool(x)        

        x = x.view(-1, 20480)

        x = self.fc1(x)

        x = self.relu(x)        

        x = self.fc2(x)

        x = self.relu(x)      

        x = self.fc3(x)

        x = self.relu(x)

        x = self.fc4(x)

        x = self.relu(x)

        x = self.fc5(x)

        x = self.sigmoid(x)

        return x

I guess you might be changing the batch size of the intermediate activations using:

x = x.view(-1, 20480)

Replace this line of code with x = x.view(x.size(0), -1) to flatten in the feature dimension and check if you would be running into shape mismatches in the following linear layers.

1 Like

Thank @ptrblck for your reponse.

Now I am getting
RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x5120 and 20480x2048)

OK, that’s great as I have expected to see the shape mismatch in the next linear layer.
Change:

self.fc1 = nn.Linear(20480,2048)

to:

self.fc1 = nn.Linear(5120, 2048)

and it should work.

1 Like

As PyTorch based on dynamic graph, how to explicitly determine the input dims of Linear layer is the common problem for each novice. :rofl:

1 Like

If you don’t like to calculate the shape (often not trivial) or don’t want to use the reported shape in the error message, the nn.Lazy* layers are here to help which will calculate the shapes for you :wink:

1 Like

it is working now. Thanks a lot @ptrblck you are a life savior :slightly_smiling_face:

You are absolutely right buddy :grin:
I am from that group :stuck_out_tongue: