Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some way to save best model only with tensorflow.estimator.train_and_evaluate()?

I try retrain TF Object Detection API model from checkpoint with already .config file for training pipeline with tf.estimator.train_and_evaluate() method like in models/research/object_detection/model_main.py. And it saves checkpoints every N steps or every N seconds.

But I want to save only one best model like in Keras. Is there some way to do it with TF Object Detection API model? Maybe some options/callbacks for tf.Estimator.train or some way to use Detection API with Keras?

like image 468
Silicon Avatar asked Mar 05 '19 14:03

Silicon


2 Answers

I have been using https://github.com/bluecamel/best_checkpoint_copier which works well for me.

Example:

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

pass it to your eval_spec:

eval_spec = tf.estimator.EvalSpec(
   ...
   exporters=best_copier,
   ...)
like image 87
prouast Avatar answered Nov 10 '22 00:11

prouast


You can try using BestExporter. As far as I know, it's the only option for what you're trying to do.

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

like image 29
Sharky Avatar answered Nov 09 '22 22:11

Sharky