【问题标题】:Obtain input_array and output_array items to convert model to tflite format获取 input_array 和 output_array 项以将模型转换为 tflite 格式
【发布时间】:2019-06-26 23:40:06
【问题描述】:

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

我已关注this Jupyter notebook 使用 Keras 进行人脸识别。然后我将模型保存到model.h5 文件中,然后使用this 将其转换为冻结图model.pb

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

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

【问题讨论】:

  • 只需从图中获取输入和输出张量即可。将它们放入数组中。
  • Shubham 的回答是正确的。但请注意,如果您使用 TFLiteConverter 的 python 接口导出到 SavedModel 或直接从 Keras 模型导出,则不必指定输入和输出,因为它们已经包含在表示中。

标签: python tensorflow tensorflow-lite


【解决方案1】:

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 ]

【讨论】:

  • 您还应该看看this 很棒的工具,它可以让您从许多不同的格式中可视化模型。我用它来检查我的输入/输出张量名称。
  • 嗨,@Romzie。我是张量流的新手。您能否解释一下如何使用该工具。是第一个节点输入,最后一个节点输出张量吗?
  • @MSK你能确认第一个/最后一个节点是输入/输出节点吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-22
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 2019-05-04
相关资源
最近更新 更多