如何使用mmconvert将Tensorflow模型(InceptionResnetV2 pb文件)转换为pytorch模型?

时间:2019-07-25 01:37:15

标签: python tensorflow pytorch

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

预先训练的模型路径:

初始Resnet v2模型(tensorflow) (来自https://github.com/tensorflow/models/tree/master/research/slim

运行脚本:

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

你好

我试图使用mmdnn将tensorflow模型(inception resnet v2的pb文件)转换为pytorch模型。

对于2个带有pb文件的模型(resnet_v1_50,inception_v3),我获得了成功的结果,但是当我尝试转换inception_resnet_v2时,出现了以下错误。

有人有解决问题或解释这些问题的想法吗?

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]

1 个答案:

答案 0 :(得分:0)

您可以使用tf2onnx包来代替mmconvert将Tensorflow转换为ONNX。然后,您可以加载.onnx模型并将其另存为.pth