@H_301_1@PS.请不要将我指向converting Keras model directly to tflite,因为我的.h5文件无法直接转换为.tflite.我以某种方式设法将我的.h5文件转换为.pb
@H_301_1@我关注了this Jupyter笔记本,使用Keras进行面部识别.然后,我将模型保存到model.h5文件,然后使用this将其转换为冻结图model.pb.
@H_301_1@现在我想在Android中使用我的tensorflow文件.为此,我需要Tensorflow Lite,这需要我将模型转换为.tflite格式.
@H_301_1@为此,我尝试遵循其官方指南here.如您所见,它需要input_array和output_array数组.如何从我的model.pb文件中获得这些内容的详细信息?
最佳答案
输入数组和输出数组是分别存储输入和输出张量的数组.
@H_301_1@
@H_301_1@They intend to inform the TFLiteConverter
about the input and output tensors which will be used at the time of inference.
@H_301_1@对于Keras模型,
@H_301_1@输入张量是第一层的占位符张量.
@H_301_1@
input_tensor = model.layers[0].input
@H_301_1@输出张量可以与激活函数有关.
@H_301_1@
output_tensor = model.layers[ LAST_LAYER_INDEX ].output
@H_301_1@对于冻结图,
@H_301_1@
import tensorflow as tf
gf = tf.GraphDef()
m_file = open('model.pb','rb')
gf.ParseFromString(m_file.read())
@H_301_1@我们得到节点的名称,
@H_301_1@
for n in gf.node:
print( n.name )
@H_301_1@为了得到张量,
@H_301_1@
tensor = n.op
@H_301_1@输入张量可以是占位符张量.输出张量是您使用session.run()运行的张量
@H_301_1@对于转换,我们得到
@H_301_1@
input_array =[ input_tensor ]
output_array = [ output_tensor ]