How can I convert a trained Tensorflow model to Keras?

前端 未结 4 1575
甜味超标
甜味超标 2020-12-13 06:43

I have a trained Tensorflow model and weights vector which have been exported to protobuf and weights files respectively.

How can I convert these to JSON or YAML and

相关标签:
4条回答
  • 2020-12-13 07:26

    Currently, there is no direct in-built support in Tensorflow or Keras to convert the frozen model or the checkpoint file to hdf5 format.

    But since you have mentioned that you have the code of Tensorflow model, you will have to rewrite that model's code in Keras. Then, you will have to read the values of your variables from the checkpoint file and assign it to Keras model using layer.load_weights(weights) method.

    More than this methodology, I would suggest to you to do the training directly in Keras as it claimed that Keras' optimizers are 5-10% times faster than Tensorflow's optimizers. Other way is to write your code in Tensorflow with tf.contrib.keras module and save the file directly in hdf5 format.

    0 讨论(0)
  • 2020-12-13 07:28

    Francois Chollet, the creator of keras, stated in 04/2017 "you cannot turn an arbitrary TensorFlow checkpoint into a Keras model. What you can do, however, is build an equivalent Keras model then load into this Keras model the weights" , see https://github.com/keras-team/keras/issues/5273 . To my knowledge this hasn't changed.

    A small example:

    First, you can extract the weights of a tensorflow checkpoint like this

    PATH_REL_META = r'checkpoint1.meta'
        
    # start tensorflow session
    with tf.Session() as sess:
        
        # import graph
        saver = tf.train.import_meta_graph(PATH_REL_META)
        
        # load weights for graph
        saver.restore(sess, PATH_REL_META[:-5])
            
        # get all global variables (including model variables)
        vars_global = tf.global_variables()
        
        # get their name and value and put them into dictionary
        sess.as_default()
        model_vars = {}
        for var in vars_global:
            try:
                model_vars[var.name] = var.eval()
            except:
                print("For var={}, an exception occurred".format(var.name))
    

    It might also be of use to export the tensorflow model for use in tensorboard, see https://stackoverflow.com/a/43569991/2135504

    Second, you build you keras model as usually and finalize it by "model.compile". Pay attention that you need to give you define each layer by name and add it to the model after that, e.g.

    layer_1 = keras.layers.Conv2D(6, (7,7), activation='relu', input_shape=(48,48,1))
    net.add(layer_1)
    ...
    net.compile(...)
    

    Third, you can set the weights with the tensorflow values, e.g.

    layer_1.set_weights([model_vars['conv7x7x1_1/kernel:0'], model_vars['conv7x7x1_1/bias:0']])
    
    0 讨论(0)
  • 2020-12-13 07:37

    I think the callback in keras is also a solution.

    The ckpt file can be saved by TF with:

    saver = tf.train.Saver()
    saver.save(sess, checkpoint_name)
    

    and to load checkpoint in Keras, you need a callback class as follow:

    class RestoreCkptCallback(keras.callbacks.Callback):
        def __init__(self, pretrained_file):
            self.pretrained_file = pretrained_file
            self.sess = keras.backend.get_session()
            self.saver = tf.train.Saver()
        def on_train_begin(self, logs=None):
            if self.pretrian_model_path:
                self.saver.restore(self.sess, self.pretrian_model_path)
                print('load weights: OK.')
    

    Then in your keras script:

     model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
     restore_ckpt_callback = RestoreCkptCallback(pretrian_model_path='./XXXX.ckpt') 
     model.fit(x_train, y_train, batch_size=128, epochs=20, callbacks=[restore_ckpt_callback])
    

    That will be fine. I think it is easy to implement and hope it helps.

    0 讨论(0)
  • 2020-12-13 07:42

    Unsure if this is what you are looking for, but I happened to just do the same with the newly released keras support in TF 1.2. You can find more on the API here: https://www.tensorflow.org/api_docs/python/tf/contrib/keras

    To save you a little time, I also found that I had to include keras modules as shown below with the additional python.keras appended to what is shown in the API docs.

    from tensorflow.contrib.keras.python.keras.models import Sequential

    Hope that helps get you where you want to go. Essentially once integrated in, you then just handle your model/weight export as usual.

    0 讨论(0)
提交回复
热议问题