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 ?