How to save keras model in different formats

Viewed 30

I am new to this field, this question might look dumb to some of you but please bear with it.

I have created a keras model which works well. I can save it in .hdf5 format using model.save("model_name.hdf5) which is good.

Main question is if there is any other format i can save my model in so that it can be used in c++/android/JavaScript. Is this even possible to do so?? If you are thinking why I am asking for all 3 languages, its because I have 3 projects each of them use respective language.

Thanks for any help in advance.

1 Answers

The answer depends on what you are going to save and use in another language.

If you just need the architecture of the model to be saved, you may save it as JSON, which can later be used in any other platform and language you are going to use.

model_json = model.to_json()

If you also need the weights and biases, I do not know any specific tool, but you can simply read the stored data in python, create a multidimensional array, and then save it in a file appropriate for any of the languages you need. For example, the weights of the second layer can be found in model.layers[2].get_weights().

Finally, if you want to run the model in another language, you need to implement the for-loops that make the processing. You might find some conversion tools for your target language. For example, for C, this tool can help.

Related