怎么把.ckpt转换成.pb?

时间:2019-06-26 06:40:29

标签: python-3.x tensorflow google-cloud-platform google-cloud-ml

我是深度学习的新手,我想使用预训练(EAST)模型从AI平台服务中进行服务,开发人员可以使用以下文件:

  1. model.ckpt-49491.data-00000-of-00001
  2. 检查点
  3. model.ckpt-49491.index
  4. model.ckpt-49491.meta

我想将其转换为TensorFlow .pb格式。有办法吗?我已经从here

中提取了模型

完整代码here可用。

我查过here,它显示了以下代码来对其进行转换:

来自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}

我无法确定要传递的值:

  • INPUT_TYPE
  • PIPELINE_CONFIG_PATH。

3 个答案:

答案 0 :(得分:0)

这是将检查点转换为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()                

答案 1 :(得分:0)

在@Puneith Kaul的回答之后,这是tensorflow 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()

答案 2 :(得分:0)

如果您将INPUT_TYPE指定为image_tensor,并且 使用此命令将PIPELINE_CONFIG_PATH作为您的配置文件。

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}

您可以在导出目录中以3种格式获取模型;

  • frozen_graph.pb
  • savedmodel.pb
  • 检查点

有关更多信息,https://github.com/tensorflow/models/tree/master/research/object_detection