2017-09-08 3 views
0

Tensorflowで画像内の数字を特定するプログラムを書いた。モデルは関数で訓練され、次に別の関数で使用されて図にラベルを付けます。訓練は私のコンピュータ上で行われ、結果のモデルは解決機能付きのawsにアップロードされます。Tensorflowプログラムは、aws lambdaに展開した後、さまざまな答えを出す

私のコンピュータはうまく動作しますが、awsでラムダを作成すると、それは異常な動作をして、同じテストデータで異なる回答を出し始めます。

解決機能でモデルがこれです:数字をラベル付けする機能コードを解決

# Recreate neural network from model file generated during training 
# input 
x = tf.placeholder(tf.float32, [None, size_of_image]) 
# weights 
W = tf.Variable(tf.zeros([size_of_image, num_chars])) 
# biases 
b = tf.Variable(tf.zeros([num_chars])) 

はこれです:同じテストデータで

for testi in range(captcha_letters_num): 

    # load model from file 
    saver = tf.train.import_meta_graph(model_path + '.meta', 
             clear_devices=True) 
    saver.restore(sess, model_path) 

    # Data to label 
    test_x = np.asarray(char_imgs[testi], dtype=np.float32) 

    predict_op = model(test_x, W, b) 

    op = sess.run(predict_op, feed_dict={x: test_x}) 

    # find max probability from the probability distribution returned by softmax 
    max_probability = op[0][0] 
    max_probability_index = -1 

    for i in range(num_chars): 
     if op[0][i] > max_probability: 
      max_probability = op[0][i] 
      max_probability_index = i 

    # append it to final output 
    final_text += char_map_list[max_probability_index] 

    # Reset the model so it can be used again 
    tf.reset_default_graph() 

それは別の答えを与え、ドン」理由を知りません。

+0

Oh no!それは自己認識しています! – dashmug

+0

@dashmug haha​​hah –

+0

あなたは 'tf.Session'をどこで作成しますか? 'saver = tf.train.import_meta_graph(...)'、 'saver.restore(...)'、 'sess = tf.Session()'を 'for testi in rangeの外に移動することをお勧めしますcaptcha_letters_num): 'それは違いがあるかどうかを確認するループです。 – mrry

答えて

0

解決済み!

私が最終的に行うことは、セッションをループの外に保ち、変数を初期化することでした。ループを終了した後、グラフをリセットします。

saver = tf.train.Saver() 

sess = tf.Session() 

# Initialize variables 
sess.run(tf.global_variables_initializer()) 

. 
. 
. 

# passing each of the 5 characters through the NNet 
for testi in range(captcha_letters_num): 

    # Data to label 
    test_x = np.asarray(char_imgs[testi], dtype=np.float32) 

    predict_op = model(test_x, W, b) 

    op = sess.run(predict_op, feed_dict={x: test_x}) 

    # find max probability from the probability distribution returned by softmax 
    max_probability = op[0][0] 
    max_probability_index = -1 

    for i in range(num_chars): 
     if op[0][i] > max_probability: 
      max_probability = op[0][i] 
      max_probability_index = i 

    # append it to final output 
    final_text += char_map_list[max_probability_index] 

# Reset the model so it can be used again 
tf.reset_default_graph() 

sess.close() 
関連する問題