5

Is there a way to save a Keras model to the format of CNTK so that we can load it by using CNTK's API?

I have tried this code below but it failed:

a = Input(shape=(224,224,3))
b = Conv2D(64, (2,2), name='conv1')(a)
model = Model(inputs=a, outputs=b)
cntk.combine(model.outputs).save('model2.dnn')
petezurich
  • 9,280
  • 9
  • 43
  • 57
logen
  • 76
  • 1
  • 5
  • 1
    Possible duplicate of [Keras-CNTK saving model-v2 format](https://stackoverflow.com/questions/44394213/keras-cntk-saving-model-v2-format) – sluki Oct 03 '18 at 13:27

3 Answers3

3

Please see Keras-CNTK saving model-v2 format

To save Keras model in CNTK format (when using CNTK as back end):

import cntk as C
from keras.backend import load_model

keras_model = load_model('my_keras_model.h5')

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

To save Keras in TensorFlow format (when using TensorFlow as backend):

model = load_model('model2.h5')
sess = K.get_session()
tf_saver = tf.train.Saver()
tf_saver.save(sess=sess, save_path=checkpoint_path)
  • Yes. I can saved the model. But when I tried to load the saved model file, it report an error: `raise ValueError('Cannot load a model that is neither a file nor a byte buffer.') RuntimeError: SWIG director method error` – logen Jul 20 '17 at 08:38
0

After your code, try this:

loaded_model = cntk.load('model2.dnn')

You will find more details here.

Zp Bappi
  • 121
  • 5
0

I use this converter to convert models between keras and CNTK.

  1. Save keras model model.save('keras_model.h5')

  2. Install converter pip install -U git+https://github.com/Microsoft/MMdnn.git@master

  3. Run command: mmconvert -sf keras -iw keras_model.h5 -df cntk -om cntk_model.dnn