获取 input_array 和 output_array 项,将模型转换为 tflite 格式 [英] Obtain input_array and output_array items to convert model to tflite format

查看:20
本文介绍了获取 input_array 和 output_array 项,将模型转换为 tflite 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

PS. Please dont point me to converting Keras model directly to tflite as my .h5 file would fail to convert to .tflite directly. I somehow managed to convert my .h5 file to .pb

我关注了this Jupyter notebook使用 Keras 进行人脸识别.然后我将我的模型保存到一个 model.h5 文件,然后使用 这个.

I have followed 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 this.

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

Now I want to use my tensorflow file in Android. For this I will need to have Tensorflow Lite, which requires me to convert my model into a .tflite format.

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

For this, I'm trying to follow the official guidelines for it here. As you can see there, it requires input_array and output_array arrays. How do I obtain details of these things from my model.pb file?

推荐答案

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

input arrays and output arrays are the arrays which store input and output tensors respectively.

他们打算通知 TFLiteConverter 关于将在推理时使用的输入和输出张量.

They intend to inform the TFLiteConverter about the input and output tensors which will be used at the time of inference.

对于 Keras 模型,

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

The input tensor is the placeholder tensor of the first layer.

input_tensor = model.layers[0].input

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

The output tensor may relate with a activation function.

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()

The input tensor may be a placeholder tensor. Output tensor is the tensor which you run using session.run()

对于转换,我们得到,

input_array =[ input_tensor ]
output_array = [ output_tensor ]

这篇关于获取 input_array 和 output_array 项,将模型转换为 tflite 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆