Compatibility between keras and tf.keras models

扶醉桌前 提交于 2020-12-29 18:16:37

问题


I am interested in training a model in tf.keras and then loading it with keras. I know this is not highly-advised, but I am interested in using tf.keras to train the model because

  1. tf.keras is easier to build input pipelines
  2. I want to take advantage of the tf.dataset API

and I am interested in loading it with keras because

  1. I want to use coreml to deploy the model to ios.
  2. I want to use coremltools to convert my model to ios, and coreml tools only works with keras, not tf.keras.

I have run into a few road-blocks, because not all of the tf.keras layers can be loaded as keras layers. For instance, I've had no trouble with a simple DNN, since all of the Dense layer parameters are the same between tf.keras and keras. However, I have had trouble with RNN layers, because tf.keras has an argument time_major that keras does not have. My RNN layers have time_major=False, which is the same behavior as keras, but keras sequential layers do not have this argument.

My solution right now is to save the tf.keras model in a json file (for the model structure) and delete the parts of the layers that keras does not support, and also save an h5 file (for the weights), like so:

model = # model trained with tf.keras

# save json
model_json = model.to_json()
with open('path_to_model_json.json', 'w') as json_file:
    json_ = json.loads(model_json)
    layers = json_['config']['layers']
    for layer in layers:
        if layer['class_name'] == 'SimpleRNN':
            del layer['config']['time_major']
    json.dump(json_, json_file)

# save weights
model.save_weights('path_to_my_weights.h5')

Then, I use the coremlconverter tool to convert from keras to coreml, like so:

with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
    coreml_model = coremltools.converters.keras.convert(
        model=('path_to_model_json','path_to_my_weights.h5'),
        input_names=#inputs, 
        output_names=#outputs,
        class_labels = #labels, 
        custom_conversion_functions = { "GlorotUniform": tf.keras.initializers.glorot_uniform
                                            }
    )
    coreml_model.save('my_core_ml_model.mlmodel')

My solution appears to be working, but I am wondering if there is a better approach? Or, is there imminent danger in this approach? For instance, is there a better way to convert tf.keras models to coreml? Or is there a better way to convert tf.keras models to keras? Or is there a better approach that I haven't thought of?

Any advice on the matter would be greatly appreciated :)


回答1:


Your approach seems good to me!

In the past, when I had to convert tf.keras model to keras model, I did following:

  • Train model in tf.keras
  • Save only the weights tf_model.save_weights("tf_model.hdf5")
  • Make Keras model architecture using all layers in keras (same as the tf keras one)
  • load weights by layer names in keras: keras_model.load_weights(by_name=True)

This seemed to work for me. Since, I was using out of box architecture (DenseNet169), I had to very less work to replicate tf.keras network to keras.



来源:https://stackoverflow.com/questions/57152123/compatibility-between-keras-and-tf-keras-models

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