Extract features from ResNet152 pretrained model

DeepLearner17: <p>Hello,</p>
<p>l would like to extract features (last fully connected layer) from  a fine tuned pretrained resnet152 on my own dataset.</p>
<p>In order to extract features from the original pretrained resnet152 model l  did simply the following :</p>
<pre><code class="lang-auto">
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)

</code></pre>
<p>However, when l train Resnet152 from scratch on my custom dataset. I store my model as <strong>model_best.pth.tar</strong><br>
And load it as follow :<br>
<code>resnet152=torch.load('/local/model_best.pth.tar')</code><br>
but can’t get children by doing :<br>
<code>modules = list(resnet152.children())[:-1]</code><br>
l tried :<br>
<code>resnet152=resnet152[u'state_dict']</code></p>
<p>However, how can l specify that l want to extract features from the last layer ?</p>
<p>l  tried then to do that with hook function as follow :</p>
<pre><code class="lang-auto">activation = {}
def get_activation(name):
    def hook(model, input, output):
        activation[name] = output.detach()

    return hook
</code></pre>
<p>But, <code>resnet152.fc.register_forward_hook(get_activation('fc'))</code> is not recognized</p>
<p>Any cue please  !!</p>

ptrblck: <p>How did you save your model? Did you save the entire model or its <code>state_dict</code>?<br>
Could you print the result of <code>torch.load('model_best.pth.tar')</code>?</p>

DeepLearner17: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>I saved my model as follow  :</p>
<pre><code class="lang-auto">        save_checkpoint({
            'epoch': epoch + 1,
            'arch': args.arch,
            'state_dict': model.state_dict(),
            'best_prec1': best_prec1,
            'optimizer': optimizer.state_dict(),
        }, is_best)
</code></pre>
<p>From<br>
<code>resnet152=torch.load('model_best.pth.tar')</code><br>
l have the following options :<br>
resnet152.clear<br>
resnet152.copy<br>
resnet152.fromkeys<br>
resnet152.get<br>
resnet152.has_key<br>
resnent152.items<br>
resnet152.iteritems<br>
resnet152.iterkeys<br>
resnet152.intervalues<br>
resnet152.keys<br>
resnet152.pop<br>
resnet152.popitem<br>
resnet152.setdefault<br>
resnet152.update<br>
resnet152.values<br>
resnet152viewtimes<br>
resnet152.viewkeys<br>
resnet152.viewvalues</p>
<p>For instance<br>
resnet152.keys() returns<br>
[u’state_dict’, u’epoch’, u’arch’, u’optimizer’, u’best_prec1’]</p>
<p>and<br>
my_dict=resnet152[u’state_dict’]<br>
my_dict.keys() returns</p>
<p>[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’]</p>
<p>Thank you for your help</p>

ptrblck: <p>Could you try the following:</p>
<pre><code class="lang-python"># 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()
</code></pre>
<p>After that try to register the hooks for your desired layer.</p>

DeepLearner17: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a> for your answer,</p>
<p>However  l get unexpected key(s) message  when l make ```<br>
<strong>model.load_state_dict(checkpoint[‘state_dict’])</strong></p>
<pre><code class="lang-auto">
	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".</code></pre>

Shriya_Kak: <p>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.</p>
<p>depth_weights = torch.load(’./PretrainedModels/depth-2a464da4ea.pth.tar’ , map_location=‘cuda’)<br>
depth.load_state_dict(depth_weights)</p>
<p>ERROR----- File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py”, line 836, in load_state_dict<br>
self.<strong>class</strong>.<strong>name</strong>, “\n\t”.join(error_msgs)))<br>
RuntimeError: Error(s) in loading state_dict for ResNetDepth:<br>
Missing key(s) in state_dict: “conv1.weight”, “bn1.weight”, “bn1.bias”, "…<br>
Unexpected key(s) in state_dict: “epoch”, “arch”, “state_dict”.</p>

ptrblck: <p>It seems you are loading a Python <code>dict</code>, which contains the actual <code>state_dict</code> besides other information like the epoch and architecture.<br>
Try to use:</p>
<pre><code class="lang-python">depth.load_state_dict(depth_weights['state_dict'])
</code></pre>
<p>instead.</p>

Lornatang: <p>looks as if you’ve trained your model using  <code>nn.DataParallel</code>  and try to load it in a plain model.<br>
Have a look at <a href="https://discuss.pytorch.org/t/solved-keyerror-unexpected-key-module-encoder-embedding-weight-in-state-dict/1686/2">this thread </a> for some solutions.</p>