VGG16 error: output size is too small

Hello everyone
I try to train a little bit modified VGG16 network but I stuck with an following error:
RuntimeError: Given input size: (128x1x1). Calculated output size: (128x0x0). Output size is too small.
I suspect that it’s something with chosen size of kernel or padding but I’m not sure what and how to choose these values better.
As an input I take tensors of shape [N, 256, 3, 3]. As an output I just wish to have a single value.
My modified code is here:

    net = models.vgg16(pretrained=True)  
    first_conv_layer = [nn.Conv2d(256, 3, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True)]
    first_conv_layer.extend(list(net.features))
    net.features = nn.Sequential(*first_conv_layer)
    last_linear = [nn.Linear(1000, 1)]
    last_linear.extend(list(net.classifier))
    last_linear = last_linear[1:] + [last_linear[0]]
    net.classifier = nn.Sequential(*last_linear)

I’ll be glad for any help! Cheers.

I think this may caused by your input is too small, both vgg models have 2^5 downsample then pass by a nn.AdaptiveAvgPool2d((7, 7)) that’s mean your input (h,w) at least is 7*2^5 = 224.

2 Likes

Thanks! That was a solution :wink: