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

时间:2019-02-02 17:38:46

标签: python tensorflow tensorflow-lite

PS。请不要将我指向converting Keras model directly to tflite,因为我的.h5文件将无法直接转换为.tflite。我以某种方式设法将.h5文件转换为.pb

我已遵循this Jupyter笔记本使用Keras进行面部识别。然后,我将模型保存到model.h5文件中,然后使用this将其转换为冻结的图形model.pb

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

为此,我正在尝试遵循here的官方准则。如您所见,它需要input_arrayoutput_array数组。如何从我的model.pb文件中获取这些内容的详细信息?

1 个答案:

答案 0 :(得分:2)

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

  

他们打算通知TFLiteConverter有关推断时将使用的输入和输出张量。

对于Keras模型,

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

input_tensor = model.layers[0].input

输出张量可能与激活函数有关。

output_tensor = model.layers[ LAST_LAYER_INDEX ].output

对于冻结图,

import tensorflow as tf
gf = tf.GraphDef()   
m_file = open('model.pb','rb')
gf.ParseFromString(m_file.read())

我们得到节点的名称,

for n in gf.node:
    print( n.name )

要获取张量,

tensor = n.op

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

运行的张量

对于转化,我们得到

input_array =[ input_tensor ]
output_array = [ output_tensor ]