KeyError: 'module.weight'
See original GitHub issueDescribe 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:
- Created 5 years ago
- Comments:12 (5 by maintainers)

Top Related StackOverflow Question
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.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: