`KeyError` with nested transposed convolution in `nn.Sequential`
See original GitHub issueDescribe the bug
When converting my model, I get a KeyError for {layer_name}.weight when it gets to a ConvTranspose2d. I’ve managed to dumb the model down to a simple reproduction case, see code below.
To Reproduce This fails:
class TestNet(nn.Module):
def __init__(self):
super().__init__()
c1x1 = nn.Sequential(
nn.Conv2d(in_channels=6, out_channels=1, kernel_size=1, padding=0, bias=False),
nn.BatchNorm2d(num_features=1)
)
self.my_op = nn.Sequential(
c1x1,
nn.ConvTranspose2d(1, 1, kernel_size=4, stride=2, padding=1)
)
def forward(self, x):
return self.my_op(x)
my_x = torch.ones(1, 6, 128, 128)
my_test_net = TestNet()
print(my_test_net(my_x).shape) # torch.Size([1, 1, 256, 256])
km = pytorch_to_keras(my_test_net, my_x, [(6, 128, 128)], verbose=True)
with:
[....]
Converting transposed convolution ...
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-39-10087659f3d4> in <module>
----> 1 km = pytorch_to_keras(my_test_net, my_x, [(6, 128, 128)], verbose=True)
~/anaconda3/envs/general/lib/python3.6/site-packages/pytorch2keras/converter.py in pytorch_to_keras(model, args, input_shapes, change_ordering, training, verbose, names)
313 node_input_names,
314 layers, state_dict,
--> 315 names
316 )
317 if node_id in graph_outputs:
~/anaconda3/envs/general/lib/python3.6/site-packages/pytorch2keras/convolution_layers.py in convert_convtranspose(params, w_name, scope_name, inputs, layers, weights, names)
244 weights_name = '{0}.weight'.format(w_name)
245
--> 246 if len(weights[weights_name].numpy().shape) == 4:
247 W = weights[weights_name].numpy().transpose(2, 3, 1, 0)
248 height, width, n_filters, channels = W.shape
KeyError: 'my_op.0.weight'
Rewriting to reduce the nn.Sequential nesting however converts successfully:
class TestNet(nn.Module):
def __init__(self):
super().__init__()
self.my_op = nn.Sequential(
nn.Conv2d(in_channels=6, out_channels=1, kernel_size=1, padding=0, bias=False),
nn.BatchNorm2d(num_features=1),
nn.ConvTranspose2d(1, 1, kernel_size=4, stride=2, padding=1)
)
def forward(self, x):
return self.my_op(x)
Of course the real model is quite a bit more complex than this, so un-nesting isn’t so trivial.
Expected behavior I would expect both models to convert just fine, but maybe I am missing something, in which case please let me know 😃.
Logs I hope the provided error suffices.
Environment (please complete the following information):
- OS: MacOS
- Python 3.7
- Version v0.1.15
Additional context
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
KeyError with nested transposed convolution in nn.Sequential ...
Describe the bug When converting my model, I get a KeyError for {layer_name}.weight when it gets to a ConvTranspose2d.
Read more >how to flatten input in `nn.Sequential` in Pytorch - Stack Overflow
You can create a new module/class as below and use it in the sequential as you are using other modules (call Flatten() )....
Read more >Python API: test/test_nn.py Source File - Caffe2
1013 l4 = nn.Linear(2, 2). 1014 subnet = nn.Sequential(l3, l4). 1015 s = nn.Sequential(). 1016 with self.assertRaises(KeyError):.
Read more >See raw diff - Hugging Face
If specified, hides nested bars + outside this bound. If unspecified, attempts to ... Sequential: Feed-forward subnetwork + """ + return nn.Sequential(nn.
Read more >Transforms — MONAI 1.1.0 Documentation
Each transform in the sequence must take a single argument and return a single value. ... torch.nn.functional.pad is used unless the mode or...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hello @ElteHupkes. Yes, it’s known issue related to nn.Sequential / nn.ModuleList. You can try to unroll your submodules into the model class and convert it with the converter. Actually, I plan to rewrite graph-traverse functions and I hope the converter will be able to convert nn.Sequential / nn.ModuleList as well.
Thanks, good to know! I don’t have time to test this right now, but when I do I’ll close this.