Size mismatch when using intermediate layers of VGG-19

Hello.

I am trying to only use the first 20 layers of VGG-19 for a model.

model_vgg = torchvision.models.vgg19(pretrained=True).to(device)

class VGG_Custom(nn.Module):
    def __init__(self):
        super(VGG_Custom, self).__init__()
        self.features = nn.Sequential(*list(model_vgg.children())[:20])
        
    def forward(self, x):
        x = self.features(x)
        return x
      
VGG_model = VGG_Custom().to(device)

However, when I try to run my input through it I get the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-23-87dcc2b9edab> in <module>()
      4 
      5 for epoch in range(1, args.epochs + 1):
----> 6     train(args, model, device, train_loader, optimizer, epoch)
      7     test(args, model, device, test_loader, epoch)
      8 

<ipython-input-22-0c3b98a032b1> in train(args, model, device, train_loader, optimizer, epoch)
     46       #print(model_vgg(output).shape)
     47       #print(output.shape)
---> 48       loss = F.mse_loss(VGG_model(output), VGG_model(data))
     49       loss.backward()
     50       optimizer.step()

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

<ipython-input-21-497320d08a09> in forward(self, x)
      8 
      9     def forward(self, x):
---> 10         x = self.features(x)
     11         print(x.shape)
     12         return x

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py in forward(self, input)
     90     def forward(self, input):
     91         for module in self._modules.values():
---> 92             input = module(input)
     93         return input
     94 

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py in forward(self, input)
     90     def forward(self, input):
     91         for module in self._modules.values():
---> 92             input = module(input)
     93         return input
     94 

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/linear.py in forward(self, input)
     65     @weak_script_method
     66     def forward(self, input):
---> 67         return F.linear(input, self.weight, self.bias)
     68 
     69     def extra_repr(self):

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1352         ret = torch.addmm(torch.jit._unwrap_optional(bias), input, weight.t())
   1353     else:
-> 1354         output = input.matmul(weight.t())
   1355         if bias is not None:
   1356             output += torch.jit._unwrap_optional(bias)

RuntimeError: size mismatch, m1: [57344 x 7], m2: [25088 x 4096] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:266

My input is a tensor of dimension [16, 3, 256, 256].

Found the answer!

The line
self.features = nn.Sequential(*list(model_vgg.children())[:20])
should actually be
self.features = nn.Sequential(*list(model_vgg.children())[0][:20])

I only wanted to extract the children form the first sequential layer of VGG-19 that has 3 layers(Sequential, AdaptiveAvgPool2d, Sequential).