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.

Can't convert the official mobilenet model

See original GitHub issue

Describe the bug I can’t convert mobilenet V2 pytorch official model.

To Reproduce

from mobilenet_helper import *
from torch.autograd import Variable
from pytorch2keras.converter import pytorch_to_keras
from torchvision.models import mobilenet_v2

model = mobilenet_v2(pretrained=True)

dummy_input = Variable(torch.randn(1, 3, 224, 224))
# we should specify shape of the input tensor
k_model = pytorch_to_keras(model, dummy_input, [(3, sz, sz,)], verbose=True)

Expected behavior Model converted without errors

Logs

....
____ 
graph node: Sequential/Sequential[0]/Sequential[0]/InvertedResidual[1]/Sequential[conv]/Conv2d[1]
node id: 363
type: onnx::Conv
inputs: ['352', '13']
outputs: ['Sequential/Sequential[0]/Sequential[0]/InvertedResidual[1]/Sequential[conv]/Conv2d[1]']
name in state_dict: 0.0.1.conv.0
attrs: {'dilations': [1, 1], 'group': 1, 'kernel_shape': [1, 1], 'pads': [0, 0, 0, 0], 'strides': [1, 1]}
is_terminal: False
Converting convolution ...
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
...

/opt/anaconda3/lib/python3.7/site-packages/pytorch2keras/convolution_layers.py in convert_conv(params, w_name, scope_name, inputs, layers, weights, names)
     33     input_name = inputs[0]
     34 
---> 35     if len(weights[weights_name].numpy().shape) == 5:  # 3D conv
     36         W = weights[weights_name].numpy().transpose(2, 3, 4, 1, 0)
     37         height, width, channels, n_layers, n_filters = W.shape

KeyError: '0.0.1.conv.0.weight'

Environment (please complete the following information):

  • OS: debian
  • Python: Python 3.7
  • Version pytorch 1.1.0, pytorch2keras 0.1.19

Additional context I think that the problem is in converter.py module, lines 279 - 294 (see me inline comment)

       try: 
            int(node_name_regex[-1])
            node_weigth_group_name = '.'.join(
                node_name_regex[:-1]
            )
          # My comment: state dict keys goes from '0.0.1.conv.0.1.num_batches_tracked' to 
         #'0.0.1.conv.1.weight' but here it's assume that will start at 0 ('0.0.1.conv.0.weight') and 
        # don't perform any validation. Doing: node_weights_name = '.'.join(node_name_regex)
        # would work. 

            node_weights_name = node_weigth_group_name + '.' + str(group_indices[node_weigth_group_name])
            group_indices[node_weigth_group_name] += 1

        except ValueError:
            node_weights_name = '.'.join(
                node_name_regex
            )
        except IndexError:

Pytorch State dict: … ‘0.0.0.1.bias’, ‘0.0.0.1.running_mean’, ‘0.0.0.1.running_var’, ‘0.0.0.1.num_batches_tracked’, ‘0.0.1.conv.0.0.weight’, ‘0.0.1.conv.0.1.weight’, ‘0.0.1.conv.0.1.bias’, ‘0.0.1.conv.0.1.running_mean’, ‘0.0.1.conv.0.1.running_var’, ‘0.0.1.conv.0.1.num_batches_tracked’, ‘0.0.1.conv.1.weight’, ‘0.0.1.conv.2.weight’, ‘0.0.1.conv.2.bias’, ‘0.0.1.conv.2.running_mean’, ‘0.0.1.conv.2.running_var’, ‘0.0.1.conv.2.num_batches_tracked’, ‘0.0.2.conv.0.0.weight’, …

The model is defined in https://github.com/pytorch/vision/blob/master/torchvision/models/mobilenet.py and the problematic region is in the InvertedResidual block, the Conv2D just after ConvBNRelU block.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gmalivenkocommented, Jun 27, 2019

@Apollo-XI. I fixed the problem with input_shapes in the latest version. Yes, there are some problems with change_ordering, so, basically, it’s not supported by now. I plan to fix it ASAP.

1reaction
gmalivenkocommented, Jun 26, 2019

Hello @Apollo-XI. I’m back with some updates:

  1. Yes, it’s known problem with wrong weight names parsing
  2. You can watch updates on #84 issue. It will be a new module for onnx->keras conversion, I plan to done it this week and keep this repository as high-level wrapper. Probably I’ll break some API, but it worth the updates.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't convert the official mobilenet model · Issue #83 - GitHub
Describe the bug I can't convert mobilenet V2 pytorch official model. To Reproduce from mobilenet_helper import * from torch.autograd import ...
Read more >
Error in converting custom ssd model using Tensorflow2 ...
Solved: Hi, I am trying to convert a custom SSD MobileNet V2 FPNLite 320x320 from TensorFlow2 model zoo to Openvino Intermediate Representation (IR)...
Read more >
Fine tune ssd_mobilenet_v1 model can't convert - Jetson Nano
Hi AngelZheng,. I am facing same issue in Mobilenet V1 and V2. I read this whole thread but could not understand your solution...
Read more >
Source code for torchvision.models.mobilenetv2 - PyTorch
Module]] = None, dropout: float = 0.2, ) -> None: """ MobileNet V2 main class ... Set to 1 to turn off rounding...
Read more >
Getting Started with PyTorch Image Models (timm)
However, this name is likely to change depending on the model architecture used. ... Note: At the time of writing, dynamic control flow...
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