ResNet Pretrained model with last FC layer stripped does not work

I have been trying to use the pretrained resnet model except for the last fc layer. So I created the self.resnet as follows.

class SimpleNetwork(nn.Module):
    def __init__(self):
        super(SimpleNetwork, self).__init__()
        # Remove the final FC layer of the pretrained Resnet
        self.pretrained_resnet = models.resnet50(pretrained=True).cuda()
        self.resnet = nn.Sequential(*list(self.pretrained_resnet.modules())[:-1]).cuda()

    def forward(self, x):
        # x : (batch_size, C, H, W)
        x = torch.randn(50, 3, 224, 224).cuda()
        x = self.resnet(x)

However, it seems like it is generating the weird error

RuntimeError: input has less dimensions than expected

Note that changing self.resnet to self.pretrained_resnet worked as fine. I don’t understand what can possibly happen by removing one last layer of resnet? Any thoughts?

Use

self.resnet = nn.Sequential(*list(self.pretrained_resnet.children())[:-1]).cuda()

resnet.modules() returns all modules recursively, but you only want the immediate children of resnet