Mr. Ace
Mr. Ace

Reputation: 365

TensorFlow: Converting SavedModel.pb file to .tflite using Tensorflow 2.2.0


OS: Windows 10

Tensorflow Version: 2.2.0

Model Type: SavedModel (.pb)

Desired Model Type: Tensorflow Lite (.tflite)


I have been going in endless circles trying to find a python script or a command line function to convert a .pb file to .tflite. I have tried using the tflite_convert, but it returns with the error:

OSError: SavedModel file does not exist at: C:/tensorflowTraining/export_dir/saved_model.pb/{saved_model.pbtxt|saved_model.pb}

I have also tried some scripts like:

import tensorflow as to

gf = tf.compat.v1.GraphDef()

m_file = open('saved_model.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())

This returns:

Exception has occurred: DecodeError

Unexpected end-group tag.

This error happens in line 4:

 gf.ParseFromString(m_file.read())

It would be really helpful if someone could provide a working script or command line function as many I have researched return errors or do not function properly.

Thank you!

Upvotes: 0

Views: 927

Answers (1)

Vishnuvardhan Janapati
Vishnuvardhan Janapati

Reputation: 3288

You can try something like below with TF2.2.

import tensorflow as tf 
graph_def_file = "./saved_model.pb"
tflite_file = 'mytflite.tflite'

input_arrays = ["input"]. # you need to change it based on your model
output_arrays = ["output"] # you need to change it based on your model
print("{} -> {}".format(graph_def_file, tflite_file))
converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph(
  graph_def_file=graph_def_file,
  input_arrays=input_arrays,
  output_arrays=output_arrays,input_shapes={'input_mel':[ 1, 50, 80]})
# If there are multiple inputs, then update the dictionary above
tflite_model = converter.convert()
open(tflite_file,'wb').write(tflite_model)

In the above code, you need to use input_arrays, output_arrays, and input_shapes corresponding to your model.

Upvotes: 1

Related Questions