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.78k stars 964 forks source link

How to convert InceptionResnetV2 pb file for Pytorch platfrom using mmconvert? #699

Open muhan16 opened 5 years ago

muhan16 commented 5 years ago

Platform : ubuntu 16.04 Python version: 3.5.2 mmdnn version : 0.2.5 Source framework with version : Tensorflow 1.14.0 with GPU Destination framework with version : Pytorch 1.1.0

Pre-trained model path (webpath or webdisk path): inception resnet v2 model (tensorflow) (from https://github.com/tensorflow/models/tree/master/research/slim)

Running scripts: mmconvert -sf tensorflow -iw inception_resnet_v2_jsy.pb --inNodeName input --inputShape 299,299,3 --dstNodeName InceptionResnetV2/Logits/Logits/BiasAdd -df pytorch -om tf_to_pytorch_inception_resnet_v2.pth

Hello,

I'm tried to convert tensorflow model (pb file of inception resnet v2 ) to pytorch model for using mmdnn.

I got successful results for 2 models with pb files (resnet_v1_50, inception_v3) , but when I tried to convert inception_resnet_v2, I got below errors.

Is there anyone who have some ideas to solve them or to explain those problems?


Error logs.

IR network structure is saved as [09db48a5839944eeb4492ee2a0959097.json]. IR network structure is saved as [09db48a5839944eeb4492ee2a0959097.pb]. IR weights are saved as [09db48a5839944eeb4492ee2a0959097.npy]. Parse file [09db48a5839944eeb4492ee2a0959097.pb] with binary format successfully. Target network code snippet is saved as [tf_to_pytorch_inception_resnet_v2.py]. Target weights are saved as [09db48a5839944eeb4492ee2a0959097.npy]. Traceback (most recent call last): File "/usr/local/bin/mmconvert", line 11, in sys.exit(_main()) File "/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/_script/convert.py", line 112, in _main dump_code(args.dstFramework, network_filename + '.py', temp_filename + '.npy', args.outputModel, args.dump_tag) File "/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/_script/dump_code.py", line 32, in dump_code save_model(MainModel, network_filepath, weight_filepath, dump_filepath) File "/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/pytorch/saver.py", line 5, in save_model model = MainModel.KitModel(weight_filepath) File "tf_to_pytorch_inception_resnet_v2.py", line 476, in init self.InceptionResnetV2_Logits_Logits_MatMul = self.__dense(name = 'InceptionResnetV2/Logits/Logits/MatMul', in_features = -1, out_features = 1001, bias = True) File "tf_to_pytorch_inception_resnet_v2.py", line 1444, in dense layer = nn.Linear(**kwargs) File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/linear.py", line 76, in init__ self.weight = Parameter(torch.Tensor(out_features, in_features))

RuntimeError: Trying to create tensor with negative dimension -1: [1001, -1]

rainLiuplus commented 5 years ago

Hi @muhan16. Seems that Pytorch 1.1.0 has changed its implementation of torch.Tensor. The new api does not accept -1 as a dim thus maybe you could downgrade Pytorch to 0.4.0 to have a try. Thanks!