Keras-CNTK saving model-v2 format

两盒软妹~` 提交于 2019-12-22 08:19:42

问题


I'm using CNTK as the backend for Keras. I'm trying to use my model which I have trained using Keras in C++.

I have trained and saved my model using Keras which is in HDF5. How do I now use CNTK API to save it in their model-v2 format?

I tried this:

model = load_model('model2.h5')
cntk.ops.functions.Function.save(model, 'CNTK_model2.pb')

but i got the following error:

TypeError: save() missing 1 required positional argument: 'filename'

If tensorflow were the backend I would have done this:

model = load_model('model2.h5')
sess = K.get_session()
tf_saver = tf.train.Saver()
tf_saver.save(sess=sess, save_path=checkpoint_path)

How can I achieve the same thing?


回答1:


As per the comments here, I was able to use this:

import cntk as C
import keras.backend as K

keras_model = K.load_model('my_keras_model.h5')

C.combine(keras_model.model.outputs).save('my_cntk_model')
cntk_model = C.load_model('my_cntk_model')



回答2:


You can do something like this model.outputs[0].save('CNTK_model2.pb') I'm assuming here you have called model.compile (i.e. that's the only case I have tried :-)




回答3:


The reason you see this error is because keras' cntk backend use a user defined function to do reshape on batch axis, which can't been serialized. We have fixed this issue in CNTK v2.2. Please upgrade your cntk to v2.2, and upgrade keras to last master. Please see this pull request: https://github.com/fchollet/keras/pull/7907



来源:https://stackoverflow.com/questions/44394213/keras-cntk-saving-model-v2-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!