简体   繁体   English

如何将 .ckpt 转换为 .pb?

[英]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:我是深度学习的新手,我想使用预训练 (EAST) 模型从 AI Platform Serving 提供服务,开发人员提供了以下文件:

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

I want to convert it into the TensorFlow .pb format.我想将其转换为 TensorFlow .pb格式。 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:我查了一下here ,它显示了以下代码来转换它:

From tensorflow/models/research/来自张量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. PIPELINE_CONFIG_PATH。

Here's the code to convert the checkpoint to SavedModel 这是将检查点转换为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()                

Following the answer of @Puneith Kaul, here is the syntax for tensorflow version 1.7: 在@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()

If you specify INPUT_TYPE as image_tensor and PIPELINE_CONFIG_PATH as your config file with this command.如果您使用此命令将 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}

you can get your model in 3 formats in your export dir;您可以在导出目录中以 3 种格式获取模型;

  • frozen_graph.pb冻结图.pb
  • savedmodel.pb保存模型.pb
  • checkpoint检查站

for more info https://github.com/tensorflow/models/tree/master/research/object_detection更多信息https://github.com/tensorflow/models/tree/master/research/object_detection

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Colab 中将 Inception V4 从 .ckpt 转换为 .pb? - How to convert Inception V4 from .ckpt to .pb in Colab? 有没有办法从 .index 和 .ckpt 文件重新生成 .pb 或 .meta 文件? - Is there any way to regenerate either .pb or .meta file from .index and .ckpt file? Tensorflow 2.0 将 keras 模型转换为 .pb 文件 - Tensorflow 2.0 Convert keras model to .pb file 将 .pb 转换为 .tflite ,RuntimeError: TOCO failed 请参阅控制台了解信息 - convert .pb to .tflite ,RuntimeError: TOCO failed see console for info 如何在Python 3中导入“Security_pb2”包? - How to import “Security_pb2” package in Python 3? 如何使用python保存带有.ckpt扩展名的神经网络模型而不是.meta extnsion in tensorflow? - How to save neural network model with .ckpt extension instead of.meta extnsion in tensorflow using python? 如何避免在包root上放置_pb2.py文件? - How to avoid placing _pb2.py files at package root? 如何获取输入/输出名称以创建.pb文件 - How to get input/output name to create .pb file 有什么方法可以将 .pb 文件的数据格式从 NCHW 转换为 NHWC? - Is there any way that can convert a data format of .pb file from NCHW into NHWC? 我正在尝试将.pb文件转换为.mlmodel文件。 我收到一个错误“ Tensorflow图不包含具有该名称的张量” - I am trying to convert .pb file to .mlmodel file. I am getting an error “Tensorflow graph does not contain a tensor with this name”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM