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

mxnet to pytorch (No module named 'MainModel') #867

Closed kevinzezel closed 4 years ago

kevinzezel commented 4 years ago

Hi,

I used this commands to convert mxnet resnet50 model to pytorch (https://www.dropbox.com/s/53ftnlarhyrpkg2/retinaface-R50.zip?dl=0):

python -m mmdnn.conversion._script.convertToIR -f mxnet -n R50-symbol.json -w R50-0000.params -d resnet50 --inputShape 3,224,224

[14:31:27] src/nnvm/legacy_json_util.cc:209: Loading symbol saved by previous version v1.3.0. Attempting to upgrade... [14:31:27] src/nnvm/legacy_json_util.cc:217: Symbol successfully upgraded! /opt/conda/lib/python3.6/site-packages/mxnet/module/base_module.py:55: UserWarning: You created Module with Module(..., label_names=['softmax_label']) but input with name 'softmax_label' is not found in symbol.list_arguments(). Did you mean one of: data warnings.warn(msg) Warning: MXNet Parser has not supported operator null with name data. Warning: convert the null operator with name [data] into input layer. IR network structure is saved as [resnet50.json]. IR network structure is saved as [resnet50.pb]. IR weights are saved as [resnet50.npy].

python -m mmdnn.conversion._script.IRToCode -f pytorch --IRModelPath resnet50.pb --dstModelPath kit_imagenet.py --IRWeightPath resnet50.npy -dw kit_pytorch.npy  

Parse file [resnet50.pb] with binary format successfully. Target network code snippet is saved as [kit_imagenet.py]. Target weights are saved as [kit_pytorch.npy].

python -m mmdnn.conversion.examples.pytorch.imagenet_test --dump resnet50Full.pth -n kit_imagenet.py -w kit_pytorch.npy

PyTorch model file is saved as [resnet50Full.pth], generated by [kit_imagenet.py.py] and [kit_pytorch.npy].

However when I import the model in pytorch I get this error:

>>> import torch
>>> import numpy as np
>>> model_address = 'resnet50Full.pth'
>>> device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
>>> model = torch.load(model_address).to(device)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/lib/python3.6/site-packages/torch/serialization.py", line 592, in load
    return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
  File "/opt/conda/lib/python3.6/site-packages/torch/serialization.py", line 772, in _legacy_load
    result = unpickler.load()
ModuleNotFoundError: No module named 'MainModel'

mxnet -> 1.6.0 torch -> 1.5.1 torchvision -> 0.6.0a0 mmdnn -> 0.3.0

Machine settings: Ubuntu Linux 18.04 LTS GPUs: 2x Nvidia Tesla V100 32gb CPUs: 2x Intel Xeon E5-2690 v4 RAM: 512GB

Regards, Kevin

linmajia commented 4 years ago

@kevinzezel , thank you very much for the feedback. Please refer to this FAQ, and add the following code:

import imp
MainModel = imp.load_source('MainModel', "kit_imagenet.py")

before

model = torch.load(model_address).to(device)
kevinzezel commented 4 years ago

Thank you!!