【问题标题】:How to save to saved_model.pb from tf.Session?如何从 tf.Session 保存到 saved_model.pb?
【发布时间】:2020-09-01 19:27:46
【问题描述】:

我正在使用 ssd_mobilenet_v2_coco_2018_03_29 预训练的 Tensorflow 模型。我想将输入更改为固定大小,并将其保存在 saved_model.pb (我正在使用需要这种格式的 Neuron Compiler)

这是我如何将输入张量更改为固定大小:

graph = tf.Graph()
with graph.as_default():
    fixed_image_tensor = tf.placeholder(tf.uint8, shape=(None, 300, 300, 3), name='image_tensor')
    graph_def = tf.GraphDef()
    with tf.io.gfile.GFile(frozen_pb_file, 'rb') as f:
        serialized_graph = f.read()
        graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(graph_def, name='', input_map={"image_tensor:0": fixed_image_tensor})

现在我使用tf.saved_model.simple_save 将修改后的图形保存为saved_model.pb 格式:

image_tensor = graph.get_tensor_by_name('image_tensor:0')
boxes_tensor = graph.get_tensor_by_name('detection_boxes:0')
scores_tensor = graph.get_tensor_by_name('detection_scores:0')
classes_tensor = graph.get_tensor_by_name('detection_classes:0')
num_detections_tensor = graph.get_tensor_by_name('num_detections:0')

sess = tf.Session(graph=graph)

tf.saved_model.simple_save(
    session=sess,
    export_dir='model/',
    inputs={image_tensor.name: image_tensor},
    outputs={
        boxes_tensor.name: boxes_tensor,
        scores_tensor.name: scores_tensor,
        classes_tensor.name: classes_tensor,
        num_detections_tensor.name: num_detections_tensor
    }
)

代码创建如下目录(变量为空)

|-model/
|---variables/
|---saved_model.pb

saved_model.pb 只有 370 字节,并且不能包含任何实际信息。我也尝试了tf.saved_model.Builder,比如thisthis,但仍然得到完全相同的结果。

我仍然可以像往常一样使用sess 进行推理,没有任何问题。我做错了什么?还有其他方法吗?我正在使用 TensorFlow 1.15.0。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    重新排列的代码 TF1.13 得到 67MBytes *.pb 文件。重新加载生成的 saved_file,输入有你的尺寸和所有列出的输出:

    import tensorflow as tf
    
    frozen_pb_file = "./ssd_mobilenet_v2_coco_2018_03_29/frozen_inference_graph.pb"
    
    graph = tf.Graph()
        with graph.as_default():
        fixed_image_tensor = tf.placeholder(tf.uint8, shape=(None, 300, 300, 3), name='image_tensor')
        graph_def = tf.GraphDef()
        with tf.io.gfile.GFile(frozen_pb_file, 'rb') as f:
            serialized_graph = f.read()
            graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(graph_def, name='', input_map={"image_tensor:0": fixed_image_tensor})
        
        image_tensor = graph.get_tensor_by_name('image_tensor:0')
        boxes_tensor = graph.get_tensor_by_name('detection_boxes:0')
        scores_tensor = graph.get_tensor_by_name('detection_scores:0')
        classes_tensor = graph.get_tensor_by_name('detection_classes:0')
        num_detections_tensor = graph.get_tensor_by_name('num_detections:0')
    
        sess = tf.Session(graph=graph)
    
        file_writer = tf.summary.FileWriter(logdir='log', graph=graph)
    
        tf.saved_model.simple_save(
            session=sess,
            export_dir='model/',
            inputs={image_tensor.name: fixed_image_tensor},
            outputs={
                boxes_tensor.name: boxes_tensor,
                scores_tensor.name: scores_tensor,
                classes_tensor.name: classes_tensor,
                num_detections_tensor.name: num_detections_tensor
            }
        )
    

    【讨论】:

    • 我仍然从 FileWriter 获得了 370 字节的 pb 文件和 140MB 的日志文件。我用 2 台机器进行了测试,同时使用了 TF1.15 和 TF1.13。如果你能得到实际的 pb 文件,那么我的环境设置肯定有问题。你能把你的机器和环境的详细信息给我吗?
    • 我已经用 Anaconda、Python3.7、TF1.13 创建了环境。你用张量板看过你的日志吗?创建的图表是否符合您的期望?
    猜你喜欢
    • 2020-04-17
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2021-11-18
    • 2011-12-15
    • 1970-01-01
    • 2020-05-21
    • 2020-08-25
    相关资源
    最近更新 更多