Skip to main content
 首页 » 编程设计

python-3.x之如何将 .ckpt 转换为 .pb

2025年12月25日34虾米姐

我是深度学习的新手,我想使用预训练 (EAST) 模型从 AI Platform Serving 提供服务,开发人员提供了以下文件:

  • model.ckpt-49491.data-00000-of-00001
  • 检查站
  • model.ckpt-49491.index
  • 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。
  • 请您参考如下方法:

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