Tensorflow Estimator API 在 eval 模式下保存图像摘要 [英] Tensorflow Estimator API save image summary in eval mode

查看:34
本文介绍了Tensorflow Estimator API 在 eval 模式下保存图像摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我尝试使用 Tensorflow 的新 Estimator API 在自定义图像数据集上训练自动编码器.

at the moment I try to train a autoencoder on a custom image dataset using the new Estimator API of Tensorflow.

到目前为止一切正常.我唯一的问题是当模型处于评估模式时将输入和输出图像保存为摘要.我在训练模式下创建的所有图像摘要都正确存储并显示在 Tensorboard 中.

So far everything is working. The only problem I have is to save the input and output images as summary when the model is in evaluation mode. All image summaries I create in train mode are stored and shown in Tensorboard properly.

这是我的代码:

def model_fn_autoencoder(features, labels, mode, params):
    is_training = mode == ModeKeys.TRAIN

    # Define model's architecture
    logits = architecture_autoencoder(features, is_training=is_training)

    # Loss, training and eval operations are not needed during inference.
    loss = None
    train_op = None
    #eval_metric_ops = {}

    if mode != ModeKeys.INFER:
        loss = tf.reduce_mean(tf.square(logits - features))
        train_op = get_train_op_fn(loss, params)

        #eval_metric_ops = get_eval_metric_ops(labels, predictions)

    if mode == ModeKeys.TRAIN:
        for i in range(10):
            tf.summary.image("Input/Train/" + str(i), tf.reshape(features[i],[1, 150, 150, 3]))
            tf.summary.image("Output/Train/" + str(i), tf.reshape(logits[i],[1, 150, 150, 3]))

    if mode == ModeKeys.EVAL:
        for i in range(10):
            tf.summary.image("Input/Eval/" + str(i), tf.reshape(features[i], [1, 150, 150, 3]))
            tf.summary.image("Output/Eval/" + str(i), tf.reshape(logits[i], [1, 150, 150, 3]))

    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=logits,
        loss=loss,
        train_op=train_op,
        #eval_metric_ops=eval_metric_ops

也许有人可以告诉我我做错了什么?

Maybe someone can tell me what I'm doing wrong?

更新以下是估算器和实验创建的函数:

Update Here are the functions for the estimator and experiment creation:

估算器:

def get_estimator(run_config, params):
    return tf.estimator.Estimator(
        model_fn=model_fn_autoencoder,  # First-class function
        params=params,  # HParams
        config=run_config  # RunConfig
    )

实验:

def experiment_fn(run_config, params):
    run_config = run_config.replace(save_checkpoints_steps=params.min_eval_frequency)

    estimator = get_estimator(run_config, params)

    tf_path = 'path/to/tfrecord'
    train_file = 'Crops-Faces-Negtives-150-150.tfrecord'
    val_file = 'Crops-Faces-Negtives-150-150-TEST.tfrecord'
    tfrecords_train = [os.path.join(tf_path, train_file)]
    tfrecords_test = [os.path.join(tf_path, val_file)]

    # Setup data loaders
    train_input_fn = get_train_inputs(batch_size=128, tfrecord_files=tfrecords_train)
    eval_input_fn = get_train_inputs(batch_size=128, tfrecord_files=tfrecords_test)

    # Define the experiment
    experiment = tf.contrib.learn.Experiment(
        estimator=estimator,  # Estimator
        train_input_fn=train_input_fn,  # First-class function
        eval_input_fn=eval_input_fn,  # First-class function
        train_steps=params.train_steps,  # Minibatch steps
        min_eval_frequency=params.min_eval_frequency,  # Eval frequency
        eval_steps=10  # Number of eval batches
    )

    return experiment

推荐答案

使用 TF1.4,您可以通过 tf.estimator.EstimatorSpecevaluation_hooks.evaluation_hooks 是一个钩子列表,您必须向其中添加以下钩子:

With TF1.4, you can pass tf.estimator.EstimatorSpec evaluation_hooks. The evaluation_hooks is a list of hooks, and you must add to it the following hook:

# Create a SummarySaverHook
eval_summary_hook = tf.train.SummarySaverHook(
                                save_steps=1,
                                output_dir= self.job_dir + "/eval_core",
                                summary_op=tf.summary.merge_all())
# Add it to the evaluation_hook list
evaluation_hooks.append(eval_summary_hook)

#Now, return the estimator:
return tf.estimator.EstimatorSpec(
                mode=mode,
                predictions=predictions,
                loss=loss,
                train_op=train_op,
                training_hooks=training_hooks,
                eval_metric_ops=eval_metric_ops,
                evaluation_hooks=evaluation_hooks)

现在您可以简单地添加 tf.summary.image 并将其放入 Tensorboard.使用您在 eval_summary 钩子中使用的指定输出目录的父目录上打开 Tensrobaord.在我的示例中,它被称为eval_core",因此我在其父目录中打开了 Tensorboard,如下图所示,它很好地显示在蓝色框中.

Now you can simply add tf.summary.image and have it in Tensorboard. Make use you open Tensrobaord on a parent directory of the specified output directory you used in the eval_summary hook. In my example it was called 'eval_core', so I opened Tensorboard on its parent directory, and as you can see in the picture below, it is showing up nicely in a blue box.

这篇关于Tensorflow Estimator API 在 eval 模式下保存图像摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆