18

I am new to deep learning and I want to use a pretrained (EAST) model to serve from the AI Platform Serving, I have these files made available by the developer:

  1. model.ckpt-49491.data-00000-of-00001
  2. checkpoint
  3. model.ckpt-49491.index
  4. model.ckpt-49491.meta

I want to convert it into the TensorFlow .pb format. Is there a way to do it? I have taken the model from here

The full code is available here.

I have looked up here and it shows the following code to convert it:

From tensorflow/models/research/

INPUT_TYPE=image_tensor
PIPELINE_CONFIG_PATH={path to pipeline config file}
TRAINED_CKPT_PREFIX={path to model.ckpt}
EXPORT_DIR={path to folder that will be used for export}

python object_detection/export_inference_graph.py \
    --input_type=${INPUT_TYPE} \
    --pipeline_config_path=${PIPELINE_CONFIG_PATH} \
    --trained_checkpoint_prefix=${TRAINED_CKPT_PREFIX} \
    --output_directory=${EXPORT_DIR}

I am unable to figure out what value to pass:

  • INPUT_TYPE
  • PIPELINE_CONFIG_PATH.
gogasca
  • 9,283
  • 6
  • 80
  • 125
Shivam Sahu
  • 195
  • 1
  • 1
  • 12
  • You need SavedModel format, please make sure you convert it to this format first and then you can use saved_model_cli tool to analyze your model – gogasca Jun 26 '19 at 22:28
  • 1
    Can you please elaborate more. How can I do it. Or suggest me some material to read, please. – Shivam Sahu Jun 27 '19 at 05:38

3 Answers3

22

Here's the code to convert the checkpoint to SavedModel

import os
import tensorflow as tf

trained_checkpoint_prefix = 'models/model.ckpt-49491'
export_dir = os.path.join('export_dir', '0')

graph = tf.Graph()
with tf.compat.v1.Session(graph=graph) as sess:
    # Restore from checkpoint
    loader = tf.compat.v1.train.import_meta_graph(trained_checkpoint_prefix + '.meta')
    loader.restore(sess, trained_checkpoint_prefix)

    # Export checkpoint to SavedModel
    builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_dir)
    builder.add_meta_graph_and_variables(sess,
                                         [tf.saved_model.TRAINING, tf.saved_model.SERVING],
                                         strip_default_attrs=True)
    builder.save()                
Puneith Kaul
  • 364
  • 2
  • 4
  • Thank a lot for your help. I successfully converted the model, but I can't deploy it as its size exceeds the given size. How can I shrink my model. – Shivam Sahu Jun 28 '19 at 14:53
  • You can try Nvidia TensorRT tool, take a look at this post: https://medium.com/google-cloud/optimizing-tensorflow-models-for-serving-959080e9ddbf – gogasca Jul 02 '19 at 07:15
  • And what if ckpt file doesn't have .meta included? – StayCool Jul 17 '23 at 12:09
1

Following the answer of @Puneith Kaul, here is the syntax for tensorflow version 1.7:

import os
import tensorflow as tf

export_dir = 'export_dir' 
trained_checkpoint_prefix = 'models/model.ckpt'
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()
mcExchange
  • 6,154
  • 12
  • 57
  • 103
  • 2
    Is there a way to do the same with object recognition when there is no ".meta"? I am using this webpage: ```https://github.com/tensorflow/models/tree/master/research/slim#exporting-the-inference-graph``` But I cannot convert the .ckpt models to .pb – Aizzaac Sep 03 '20 at 19:32
-1

If you specify INPUT_TYPE as image_tensor and PIPELINE_CONFIG_PATH as your config file with this command.

python object_detection/export_inference_graph.py \
--input_type=${INPUT_TYPE} \
--pipeline_config_path=${PIPELINE_CONFIG_PATH} \
--trained_checkpoint_prefix=${TRAINED_CKPT_PREFIX} \
--output_directory=${EXPORT_DIR}

you can get your model in 3 formats in your export dir;

  • frozen_graph.pb
  • savedmodel.pb
  • checkpoint

for more info https://github.com/tensorflow/models/tree/master/research/object_detection

  • Is there something similar to object recognition? I am using this webpage: ```https://github.com/tensorflow/models/tree/master/research/slim#exporting-the-inference-graph ``` but I cannot convert to .pb – Aizzaac Sep 03 '20 at 19:30