microsoft / MMdnn

MMdnn is a set of tools to help users inter-operate among different deep learning frameworks. E.g. model conversion and visualization. Convert models between Caffe, Keras, MXNet, Tensorflow, CNTK, PyTorch Onnx and CoreML.
MIT License
5.8k stars 965 forks source link

Error while trying to convert TorchScript / Traced-Pytorch model to Tensorflow #901

Closed AlexeyAB closed 3 years ago

AlexeyAB commented 4 years ago

Error while trying to convert TorchScript / Traced-Pytorch model to Tensorflow

Pre-trained model path (webpath or webdisk path): EfficientNet-Lite3 (traced_model) from Pytorch-hub

import torch
import torch.nn as nn

model = torch.hub.load(
            "rwightman/gen-efficientnet-pytorch",
            "tf_efficientnet_lite0",
            pretrained=True,
            exportable=True
        )

Running scripts: https://colab.research.google.com/gist/AlexeyAB/337aa764a43e369245a14156fe0b0b5e/mmdnn_pt_tf.ipynb

mmdnn_pt_tf_ipynb.zip

Error message:

/usr/local/lib/python3.6/dist-packages/torch/serialization.py:581: UserWarning: 'torch.load' received a zip file that looks like a TorchScript archive dispatching to 'torch.jit.load' (call 'torch.jit.load' directly to silence this warning)
  " silence this warning)", UserWarning)
Traceback (most recent call last):
  File "/usr/local/bin/mmconvert", line 8, in <module>
    sys.exit(_main())
  File "/usr/local/lib/python3.6/dist-packages/mmdnn/conversion/_script/convert.py", line 102, in _main
    ret = convertToIR._convert(ir_args)
  File "/usr/local/lib/python3.6/dist-packages/mmdnn/conversion/_script/convertToIR.py", line 97, in _convert
    parser = PytorchParser151(model, inputshape[0])
  File "/usr/local/lib/python3.6/dist-packages/mmdnn/conversion/pytorch/pytorch_parser.py", line 533, in __init__
    self.build_graph(input_shape)
  File "/usr/local/lib/python3.6/dist-packages/mmdnn/conversion/pytorch/pytorch_parser.py", line 92, in build_graph
    self.pytorch_graph.build(self.input_shape)
  File "/usr/local/lib/python3.6/dist-packages/mmdnn/conversion/pytorch/pytorch_graph.py", line 126, in build
    graph, nodes = self.extractgraph(dummy_input)
  File "/usr/local/lib/python3.6/dist-packages/mmdnn/conversion/pytorch/pytorch_graph.py", line 231, in extractgraph
    graph = _trace(self.model, dummy_input, OperatorExportTypes.ONNX)
  File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 303, in _trace
    torch.jit._get_trace_graph(func, args, strict=False, _force_outplace=False, _return_inputs_states=True)
  File "/usr/local/lib/python3.6/dist-packages/torch/jit/__init__.py", line 338, in _get_trace_graph
    outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/torch/jit/__init__.py", line 426, in forward
    self._force_outplace,
  File "/usr/local/lib/python3.6/dist-packages/torch/jit/__init__.py", line 412, in wrapper
    outs.append(self.inner(*trace_inputs))
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 720, in _call_impl
    result = self._slow_forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/mmdnn/conversion/pytorch/pytorch_graph.py", line 30, in _slow_forward
    return self_.forward(*input, **kwargs)
RuntimeError: Tried to trace <__torch__.geffnet.gen_efficientnet.GenEfficientNet object at 0x9e14b90> but it is not part of the active trace. Modules that are called during a trace must be registered as submodules of the thing being traced.
cookieli commented 3 years ago

Sorry for replying too late.Mmdnn doesn't support torchscript/Traced-Pytorch model for now.