是否可以从保存的模型中恢复张量流估计器?
Is it possible to restore a tensorflow estimator from saved model?
我使用 tf.estimator.train_and_evaluate()
来训练我的自定义估算器。我的数据集被分区 8:1:1 用于训练、评估和测试。在训练结束时,我想恢复最好的模型,并使用 tf.estimator.Estimator.evaluate()
和测试数据评估模型。目前使用 tf.estimator.BestExporter
.
导出最佳模型
虽然 tf.estimator.Estimator.evaluate()
接受 checkpoint_path
并恢复变量,但我找不到任何简单的方法来使用 tf.estimator.BestExporter
生成的导出模型。我当然可以在训练期间保留所有检查点,然后自己寻找最好的模型,但这似乎不太理想。
谁能告诉我一个简单的解决方法?也许可以将保存的模型转换为检查点?
希望其他人能找到更清洁的方法..
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='<your_estimator.model_dir>/export/best_exporter/<xxxxxxxxxx>/variables/variables')
,但这没有用。
复制 your_estimator.model_dir
中的一个图元文件并重命名后 "variables.meta",评估似乎正常。
我也是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 没有 "proper" 加载 SavedModel
的方式让您感到厌烦,我已经创建了一个 issue 在 GitHub.
但是,如果您尝试将它加载到不同的设备上,请参阅我的其他问题:
TensorFlow v1.10+ load SavedModel with different device placement or manually set dynamic device placement?
TensorFlow Estimator clear_deivces in exporters?
哪个地址设备放置,还有其他 GitHub 个问题
简而言之,目前,如果您使用 Estimator
出口商。如果您在设置 clear_devices
的 model_fn
中手动导出 Estimator
,那么您应该可以开始了。目前似乎没有办法在您导出模型后更改此设置。
根据@SumNeuron 的 Github 问题的解决方案 tf.contrib.estimator.SavedModelEstimator
是从保存的模型加载到 Estimator
的方法。
以下对我有用:
estimator = tf.contrib.estimator.SavedModelEstimator(saved_model_dir)
prediction_results = estimator.predict(input_fn)
令人困惑的是,这基本上完全没有记录。
也许你可以试试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.estimator.train_and_evaluate()
来训练我的自定义估算器。我的数据集被分区 8:1:1 用于训练、评估和测试。在训练结束时,我想恢复最好的模型,并使用 tf.estimator.Estimator.evaluate()
和测试数据评估模型。目前使用 tf.estimator.BestExporter
.
虽然 tf.estimator.Estimator.evaluate()
接受 checkpoint_path
并恢复变量,但我找不到任何简单的方法来使用 tf.estimator.BestExporter
生成的导出模型。我当然可以在训练期间保留所有检查点,然后自己寻找最好的模型,但这似乎不太理想。
谁能告诉我一个简单的解决方法?也许可以将保存的模型转换为检查点?
希望其他人能找到更清洁的方法..
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='<your_estimator.model_dir>/export/best_exporter/<xxxxxxxxxx>/variables/variables')
,但这没有用。
复制 your_estimator.model_dir
中的一个图元文件并重命名后 "variables.meta",评估似乎正常。
我也是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 没有 "proper" 加载 SavedModel
的方式让您感到厌烦,我已经创建了一个 issue 在 GitHub.
但是,如果您尝试将它加载到不同的设备上,请参阅我的其他问题:
TensorFlow v1.10+ load SavedModel with different device placement or manually set dynamic device placement?
TensorFlow Estimator clear_deivces in exporters?
哪个地址设备放置,还有其他 GitHub 个问题
简而言之,目前,如果您使用 Estimator
出口商。如果您在设置 clear_devices
的 model_fn
中手动导出 Estimator
,那么您应该可以开始了。目前似乎没有办法在您导出模型后更改此设置。
根据@SumNeuron 的 Github 问题的解决方案 tf.contrib.estimator.SavedModelEstimator
是从保存的模型加载到 Estimator
的方法。
以下对我有用:
estimator = tf.contrib.estimator.SavedModelEstimator(saved_model_dir)
prediction_results = estimator.predict(input_fn)
令人困惑的是,这基本上完全没有记录。
也许你可以试试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")
然后一切都会好的