【问题标题】:Is it possible to restore a tensorflow estimator from saved model?是否可以从保存的模型中恢复张量流估计器?
【发布时间】:2020-07-03 16:21:19
【问题描述】:

我使用tf.estimator.train_and_evaluate() 来训练我的自定义估算器。我的数据集按 8:1:1 划分用于训练、评估和测试。在训练结束时,我想恢复最好的模型,并使用tf.estimator.Estimator.evaluate() 和测试数据评估模型。目前使用tf.estimator.BestExporter导出最佳模型。

虽然tf.estimator.Estimator.evaluate() 接受checkpoint_path 并恢复变量,但我找不到任何简单的方法来使用tf.estimator.BestExporter 生成的导出模型。我当然可以在训练期间保留所有检查点,并自己寻找最佳模型,但这似乎并不理想。

谁能告诉我一个简单的解决方法?也许可以将保存的模型转换为检查点?

【问题讨论】:

    标签: tensorflow tensorflow-estimator


    【解决方案1】:

    也许你可以试试 tf.estimator.WarmStartSettings:https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/estimator/WarmStartSettings

    它可以在 pb 文件中加载权重并继续训练,这对我的项目有效。

    您可以按如下方式设置热启动:

    ws = WarmStartSettings(ckpt_to_initialize_from="/[model_dir]/export/best-exporter/[timestamp]/variables/variables")
    

    然后一切都会好的

    【讨论】:

    • 在 tf 1.15 中工作!将答案中的ws 输入model = tf.estimator.Estimator( ... , warm_start_from=ws) 并调用model.evaluatetf.estimator.train_and_evaluate(model, ... ) 只要模型目录中没有检查点就可以工作。所以确保 model_dir 是空的,然后你可以通过这种方式进行 eval 或继续训练。
    【解决方案2】:

    基于@SumNeuron 的Github 问题的解决方案tf.contrib.estimator.SavedModelEstimator 是从保存的模型加载到Estimator 的方法。

    以下对我有用:

    estimator = tf.contrib.estimator.SavedModelEstimator(saved_model_dir)
    prediction_results = estimator.predict(input_fn)
    

    令人困惑的是,这基本上完全没有记录。

    【讨论】:

    • +1 但要使其正常工作,您需要保存所有模式(训练、评估、预测)。例如使用tf.estimator.BestExporter 保存时,似乎只保存了预测模式
    【解决方案3】:

    希望其他人能找到更清洁的方法..

    tf.estimator.BestExporter 导出最好的模型如下:

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

    另一方面,在your_estimator.model_dir 中,检查点存储在三个文件中。

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

    首先,我使用了tf.estimator.Estimator.evaluate(..., checkpoint_path='&lt;your_estimator.model_dir&gt;/export/best_exporter/&lt;xxxxxxxxxx&gt;/variables/variables'),但这不起作用。

    在复制your_estimator.model_dir 中的一个元文件并将其重命名为“variables.meta”后,评估似乎工作正常。

    【讨论】:

      【解决方案4】:

      我也是 Estimator API 的新手,但我想我知道您在寻找什么,尽管它同样令人讨厌。

      来自这个colab,这是一个玩具定制Estimator,添加了一些花里胡哨:

      from tensorflow.contrib import predictor
      predict_fn = predictor.from_saved_model(<model_dir>)
      predict_fn(pred_features) # pred_features corresponds to your input features
      

      这个估计器都使用BestExporter

      exporter = tf.estimator.BestExporter(
          name="best_exporter",
          serving_input_receiver_fn=serving_input_receiver_fn,
          exports_to_keep=5
      ) # this will keep the 5 best checkpoints
      

      以及在训练后导出模型:

      est.export_savedmodel('./here', serving_input_receiver_fn)

      如果您对Estimator API 没有“正确”的方式来加载SavedModel 感到厌烦,我已经在 GitHub 上创建了一个issue

      但是,如果您尝试将其加载到其他设备上,请参阅我的其他问题:

      解决设备放置问题,还有其他 GitHub 问题

      简而言之,目前,如果您使用 Estimator 导出器进行导出,则您在加载 Estimator必须拥有的设备是您训练的设备。如果您在model_fn 中手动导出您的Estimator,如果您设置了clear_devices,那么您应该很高兴。目前似乎没有办法在您导出模型后更改此设置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-03
        相关资源
        最近更新 更多