question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyError: 'module.weight'

See original GitHub issue

Describe the bug I am trying to convert a simple Pytorch Gender Classifier to Keras (model is provided below). Whenever I run model = pytorch_to_keras(model, orig_img, [np.shape(orig_img.cpu().numpy())], names='short'), I get

Traceback (most recent call last): File “main.py”, line 154, in <module> main(args) File “main.py”, line 92, in main model = pytorch_to_keras(model, orig_img, [np.shape(orig_img.cpu().numpy())], names=‘short’) File “/mnt/c/Users/Yannis/CIFAR10S/real_world_experiments/CEM/venv/lib/python3.6/site-packages/pytorch2keras/converter.py”, line 325, in pytorch_to_keras names File “/mnt/c/Users/Yannis/CIFAR10S/real_world_experiments/CEM/venv/lib/python3.6/site-packages/pytorch2keras/convolution_layers.py”, line 35, in convert_conv if len(weights[weights_name].numpy().shape) == 5: # 3D conv KeyError: ‘module.weight’

To Reproduce Here is my pytorch model:

class resnet_modified_small(nn.Module):
    def base_size(self): return 512
    def rep_size(self): return 1024

    def __init__(self, n_classes):
        super(resnet_modified_small, self).__init__()
        self.resnet = tv.models.resnet34(pretrained=True)

        # define layers
        self.n_classes = n_classes
        self.linear = nn.Linear(7 * 7 * self.base_size(), self.rep_size())
        self.cls = nn.Linear(self.rep_size(), self.n_classes)
        self.dropout2d = nn.Dropout2d(.5)
        self.dropout = nn.Dropout(.5)
        self.relu = nn.LeakyReLU()
        initLinear(self.linear)

    def forward(self, out0):
        x = self.resnet.conv1(out0)
        x = self.resnet.bn1(x)
        x = self.resnet.relu(x)
        out1 = self.resnet.maxpool(x)

        out2 = self.resnet.layer1(out1)
        out3 = self.resnet.layer2(out2)
        out4 = self.resnet.layer3(out3)
        out5 = self.resnet.layer4(out4)
     
        x = self.dropout2d(out5)

        features = self.dropout(self.relu(self.linear(x.view(-1, 7*7*self.base_size()))))
        cls_scores = self.cls(features)
        return [out0, out1, out2, out3, out4, out5, features, cls_scores]

Environment:

  • OS: Bash for Windows
  • Python 3.6
  • Pytorch 0.4.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gmalivenkocommented, Jan 28, 2019

I’ve just converted the model without a problem. So, have you tried the latest converter version? Please, upgrade the package with pip install --upgrade pytorch2keras, I published the release 5 minutes ago. I hope it will help.

1reaction
gmalivenkocommented, Jan 27, 2019

Hi, @I-C-Karakozis. Here are many problems in the converter related to your model. All nested modules (ModuleList, big submodules, etc) are very complex to trace. Actually I plan to refactor the tracing part, but a bit later. So, If you want to convert your model right now:

  1. Set resnet layers as a submodules
  2. Don’t return an input.
class resnet_modified_small(nn.Module):
    def base_size(self): return 512
    def rep_size(self): return 1024

    def __init__(self, n_classes):
        super(resnet_modified_small, self).__init__()
        self.resnet = tv.models.resnet34(pretrained=True)
        self.conv1 = self.resnet.conv1
        self.bn1 = self.resnet.bn1
        self.layer1 = self.resnet.layer1
        self.layer2 = self.resnet.layer2
        self.layer3 = self.resnet.layer3
        self.layer4 = self.resnet.layer4

        # define layers
        self.n_classes = n_classes
        self.linear = nn.Linear(7 * 7 * self.base_size(), self.rep_size())
        self.cls = nn.Linear(self.rep_size(), self.n_classes)
        self.dropout2d = nn.Dropout2d(.5)
        self.dropout = nn.Dropout(.5)
        self.relu = nn.LeakyReLU()

    def forward(self, out0):
        x = self.conv1(out0)
        x = self.bn1(x)
        x = self.resnet.relu(x)
        out1 = self.resnet.maxpool(x)

        out2 = self.layer1(out1)
        out3 = self.layer2(out2)
        out4 = self.layer3(out3)
        out5 = self.layer4(out4)
     
        x = self.dropout2d(out5)

        features_ = self.relu(self.linear(x.view(-1, 7*7*self.base_size())))
        features = self.dropout(features_.clone())
        cls_scores = self.cls(features.clone())
        return [out1, out2, out3, out4, out5, features, cls_scores]
Read more comments on GitHub >

github_iconTop Results From Across the Web

'unexpected key "module.conv1_1.weight" in state_dict' · Issue ...
This problem is about torch.nn.DataParallel() . Normally DataParallel() will cover the original model, which means you can only use model.module ...
Read more >
[solved] KeyError: 'unexpected key "module.encoder ...
I am getting the following error while trying to load a saved model. KeyError: 'unexpected key "module.encoder.embedding.weight" in state_dict' This is the ...
Read more >
'unexpected key "module.encoder.embedding.weight" in ...
KeyError : 'unexpected key "module.encoder.embedding.weight" in state_dict'. This is the function I am using to load a saved model.
Read more >
(9)训练时报错KeyError: 'conv1.1.0.weight' - CSDN博客
该问题的错误原因:加载的模型pretrained_dict文件中没有conv1.1.0.weight这个权重内容,打印网络模型和预训练模型的键值,发现键值不同。
Read more >
How to solve KeyError: 0 - Python Forum
weights.insert( 0 , weights[ 0 ]) ... File "/home/support/Pictures/rcpsp/rcpsp.py", line 198, in <module> rcpsp(A, F, P, s0, ... KeyError: 0 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found