【问题标题】:How to get evaluation result during training convolutional neural network (cnn) using tensorflow如何在使用 tensorflow 训练卷积神经网络 (cnn) 期间获得评估结果
【发布时间】:2018-05-14 19:25:01
【问题描述】:

我使用 TensorFlow 构建超分辨率卷积神经网络以提高图像分辨率。网络接受低分辨率图像作为输入并生成高分辨率图像作为输出。

对于训练,我使用tf.estimator.Estimator

def get_estimator(run_config=None, params=None):
    """Return the model as a Tensorflow Estimator object.
    Args:
         run_config (RunConfig): Configuration for Estimator run.
         params (HParams): hyperparameters.
    """
    return tf.estimator.Estimator(
        model_fn=model_fn,  # First-class function
        params=params,  # HParams
        config=run_config  # RunConfig
    )

tf.contrib.learn.Experiment包裹

def experiment_fn(run_config, params):
    """Create an experiment to train and evaluate the model.
    Args:
        run_config (RunConfig): Configuration for Estimator run.
        params (HParam): Hyperparameters
    Returns:
        (Experiment) Experiment for training the mnist model.
    """
    # You can change a subset of the run_config properties as
    run_config = run_config.replace(save_checkpoints_steps=params.min_eval_frequency)
    estimator = get_estimator(run_config, params)
    # # Setup data loaders
    train_input_fn = get_input_fn(params.filenames, params.epoch, True, params.batch_size)
    eval_input_fn = get_input_fn(params.filenames, 1, False, params.batch_size)

    # 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=params.eval_steps  # Minibatch steps
    )
    return experiment

我通过tf.contrib.learn.learn_runner 运行它,如下所示:

def run_experiment(config, session):
    assert os.path.exists(config.tfrecord_dir)
    assert os.path.exists(os.path.join(config.tfrecord_dir, config.dataset, config.subset))


    save_config(config.summaries_dir, config)

    filenames = get_tfrecord_files(config)
    batch_number = min(len(filenames), config.train_size) // config.batch_size
    logging.info('Total number of batches  %d' % batch_number)

    params = tf.contrib.training.HParams(
        learning_rate=config.learning_rate,
        device=config.device,
        epoch=config.epoch,
        batch_size=config.batch_size,
        min_eval_frequency=100,
        train_steps=None,  # Use train feeder until its empty
        eval_steps=1,  # Use 1 step of evaluation feeder
        filenames=filenames
    )
    run_config = tf.contrib.learn.RunConfig(model_dir=config.checkpoint_dir)

    learn_runner.run(
        experiment_fn=experiment_fn,  # First-class function
        run_config=run_config,  # RunConfig
        schedule="train_and_evaluate",  # What to run
        hparams=params  # HParams
    )

Experiment 类提供了在训练期间进行评估的方法 train_and_evaluate。

我的问题是如何在训练 cnn 的过程中获得评估结果(输出图像)?我想查看时间训练结果。

我在github 上的项目

【问题讨论】:

    标签: image-processing tensorflow conv-neural-network tensorflow-estimator


    【解决方案1】:

    我认为您正在寻找使用 tf.summary.image 向您的模型添加图像摘要。

    它使在 Tensorboard 中训练期间的图像可视化变得容易:

    def model_fn(...):
        ...
        # max_outputs control the number of images in the batch you want to display
        tf.summary.image("train_images", images, max_outputs=3)
        # ...
        return tf.estimator.EstimatorSpec(...)
    

    在评估期间,我认为没有一种简单的方法可以在tf.estimator 中显示图像。问题是在评估期间,只能显示整数或浮点值。

    更详细地说,在评估时,您返回 eval_metric_ops,其中包含例如您的准确性。 TensorFlow 将在 TensorBoard 中显示此 dict 中的每个整数或浮点值,但如果您尝试显示其他任何内容(例如:图像),则会向您发出警告。 (源码:函数_write_dict_to_summary

    WARNING:tensorflow:skipping summary for eval_images, must be a float, np.float32, np.int64, np.int32 or int.

    一种解决方法可能是取回 tf.estimator 之外的图像值并在 TensorBoard 中手动显示它们。


    编辑:stackoverflow 上有另一个相关的question,还有两个GitHub 问题herehere 来跟踪这方面的进展。 据我了解,他们将尝试让返回 eval_metric_ops 中的图像摘要变得容易,该摘要将自动出现在 TensorBoard 中。

    【讨论】:

      猜你喜欢
      • 2016-09-30
      • 2018-05-01
      • 2020-12-07
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多