简体   繁体   English

如何使用checkpoint的tf.estimator.Estimator进行预测?

[英]How to make predictions with tf.estimator.Estimator from checkpoint?

I just trained a CNN to recognise sunspots with tensorflow. 我刚训练CNN识别具有张量流的太阳黑子。 My model is pretty much the same as this . 我的模型与此基本相同。 The problem is that I cannot find anywhere a clear explanation on how to make predictions with the checkpoint generated by the training phase. 问题是我无法找到关于如何使用训练阶段生成的检查点进行预测的明确解释。

Tried using the standard restore method: 尝试使用标准还原方法:

saver = tf.train.import_meta_graph('./model/model.ckpt.meta')
saver.restore(sess,'./model/model.ckpt')

but then I cannot figure out how to run it. 但后来我无法弄清楚如何运行它。
Tried using tf.estimator.Estimator.predict() like this: 尝试使用tf.estimator.Estimator.predict()这样:

# Create the Estimator (should reload the last checkpoint but it doesn't)
sunspot_classifier = tf.estimator.Estimator(
    model_fn=cnn_model_fn, model_dir="./model")

# Set up logging for predictions
# Log the values in the "Softmax" tensor with label "probabilities"
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
    tensors=tensors_to_log, every_n_iter=50)

# predict with the model and print results
pred_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": pred_data},
    shuffle=False)
pred_results = sunspot_classifier.predict(input_fn=pred_input_fn)
print(pred_results)

but what it does is spitting out <generator object Estimator.predict at 0x10dda6bf8> . 但它做的是吐出<generator object Estimator.predict at 0x10dda6bf8> While if I use the same code but with tf.estimator.Estimator.evaluate() it works like a charm (reloads the model, performs evaluation and sends it to TensorBoard). 虽然如果我使用相同的代码,但使用tf.estimator.Estimator.evaluate()它就像一个魅力(重新加载模型,执行评估并将其发送到TensorBoard)。

I know there are many similar questions but I couldn't really find the way that worked for me. 我知道有很多类似的问题,但我真的找不到对我有用的方法。

sunspot_classifier.predict(input_fn=pred_input_fn) returns generator. sunspot_classifier.predict(input_fn=pred_input_fn)返回生成器。 So pred_results is generator object. 所以pred_results是生成器对象。 To get value from it you need to iterate it by next(pred_results) 要从中获取价值,您需要next(pred_results)迭代它next(pred_results)

The solution is print(next(pred_results)) 解决方案是print(next(pred_results))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM