Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将keras h5转换为tensorflow pb进行批量推断_Python_Tensorflow_Keras_Protocol Buffers - Fatal编程技术网

Python 将keras h5转换为tensorflow pb进行批量推断

Python 将keras h5转换为tensorflow pb进行批量推断,python,tensorflow,keras,protocol-buffers,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

我在使用从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++的TysFooSead进行批量推理。
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很难跟踪