Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert .ckpt to .pb?

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.
like image 224
Shivam Sahu Avatar asked Jun 26 '19 06:06

Shivam Sahu


People also ask

What is Ckpt file?

The Checkpoint file is a VSAM KSDS that contains checkpoint information generated by the DTF during execution of a copy operation. The Checkpoint file consists of variable length records, one per Process that has checkpointing specified. The average record length is 256 bytes.


2 Answers

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()                
like image 122
Puneith Kaul Avatar answered Nov 16 '22 01:11

Puneith Kaul


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()
like image 45
mcExchange Avatar answered Nov 16 '22 01:11

mcExchange