9

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

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.

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.

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?

Sparker0i
  • 1,787
  • 4
  • 35
  • 60
  • Just get the input and output tensors from the graph. Put them in arrays. – Shubham Panchal Feb 03 '19 at 03:44
  • Shubham's answer is correct. But note that if you export to a SavedModel or directly from a Keras model using TFLiteConverter's python interface, you do not have to specify the input and outputs as they are already included in the representation. – suharshs Feb 05 '19 at 01:41

1 Answers1

6

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

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

For a Keras model,

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

For a Frozen Graph,

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

We get the names of the nodes,

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

To get the tensor,

tensor = n.op

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

For conversion, we get,

input_array =[ input_tensor ]
output_array = [ output_tensor ]
Shubham Panchal
  • 4,061
  • 2
  • 11
  • 36
  • 4
    You should also look out [this](https://lutzroeder.github.io/netron/) awesome tool that lets you visualize a model from a lot of different formats. I used it to check my input/output tensor names. – Romzie Mar 12 '19 at 16:11
  • 1
    Hi, @Romzie. I'm new at tensorflow. Could you please explaint how to use that tool. Is first node input and the last node output tensor? – MSK Apr 24 '19 at 06:53
  • @MSK Can you confirm if the first/last node are the input/output nodes? – CamHart Sep 19 '19 at 12:33