简体   繁体   English

将 Keras 模型导出到具有 (None, 2) 输出形状的 protobuf

[英]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.

相关问题 Keras 模型输出形状为“(无,)” - Keras Model output shape is "(None,)" 将 keras 模型导出到 tflite - Exporting keras model into tflite keras,尽管有input_shape(None,…),模型仍然通过训练设置了预期的输入形状 - keras, model still setting expected input shape from training despite input_shape(None, …) Python类给出“无所不能” - Python Class gives “None in the ouptut” keras:ValueError:检查模型目标时出错:预期activation_1具有形状(无,60),但数组的形状为(10,100) - keras: ValueError: Error when checking model target: expected activation_1 to have shape (None, 60) but got array with shape (10, 100) Tensorflow / Keras ValueError:层“模型”的输入 0 与层不兼容:预期形状=(无,224,224,3),发现形状=(32,224,3) - Tensorflow / Keras ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(32, 224, 3) 加载集合 keras model 给出 ValueError: Invalid input_shape argument (None, 224, 224, 3): Z20F35E630DAF44DBFA4C3F608F5399D8C 有输入 - Loading ensemble keras model gives ValueError: Invalid input_shape argument (None, 224, 224, 3): model has 0 tensor inputs 简单的Keras模型形状问题 - Simple keras model shape issue 在keras中设置模型的输入形状 - Set input shape of model in keras Keras 顺序 model 适合形状 - Keras Sequential model fit shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM