Hi,
I changed resnet-18 model and I want to load weights from pretrained-models only for layer3 and layer4 of resnet-18. is the following piece of code, right way to do it ?
base_model = 'pretrainedmodels/resnet18-5c106cde.pth' model = net.ResNet(net.BasicBlock, [2,2,2,2], num_classes=1000).cuda() if params.cuda else net.ResNet(net.BasicBlock, [2,2,2,2], num_classes=1000) model.load_state_dict(torch.load(base_model)) base_model = '/pretrainedmodels/resnet18-5c106cde.pth' other_model = net.ResNet(net.BasicBlock, [2,2,2,2], num_classes=1000).cuda() if params.cuda else net.ResNet(net.BasicBlock, [2,2,2,2], num_classes=1000) other_model.load_state_dict(torch.load(base_model)) model.layer3 = net.Inception() model.layer3.paral_0[0].load_state_dict(other_model.layer3[0].state_dict()) model.layer3.paral_0[1].load_state_dict(other_model.layer3[1].state_dict()) model.layer4 = net.Inception1() model.layer4.paral_0[0].load_state_dict(other_model.layer4[0].state_dict()) model.layer4.paral_0[1].load_state_dict(other_model.layer4[1].state_dict()) ....
side note: I defined incpetion() and inception1() exactly same as layer3 and layer4 of resnet-18 respectively. so there is not dimension mismatch between them(as shown in the following)
Inception(
(paral_0): Sequential(
(0): BasicBlock0(
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(128, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock1(
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
)
Inception1(
(paral_0): Sequential(
(0): BasicBlock2(
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(128, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock3(
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
)