How could I load pretrained parameters partially

I tried to construct a resnet50 model like this:


resnet50_url = 'https://download.pytorch.org/models/resnet50-19c8e357.pth'

class Backbone(nn.Module):
    def __init__(self, *args, **kwargs):
        super(Backbone, self).__init__(*args, **kwargs)
        resnet50 = torchvision.models.resnet50()

        self.conv1 = resnet50.conv1
        self.bn1 = resnet50.bn1
        self.relu = resnet50.relu
        self.maxpool = resnet50.maxpool
        self.layer1 = resnet50.layer1
        self.layer2 = resnet50.layer2
        self.layer3 = resnet50.layer3
        self.layer4 = resnet50.layer4
        self.bn2 = nn.BatchNorm1d(2048)
        self.dp = nn.Dropout(0.5)
        self.fc = nn.Linear(in_features = 2048, out_features = 1024, bias = True)
        self.bn3 = nn.BatchNorm1d(1024)

        w1 = self.conv1.weight.detach().numpy()
        print(w1[1][1][1])
        state = model_zoo.load_url(resnet50_url)
        for k, v in state.items():
            if 'fc' in k:
                continue
            self.state_dict().update({k: v})
        w2 = self.conv1.weight.detach().numpy()
        print(w2[1][1][1])
        w3 = state['conv1.weight'][1][1][1].detach().numpy()
        print(w3)

When I tried to construct this model, I have result like this:

net = Backbone()
[-0.011732 ...]
[-0.011732 ...]
[-0.07062116...]

Which means that the pretrained parameters are not loaded. What is the correct way to do this then ?

Iā€™m not sure I understand the issue.
It seems you are creating a resnet50 inside your model without the pretrained weigths.
If you want the pretrained model, you have to pass pretrained=True to the instantiation:

resnet50 = torchvision.models.resnet50(pretrained=True)

I am actually borrowing the first few blocks of resnet50 and append a few of m own layers to their rear to create my own model. I need the resnet potion of the model to use pretrained weight, so I load the weights from some url and update the state_dict of my new created model with these url weights:

state = model_zoo.load_url(resnet50_url)
        for k, v in state.items():
            if 'fc' in k:
                continue
            self.state_dict().update({k: v})

But it turns out that the weights are not updated at all. So what is my fault doing this ? And is my best choice actually is simply assign a pretrained=True to my borrowing resnet50 ?

Thanks for clarifying the use case!
There are some minor issues in the code.
You should store the state_dict, update it, and reload it afterwards:

    def __init__(...):
        ...
        w1 = self.conv1.weight.detach().numpy()
        print(w1[1][1][1])
        state = model_zoo.load_url(resnet50_url)
        state_dict = self.state_dict()
        for k, v in state.items():
            if 'fc' in k:
                continue
            state_dict.update({k: v})
        self.load_state_dict(state_dict)
        w2 = self.conv1.weight.detach().numpy()
        print(w2[1][1][1])
        w3 = state['conv1.weight'][1][1][1].detach().numpy()
        print(w3)
3 Likes