我使用 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 期间获得评估结果(输出图像)?我想看到一个时间训练结果。