【问题标题】:Convert TensorFlow model to Keras hdf5将 TensorFlow 模型转换为 Keras hdf5
【发布时间】:2021-04-08 16:24:18
【问题描述】:

嘿,我是 tensorflow 和 keras 的新手。我想知道是否有任何方法可以转换具有四个文件的 tensorflow 模型:

  • 检查点
  • 型号
  • model-18540.data-00000-of-00001
  • model-18540.index

有什么办法可以把这四个文件转成单一的keras文件格式转成hdf5

【问题讨论】:

    标签: tensorflow keras hdf5 tf.keras


    【解决方案1】:

    目前,Tensorflow 或 Keras 没有直接内置支持将冻结模型或检查点文件转换为 hdf5 格式。

    但是你可以这样。ckpt文件可以通过TF保存:

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

    要在 Keras 中加载检查点,您需要一个回调类,如下所示:

    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.')
    

    然后在你的 keras 脚本中:

     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])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-08
      • 2019-05-14
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多