Keras 将最佳检查点恢复到估计量tensorflow 2.x

Keras 将最佳检查点恢复到估计量tensorflow 2.x,keras,tensorflow2.0,tensorflow-datasets,tensorflow-estimator,Keras,Tensorflow2.0,Tensorflow Datasets,Tensorflow Estimator,简单地说,我使用tensorflow数据集API设置了一个数据输入管线。然后,我使用keras实现了一个CNN分类模型,并将其转换为估计器。我将我的输入反馈给我的估计器训练和评估规格,为训练和评估提供输入数据。作为最后一步,我启动了带有tf.estimator.train\u和\u evaluate def my_input_fn(tfrecords_path): dataset = (...) return batch_fbanks, batch_labels def bu

简单地说,我使用tensorflow数据集API设置了一个数据输入管线。然后,我使用keras实现了一个CNN分类模型,并将其转换为估计器。我将我的输入反馈给我的估计器训练和评估规格,为训练和评估提供输入数据。作为最后一步,我启动了带有
tf.estimator.train\u和\u evaluate

def my_input_fn(tfrecords_path):

    dataset = (...)
    return batch_fbanks, batch_labels

def build_model():
    model = tf.keras.models.Sequential()
    model.add(...)
    model.compile(...)

    return model

model = build_model()

run_config=tf.estimator.RunConfig(model_dir,save_summary_steps=100,save_checkpoints_steps=1000)
estimator = tf.keras.estimator.model_to_estimator(model,config=run_config)

def serving_input_receiver_fn():
    inputs = {'Conv1_input': tf.compat.v1.placeholder(shape=[None, 11,120,1], dtype=tf.float32)}
    return tf.estimator.export.ServingInputReceiver(inputs, inputs)

exporter = tf.estimator.BestExporter(serving_input_receiver_fn, name="best_exporter", exports_to_keep=5)

train_spec_dnn = tf.estimator.TrainSpec(input_fn = lambda: my_input_fn(train_data_path),hooks=[hook])
eval_spec_dnn = tf.estimator.EvalSpec(input_fn = lambda: my_eval_input_fn(eval_data_path),exporters=exporter,start_delay_secs=0,throttle_secs=15)

tf.estimator.train_and_evaluate(estimator, train_spec_dnn, eval_spec_dnn)

我使用
tf.estimator.BestExporter
保存5个最佳检查点,如上所示。完成培训后,我想重新加载最佳模型并将其转换为估计器,以重新评估模型并在新数据集上进行预测。然而,我的问题是将检查点恢复到估计器。我尝试了几种解决方案,但每次我没有得到估计器对象时,我需要运行其
evaluate
predict
方法

为了进一步说明,每个最佳检查点目录的组织如下:

./
  variables/
      variables.data-00000-of-00002
      variables.data-00001-of-00002
      variables.index
  saved_model.pb
所以问题是,我怎样才能从最佳检查点得到一个估计器对象,这样我就可以用它来评估我的模型并预测新的数据

注意:我发现一些建议的解决方案依赖于TensorFlow v1功能,但这些功能无法解决我的问题,因为我使用的是TF v2


非常感谢,非常感谢您的帮助。

您可以使用下面从tf.estimator.BestExporter创建的类。

它所做的是,除了保存最佳模型(.pb文件等),它还将保存 最佳导出模型检查点位于不同文件夹上

以下是课程:

import shutil, glob, os
# import tensorflow.logging as logging
## the path where all the checkpoint reside
BEST_CHECKPOINTS_PATH_FROM = 'PATH TO ALL CHECKPOINT FILES'
## the path it will save the best exporter checkpoint files
BEST_CHECKPOINTS_PATH_TO = 'PATH TO BEST EXPORTER CHECKPOINT FILES TO BE SAVE' 

class BestCheckpointsExporter(tf.estimator.BestExporter):
      def export(self, estimator, export_path, checkpoint_path, eval_result,is_the_final_export):
          if self._best_eval_result is None or \
                    self._compare_fn(self._best_eval_result, eval_result):
                    #print('Exporting a better model ({} instead of {})...'.format(eval_result, self._best_eval_result))
              for name in glob.glob(checkpoint_path + '.*'):
                    print(name)
                    print(os.path.join(BEST_CHECKPOINTS_PATH_TO, os.path.basename(name)))
                    shutil.copy(name, os.path.join(BEST_CHECKPOINTS_PATH_TO, os.path.basename(name)))
                # also save the text file used by the estimator api to find the best checkpoint
              with open(os.path.join(BEST_CHECKPOINTS_PATH_TO, "checkpoint"), 'w') as f:
                    f.write("model_checkpoint_path: \"{}\"".format(os.path.basename(checkpoint_path)))
              self._best_eval_result = eval_result
          else:
              print('Keeping the current best model ({} instead of {}).'.format(self._best_eval_result, eval_result))
类的示例用法
您只需通过调用类来替换导出器,并传递服务\u input\u receiver\u fn

def serving_input_receiver_fn():
    inputs = {'my_dense_input': tf.compat.v1.placeholder(shape=[None, 4], dtype=tf.float32)}
    return tf.estimator.export.ServingInputReceiver(inputs, inputs)

exporter = BestCheckpointsExporter(serving_input_receiver_fn=serving_input_receiver_fn) 

train_spec_dnn = tf.estimator.TrainSpec(input_fn = input_fn, max_steps=5)

eval_spec_dnn = tf.estimator.EvalSpec(input_fn=input_fn,exporters=exporter,start_delay_secs=0,throttle_secs=15)
(x, y) =  tf.estimator.train_and_evaluate(keras_estimator, train_spec_dnn, eval_spec_dnn)
此时,它将在指定的文件夹中保存最佳导出的模型检查点文件

要加载检查点文件,您需要执行以下步骤:
步骤1:重建模型实例

def build_model():
    model = tf.keras.models.Sequential()
    model.add(...)
    model.compile(...)

    return model

model = build_model()
步骤2:使用模型负载权重API
参考URL:


注意:所有这些都是在google colab上模拟的。

您可以使用下面从tf.estimator.BestExporter创建的类

它所做的是,除了保存最佳模型(.pb文件等),它还将保存 最佳导出模型检查点位于不同文件夹上

以下是课程:

import shutil, glob, os
# import tensorflow.logging as logging
## the path where all the checkpoint reside
BEST_CHECKPOINTS_PATH_FROM = 'PATH TO ALL CHECKPOINT FILES'
## the path it will save the best exporter checkpoint files
BEST_CHECKPOINTS_PATH_TO = 'PATH TO BEST EXPORTER CHECKPOINT FILES TO BE SAVE' 

class BestCheckpointsExporter(tf.estimator.BestExporter):
      def export(self, estimator, export_path, checkpoint_path, eval_result,is_the_final_export):
          if self._best_eval_result is None or \
                    self._compare_fn(self._best_eval_result, eval_result):
                    #print('Exporting a better model ({} instead of {})...'.format(eval_result, self._best_eval_result))
              for name in glob.glob(checkpoint_path + '.*'):
                    print(name)
                    print(os.path.join(BEST_CHECKPOINTS_PATH_TO, os.path.basename(name)))
                    shutil.copy(name, os.path.join(BEST_CHECKPOINTS_PATH_TO, os.path.basename(name)))
                # also save the text file used by the estimator api to find the best checkpoint
              with open(os.path.join(BEST_CHECKPOINTS_PATH_TO, "checkpoint"), 'w') as f:
                    f.write("model_checkpoint_path: \"{}\"".format(os.path.basename(checkpoint_path)))
              self._best_eval_result = eval_result
          else:
              print('Keeping the current best model ({} instead of {}).'.format(self._best_eval_result, eval_result))
类的示例用法
您只需通过调用类来替换导出器,并传递服务\u input\u receiver\u fn

def serving_input_receiver_fn():
    inputs = {'my_dense_input': tf.compat.v1.placeholder(shape=[None, 4], dtype=tf.float32)}
    return tf.estimator.export.ServingInputReceiver(inputs, inputs)

exporter = BestCheckpointsExporter(serving_input_receiver_fn=serving_input_receiver_fn) 

train_spec_dnn = tf.estimator.TrainSpec(input_fn = input_fn, max_steps=5)

eval_spec_dnn = tf.estimator.EvalSpec(input_fn=input_fn,exporters=exporter,start_delay_secs=0,throttle_secs=15)
(x, y) =  tf.estimator.train_and_evaluate(keras_estimator, train_spec_dnn, eval_spec_dnn)
此时,它将在指定的文件夹中保存最佳导出的模型检查点文件

要加载检查点文件,您需要执行以下步骤:
步骤1:重建模型实例

def build_model():
    model = tf.keras.models.Sequential()
    model.add(...)
    model.compile(...)

    return model

model = build_model()
步骤2:使用模型负载权重API
参考URL:

注:所有这些都是在google colab上模拟的