如何在训练卷积神经网络(cnn)中使用张量流获得评估结果

时间:2017-11-30 18:53:22

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

我使用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
    )

课程实验提供了训练期间评估的方法train_and_evaluate。

我的问题是如何在训练期间获得评估结果(输出图像)?我希望看到时间训练结果。

我在[{3}}

上的项目

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找使用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中的每个整数或浮点值,但如果您尝试显示任何其他内容(例如图像),则会给出警告。 (源代码:function _write_dict_to_summary

  

警告:tensorflow:eval_images的摘要摘要,必须是float,np.float32,np.int64,np.int32或int。

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

修改:stackoverflow上有另一个相关的question,还有两个GitHub问题herehere来跟踪此进度。 根据我的理解,他们会尝试在eval_metric_ops中返回自动显示在TensorBoard中的图像摘要。

相关问题