Extract features from ResNet152 pretrained model

Hello,

l would like to extract features (last fully connected layer) from a fine tuned pretrained resnet152 on my own dataset.

In order to extract features from the original pretrained resnet152 model l did simply the following :


resnet152 = models.resnet152(pretrained=True)
modules = list(resnet152.children())[:-1]
resnet152 = nn.Sequential(*modules)
resnet152.cuda()
resnet152.eval()
for name in files:
        open_img = Image.open(os.path.join(root, name))
        image = ToTensor()(open_img).unsqueeze(0)
        image = image.cuda()
        img_var = Variable(image)
        features_var = resnet152(img_var)

However, when l train Resnet152 from scratch on my custom dataset. I store my model as model_best.pth.tar
And load it as follow :
resnet152=torch.load('/local/model_best.pth.tar')
but can’t get children by doing :
modules = list(resnet152.children())[:-1]
l tried :
resnet152=resnet152[u'state_dict']

However, how can l specify that l want to extract features from the last layer ?

l tried then to do that with hook function as follow :

activation = {}
def get_activation(name):
    def hook(model, input, output):
        activation[name] = output.detach()

    return hook

But, resnet152.fc.register_forward_hook(get_activation('fc')) is not recognized

Any cue please !!

How did you save your model? Did you save the entire model or its state_dict?
Could you print the result of torch.load('model_best.pth.tar')?

Hi @ptrblck,

I saved my model as follow :

        save_checkpoint({
            'epoch': epoch + 1,
            'arch': args.arch,
            'state_dict': model.state_dict(),
            'best_prec1': best_prec1,
            'optimizer': optimizer.state_dict(),
        }, is_best)

From
resnet152=torch.load('model_best.pth.tar')
l have the following options :
resnet152.clear
resnet152.copy
resnet152.fromkeys
resnet152.get
resnet152.has_key
resnent152.items
resnet152.iteritems
resnet152.iterkeys
resnet152.intervalues
resnet152.keys
resnet152.pop
resnet152.popitem
resnet152.setdefault
resnet152.update
resnet152.values
resnet152viewtimes
resnet152.viewkeys
resnet152.viewvalues

For instance
resnet152.keys() returns
[u’state_dict’, u’epoch’, u’arch’, u’optimizer’, u’best_prec1’]

and
my_dict=resnet152[u’state_dict’]
my_dict.keys() returns

[u’module.conv1.weight’, u’module.bn1.weight’, u’module.bn1.bias’, u’module.bn1.running_mean’, u’module.bn1.running_var’, u’module.layer1.0.conv1.weight’, u’module.layer1.0.bn1.weight’, u’module.layer1.0.bn1.bias’, u’module.layer1.0.bn1.running_mean’, u’module.layer1.0.bn1.running_var’, u’module.layer1.0.conv2.weight’, u’module.layer1.0.bn2.weight’, u’module.layer1.0.bn2.bias’, u’module.layer1.0.bn2.running_mean’, u’module.layer1.0.bn2.running_var’, u’module.layer1.0.conv3.weight’, u’module.layer1.0.bn3.weight’, u’module.layer1.0.bn3.bias’, u’module.layer1.0.bn3.running_mean’, u’module.layer1.0.bn3.running_var’, u’module.layer1.0.downsample.0.weight’, u’module.layer1.0.downsample.1.weight’, u’module.layer1.0.downsample.1.bias’, u’module.layer1.0.downsample.1.running_mean’, u’module.layer1.0.downsample.1.running_var’, u’module.layer1.1.conv1.weight’, u’module.layer1.1.bn1.weight’, u’module.layer1.1.bn1.bias’, u’module.layer1.1.bn1.running_mean’, u’module.layer1.1.bn1.running_var’, u’module.layer1.1.conv2.weight’, u’module.layer1.1.bn2.weight’, u’module.layer1.1.bn2.bias’, u’module.layer1.1.bn2.running_mean’, u’module.layer1.1.bn2.running_var’, u’module.layer1.1.conv3.weight’, u’module.layer1.1.bn3.weight’, u’module.layer1.1.bn3.bias’, u’module.layer1.1.bn3.running_mean’, u’module.layer1.1.bn3.running_var’, u’module.layer1.2.conv1.weight’, u’module.layer1.2.bn1.weight’, u’module.layer1.2.bn1.bias’, u’module.layer1.2.bn1.running_mean’, u’module.layer1.2.bn1.running_var’, u’module.layer1.2.conv2.weight’, u’module.layer1.2.bn2.weight’, u’module.layer1.2.bn2.bias’, u’module.layer1.2.bn2.running_mean’, u’module.layer1.2.bn2.running_var’, u’module.layer1.2.conv3.weight’, u’module.layer1.2.bn3.weight’, u’module.layer1.2.bn3.bias’, u’module.layer1.2.bn3.running_mean’, u’module.layer1.2.bn3.running_var’, u’module.layer2.0.conv1.weight’, u’module.layer2.0.bn1.weight’, u’module.layer2.0.bn1.bias’, u’module.layer2.0.bn1.running_mean’, u’module.layer2.0.bn1.running_var’, u’module.layer2.0.conv2.weight’, u’module.layer2.0.bn2.weight’, u’module.layer2.0.bn2.bias’, u’module.layer2.0.bn2.running_mean’, u’module.layer2.0.bn2.running_var’, u’module.layer2.0.conv3.weight’, u’module.layer2.0.bn3.weight’, u’module.layer2.0.bn3.bias’, u’module.layer2.0.bn3.running_mean’, u’module.layer2.0.bn3.running_var’, u’module.layer2.0.downsample.0.weight’, u’module.layer2.0.downsample.1.weight’, u’module.layer2.0.downsample.1.bias’, u’module.layer2.0.downsample.1.running_mean’, u’module.layer2.0.downsample.1.running_var’, u’module.layer2.1.conv1.weight’, u’module.layer2.1.bn1.weight’, u’module.layer2.1.bn1.bias’, u’module.layer2.1.bn1.running_mean’, u’module.layer2.1.bn1.running_var’, u’module.layer2.1.conv2.weight’, u’module.layer2.1.bn2.weight’, u’module.layer2.1.bn2.bias’, u’module.layer2.1.bn2.running_mean’, u’module.layer2.1.bn2.running_var’, u’module.layer2.1.conv3.weight’, u’module.layer2.1.bn3.weight’, u’module.layer2.1.bn3.bias’, u’module.layer2.1.bn3.running_mean’, u’module.layer2.1.bn3.running_var’, u’module.layer2.2.conv1.weight’, u’module.layer2.2.bn1.weight’, u’module.layer2.2.bn1.bias’, u’module.layer2.2.bn1.running_mean’, u’module.layer2.2.bn1.running_var’, u’module.layer2.2.conv2.weight’, u’module.layer2.2.bn2.weight’, u’module.layer2.2.bn2.bias’, u’module.layer2.2.bn2.running_mean’, u’module.layer2.2.bn2.running_var’, u’module.layer2.2.conv3.weight’, u’module.layer2.2.bn3.weight’, u’module.layer2.2.bn3.bias’, u’module.layer2.2.bn3.running_mean’, u’module.layer2.2.bn3.running_var’, u’module.layer2.3.conv1.weight’, u’module.layer2.3.bn1.weight’, u’module.layer2.3.bn1.bias’, u’module.layer2.3.bn1.running_mean’, u’module.layer2.3.bn1.running_var’, u’module.layer2.3.conv2.weight’, u’module.layer2.3.bn2.weight’, u’module.layer2.3.bn2.bias’, u’module.layer2.3.bn2.running_mean’, u’module.layer2.3.bn2.running_var’, u’module.layer2.3.conv3.weight’, u’module.layer2.3.bn3.weight’, u’module.layer2.3.bn3.bias’, u’module.layer2.3.bn3.running_mean’, u’module.layer2.3.bn3.running_var’, u’module.layer2.4.conv1.weight’, u’module.layer2.4.bn1.weight’, u’module.layer2.4.bn1.bias’, u’module.layer2.4.bn1.running_mean’, u’module.layer2.4.bn1.running_var’, u’module.layer2.4.conv2.weight’, u’module.layer2.4.bn2.weight’, u’module.layer2.4.bn2.bias’, u’module.layer2.4.bn2.running_mean’, u’module.layer2.4.bn2.running_var’, u’module.layer2.4.conv3.weight’, u’module.layer2.4.bn3.weight’, u’module.layer2.4.bn3.bias’, u’module.layer2.4.bn3.running_mean’, u’module.layer2.4.bn3.running_var’, u’module.layer2.5.conv1.weight’, u’module.layer2.5.bn1.weight’, u’module.layer2.5.bn1.bias’, u’module.layer2.5.bn1.running_mean’, u’module.layer2.5.bn1.running_var’, u’module.layer2.5.conv2.weight’, u’module.layer2.5.bn2.weight’, u’module.layer2.5.bn2.bias’, u’module.layer2.5.bn2.running_mean’, u’module.layer2.5.bn2.running_var’, u’module.layer2.5.conv3.weight’, u’module.layer2.5.bn3.weight’, u’module.layer2.5.bn3.bias’, u’module.layer2.5.bn3.running_mean’, u’module.layer2.5.bn3.running_var’, u’module.layer2.6.conv1.weight’, u’module.layer2.6.bn1.weight’, u’module.layer2.6.bn1.bias’, u’module.layer2.6.bn1.running_mean’, u’module.layer2.6.bn1.running_var’, u’module.layer2.6.conv2.weight’, u’module.layer2.6.bn2.weight’, u’module.layer2.6.bn2.bias’, u’module.layer2.6.bn2.running_mean’, u’module.layer2.6.bn2.running_var’, u’module.layer2.6.conv3.weight’, u’module.layer2.6.bn3.weight’, u’module.layer2.6.bn3.bias’, u’module.layer2.6.bn3.running_mean’, u’module.layer2.6.bn3.running_var’, u’module.layer2.7.conv1.weight’, u’module.layer2.7.bn1.weight’, u’module.layer2.7.bn1.bias’, u’module.layer2.7.bn1.running_mean’, u’module.layer2.7.bn1.running_var’, u’module.layer2.7.conv2.weight’, u’module.layer2.7.bn2.weight’, u’module.layer2.7.bn2.bias’, u’module.layer2.7.bn2.running_mean’, u’module.layer2.7.bn2.running_var’, u’module.layer2.7.conv3.weight’, u’module.layer2.7.bn3.weight’, u’module.layer2.7.bn3.bias’, u’module.layer2.7.bn3.running_mean’, u’module.layer2.7.bn3.running_var’, u’module.layer3.0.conv1.weight’, u’module.layer3.0.bn1.weight’, u’module.layer3.0.bn1.bias’, u’module.layer3.0.bn1.running_mean’, u’module.layer3.0.bn1.running_var’, u’module.layer3.0.conv2.weight’, u’module.layer3.0.bn2.weight’, u’module.layer3.0.bn2.bias’, u’module.layer3.0.bn2.running_mean’, u’module.layer3.0.bn2.running_var’, u’module.layer3.0.conv3.weight’, u’module.layer3.0.bn3.weight’, u’module.layer3.0.bn3.bias’, u’module.layer3.0.bn3.running_mean’, u’module.layer3.0.bn3.running_var’, u’module.layer3.0.downsample.0.weight’, u’module.layer3.0.downsample.1.weight’, u’module.layer3.0.downsample.1.bias’, u’module.layer3.0.downsample.1.running_mean’, u’module.layer3.0.downsample.1.running_var’, u’module.layer3.1.conv1.weight’, u’module.layer3.1.bn1.weight’, u’module.layer3.1.bn1.bias’, u’module.layer3.1.bn1.running_mean’, u’module.layer3.1.bn1.running_var’, u’module.layer3.1.conv2.weight’, u’module.layer3.1.bn2.weight’, u’module.layer3.1.bn2.bias’, u’module.layer3.1.bn2.running_mean’, u’module.layer3.1.bn2.running_var’, u’module.layer3.1.conv3.weight’, u’module.layer3.1.bn3.weight’, u’module.layer3.1.bn3.bias’, u’module.layer3.1.bn3.running_mean’, u’module.layer3.1.bn3.running_var’, u’module.layer3.2.conv1.weight’, u’module.layer3.2.bn1.weight’, u’module.layer3.2.bn1.bias’, u’module.layer3.2.bn1.running_mean’, u’module.layer3.2.bn1.running_var’, u’module.layer3.2.conv2.weight’, u’module.layer3.2.bn2.weight’, u’module.layer3.2.bn2.bias’, u’module.layer3.2.bn2.running_mean’, u’module.layer3.2.bn2.running_var’, u’module.layer3.2.conv3.weight’, u’module.layer3.2.bn3.weight’, u’module.layer3.2.bn3.bias’, u’module.layer3.2.bn3.running_mean’, u’module.layer3.2.bn3.running_var’, u’module.layer3.3.conv1.weight’, u’module.layer3.3.bn1.weight’, u’module.layer3.3.bn1.bias’, u’module.layer3.3.bn1.running_mean’, u’module.layer3.3.bn1.running_var’, u’module.layer3.3.conv2.weight’, u’module.layer3.3.bn2.weight’, u’module.layer3.3.bn2.bias’, u’module.layer3.3.bn2.running_mean’, u’module.layer3.3.bn2.running_var’, u’module.layer3.3.conv3.weight’, u’module.layer3.3.bn3.weight’, u’module.layer3.3.bn3.bias’, u’module.layer3.3.bn3.running_mean’, u’module.layer3.3.bn3.running_var’, u’module.layer3.4.conv1.weight’, u’module.layer3.4.bn1.weight’, u’module.layer3.4.bn1.bias’, u’module.layer3.4.bn1.running_mean’, u’module.layer3.4.bn1.running_var’, u’module.layer3.4.conv2.weight’, u’module.layer3.4.bn2.weight’, u’module.layer3.4.bn2.bias’, u’module.layer3.4.bn2.running_mean’, u’module.layer3.4.bn2.running_var’, u’module.layer3.4.conv3.weight’, u’module.layer3.4.bn3.weight’, u’module.layer3.4.bn3.bias’, u’module.layer3.4.bn3.running_mean’, u’module.layer3.4.bn3.running_var’, u’module.layer3.5.conv1.weight’, u’module.layer3.5.bn1.weight’, u’module.layer3.5.bn1.bias’, u’module.layer3.5.bn1.running_mean’, u’module.layer3.5.bn1.running_var’, u’module.layer3.5.conv2.weight’, u’module.layer3.5.bn2.weight’, u’module.layer3.5.bn2.bias’, u’module.layer3.5.bn2.running_mean’, u’module.layer3.5.bn2.running_var’, u’module.layer3.5.conv3.weight’, u’module.layer3.5.bn3.weight’, u’module.layer3.5.bn3.bias’, u’module.layer3.5.bn3.running_mean’, u’module.layer3.5.bn3.running_var’, u’module.layer3.6.conv1.weight’, u’module.layer3.6.bn1.weight’, u’module.layer3.6.bn1.bias’, u’module.layer3.6.bn1.running_mean’, u’module.layer3.6.bn1.running_var’, u’module.layer3.6.conv2.weight’, u’module.layer3.6.bn2.weight’, u’module.layer3.6.bn2.bias’, u’module.layer3.6.bn2.running_mean’, u’module.layer3.6.bn2.running_var’, u’module.layer3.6.conv3.weight’, u’module.layer3.6.bn3.weight’, u’module.layer3.6.bn3.bias’, u’module.layer3.6.bn3.running_mean’, u’module.layer3.6.bn3.running_var’, u’module.layer3.7.conv1.weight’, u’module.layer3.7.bn1.weight’, u’module.layer3.7.bn1.bias’, u’module.layer3.7.bn1.running_mean’, u’module.layer3.7.bn1.running_var’, u’module.layer3.7.conv2.weight’, u’module.layer3.7.bn2.weight’, u’module.layer3.7.bn2.bias’, u’module.layer3.7.bn2.running_mean’, u’module.layer3.7.bn2.running_var’, u’module.layer3.7.conv3.weight’, u’module.layer3.7.bn3.weight’, u’module.layer3.7.bn3.bias’, u’module.layer3.7.bn3.running_mean’, u’module.layer3.7.bn3.running_var’, u’module.layer3.8.conv1.weight’, u’module.layer3.8.bn1.weight’, u’module.layer3.8.bn1.bias’, u’module.layer3.8.bn1.running_mean’, u’module.layer3.8.bn1.running_var’, u’module.layer3.8.conv2.weight’, u’module.layer3.8.bn2.weight’, u’module.layer3.8.bn2.bias’, u’module.layer3.8.bn2.running_mean’, u’module.layer3.8.bn2.running_var’, u’module.layer3.8.conv3.weight’, u’module.layer3.8.bn3.weight’, u’module.layer3.8.bn3.bias’, u’module.layer3.8.bn3.running_mean’, u’module.layer3.8.bn3.running_var’, u’module.layer3.9.conv1.weight’, u’module.layer3.9.bn1.weight’, u’module.layer3.9.bn1.bias’, u’module.layer3.9.bn1.running_mean’, u’module.layer3.9.bn1.running_var’, u’module.layer3.9.conv2.weight’, u’module.layer3.9.bn2.weight’, u’module.layer3.9.bn2.bias’, u’module.layer3.9.bn2.running_mean’, u’module.layer3.9.bn2.running_var’, u’module.layer3.9.conv3.weight’, u’module.layer3.9.bn3.weight’, u’module.layer3.9.bn3.bias’, u’module.layer3.9.bn3.running_mean’, u’module.layer3.9.bn3.running_var’, u’module.layer3.10.conv1.weight’, u’module.layer3.10.bn1.weight’, u’module.layer3.10.bn1.bias’, u’module.layer3.10.bn1.running_mean’, u’module.layer3.10.bn1.running_var’, u’module.layer3.10.conv2.weight’, u’module.layer3.10.bn2.weight’, u’module.layer3.10.bn2.bias’, u’module.layer3.10.bn2.running_mean’, u’module.layer3.10.bn2.running_var’, u’module.layer3.10.conv3.weight’, u’module.layer3.10.bn3.weight’, u’module.layer3.10.bn3.bias’, u’module.layer3.10.bn3.running_mean’, u’module.layer3.10.bn3.running_var’, u’module.layer3.11.conv1.weight’, u’module.layer3.11.bn1.weight’, u’module.layer3.11.bn1.bias’, u’module.layer3.11.bn1.running_mean’, u’module.layer3.11.bn1.running_var’, u’module.layer3.11.conv2.weight’, u’module.layer3.11.bn2.weight’, u’module.layer3.11.bn2.bias’, u’module.layer3.11.bn2.running_mean’, u’module.layer3.11.bn2.running_var’, u’module.layer3.11.conv3.weight’, u’module.layer3.11.bn3.weight’, u’module.layer3.11.bn3.bias’, u’module.layer3.11.bn3.running_mean’, u’module.layer3.11.bn3.running_var’, u’module.layer3.12.conv1.weight’, u’module.layer3.12.bn1.weight’, u’module.layer3.12.bn1.bias’, u’module.layer3.12.bn1.running_mean’, u’module.layer3.12.bn1.running_var’, u’module.layer3.12.conv2.weight’, u’module.layer3.12.bn2.weight’, u’module.layer3.12.bn2.bias’, u’module.layer3.12.bn2.running_mean’, u’module.layer3.12.bn2.running_var’, u’module.layer3.12.conv3.weight’, u’module.layer3.12.bn3.weight’, u’module.layer3.12.bn3.bias’, u’module.layer3.12.bn3.running_mean’, u’module.layer3.12.bn3.running_var’, u’module.layer3.13.conv1.weight’, u’module.layer3.13.bn1.weight’, u’module.layer3.13.bn1.bias’, u’module.layer3.13.bn1.running_mean’, u’module.layer3.13.bn1.running_var’, u’module.layer3.13.conv2.weight’, u’module.layer3.13.bn2.weight’, u’module.layer3.13.bn2.bias’, u’module.layer3.13.bn2.running_mean’, u’module.layer3.13.bn2.running_var’, u’module.layer3.13.conv3.weight’, u’module.layer3.13.bn3.weight’, u’module.layer3.13.bn3.bias’, u’module.layer3.13.bn3.running_mean’, u’module.layer3.13.bn3.running_var’, u’module.layer3.14.conv1.weight’, u’module.layer3.14.bn1.weight’, u’module.layer3.14.bn1.bias’, u’module.layer3.14.bn1.running_mean’, u’module.layer3.14.bn1.running_var’, u’module.layer3.14.conv2.weight’, u’module.layer3.14.bn2.weight’, u’module.layer3.14.bn2.bias’, u’module.layer3.14.bn2.running_mean’, u’module.layer3.14.bn2.running_var’, u’module.layer3.14.conv3.weight’, u’module.layer3.14.bn3.weight’, u’module.layer3.14.bn3.bias’, u’module.layer3.14.bn3.running_mean’, u’module.layer3.14.bn3.running_var’, u’module.layer3.15.conv1.weight’, u’module.layer3.15.bn1.weight’, u’module.layer3.15.bn1.bias’, u’module.layer3.15.bn1.running_mean’, u’module.layer3.15.bn1.running_var’, u’module.layer3.15.conv2.weight’, u’module.layer3.15.bn2.weight’, u’module.layer3.15.bn2.bias’, u’module.layer3.15.bn2.running_mean’, u’module.layer3.15.bn2.running_var’, u’module.layer3.15.conv3.weight’, u’module.layer3.15.bn3.weight’, u’module.layer3.15.bn3.bias’, u’module.layer3.15.bn3.running_mean’, u’module.layer3.15.bn3.running_var’, u’module.layer3.16.conv1.weight’, u’module.layer3.16.bn1.weight’, u’module.layer3.16.bn1.bias’, u’module.layer3.16.bn1.running_mean’, u’module.layer3.16.bn1.running_var’, u’module.layer3.16.conv2.weight’, u’module.layer3.16.bn2.weight’, u’module.layer3.16.bn2.bias’, u’module.layer3.16.bn2.running_mean’, u’module.layer3.16.bn2.running_var’, u’module.layer3.16.conv3.weight’, u’module.layer3.16.bn3.weight’, u’module.layer3.16.bn3.bias’, u’module.layer3.16.bn3.running_mean’, u’module.layer3.16.bn3.running_var’, u’module.layer3.17.conv1.weight’, u’module.layer3.17.bn1.weight’, u’module.layer3.17.bn1.bias’, u’module.layer3.17.bn1.running_mean’, u’module.layer3.17.bn1.running_var’, u’module.layer3.17.conv2.weight’, u’module.layer3.17.bn2.weight’, u’module.layer3.17.bn2.bias’, u’module.layer3.17.bn2.running_mean’, u’module.layer3.17.bn2.running_var’, u’module.layer3.17.conv3.weight’, u’module.layer3.17.bn3.weight’, u’module.layer3.17.bn3.bias’, u’module.layer3.17.bn3.running_mean’, u’module.layer3.17.bn3.running_var’, u’module.layer3.18.conv1.weight’, u’module.layer3.18.bn1.weight’, u’module.layer3.18.bn1.bias’, u’module.layer3.18.bn1.running_mean’, u’module.layer3.18.bn1.running_var’, u’module.layer3.18.conv2.weight’, u’module.layer3.18.bn2.weight’, u’module.layer3.18.bn2.bias’, u’module.layer3.18.bn2.running_mean’, u’module.layer3.18.bn2.running_var’, u’module.layer3.18.conv3.weight’, u’module.layer3.18.bn3.weight’, u’module.layer3.18.bn3.bias’, u’module.layer3.18.bn3.running_mean’, u’module.layer3.18.bn3.running_var’, u’module.layer3.19.conv1.weight’, u’module.layer3.19.bn1.weight’, u’module.layer3.19.bn1.bias’, u’module.layer3.19.bn1.running_mean’, u’module.layer3.19.bn1.running_var’, u’module.layer3.19.conv2.weight’, u’module.layer3.19.bn2.weight’, u’module.layer3.19.bn2.bias’, u’module.layer3.19.bn2.running_mean’, u’module.layer3.19.bn2.running_var’, u’module.layer3.19.conv3.weight’, u’module.layer3.19.bn3.weight’, u’module.layer3.19.bn3.bias’, u’module.layer3.19.bn3.running_mean’, u’module.layer3.19.bn3.running_var’, u’module.layer3.20.conv1.weight’, u’module.layer3.20.bn1.weight’, u’module.layer3.20.bn1.bias’, u’module.layer3.20.bn1.running_mean’, u’module.layer3.20.bn1.running_var’, u’module.layer3.20.conv2.weight’, u’module.layer3.20.bn2.weight’, u’module.layer3.20.bn2.bias’, u’module.layer3.20.bn2.running_mean’, u’module.layer3.20.bn2.running_var’, u’module.layer3.20.conv3.weight’, u’module.layer3.20.bn3.weight’, u’module.layer3.20.bn3.bias’, u’module.layer3.20.bn3.running_mean’, u’module.layer3.20.bn3.running_var’, u’module.layer3.21.conv1.weight’, u’module.layer3.21.bn1.weight’, u’module.layer3.21.bn1.bias’, u’module.layer3.21.bn1.running_mean’, u’module.layer3.21.bn1.running_var’, u’module.layer3.21.conv2.weight’, u’module.layer3.21.bn2.weight’, u’module.layer3.21.bn2.bias’, u’module.layer3.21.bn2.running_mean’, u’module.layer3.21.bn2.running_var’, u’module.layer3.21.conv3.weight’, u’module.layer3.21.bn3.weight’, u’module.layer3.21.bn3.bias’, u’module.layer3.21.bn3.running_mean’, u’module.layer3.21.bn3.running_var’, u’module.layer3.22.conv1.weight’, u’module.layer3.22.bn1.weight’, u’module.layer3.22.bn1.bias’, u’module.layer3.22.bn1.running_mean’, u’module.layer3.22.bn1.running_var’, u’module.layer3.22.conv2.weight’, u’module.layer3.22.bn2.weight’, u’module.layer3.22.bn2.bias’, u’module.layer3.22.bn2.running_mean’, u’module.layer3.22.bn2.running_var’, u’module.layer3.22.conv3.weight’, u’module.layer3.22.bn3.weight’, u’module.layer3.22.bn3.bias’, u’module.layer3.22.bn3.running_mean’, u’module.layer3.22.bn3.running_var’, u’module.layer3.23.conv1.weight’, u’module.layer3.23.bn1.weight’, u’module.layer3.23.bn1.bias’, u’module.layer3.23.bn1.running_mean’, u’module.layer3.23.bn1.running_var’, u’module.layer3.23.conv2.weight’, u’module.layer3.23.bn2.weight’, u’module.layer3.23.bn2.bias’, u’module.layer3.23.bn2.running_mean’, u’module.layer3.23.bn2.running_var’, u’module.layer3.23.conv3.weight’, u’module.layer3.23.bn3.weight’, u’module.layer3.23.bn3.bias’, u’module.layer3.23.bn3.running_mean’, u’module.layer3.23.bn3.running_var’, u’module.layer3.24.conv1.weight’, u’module.layer3.24.bn1.weight’, u’module.layer3.24.bn1.bias’, u’module.layer3.24.bn1.running_mean’, u’module.layer3.24.bn1.running_var’, u’module.layer3.24.conv2.weight’, u’module.layer3.24.bn2.weight’, u’module.layer3.24.bn2.bias’, u’module.layer3.24.bn2.running_mean’, u’module.layer3.24.bn2.running_var’, u’module.layer3.24.conv3.weight’, u’module.layer3.24.bn3.weight’, u’module.layer3.24.bn3.bias’, u’module.layer3.24.bn3.running_mean’, u’module.layer3.24.bn3.running_var’, u’module.layer3.25.conv1.weight’, u’module.layer3.25.bn1.weight’, u’module.layer3.25.bn1.bias’, u’module.layer3.25.bn1.running_mean’, u’module.layer3.25.bn1.running_var’, u’module.layer3.25.conv2.weight’, u’module.layer3.25.bn2.weight’, u’module.layer3.25.bn2.bias’, u’module.layer3.25.bn2.running_mean’, u’module.layer3.25.bn2.running_var’, u’module.layer3.25.conv3.weight’, u’module.layer3.25.bn3.weight’, u’module.layer3.25.bn3.bias’, u’module.layer3.25.bn3.running_mean’, u’module.layer3.25.bn3.running_var’, u’module.layer3.26.conv1.weight’, u’module.layer3.26.bn1.weight’, u’module.layer3.26.bn1.bias’, u’module.layer3.26.bn1.running_mean’, u’module.layer3.26.bn1.running_var’, u’module.layer3.26.conv2.weight’, u’module.layer3.26.bn2.weight’, u’module.layer3.26.bn2.bias’, u’module.layer3.26.bn2.running_mean’, u’module.layer3.26.bn2.running_var’, u’module.layer3.26.conv3.weight’, u’module.layer3.26.bn3.weight’, u’module.layer3.26.bn3.bias’, u’module.layer3.26.bn3.running_mean’, u’module.layer3.26.bn3.running_var’, u’module.layer3.27.conv1.weight’, u’module.layer3.27.bn1.weight’, u’module.layer3.27.bn1.bias’, u’module.layer3.27.bn1.running_mean’, u’module.layer3.27.bn1.running_var’, u’module.layer3.27.conv2.weight’, u’module.layer3.27.bn2.weight’, u’module.layer3.27.bn2.bias’, u’module.layer3.27.bn2.running_mean’, u’module.layer3.27.bn2.running_var’, u’module.layer3.27.conv3.weight’, u’module.layer3.27.bn3.weight’, u’module.layer3.27.bn3.bias’, u’module.layer3.27.bn3.running_mean’, u’module.layer3.27.bn3.running_var’, u’module.layer3.28.conv1.weight’, u’module.layer3.28.bn1.weight’, u’module.layer3.28.bn1.bias’, u’module.layer3.28.bn1.running_mean’, u’module.layer3.28.bn1.running_var’, u’module.layer3.28.conv2.weight’, u’module.layer3.28.bn2.weight’, u’module.layer3.28.bn2.bias’, u’module.layer3.28.bn2.running_mean’, u’module.layer3.28.bn2.running_var’, u’module.layer3.28.conv3.weight’, u’module.layer3.28.bn3.weight’, u’module.layer3.28.bn3.bias’, u’module.layer3.28.bn3.running_mean’, u’module.layer3.28.bn3.running_var’, u’module.layer3.29.conv1.weight’, u’module.layer3.29.bn1.weight’, u’module.layer3.29.bn1.bias’, u’module.layer3.29.bn1.running_mean’, u’module.layer3.29.bn1.running_var’, u’module.layer3.29.conv2.weight’, u’module.layer3.29.bn2.weight’, u’module.layer3.29.bn2.bias’, u’module.layer3.29.bn2.running_mean’, u’module.layer3.29.bn2.running_var’, u’module.layer3.29.conv3.weight’, u’module.layer3.29.bn3.weight’, u’module.layer3.29.bn3.bias’, u’module.layer3.29.bn3.running_mean’, u’module.layer3.29.bn3.running_var’, u’module.layer3.30.conv1.weight’, u’module.layer3.30.bn1.weight’, u’module.layer3.30.bn1.bias’, u’module.layer3.30.bn1.running_mean’, u’module.layer3.30.bn1.running_var’, u’module.layer3.30.conv2.weight’, u’module.layer3.30.bn2.weight’, u’module.layer3.30.bn2.bias’, u’module.layer3.30.bn2.running_mean’, u’module.layer3.30.bn2.running_var’, u’module.layer3.30.conv3.weight’, u’module.layer3.30.bn3.weight’, u’module.layer3.30.bn3.bias’, u’module.layer3.30.bn3.running_mean’, u’module.layer3.30.bn3.running_var’, u’module.layer3.31.conv1.weight’, u’module.layer3.31.bn1.weight’, u’module.layer3.31.bn1.bias’, u’module.layer3.31.bn1.running_mean’, u’module.layer3.31.bn1.running_var’, u’module.layer3.31.conv2.weight’, u’module.layer3.31.bn2.weight’, u’module.layer3.31.bn2.bias’, u’module.layer3.31.bn2.running_mean’, u’module.layer3.31.bn2.running_var’, u’module.layer3.31.conv3.weight’, u’module.layer3.31.bn3.weight’, u’module.layer3.31.bn3.bias’, u’module.layer3.31.bn3.running_mean’, u’module.layer3.31.bn3.running_var’, u’module.layer3.32.conv1.weight’, u’module.layer3.32.bn1.weight’, u’module.layer3.32.bn1.bias’, u’module.layer3.32.bn1.running_mean’, u’module.layer3.32.bn1.running_var’, u’module.layer3.32.conv2.weight’, u’module.layer3.32.bn2.weight’, u’module.layer3.32.bn2.bias’, u’module.layer3.32.bn2.running_mean’, u’module.layer3.32.bn2.running_var’, u’module.layer3.32.conv3.weight’, u’module.layer3.32.bn3.weight’, u’module.layer3.32.bn3.bias’, u’module.layer3.32.bn3.running_mean’, u’module.layer3.32.bn3.running_var’, u’module.layer3.33.conv1.weight’, u’module.layer3.33.bn1.weight’, u’module.layer3.33.bn1.bias’, u’module.layer3.33.bn1.running_mean’, u’module.layer3.33.bn1.running_var’, u’module.layer3.33.conv2.weight’, u’module.layer3.33.bn2.weight’, u’module.layer3.33.bn2.bias’, u’module.layer3.33.bn2.running_mean’, u’module.layer3.33.bn2.running_var’, u’module.layer3.33.conv3.weight’, u’module.layer3.33.bn3.weight’, u’module.layer3.33.bn3.bias’, u’module.layer3.33.bn3.running_mean’, u’module.layer3.33.bn3.running_var’, u’module.layer3.34.conv1.weight’, u’module.layer3.34.bn1.weight’, u’module.layer3.34.bn1.bias’, u’module.layer3.34.bn1.running_mean’, u’module.layer3.34.bn1.running_var’, u’module.layer3.34.conv2.weight’, u’module.layer3.34.bn2.weight’, u’module.layer3.34.bn2.bias’, u’module.layer3.34.bn2.running_mean’, u’module.layer3.34.bn2.running_var’, u’module.layer3.34.conv3.weight’, u’module.layer3.34.bn3.weight’, u’module.layer3.34.bn3.bias’, u’module.layer3.34.bn3.running_mean’, u’module.layer3.34.bn3.running_var’, u’module.layer3.35.conv1.weight’, u’module.layer3.35.bn1.weight’, u’module.layer3.35.bn1.bias’, u’module.layer3.35.bn1.running_mean’, u’module.layer3.35.bn1.running_var’, u’module.layer3.35.conv2.weight’, u’module.layer3.35.bn2.weight’, u’module.layer3.35.bn2.bias’, u’module.layer3.35.bn2.running_mean’, u’module.layer3.35.bn2.running_var’, u’module.layer3.35.conv3.weight’, u’module.layer3.35.bn3.weight’, u’module.layer3.35.bn3.bias’, u’module.layer3.35.bn3.running_mean’, u’module.layer3.35.bn3.running_var’, u’module.layer4.0.conv1.weight’, u’module.layer4.0.bn1.weight’, u’module.layer4.0.bn1.bias’, u’module.layer4.0.bn1.running_mean’, u’module.layer4.0.bn1.running_var’, u’module.layer4.0.conv2.weight’, u’module.layer4.0.bn2.weight’, u’module.layer4.0.bn2.bias’, u’module.layer4.0.bn2.running_mean’, u’module.layer4.0.bn2.running_var’, u’module.layer4.0.conv3.weight’, u’module.layer4.0.bn3.weight’, u’module.layer4.0.bn3.bias’, u’module.layer4.0.bn3.running_mean’, u’module.layer4.0.bn3.running_var’, u’module.layer4.0.downsample.0.weight’, u’module.layer4.0.downsample.1.weight’, u’module.layer4.0.downsample.1.bias’, u’module.layer4.0.downsample.1.running_mean’, u’module.layer4.0.downsample.1.running_var’, u’module.layer4.1.conv1.weight’, u’module.layer4.1.bn1.weight’, u’module.layer4.1.bn1.bias’, u’module.layer4.1.bn1.running_mean’, u’module.layer4.1.bn1.running_var’, u’module.layer4.1.conv2.weight’, u’module.layer4.1.bn2.weight’, u’module.layer4.1.bn2.bias’, u’module.layer4.1.bn2.running_mean’, u’module.layer4.1.bn2.running_var’, u’module.layer4.1.conv3.weight’, u’module.layer4.1.bn3.weight’, u’module.layer4.1.bn3.bias’, u’module.layer4.1.bn3.running_mean’, u’module.layer4.1.bn3.running_var’, u’module.layer4.2.conv1.weight’, u’module.layer4.2.bn1.weight’, u’module.layer4.2.bn1.bias’, u’module.layer4.2.bn1.running_mean’, u’module.layer4.2.bn1.running_var’, u’module.layer4.2.conv2.weight’, u’module.layer4.2.bn2.weight’, u’module.layer4.2.bn2.bias’, u’module.layer4.2.bn2.running_mean’, u’module.layer4.2.bn2.running_var’, u’module.layer4.2.conv3.weight’, u’module.layer4.2.bn3.weight’, u’module.layer4.2.bn3.bias’, u’module.layer4.2.bn3.running_mean’, u’module.layer4.2.bn3.running_var’, u’module.fc.weight’, u’module.fc.bias’]

Thank you for your help

Could you try the following:

# Create the model
model = models.resnet152(pretrained=True)
# Load the state_dict
checkpoint = torch.load('model_best.pth.tar')
model.load_state_dict(checkpoint['state_dict'])
model.eval()

After that try to register the hooks for your desired layer.

Thank you @ptrblck for your answer,

However l get unexpected key(s) message when l make ```
model.load_state_dict(checkpoint[‘state_dict’])


	Unexpected key(s) in state_dict: "module.conv1.weight", "module.bn1.weight", "module.bn1.bias", "module.bn1.running_mean", "module.bn1.running_var", "module.layer1.0.conv1.weight", "module.layer1.0.bn1.weight", "module.layer1.0.bn1.bias", "module.layer1.0.bn1.running_mean", "module.layer1.0.bn1.running_var", "module.layer1.0.conv2.weight", "module.layer1.0.bn2.weight", "module.layer1.0.bn2.bias", "module.layer1.0.bn2.running_mean", "module.layer1.0.bn2.running_var", "module.layer1.1.conv1.weight", "module.layer1.1.bn1.weight", "module.layer1.1.bn1.bias", "module.layer1.1.bn1.running_mean", "module.layer1.1.bn1.running_var", "module.layer1.1.conv2.weight", "module.layer1.1.bn2.weight", "module.layer1.1.bn2.bias", "module.layer1.1.bn2.running_mean", "module.layer1.1.bn2.running_var", "module.layer2.0.conv1.weight", "module.layer2.0.bn1.weight", "module.layer2.0.bn1.bias", "module.layer2.0.bn1.running_mean", "module.layer2.0.bn1.running_var", "module.layer2.0.conv2.weight", "module.layer2.0.bn2.weight", "module.layer2.0.bn2.bias", "module.layer2.0.bn2.running_mean", "module.layer2.0.bn2.running_var", "module.layer2.0.downsample.0.weight", "module.layer2.0.downsample.1.weight", "module.layer2.0.downsample.1.bias", "module.layer2.0.downsample.1.running_mean", "module.layer2.0.downsample.1.running_var", "module.layer2.1.conv1.weight", "module.layer2.1.bn1.weight", "module.layer2.1.bn1.bias", "module.layer2.1.bn1.running_mean", "module.layer2.1.bn1.running_var", "module.layer2.1.conv2.weight", "module.layer2.1.bn2.weight", "module.layer2.1.bn2.bias", "module.layer2.1.bn2.running_mean", "module.layer2.1.bn2.running_var", "module.layer3.0.conv1.weight", "module.layer3.0.bn1.weight", "module.layer3.0.bn1.bias", "module.layer3.0.bn1.running_mean", "module.layer3.0.bn1.running_var", "module.layer3.0.conv2.weight", "module.layer3.0.bn2.weight", "module.layer3.0.bn2.bias", "module.layer3.0.bn2.running_mean", "module.layer3.0.bn2.running_var", "module.layer3.0.downsample.0.weight", "module.layer3.0.downsample.1.weight", "module.layer3.0.downsample.1.bias", "module.layer3.0.downsample.1.running_mean", "module.layer3.0.downsample.1.running_var", "module.layer3.1.conv1.weight", "module.layer3.1.bn1.weight", "module.layer3.1.bn1.bias", "module.layer3.1.bn1.running_mean", "module.layer3.1.bn1.running_var", "module.layer3.1.conv2.weight", "module.layer3.1.bn2.weight", "module.layer3.1.bn2.bias", "module.layer3.1.bn2.running_mean", "module.layer3.1.bn2.running_var", "module.layer4.0.conv1.weight", "module.layer4.0.bn1.weight", "module.layer4.0.bn1.bias", "module.layer4.0.bn1.running_mean", "module.layer4.0.bn1.running_var", "module.layer4.0.conv2.weight", "module.layer4.0.bn2.weight", "module.layer4.0.bn2.bias", "module.layer4.0.bn2.running_mean", "module.layer4.0.bn2.running_var", "module.layer4.0.downsample.0.weight", "module.layer4.0.downsample.1.weight", "module.layer4.0.downsample.1.bias", "module.layer4.0.downsample.1.running_mean", "module.layer4.0.downsample.1.running_var", "module.layer4.1.conv1.weight", "module.layer4.1.bn1.weight", "module.layer4.1.bn1.bias", "module.layer4.1.bn1.running_mean", "module.layer4.1.bn1.running_var", "module.layer4.1.conv2.weight", "module.layer4.1.bn2.weight", "module.layer4.1.bn2.bias", "module.layer4.1.bn2.running_mean", "module.layer4.1.bn2.running_var", "module.fc.weight", "module.fc.bias".

I am also getting the same error. This is how I am trying to load model but not working. I have two network for one I did the same way but not for this one. Don’t understand. Could you please confirm if this issue is resolved.

depth_weights = torch.load(’./PretrainedModels/depth-2a464da4ea.pth.tar’ , map_location=‘cuda’)
depth.load_state_dict(depth_weights)

ERROR----- File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py”, line 836, in load_state_dict
self.class.name, “\n\t”.join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for ResNetDepth:
Missing key(s) in state_dict: “conv1.weight”, “bn1.weight”, “bn1.bias”, "…
Unexpected key(s) in state_dict: “epoch”, “arch”, “state_dict”.

It seems you are loading a Python dict, which contains the actual state_dict besides other information like the epoch and architecture.
Try to use:

depth.load_state_dict(depth_weights['state_dict'])

instead.

1 Like

looks as if you’ve trained your model using nn.DataParallel and try to load it in a plain model.
Have a look at this thread for some solutions.