tensorflowtensorflow-estimator

Is it possible to restore a tensorflow estimator from saved model?


I use tf.estimator.train_and_evaluate() to train my custom estimator. My dataset is partitioned 8:1:1 for training, evaluation and test. At the end of the training, I would like to restore the best model, and evaluate the model using tf.estimator.Estimator.evaluate() with the test data. The best model is currently exported using tf.estimator.BestExporter.

While tf.estimator.Estimator.evaluate() accepts checkpoint_path and restores variables, I cannot find any easy way to use the exported model generated by tf.estimator.BestExporter. I could of course keep all checkpoints during training, and look for the best model by myself, but that seems quite suboptimal.

Could anyone tell me an easy workaround? Maybe it is possible to convert a saved model to a checkpoint?


Solution

  • Hope someone else will find a cleaner way..

    tf.estimator.BestExporter exports the best model like this:

    <your_estimator.model_dir>
    +--export
       +--best_exporter
          +--xxxxxxxxxx(timestamp)
             +--saved_model.pb
             +--variables
                +--variables.data-00000-of-00001
                +--variables.index
    

    On the other hand, in your_estimator.model_dir, checkpoints are stored in three files.

    model.ckpt-xxxx.data-00000-of-00001
    model.ckpt-xxxx.index
    model.ckpt-xxxx.meta
    

    First, I used tf.estimator.Estimator.evaluate(..., checkpoint_path='<your_estimator.model_dir>/export/best_exporter/<xxxxxxxxxx>/variables/variables'), but this did not work.

    After copying one of the metafiles in your_estimator.model_dir, and renaming it "variables.meta", the evaluation seemed to work all right.