22

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 them in our production environment.

Unfortunately the production environment is C++, so our plan is to:

  • Use the TensorFlow backend to save the model to a protobuf
  • Link our production code to TensorFlow, and then load in the protobuf

Unfortunately I don't know how to access the TensorFlow saving utilities from Keras, which normally saves to HDF5 and JSON. How do I save to protobuf?

mrry
  • 125,488
  • 26
  • 399
  • 400
Shep
  • 7,990
  • 8
  • 49
  • 71
  • 1
    Not familiar with Keras, but if it's using the default graph, you can get the protobuf as `tf.get_default_graph().as_graph_def()` – Yaroslav Bulatov Apr 05 '16 at 16:09

5 Answers5

7

In case you don't need to utilize a GPU in the environment you are deploying to, you could also use my library, called frugally-deep. It is available on GitHub and published under the MIT License: https://github.com/Dobiasd/frugally-deep

frugally-deep allows running forward passes on already-trained Keras models directly in C++ without the need to link against TensorFlow or any other backend.

Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
  • Any plans to support RNNs? Interesting overlap with [lwtnn](https://github.com/lwtnn/lwtnn) which handles RNNs (but not convolutions). – Shep Jan 12 '18 at 02:06
  • @Shep I would like to support them in the future, but I don't have a schedule planned for this yet. – Tobias Hermann Jan 12 '18 at 10:16
4

You can access TensorFlow backend by:

import keras.backend.tensorflow_backend as K

Then you can call any TensorFlow utility or function like:

K.tf.ConfigProto
Ahmed
  • 59
  • 1
  • 2
4

This seems to be answered in "Keras as a simplified interface to TensorFlow: tutorial", posted on The Keras Blog by Francois Chollet.

In particular, section II, "Using Keras models with TensorFlow".

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Louis M
  • 4,036
  • 4
  • 21
  • 25
2

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

Amir
  • 2,259
  • 1
  • 19
  • 29
1

Make sure you change the learning phase of keras backend to store proper values of the layers (like dropout or batch normalization). Here is a discussion about it.

Hazarapet Tunanyan
  • 2,809
  • 26
  • 30