2017-10-20 89 views
1

此刻我嘗試使用Tensorflow的新Estimator API在自定義圖像數據集上訓練自動編碼器。Tensorflow Estimator API以eval模式保存圖像摘要

到目前爲止一切正常。我遇到的唯一問題是當模型處於評估模式時,將輸入和輸出圖像保存爲摘要。我在列車模式下創建的所有圖像摘要均已妥善保存並顯示在Tensorboard中。

這裏是我的代碼:

def model_fn_autoencoder(features, labels, mode, params): 
    is_training = mode == ModeKeys.TRAIN 

    # Define model's architecture 
    logits = architecture_autoencoder(features, is_training=is_training) 

    # Loss, training and eval operations are not needed during inference. 
    loss = None 
    train_op = None 
    #eval_metric_ops = {} 

    if mode != ModeKeys.INFER: 
     loss = tf.reduce_mean(tf.square(logits - features)) 
     train_op = get_train_op_fn(loss, params) 

     #eval_metric_ops = get_eval_metric_ops(labels, predictions) 

    if mode == ModeKeys.TRAIN: 
     for i in range(10): 
      tf.summary.image("Input/Train/" + str(i), tf.reshape(features[i],[1, 150, 150, 3])) 
      tf.summary.image("Output/Train/" + str(i), tf.reshape(logits[i],[1, 150, 150, 3])) 

    if mode == ModeKeys.EVAL: 
     for i in range(10): 
      tf.summary.image("Input/Eval/" + str(i), tf.reshape(features[i], [1, 150, 150, 3])) 
      tf.summary.image("Output/Eval/" + str(i), tf.reshape(logits[i], [1, 150, 150, 3])) 

    return tf.estimator.EstimatorSpec(
     mode=mode, 
     predictions=logits, 
     loss=loss, 
     train_op=train_op, 
     #eval_metric_ops=eval_metric_ops 

也許有人可以告訴我什麼,我做錯了什麼?

更新 下面是估計和實驗創作功能:

估算:

def get_estimator(run_config, params): 
    return tf.estimator.Estimator(
     model_fn=model_fn_autoencoder, # First-class function 
     params=params, # HParams 
     config=run_config # RunConfig 
    ) 

實驗:

def experiment_fn(run_config, params): 
    run_config = run_config.replace(save_checkpoints_steps=params.min_eval_frequency) 

    estimator = get_estimator(run_config, params) 

    tf_path = 'path/to/tfrecord' 
    train_file = 'Crops-Faces-Negtives-150-150.tfrecord' 
    val_file = 'Crops-Faces-Negtives-150-150-TEST.tfrecord' 
    tfrecords_train = [os.path.join(tf_path, train_file)] 
    tfrecords_test = [os.path.join(tf_path, val_file)] 

    # Setup data loaders 
    train_input_fn = get_train_inputs(batch_size=128, tfrecord_files=tfrecords_train) 
    eval_input_fn = get_train_inputs(batch_size=128, tfrecord_files=tfrecords_test) 

    # 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=10 # Number of eval batches 
    ) 

    return experiment 
+0

你能更新你的代碼,包括你怎麼稱呼的估計? – Mingxing

+0

對不起,我添加了估算器和實驗創建的代碼。 –

+2

TF團隊正在努力增加一種方法來在eval模式下保存摘要,就像您在EstimatorSpec構造函數中使用'training_hooks'進行訓練模式一樣。看看這裏的github問題:https://github.com/tensorflow/tensorflow/issues/14042 –

回答

0

隨着TF1.4,你可以通過TF .estimator.EstimatorSpec evaluation_hooks。 的evaluation_hooks是掛鉤的列表,並且必須添加到它下面的掛鉤:

# Create a SummarySaverHook 
eval_summary_hook = tf.train.SummarySaverHook(
           save_steps=1, 
           output_dir= self.job_dir + "/eval_core", 
           summary_op=tf.summary.merge_all()) 
# Add it to the evaluation_hook list 
evaluation_hooks.append(eval_summary_hook) 

#Now, return the estimator: 
return tf.estimator.EstimatorSpec(
       mode=mode, 
       predictions=predictions, 
       loss=loss, 
       train_op=train_op, 
       training_hooks=training_hooks, 
       eval_metric_ops=eval_metric_ops, 
       evaluation_hooks=evaluation_hooks) 

現在,你可以簡單地添加tf.summary.image和有它在Tensorboard。 使用您在eval_summary掛鉤中使用的指定輸出目錄的父目錄上打開Tensrobaord。在我的例子中,它被稱爲'eval_core',所以我在其父目錄上打開了Tensorboard,正如您在下面的圖片中看到的,它在藍色框中顯示得很好。

enter image description here