Search code examples
pythontensorflowtensorflow-estimator

tf.summary.image seems not work for estimator prediction


I want visualize my input image use tf.estimator when predict, but it seems tf.summary.image not save image. But it work for training.

This is my code in model_fn:

...
summary_hook = tf.train.SummarySaverHook(
        save_secs=2,
        output_dir='summary',
        scaffold=tf.train.Scaffold(summary_op=tf.summary.merge_all()))
        #summary_op=tf.summary.merge_all())
tf.summary.histogram("logit",logits)
tf.summary.image('feat', feat)
if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(mode, predictions=preds, prediction_hooks=[summary_hook])
...

and this my prediction code:

config = tf.estimator.RunConfig(save_summary_steps=0)
estimator = tf.estimator.Estimator(model_fn=model_fn, model_dir='logs', config=config)
preds = estimator.predict(input_fn=eval_input_fn)

Is there something wrong for using tf.train.SummarySaverHook?


Solution

  • I would assume that you need to put the summary ops (histogram/image) before calling merge_all so that merge_all actually has something to merge.

    ...
    tf.summary.histogram("logit",logits)
    tf.summary.image('feat', feat)
    summary_hook = tf.train.SummarySaverHook(
        save_secs=2,
        output_dir='summary',
        scaffold=tf.train.Scaffold(summary_op=tf.summary.merge_all()))
        #summary_op=tf.summary.merge_all())
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode, predictions=preds, prediction_hooks=[summary_hook])
    ...