Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert .pb to TFLite format?

I downloaded a retrained_graph.pb and retrained_labels.txt file of a model I trained in Azure cognitive service. Now I want to make an Android app using that model and to do so I have to convert it to TFLite format. I used toco and I am getting the following error:

ValueError: Invalid tensors 'input' were found.

I am basically following this tutorial and have problem on step 4 and direcly copy pasted the terminal code: https://heartbeat.fritz.ai/neural-networks-on-mobile-devices-with-tensorflow-lite-a-tutorial-85b41f53230c

like image 591
Ayush Saxena Avatar asked Oct 21 '18 17:10

Ayush Saxena


People also ask

How do you convert TensorFlow PB model to Tflite?

This is a three-step process: Export frozen inference graph for TFLite. Build Tensorflow from source (needed for the third step) Using TOCO to create an optimized TensorFlow Lite Model.

What is a Tflite file?

Jul 13, 2022. 1 min read. TensorFlow Lite, often referred to as TFLite, is an open source library developed by Google for deploying machine learning models to edge devices. Examples of edge deployments would be mobile (iOS/Android), embedded, and on-device.


2 Answers

I am making a wild guess here, maybe you entered input_arrays=input. Which may not be true. Use this script to find the name of the input and output arrays of the frozen inference graph

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

with open('somefile.txt', 'a') as the_file:
    for n in gf.node:
        the_file.write(n.name+'\n')

file = open('somefile.txt','r')
data = file.readlines()
print "output name = "
print data[len(data)-1]

print "Input name = "
file.seek ( 0 )
print file.readline()

In my case they are:

output name: SemanticPredictions
input name: ImageTensor
like image 148
Ajinkya Avatar answered Oct 13 '22 22:10

Ajinkya


You can use utility tflite_convert which is the part of tensorflow 1.10 (or higher) package.

The simple use for float inference is something like:

tflite_convert \
    --output_file=/tmp/retrained_graph.tflite \
    --graph_def_file=/tmp/retrained_graph.pb \
    --input_arrays=input \
    --output_arrays=output

Where input and output - are input and ouput tensors of your tensorflow graph

like image 11
Aleksandr Kondratyev Avatar answered Oct 14 '22 00:10

Aleksandr Kondratyev