Convert Keras model to TensorFlow protobuf

后端 未结 5 1233
盖世英雄少女心
盖世英雄少女心 2021-02-02 11:33

We\'re currently training various neural networks using Keras, which is ideal because it has a nice interface and is relatively easy to use, but we\'d like to be able to apply t

5条回答
  •  执念已碎
    2021-02-02 12:24

    Save your keras model as an HDF5 file.

    You can then do the conversion with the following code:

    from keras import backend as K
    from tensorflow.python.framework import graph_util
    from tensorflow.python.framework import graph_io
    
    weight_file_path = 'path to your keras model'
    net_model = load_model(weight_file_path)
    sess = K.get_session()
    
    constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), 'name of the output tensor')
    graph_io.write_graph(constant_graph, 'output_folder_path', 'output.pb', as_text=False)
    print('saved the constant graph (ready for inference) at: ', osp.join('output_folder_path', 'output.pb'))
    

    Here is my sample code which handles multiple input and multiple output cases: https://github.com/amir-abdi/keras_to_tensorflow

提交回复
热议问题