如何从PB文件还原Keras模型

时间:2020-10-19 02:28:53

标签: python tensorflow keras deep-learning restore

假设我有一个冻结的.pb文件,并且我想知道模型的设计方式。 一种方法是读取其图形定义并在tensorflow中打印出op名称和op值。像这样:

import tensorflow as tf
frozen_graph_filename = './frozen_model.pb'
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
    tf.import_graph_def(
        graph_def, 
        input_map=None, 
        return_elements=None, 
        name="", 
        op_dict=None, 
        producer_op_list=None
    )
    for op in graph.get_operations():
        print(op.name)

但是以这种方式,op的组织性很差并且很难阅读。就像这样:

输入
op1
op2
op1 / subop1
op1 / subop2
...

是否可以从.pb文件恢复 Keras 模型? 这样我就可以用更易读的格式打印模型摘要。像这样:

>> model.summary()
 _________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_node (LSTM)            (None, 128)               67072     
_________________________________________________________________
output_node (Dense)          (None, 1)                 33        
=================================================================
Total params: 71,233
Trainable params: 71,233
Non-trainable params: 0

0 个答案:

没有答案