2017-06-01 83 views
2

我寫了一個測試代碼,當我運行它時,它表示Fetch參數不能被解釋爲Tensor。我真的不知道發生了什麼,可以有人告訴我如何解決它?非常感謝你。這裏是代碼獲取參數<tf.Tensor'batch:0'shape =(128,56,56,3)dtype = float32>不能被解釋爲張量。

# coding=utf-8 
from color_1 import read_and_decode, get_batch, get_test_batch 
import color_inference 
import cv2 
import os 
import time 
import numpy as np 
import tensorflow as tf 
import color_train 
import math 

EVAL_INTERVAL_SECS=10 
batch_size=128 
num_examples = 10000 
crop_size=56 
def test(test_x, test_y): 
    with tf.Graph().as_default() as g: 
     image_holder = tf.placeholder(tf.float32, [batch_size, 56, 56, 3], name='x-input') 
     label_holder = tf.placeholder(tf.int32, [batch_size], name='y-input') 

     y=color_inference.inference(image_holder) 

     num_iter = int(math.ceil(num_examples/batch_size)) 
     true_count = 0 
     total_sample_count = num_iter * batch_size 
     saver=tf.train.Saver() 
     top_k_op = tf.nn.in_top_k(y, label_holder, 1) 
     while True: 
      with tf.Session() as sess: 
       ckpt=tf.train.get_checkpoint_state(color_train.MODEL_SAVE_PATH) 
       if ckpt and ckpt.model_checkpoint_path: 
        saver.restore(sess,ckpt.model_checkpoint_path) 
        global_step=ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 
        image_batch, label_batch = sess.run([test_x, test_y]) 
        predictions = sess.run([top_k_op], feed_dict={image_holder: image_batch, 
                    label_holder: label_batch}) 
        true_count += np.sum(predictions) 
        precision = true_count * 1.0/total_sample_count 
        print("After %s training step,the prediction is :%g",global_step,precision) 
       else: 
        print('No checkpoint file found') 
        return 
      time.sleep(EVAL_INTERVAL_SECS) 

def main(argv=None): 
    test_image, test_label = read_and_decode('val.tfrecords') 
    test_images, test_labels = get_test_batch(test_image, test_label, batch_size, crop_size) 
    test(test_images, test_labels) 

if __name__=='__main__': 
    tf.app.run() 

和錯誤是在這裏:

File "/home/vrview/tensorflow/example/char/tfrecords/color_test.py", line 57, in <module> 
    tf.app.run() 
    File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run 
    _sys.exit(main(_sys.argv[:1] + flags_passthrough)) 
    File "/home/vrview/tensorflow/example/char/tfrecords/color_test.py", line 54, in main 
    test(test_images, test_labels) 
    File "/home/vrview/tensorflow/example/char/tfrecords/color_test.py", line 39, in test 
    image_batch, label_batch = sess.run([test_x, test_y]) 
    File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run 
    run_metadata_ptr) 
    File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 952, in _run 
    fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string) 
    File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 408, in __init__ 
    self._fetch_mapper = _FetchMapper.for_fetch(fetches) 
    File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 230, in for_fetch 
    return _ListFetchMapper(fetch) 
    File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 337, in __init__ 
    self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] 
    File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 238, in for_fetch 
    return _ElementFetchMapper(fetches, contraction_fn) 
    File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 274, in __init__ 
    'Tensor. (%s)' % (fetch, str(e))) 
ValueError: Fetch argument <tf.Tensor 'batch:0' shape=(128, 56, 56, 3) dtype=float32> cannot be interpreted as a Tensor. (Tensor Tensor("batch:0", shape=(128, 56, 56, 3), dtype=float32) is not an element of this graph.) 
+0

我有同樣的問題。我沒有設法在不同的功能中分離建築物和運行圖。無論如何,如果你需要改變圖中的任何東西,你可以使用佔位符。另一個解決方法是保存圖形並進行初始化https://github.com/tensorflow/tensorflow/issues/1758 – zina

回答

1

您專注於錯誤信息的錯誤部分。相關部分是

張量不是該圖的元素。

的問題是,你在你的函數test創建一個圖形g,是不是在其中作爲參數提供的佔位符test_xtest_y已經創建了一個相同的。

最簡單的解決辦法是在main創建您的圖形g

def main(argv=None): 
    test_image, test_label = read_and_decode('val.tfrecords') 
    with tf.Graph().as_default(): 
     test_images, test_labels = get_test_batch(test_image, test_label, 
                batch_size, crop_size) 
     test(test_images, test_labels) 
+0

對不起,我嘗試了你所說的,但它仍然是錯誤的。我得到了同樣的錯誤。你是否意識到它在「image_batch,label_batch = sess.run([test_x,test_y])這一行中說的錯誤」 –

+0

你是否用't.Graph()。as_default()在'測試'? – user1735003

+0

是的,當然,我嘗試了他們兩個,但仍然沒有工作。我認爲行「image_batch,label_batch = sess.run([test_x,test_y])」是錯誤的關鍵,但我不' t知道如何解決 –

相關問題