tensorflow转换ckpt为savermodel模型

ckpt转换成SavedModel

convert_ckpt_to_savermodel.py

import tensorflow as tf
import sys

trained_checkpoint_prefix = sys.argv[1]
export_dir = sys.argv[2]
graph = tf.Graph()
config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
with tf.compat.v1.Session(graph=graph, config=config) 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()

假设已经生成了ckpt模型checkpoint   hello_model.data-00000-of-00001  hello_model.index  hello_model.meta

python ./convert_ckpt_to_savermodel.py  hello_model ./save

会在save目录下生成

save
├── saved_model.pb
└── variables
   ├── variables.data-00000-of-00001
   └── variables.index

发布了201 篇原创文章 · 获赞 20 · 访问量 41万+

猜你喜欢

转载自blog.csdn.net/zmlovelx/article/details/100511406