将keras h5转换为tensorflow pb以进行批处理推断 [英] convert keras h5 to tensorflow pb for batch inference

查看:619
本文介绍了将keras h5转换为tensorflow pb以进行批处理推断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with making batch inference using a tensorflow protobuf graph exported from a keras h5 model. Eventhough the exported pb graph can accept multiple inputs (samples), it always gives a single output regardless of the number of inputs. Here is a simple example to demonstrate the problem.

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进行批处理推断.

You can see the keras h5 graphs gives 3 ouputs wile the tensorflow pb graph just gives the first output. What am I doing wrong? I would like to modify the h5 to pb conversion process so that I can do batch inference using the pb grapth with the python and c++ tensorflow backends.

推荐答案

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

It turns out this is due to a bug I inherited from 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难以跟踪.

It seems the keras model class has both 'output' and 'outputs' members that makes this bug hard to track.

这篇关于将keras h5转换为tensorflow pb以进行批处理推断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆