New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert Keras model to TensorFlow #3223
Comments
When you are using the TensorFlow backend, your Keras code is actually building a TF graph. You can just grab this graph. Keras only uses one graph and one session. You can access the session via: |
Also this should be relevant to you: http://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html |
I have a CNN model built with Keras and I am trying to run in on Android. In order to do this, I need to convert my model to TF. Thanks! |
The above tutorial is outdated. I made it work by replacing |
Here is my sample code: Just set the parameters, and run. |
Nice job amir, I haven't gotten around to trying your method yet. I'm currently trying to use tf.SavedModel as mentioned above. Saving the meta-graph seems to work fine. but on reloading, most tutorials just stop at the loading part. We save the information about input/output inside of signature_def inside the pb , however I'm unsure of how to read in the information from signature_def. |
Any suggestions on converting TF model to Keras? Thanks |
Anyone has the example in the reverse way? Tensorflow to Keras? |
@amir-abdi Thank you for your post, I tried your script and I successfully created protobuf model. So can you tell me whether It is possible to use this model in android device with TFdetect in here |
@VajiraPrabuddhaka I have successfully deployed the converted TF models on Android. So, yes, the models work on Android. |
@amir-abdi did you test it with TF detect example here?? |
@VajiraPrabuddhaka No, I have not. |
@VajiraPrabuddhaka @amir-abdi just wanted to reconfirm if you are able to seamlessly export keras models and use them in android tensorflow examples ? we are debating between tensorflow and keras - and this would be very helpful |
A very interesting issue! I think this should be an example code here. |
@sandys I can confirm that I have converted several Keras models to TensorFlow models using this code and deployed the models on Android phone. |
@amir-abdi tried your sample code. The generated .pb file is valid and I am able to load it into my Android application. However, the results from TensorflowInferenceInterface seem to be wrong. The results dont match to my Keras testing results. |
@anilmaddala I wouldn't know what the source of error might be. However, I can confirm that we have used the same code to convert our models to tensorflow, deployed them on Android, and the results were OK. Our data are medical images. |
@amir-abdi re-did my training and app, your process works successfully! for MNIST CNN trained in Keras. I am able to port Keras -> Tensorflow -> Android and classify input digits. However I am having trouble with LSTM/GRU, any idea what needs to change? Thanks |
@anilmaddala My models have LSTMs and they are converted with no problem using the same code (https://github.com/amir-abdi/keras_to_tensorflow). My only hypothesis is that maybe your models were not generated using the latest version of the library. Try updating your libraries. |
@amir-abdi I tried using your script to convert my eras .h5 model to tensorflow .pb file but it does not seem to work. The script generates a .pb file but the print out of output nodes does not show any nodes at all. Which seems to suggest that the tensorflow model conversion did not go through correctly. Below is the full output of the script -UserWarning: No training configuration found in save file: the model was not compiled. Compile it manually.
|
@anil80 Is your problem resolved? |
Hi @amir-abdi ..I tried your script but my couldnt do it.. |
@adityabansal123 Are you talking about my code here: https://github.com/amir-abdi/keras_to_tensorflow ? |
hi @amir-abdi I try use your code, but its failed, error is: |
@trangtv57 |
I already have model file and weight file, and still can load model trained for using predict by load_model, however when I try use your code @amir-abdi its show error:
|
thanks you @amir-abdi , |
@trangtv57 How to solve it, I met the same error |
@mengxiabing you should show me the error detail. |
@trangtv57 Please help analysis set input arguments optional arguments: |
model.h5 you should provide absolute path of file, like /home/yourname/name_of_file. Because this error show function read file can't read file because it's not recognize that string: "model.h5" is file not found. |
as @trangtv57 mentioned, you either need to set the input folder by the |
@trangtv57 Thank you, the new error, the custom layer cannot be converted |
i wonder that you save your model to 2 file, model config, and weight or just file h5( file save weight). Because you show error make me difficult to understand your error |
Doesn't work out-of-the-box for python 2.7, but the code is there and that enough for me! thanks!! |
@yfarjoun could you share your version thats working for py2.7? |
I don't have this code in 2.7, but the heart of the matter is import keras as K
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io
from keras.models import Model, load_model
sess = K.get_session()
model=load_model("model.hd5")
constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
output="graph.pb"
graph_io.write_graph(constant_graph, "/", output, as_text=False)
print('saved the freezed graph (ready for inference) at: ', output) |
@yfarjoun Thanks! I just had to make the minor change of |
Above mentioned methods are working on minor changes but can you suggest a way to load converted .pb to .h5 model again |
I found it's still hard to convert keras or tensorflow model to tensorflow lite format. There're not any tutorials about this. |
I don't know if it is still useful for anyone, I referred to a standard code provided by Google on the Cloudml git for a similar requirement where they are converting Keras' HDF5 model to a Tensorflow serving model. You can try to reuse it: https://github.com/GoogleCloudPlatform/cloudml-samples/blob/master/census/keras/trainer/model.py
|
@amir-abdi Can you please provide the versions of Keras and Tensorflow you have used? I am facing this issue using your repo.
|
Firstly, checkout the updated keras_to_tensorflow tool. Moreover, you are trying to convert (I assume it is something similar to mobilenet). Your model has defined custom layers which are not being recognized by you keras library. Check this thread. You can introduce custom layers to keras for your model to load like this:
|
Good work @amir-abdi /i need help in signature Defs part. I have to deploy my code on google cloud which basically consists of two lstm models working asynchronously to predict a sentence as an output. I have build my model in keras and converted my hdf5 file into pb using @amir-abdi tool. Now I want to know that how can i access my signatureDefs parameters like input output and model as mentioned here? with tf.Session(graph=tf.Graph()) as sess: builder.add_meta_graph_and_variables(sess, with tf.Session(graph=tf.Graph()) as sess: builder.add_meta_graph(["bar-tag", "baz-tag"], builder.save()` as given here |
I know there are a lot of scripts online that can easily convert a keras model to a tf model but just wondering why keras team doesn't wanna include this util function into keras so that people doesn't need to look at SO or github to find the solutions. Is there any consideration of not doing that? if not probably I can contribute directly. Let me know. Thanks! |
Got here to search for a solution but fortunately found one myself.
This seems to work for me. |
the version I used: |
i still do not understand how to use this code. Anyone can help me? I have model.h5 and want to convert it to model.pb but I do not know how to do it using this code. Where should I change in the code? |
We have models produced by Keras from our researchers. For production deployment, we want run pure TensorFlow.
How is it possible to convert Keras model to a TensorFlow? I understand that Keras must be doing this as it supports TensorFlow runtime.
Thanks!
The text was updated successfully, but these errors were encountered: