获取 input_array 和 output_array 项,将模型转换为 tflite 格式

Spa*_*r0i 9 python tensorflow tensorflow-lite

附注。请不要指出我将 Keras 模型直接转换为 tflite,因为我的 .h5 文件将无法直接转换为 .tflite。我以某种方式设法将我的 .h5 文件转换为 .pb

我已经按照这个Jupyter notebook 使用 Keras 进行了人脸识别。然后我将我的模型保存到一个model.h5文件中,然后model.pb使用这个.

现在我想在 Android 中使用我的 tensorflow 文件。为此,我需要使用 Tensorflow Lite,这需要我将模型转换为某种.tflite格式。

为此,我试图在此处遵循官方指南。正如你在那里看到的,它需要input_arrayoutput_array数组。如何从我的model.pb文件中获取这些内容的详细信息?

Shu*_*hal 6

input arraysoutput arrays是分别存储输入和输出张量的数组。

他们打算告知TFLiteConverter将在推理时使用的输入和输出张量。

对于 Keras 模型,

输入张量是第一层的占位张量。

input_tensor = model.layers[0].input
Run Code Online (Sandbox Code Playgroud)

输出张量可以与激活函数相关。

output_tensor = model.layers[ LAST_LAYER_INDEX ].output
Run Code Online (Sandbox Code Playgroud)

对于冻结图,

import tensorflow as tf
gf = tf.GraphDef()   
m_file = open('model.pb','rb')
gf.ParseFromString(m_file.read())
Run Code Online (Sandbox Code Playgroud)

我们得到节点的名字,

for n in gf.node:
    print( n.name )
Run Code Online (Sandbox Code Playgroud)

为了得到张量,

tensor = n.op
Run Code Online (Sandbox Code Playgroud)

输入张量可以是占位符张量。输出张量是您使用的张量session.run()

对于转换,我们得到,

input_array =[ input_tensor ]
output_array = [ output_tensor ]
Run Code Online (Sandbox Code Playgroud)

  • 你还应该看看 [this](https://lutzroeder.github.io/netron/) 很棒的工具,它可以让你从许多不同的格式可视化模型。我用它来检查我的输入/输出张量名称。 (4认同)