[英]Exporting Keras model to protobuf with a (None, 2) ouptut shape
I have a Keras model I'm trying to export to ProtoBuf我有一个 Keras 模型,我想导出到 ProtoBuf
The final couple of layers look like this:最后几层看起来像这样:
features (Dense) (None, 128) 49280 concatenate_1[0][0]
__________________________________________________________________________________________________
gaze_target (Dense) (None, 2) 258 features[0][0]
I try exporting it like this:我尝试像这样导出它:
sess = K.get_session()
constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), 'gaze_target')
graph_io.write_graph(constant_graph, 'export', 'output.pb', as_text=False)
This errors with this:这个错误:
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/graph_util_impl.py in extract_sub_graph(graph_def, dest_nodes)
191
192 if isinstance(dest_nodes, six.string_types):
--> 193 raise TypeError("dest_nodes must be a list.")
194
195 name_to_input_name, name_to_node, name_to_seq_num = _extract_graph_summary(
TypeError: dest_nodes must be a list.
How do I export this model to ProtoBuf?如何将此模型导出到 ProtoBuf? (Ultimately for use on SageMaker) (最终用于 SageMaker)
Thanks to a colleague at work we worked this out.感谢一位同事,我们解决了这个问题。 The parameter to the graph_util.convert_variables_to_constants
method isn't the layer name, but instead is the operation name ( op.name
). graph_util.convert_variables_to_constants
方法的参数不是层名称,而是操作名称( op.name
)。
The correct code is:正确的代码是:
sess = K.get_session()
outputs = [out.op.name for out in model.outputs] # Note this new line
constant_graph = graph_util.convert_variables_to_constants(sess,
sess.graph.as_graph_def(),
outputs)
graph_io.write_graph(constant_graph, 'export', 'output.pb', as_text=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.