我想在tensorflow中制作带标签的通用输入图像。 我检查了问题Tensorflow read images with labels并编写了我的测试代码,但是有一些错误。
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.framework import dtypes
txtfilename = '/home/kang/Documents/work_code_PC1/data/UCLandUsedImages/UCImage_Labels.txt'
def read_labeled_image_list(image_list_file):
"""Reads a .txt file containing pathes and labeles
Args:
image_list_file: a .txt file with one /path/to/image per line
label: optionally, if set label will be pasted after each line
Returns:
List with all filenames in file image_list_file
"""
f = open(image_list_file, 'r')
filenames = []
labels = []
for line in f:
filename, label = line[:-1].split(' ')
filenames.append(filename)
labels.append(int(label))
return filenames, labels
def read_images_from_disk(input_queue):
"""Consumes a single filename and label as a ' '-delimited string.
Args:
filename_and_label_tensor: A scalar string tensor.
Returns:
Two tensors: the decoded image, and the string label.
"""
label = input_queue[1]
file_contents = tf.read_file(input_queue[0])
example = tf.image.decode_png(file_contents, channels=3)
return example, label
# Reads pfathes of images together with their labels
image_list, label_list = read_labeled_image_list(txtfilename)
images = ops.convert_to_tensor(image_list, dtype=dtypes.string)
labels = ops.convert_to_tensor(label_list, dtype=dtypes.int32)
# Makes an input queue
input_queue = tf.train.slice_input_producer([images, labels],
shuffle=True)
with tf.Session() as sess:
result = sess.run([input_queue])
我想运行会话并查看input_queue,但是我收到以下错误:
File "<ipython-input-3-ddf11f8f84bc>", line 1, in <module>
runfile('/home/kang/Documents/work_code_PC1/VGG_tensorflow_UCMerced/readUClandUsedImagetxt.py', wdir='/home/kang/Documents/work_code_PC1/VGG_tensorflow_UCMerced')
File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 714, in runfile
execfile(filename, namespace)
File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile
builtins.execfile(filename, *where)
File "/home/kang/Documents/work_code_PC1/VGG_tensorflow_UCMerced/readUClandUsedImagetxt.py", line 69, in <module>
result = sess.run([input_queue])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 340, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 523, in _run
processed_fetches = self._process_fetches(fetches)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 493, in _process_fetches
% (subfetch, fetch, type(subfetch), str(e)))
TypeError: Fetch argument [<tf.Tensor 'input_producer_2/Gather:0' shape=() dtype=string>, <tf.Tensor 'input_producer_2/Gather_1:0' shape=() dtype=int32>] of [<tf.Tensor 'input_producer_2/Gather:0' shape=() dtype=string>, <tf.Tensor 'input_producer_2/Gather_1:0' shape=() dtype=int32>] has invalid type <type 'list'>, must be a string or Tensor. (Can not convert a list into a Tensor or Operation.)
我检查了image_list,label_list的值,输出是对的,那么这里有什么问题? 非常感谢你。
答案 0 :(得分:0)
我会尝试删除result = sess.run([input_queue])
中input_queue周围的方括号,因为您要将输出Feed指定为单个张量,而不是列表。