如何使用TensorFlow在训练卷积神经网络(CNN)期间获得评估结果



i使用TensorFlow构建超分辨率卷积神经网络来增强图像分辨率。网络接受低分辨率图像作为输入,并产生高分辨率图像作为输出。

进行培训,我使用tf.estimator.stimator

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.lealen.conpperiment

包裹
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上的项目

我想您正在使用 tf.summary.image向模型添加图像摘要。

在张量板中的训练过程中,可以轻松地可视化图像:

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将在张量板中显示此dict中的每个整数或浮动值,但是如果您尝试显示其他任何内容(例如:images),则会给您警告。(源代码:函数_write_dict_to_summary

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

解决方法可能是恢复tf.estimator之外图像的值并在张板中手动显示。


编辑:stackoverflow上还有另一个相关问题,在此处和此处都有两个GitHub问题来跟踪此过程。据我了解,他们将尝试使eval_metric_ops中的图像摘要变得容易自动出现在Tensorboard中。

最新更新