Python 获取参数<;tf.张量';批次:0';形状=(128,56,56,3)数据类型=浮动32>;不能解释为张量。

Python 获取参数<;tf.张量';批次:0';形状=(128,56,56,3)数据类型=浮动32>;不能解释为张量。,python,tensorflow,deep-learning,Python,Tensorflow,Deep Learning,我写了一个测试代码,当我运行它时,它说Fetch参数不能被解释为张量。我真的不知道发生了什么。有人能告诉我如何修复它吗?非常感谢。这是密码 # 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 im

我写了一个测试代码,当我运行它时,它说Fetch参数不能被解释为张量。我真的不知道发生了什么。有人能告诉我如何修复它吗?非常感谢。这是密码

# 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.)
文件“/home/vrview/tensorflow/example/char/tfrecords/color_test.py”,第57行,在
tf.app.run()
文件“/home/vrview/tensorflow/local/lib/python2.7/site packages/tensorflow/python/platform/app.py”,第44行,正在运行
_系统出口(主(_sys.argv[:1]+标志_passthrough))
文件“/home/vrview/tensorflow/example/char/tfrecords/color_test.py”,第54行,在main中
测试(测试图像、测试标签)
文件“/home/vrview/tensorflow/example/char/tfrecords/color_test.py”,第39行,测试中
image\u batch,label\u batch=sess.run([test\u x,test\u y])
文件“/home/vrview/tensorflow/local/lib/python2.7/site packages/tensorflow/python/client/session.py”,第767行,正在运行
运行_元数据_ptr)
文件“/home/vrview/tensorflow/local/lib/python2.7/site packages/tensorflow/python/client/session.py”,第952行,正在运行
fetch\u handler=\u FetchHandler(self.\u图形、fetches、feed\u dict\u字符串)
文件“/home/vrview/tensorflow/local/lib/python2.7/site packages/tensorflow/python/client/session.py”,第408行,在__
self.\u fetch\u mapper=\u FetchMapper.for\u fetch(fetches)
文件“/home/vrview/tensorflow/local/lib/python2.7/site packages/tensorflow/python/client/session.py”,第230行,用于获取
return\u ListFetchMapper(fetch)
文件“/home/vrview/tensorflow/local/lib/python2.7/site packages/tensorflow/python/client/session.py”,第337行,在__
self._mappers=[_FetchMapper.for_fetch(fetch)for fetch in fetches]
文件“/home/vrview/tensorflow/local/lib/python2.7/site packages/tensorflow/python/client/session.py”,第238行,for_fetch
return\u ElementFetchMapper(fetches,contraction\u fn)
文件“/home/vrview/tensorflow/local/lib/python2.7/site packages/tensorflow/python/client/session.py”,第274行,在__
张量。(%s)'(提取,str(e)))
ValueError:无法将Fetch参数解释为张量。(张量张量(“批次:0”,形状=(128,56,56,3),dtype=float32)不是此图的元素。)

您关注错误消息的错误部分。相关部分是

张量不是这个图的一个元素

问题在于,您在函数
test
中创建了一个图形
g
,该图形与创建占位符
test_x
test_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)

很抱歉我尝试了你说的话,但还是错了。我得到了同样的错误。你意识到它在“image\u batch,label\u batch=sess.run([test\u x,test\u y])”一行中说的错误了吗?你是否用tf.Graph()删除了
。在
test
中作为g
默认值()?是的,当然。我尝试了这两个,但仍然不起作用。我认为这行“image\u batch,label\u batch=sess.run([test\u x,test\y])”是错误的关键,但我不知道如何解决,您是否也在
读取和解码中创建了任何tensorflow对象?另一个原因可能是您实际如何定义
测试x
测试y
,但代码缺失。请加上。我也有同样的问题。我没能在不同的功能中将构建图和运行图分开。无论如何,如果需要更改图形中的任何内容,可以使用占位符。另一个解决方法是保存图形并执行初始操作