将keras h5转换为tensorflow pb以进行批处理推断

时间:2018-08-22 13:03:06

标签: python tensorflow keras protocol-buffers

我在使用从keras h5模型导出的tensorflow protobuf图进行批处理推理时遇到问题。尽管导出的pb图可以接受多个输入(样本),但无论输入数量如何,它始终提供单个输出。这是一个简单的示例来演示此问题。

from keras.models import Model,load_model
from keras.layers import Dense, Input
from keras import backend as K
import tensorflow as tf
import numpy as np
import os
import os.path as osp

pinput = Input(shape=[10,], name='my_input')
poutput = Dense(1, activation='sigmoid')(pinput)
model = Model(inputs=[pinput], outputs=[poutput])

model.compile(loss='mean_squared_error',optimizer='sgd',metrics=['accuracy'])
data = np.random.random((100, 10))
labels = np.random.randint(2, size=(100, 1))
model.fit(data, labels, epochs=1, batch_size=32)

x = np.random.random((3, 10))
y = model.predict(x)
print y

####################################
# Save keras h5 to tensorflow pb
####################################

K.set_learning_phase(0)

#alias output names
numoutputs = 1
pred = [None]*numoutputs
pred_node_names = [None]*numoutputs
for i in range(numoutputs):
    pred_node_names[i] = 'output'+'_'+str(i)
    pred[i] = tf.identity(model.output[i], name=pred_node_names[i])
print('Output nodes names are: ', pred_node_names)

sess = K.get_session()

# Write the graph in human readable
f = 'graph_def_for_reference.pb.ascii'
tf.train.write_graph(sess.graph.as_graph_def(), '.', f, as_text=True)

input_graph_def = sess.graph.as_graph_def()

#freeze graph
from tensorflow.python.framework.graph_util import convert_variables_to_constants
output_names = pred_node_names
output_names += [v.op.name for v in tf.global_variables()]
constant_graph = convert_variables_to_constants(sess, input_graph_def,output_names)

# Write the graph in binary .pb file
from tensorflow.python.framework import graph_io
graph_io.write_graph(constant_graph, '.', 'model.pb', as_text=False)

def load_graph(frozen_graph_filename):
    # We load the protobuf file from the disk and parse it to retrieve the 
    # unserialized graph_def
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    # Then, we import the graph_def into a new Graph and returns it 
    with tf.Graph().as_default() as graph:
        # The name var will prefix every op/nodes in your graph
        # Since we load everything in a new graph, this is not needed
        tf.import_graph_def(graph_def, name="prefix")
    return graph

###################################
# Test batch inference with tf
###################################
graph = load_graph("model.pb")
for op in graph.get_operations():
    print(op.name)

minput = graph.get_tensor_by_name('prefix/my_input:0')
moutput = graph.get_tensor_by_name('prefix/output_0:0')
with tf.Session(graph=graph) as sess:
    y = sess.run(moutput, feed_dict={minput: x})
print y

运行的输出是

Epoch 1/1
100/100 [==============================] - 0s 661us/step - loss: 0.2655 - acc: 0.3900
[[0.62018263]
 [0.41664478]
 [0.40322617]]
('Output nodes names are: ', ['output_0'])
prefix/my_input
prefix/dense_1/kernel
prefix/dense_1/kernel/read
prefix/dense_1/bias
prefix/dense_1/bias/read
prefix/dense_1/MatMul
prefix/dense_1/BiasAdd
prefix/dense_1/Sigmoid
prefix/SGD/iterations
prefix/SGD/lr
prefix/SGD/momentum
prefix/SGD/decay
prefix/training/SGD/Variable
prefix/training/SGD/Variable_1
prefix/strided_slice/stack
prefix/strided_slice/stack_1
prefix/strided_slice/stack_2
prefix/strided_slice
prefix/output_0
[0.62018263]

您可以看到keras h5图给出了3个输出,而tensorflow pb图仅给出了第一个输出。我究竟做错了什么?我想要 修改h5到pb的转换过程,以便我可以使用python和c ++ tensorflow后端的pb grapth进行批处理推断。

1 个答案:

答案 0 :(得分:1)

事实证明,这是由于我从k2tf_convert继承的错误

pred[i] = tf.identity(model.output[i], name=pred_node_names[i])

应该是

pred[i] = tf.identity(model.outputs[i], name=pred_node_names[i])

看来keras模型类同时具有'output'和'outputs'成员,这使得此bug难以跟踪。