ckpt转换成SavedModel

convert_ckpt_to_savermodel.py

点击(此处)折叠或打开

  1. import tensorflow as tf
  2. import sys

  3. trained_checkpoint_prefix = sys.argv[1]
  4. export_dir = sys.argv[2]
  5. graph = tf.Graph()
  6. config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
  7. with tf.compat.v1.Session(graph=graph, config=config) as sess:
  8.     # Restore from checkpoint
  9.     loader = tf.compat.v1.train.import_meta_graph(trained_checkpoint_prefix + '.meta')
  10.     loader.restore(sess, trained_checkpoint_prefix)

  11.     # Export checkpoint to SavedModel
  12.     builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_dir)
  13.     builder.add_meta_graph_and_variables(sess, [tf.saved_model.TRAINING, tf.saved_model.SERVING], strip_default_attrs=True)
  14.     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

[tf.saved_model.TRAINING, tf.saved_model.SERVING]  可根据需要修改此列表,比如如果模型中只有SERVING那么要改成
[tf.saved_model.SERVING] ,否则会提示 TRAINING不存在的错误。

作者:帅得不敢出门
12-26 16:47
查看更多