获取input_array和output_array项将模型转换为tflite格式
Obtain input_array and output_array items to convert model to tflite format
PS。请不要将我指向 converting Keras model directly to tflite,因为我的 .h5 文件无法直接转换为 .tflite。我以某种方式设法将我的 .h5 文件转换为 .pb
我关注了this Jupyter notebook for face recognition using Keras. I then saved my model to a model.h5
file, then converted it to a frozen graph, model.pb
using 。
现在我想在 Android 中使用我的 tensorflow 文件。为此,我需要 Tensorflow Lite,这需要我将我的模型转换为 .tflite
格式。
为此,我尝试遵循官方指南 here。如您所见,它需要 input_array
和 output_array
数组。如何从我的 model.pb
文件中获取这些内容的详细信息?
input arrays
和output arrays
分别是存储输入张量和输出张量的数组。
They intend to inform the TFLiteConverter
about the input and output tensors which will be used at the time of inference.
对于 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 ]
PS。请不要将我指向 converting Keras model directly to tflite,因为我的 .h5 文件无法直接转换为 .tflite。我以某种方式设法将我的 .h5 文件转换为 .pb
我关注了this Jupyter notebook for face recognition using Keras. I then saved my model to a model.h5
file, then converted it to a frozen graph, model.pb
using
现在我想在 Android 中使用我的 tensorflow 文件。为此,我需要 Tensorflow Lite,这需要我将我的模型转换为 .tflite
格式。
为此,我尝试遵循官方指南 here。如您所见,它需要 input_array
和 output_array
数组。如何从我的 model.pb
文件中获取这些内容的详细信息?
input arrays
和output arrays
分别是存储输入张量和输出张量的数组。
They intend to inform the
TFLiteConverter
about the input and output tensors which will be used at the time of inference.
对于 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 ]