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

标签 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

你好,

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

我使用 pb 文件 (resnet_v1_50, inception_v3) 获得了 2 个模型的成功结果,但是当我尝试转换 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]

最佳答案

而是 mmconvert您可以使用 tf2onnx将 Tensorflow 转换为 ONNX 的包。然后你可以加载 .onnx模型并将其另存为 .pth .

关于python - 如何使用 mmconvert 将 tensorflow 模型(InceptionResnetV2 pb 文件)转换为 pytorch 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57193146/

相关文章:

javascript - 如何从 ReactJS 网络应用程序向 Django API 发送 POST 请求?

python - 保持示例索引与 tf.keras.predict 和 tf.data.Dataset 的对应关系

python - 稀疏张量以减少训练时间

python - 在分布式计算中,世界规模和等级是多少?

python - 比较大于 > 或小于 < 的两个列表

python - 如何找到一个很长的字符串的所有唯一子串?

python - 为什么 008 和 009 是 Python 字典的无效键?

tensorflow - 聊天机器人的 Seq2Seq 模型

python - tensorflow r1.0 : could not a find a version that satisfies the requirement tensorflow

python - 深度 Q 学习 : torch. nn.functional.softmax 崩溃