RuntimeError: The size of tensor a (38) must match the size of tensor b (34) at non-singleton dimension 3

Question:

I studied Resnet 50 using cifar-10

but, I faced RuntimeError.

Here is code

class BasicBlock(nn.Module):
    def __init__(self, in_planes, planes, stride = 1):
        super(BasicBlock, self).__init__()

        self.conv1 = nn.Conv2d(in_planes, planes, kernel_size = 1, stride = stride, padding = 1, bias = False)
        self.bn1 = nn.BatchNorm2d(planes)

        self.conv2 = nn.Conv2d(planes, planes, kernel_size = 3, stride = 1, padding = 1, bias = False)
        self.bn2 = nn.BatchNorm2d(planes)

        self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size = 1, stride = 1, padding = 1, bias = False)
        self.bn3 = nn.BatchNorm2d(planes * 4)

        if stride != 1:
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_planes, planes, kernel_size = 1, stride = stride, bias = False),
                nn.BatchNorm2d(planes)
            )
        else:
            self.shortcut = nn.Sequential()

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.bn2(self.conv2(out))
        out = self.bn3(self.conv3(out))
        out += self.shortcut(x) #shortcut connection
        out = F.relu(out)

and Error is

RuntimeError: The size of tensor a (38) must match the size of tensor b (34) at non-singleton dimension 3

How can I fix it?

Asked By: KongGan

||

Answers:

You don’t want padding in your self.conv1 nor self.conv3, because you’ll be increasing your image size by 2 (1 each size) each time. Padding should only be used to avoid reducing your image size when using a kernel size of above 1.

Answered By: jhso
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.