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

问题描述 投票:0回答:1

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

我的问题是:如何在训练cnn期间获得评估结果(输出图像)?我想看一个时间训练结果。

我在github的项目

image-processing tensorflow conv-neural-network tensorflow-estimator
1个回答
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时间你返回包含例如你的准确性的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中的图像摘要。

© www.soinside.com 2019 - 2024. All rights reserved.