有没有办法只使用tensorflow.estimator.train_and_evaluate()来保存最佳模型?

问题描述 投票:0回答:2

我尝试使用tf.estimator.train_and_evaluate()方法从checkpoint中使用已经.config文件重新训练TF对象检测API模型以获取训练管道,例如models / research / object_detection / model_main.py。它每N步或每N秒节省一次检查点。

但我想只保存一个像Keras一样的最佳模型。有没有办法用TF Object Detection API模型做到这一点?也许某些选项/回调用于tf.Estimator.train或某种方式使用Check API与Keras?

python tensorflow machine-learning computer-vision object-detection-api
2个回答
4
投票

你可以尝试使用BestExporter。据我所知,这是你想要做的唯一选择。

exporter = tf.estimator.BestExporter(
      compare_fn=_loss_smaller,
      exports_to_keep=5)

eval_spec = tf.estimator.EvalSpec(
    input_fn,
    steps,
    exporters)

https://www.tensorflow.org/api_docs/python/tf/estimator/BestExporter


2
投票

我一直在使用适合我的https://github.com/bluecamel/best_checkpoint_copier

例:

best_copier = BestCheckpointCopier(
   name='best', # directory within model directory to copy checkpoints to
   checkpoints_to_keep=10, # number of checkpoints to keep
   score_metric='metrics/total_loss', # metric to use to determine "best"
   compare_fn=lambda x,y: x.score < y.score, # comparison function used to determine "best" checkpoint (x is the current checkpoint; y is the previously copied checkpoint with the highest/worst score)
   sort_key_fn=lambda x: x.score,
   sort_reverse=False) # sort order when discarding excess checkpoints

将它传递给你的eval_spec:

eval_spec = tf.estimator.EvalSpec(
   ...
   exporters=best_copier,
   ...)
© www.soinside.com 2019 - 2024. All rights reserved.