tensorflow的pb模型转为onnx模型

        tensorflow模型转为onnx模型的工具:Tf2onnx

        git源码:https://github.com/onnx/tensorflow-onnx

1、python和tensorflow版本要求

         Python版本:3.6

        Tensorflow版本:1.13

2、Tf2onnx安装

pip install -U tf2onnx

3、模型转换

python -m tf2onnx.convert --graphdef saved_model.pb --output model.onnx --inputs input/audio/preprocessed:0 --outputs output/softmax:0

参数说明:

-graphdef:需要进行转换的pb模型

--output:转换后的onnx模型名称

-inputs:pb模型输入层的名字

--outputs:pb模型输出层的名字

(模型输入名称见4)

4、summarize graph tool

        查看pb模型输入输出的工具

1)安装

        下载tensorflow源码:Git:https://github.com/tensorflow

        安装bazel:下载:bazel-4.0.0-installer-linux-x86_64.sh

                            地址:Index of bazel-local/4.0.0

                            执行命令:chmod +x bazel--installer-linux-x86_64.sh

                                              ./bazel--installer-linux-x86_64.sh

2)使用

在下载的tensorflow源码中分别执行:

bazel build tensorflow/tools/graph_transforms:summarize_graph

bazel-bin/tensorflow/tools/graph_transforms/summarize_graph 
--in_graph=tensorflow_inception_graph.pb

输出的即为模型的输入输出层模型名称

eg:

 

5.可能出现的问题

 解决:不使用带输入输出层名称的参数

             将模型当道tmp_model文件夹中,然后执行:

python -m tf2onnx.convert --saved-model tmp_model --output simbert.onnx --opset 13

6.直接从ckpt模型转换为onnx模型

        但是需要知道输入和输出层名字,多个输入用逗号隔开

python -m tf2onnx.convert --checkpoint ckpt/model.ckpt.meta --inputs input1:0,input2:0,input3:0 --outputs output:0 --output model.onnx --opset 13

你可能感兴趣的:(tensorflow,深度学习,人工智能)