2017-01-12 12 views
0

GPUでTensorflow 0.12.1を実行しています。私は、訓練されたDeep CNNモデルを持っています。そのモデルの重みは、チェックポイントファイルを使って保存しました。推論の間に、私はrestorer.restore(sess, tf.train.latest_checkpoint(FLAGS.train_dir))を使って保存されたチェックポイントをリロードします。コードは問題なしで実行されているようですが、スクリプトを再実行するたびに、私は出力が乱れてしまいます。 AFAIK、私はテストセットの入力をシャッフルしません。入力がロードされ、ネットワークに正しく供給されています。それは同じテストセット上のCNNの異なる走行の出力だけであり、同じオーダーを使用すると非常に異なる出力を生成します。私は困惑している!また、推測の間にinit_opを実行せずに、保存されたチェックポイントでロードされたグラフを実行するにはどうすればよいですか?私のコードは、実行前にすべてのグローバル変数とローカル変数を初期化する必要があります。 (私が最初に初期化し、その後、唯一のチェックポイントを復元!)ここに私のコードのスニペットがあります:。保存されたチェックポイントを使用する複数の推論モデルが確率的エラーを生成する - Tensorflow

import tensorflow as tf 
import numpy as np 
import os 
import os.path 
from datetime import datetime 
import time 
import random 
import json 

from tensorflow.python.framework import ops 
from tensorflow.python.framework import dtypes 


from modelFCNN3 import model 

def read_input(inp_queue,height=224,width=224,channels=3, mask=False): 
    value = tf.read_file(inp_queue) 
    image = tf.image.decode_png(value) 
    image = tf.image.resize_images(image, [height, width],method=2) 
    image = tf.cast(image, tf.uint8) 
    image.set_shape([height,width,channels]) 
    image = tf.reshape(image,[height,width,channels]) 
    if mask: 
     image = tf.to_float(tf.greater_equal(image,128)) 
     image = tf.cast(image,tf.float32) 
    else: 
     image = tf.image.per_image_standardization(image) 
     image = tf.cast(image,tf.float32) 
    return image 




if __name__ == '__main__': 

    tf.reset_default_graph() 

    with open('X_test.json', 'r') as infile: 
     X_test = json.load(infile) 

    with open('y_test.json', 'r') as infile: 
     y_test = json.load(infile) 

    imagelist = ops.convert_to_tensor(X_test, dtype=dtypes.string) 
    labellist = ops.convert_to_tensor(y_test, dtype=dtypes.string) 

    input_queue = tf.train.slice_input_producer([imagelist, labellist], 
              num_epochs=1, 
              shuffle=False) 

    image = read_input(input_queue[0],height=224,width=224,channels=3, mask=False) 

    label = read_input(input_queue[1],height=224,width=224,channels=1, mask=True) 

    images_batch, labels_batch = tf.train.batch([image, label], batch_size=FLAGS.batch_size, 
     enqueue_many=False,shapes=None, allow_smaller_final_batch=True) 

    global_step = tf.Variable(0, trainable=False) 
    images = tf.placeholder_with_default(images_batch, shape=[None, 224,224,3]) 
    labels = tf.placeholder_with_default(labels_batch, shape=[None, 224,224,1]) 

    restorer = tf.train.Saver() 

    logits = model(images).logits 
    labels = tf.cast(labels,tf.int32) 
    labels.set_shape([FLAGS.batch_size,224,224,1]) 

    valid_prediction = tf.argmax(tf.nn.softmax(logits), dimension=3) 
    valid_prediction.set_shape([FLAGS.batch_size,224,224]) 

    meanIOU,update_op_mIOU= tf.contrib.metrics.streaming_mean_iou(tf.cast(valid_prediction,tf.int32), tf.squeeze(labels),FLAGS.num_classes) 

    init = tf.global_variables_initializer() 
    init_locals = tf.local_variables_initializer() 


    with tf.Session() as sess: 

     sess.run([init, init_locals]) 

     restorer.restore(sess, tf.train.latest_checkpoint(FLAGS.train_dir)) 
     print("Model restored.") 


     coord = tf.train.Coordinator() 
     threads = tf.train.start_queue_runners(coord=coord,sess=sess) 
     summary_writer = tf.summary.FileWriter(FLAGS.train_dir, sess.graph) 

     try: 
      step = 0 
      avg = [] 
      while not coord.should_stop(): 
       myimg, predimg, mylbl= sess.run([images,valid_prediction,labels]) 
       mIOU,_ = sess.run([meanIOU,update_op_mIOU]) 
       avg.append(mIOU) 

      step += 1 

     except tf.errors.OutOfRangeError: 
      print('Done training -- epoch limit reached') 

     finally: 

      coord.request_stop() 
      coord.join(threads) 
      sess.close() 

答えて

1

あなたが同じマシンまたは別のマシン上で実行しています #saver = tf.train.Saver()

次のコメントはtensorflow文書に記載されています #注:保存されたmeta_graphからの再起動トレーニングは、デバイスの割り当てが変更されていない場合にのみ機能します。 #saver = tf.train.import_meta_graph(メタファイル)

関連する問題