ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – Tensorflow Estimator API在eval模式下保存图像摘要

2019-10-06 04:00:03  阅读:261  来源: 互联网

标签:python tensorflow tensorflow-estimator


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

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

这是我的代码:

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

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

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

估算:

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.EstimatorSpec evaluation_hooks.
evaluate_hooks是一个钩子列表,你必须添加以下钩子:

# 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,正如你在下面的图片中看到的那样,它在蓝色框中很好地显示出来.

enter image description here

标签:python,tensorflow,tensorflow-estimator
来源: https://codeday.me/bug/20191006/1858281.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有