0
我想在張量流中製作帶有標籤的通用輸入圖像。 我檢查了問題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的值時,輸出是正確的,所以這裏有什麼問題? 非常感謝。