Vendredi
Vendredi

Reputation: 25

How do I convert a .meta .index and .data file into SavedModel (.pb) format without losing metagraphdef?

I'm trying to convert these three files of a pre-trained model:

  1. semantic_model.data-00000-of-00001
  2. semantic_model.index
  3. semantic_model.meta

into a Saved Model format, so that I can later convert it into TFLite format for Inference. Searching StackOverflow, I'd come across this code, which properly generates the Saved_model.pb, however as noted in some comments, doing it in this way doesn't keep the Meta Graph Definitions, which causes an error when I later try to convert it into TFlite format or freeze it.

import os
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
export_dir = '/tf-end-to-end/export_dir' 
#trained_checkpoint_prefix = 'Models/semantic_model' \tf-end-to-end\Models
trained_checkpoint_prefix = 'PATH TO MODEL DIRECTORY'
tf.reset_default_graph()
graph = tf.Graph()
loader = tf.train.import_meta_graph(trained_checkpoint_prefix + ".meta" )
sess = tf.Session()
loader.restore(sess,trained_checkpoint_prefix)
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.TRAINING, tf.saved_model.tag_constants.SERVING], strip_default_attrs=True)
builder.save()

This is the error I get when trying to use the saved_model:

RuntTimeError: MetaGraphDef associated with tags {'serve'} could not be found in SavedModel

Running the showsavedmodelcli --all doesn't display anything under signature definitions for the created saved_model.

My question is, how do I maintain the data and convert this to saved_model, for later conversion into TFLite format?

Model Structure and creation details can be seen here, including the checkpoint files mentioned: https://github.com/OMR-Research/tf-end-to-end

Upvotes: 0

Views: 849

Answers (1)

Meghna Natraj
Meghna Natraj

Reputation: 691

Refer to these steps for converting checkpoints to a TFLite model: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/r1/convert/python_api.md#convert-checkpoints-

Upvotes: 1

Related Questions