我是Tensorflow的新手adn正在尝试阅读图片和标签。下面的代码工作正常但每次完成它都会产生一个巨大的错误。
import tensorflow as tf
import numpy as np
import os
from tensorflow.python.framework import ops
from tensorflow.python.framework import dtypes
class DataSet():
def __init__(self, path, info_train, info_test):
self.image_path = path
self.info_train = info_train
self.info_test = info_test
def read_labeled_info(self):
folder = self.image_path + "/"
f = open(folder + self.info_train,"r")
lines = f.readlines()
fileNames = []
landmarks = []
attributes = []
for line in lines:
line = line.strip("\n ").split(" ")
if line == [""]:
break
fileNames.append(folder + line[0].replace("\\", "/"))
coords = []
for i in range(1,6):
coords.append([float(line[i]),float(line[i+5])])
attributes.append([int(line[i]) for i in range(11,15)])
landmarks.append([coords])
return fileNames, landmarks, attributes
def get_input_que(self):
image_list, landmark_list, attribute_list = self.read_labeled_info()
images_tensor = ops.convert_to_tensor(image_list, dtype=dtypes.string)
landmark_tensor = ops.convert_to_tensor(landmark_list, dtype=dtypes.float32)
attribute_tensor = ops.convert_to_tensor(attribute_list, dtype=dtypes.int32)
inputQue = tf.train.slice_input_producer([images_tensor, landmark_tensor, attribute_tensor])
return inputQue
def read_images_from_disk(self):
input_que = self.get_input_que()
landmarks = input_que[1]
attributes = input_que[2]
file_contents = tf.read_file(input_que[0])
images = tf.image.decode_jpeg(file_contents, channels=3)
return images, landmarks, attributes
我用以下方法测试:
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
data = DataSet("some_location", "training.txt", "testing.txt")
images, landmarks, attributes = data.read_images_from_disk()
tf.train.start_queue_runners()
image = images.eval()
print(image.shape)
coord.request_stop()
coord.join(threads)
sess.close()
我可以访问图像,但程序完成后会产生错误:
ERROR:tensorflow:Exception in QueueRunner: Enqueue operation was cancelled
[[Node: input_producer/input_producer/input_producer_EnqueueMany = QueueEnqueueManyV2[Tcomponents=[DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](input_producer/input_producer, input_producer/input_producer/RandomShuffle)]]
Caused by op 'input_producer/input_producer/input_producer_EnqueueMany', defined at:
File "<string>", line 1, in <module>
File "/usr/lib/python3.5/idlelib/run.py", line 124, in main
ret = method(*args, **kwargs)
File "/usr/lib/python3.5/idlelib/run.py", line 351, in runcode
exec(code, self.locals)
File "/home/joar/Documents/deep_learning/Deep-learning-MTFL/code/test3.py", line 58, in <module>
images, landmarks, attributes = data.read_images_from_disk()
File "/home/joar/Documents/deep_learning/Deep-learning-MTFL/code/test3.py", line 41, in read_images_from_disk
input_que = self.get_input_que()
File "/home/joar/Documents/deep_learning/Deep-learning-MTFL/code/test3.py", line 37, in get_input_que
inputQue = tf.train.slice_input_producer([images_tensor, landmark_tensor, attribute_tensor])
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/training/input.py", line 307, in slice_input_producer
shared_name=shared_name)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/training/input.py", line 264, in range_input_producer
shared_name, "fraction_of_%d_full" % capacity, name)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/training/input.py", line 164, in input_producer
enq = q.enqueue_many([input_tensor])
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/ops/data_flow_ops.py", line 367, in enqueue_many
self._queue_ref, vals, name=scope)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_data_flow_ops.py", line 1556, in _queue_enqueue_many_v2
name=name)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
op_def=op_def)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
self._traceback = _extract_stack()
CancelledError (see above for traceback): Enqueue operation was cancelled
[[Node: input_producer/input_producer/input_producer_EnqueueMany = QueueEnqueueManyV2[Tcomponents=[DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](input_producer/input_producer, input_producer/input_producer/RandomShuffle)]]
>>>
Exception in thread Thread-1:
Traceback (most recent call last):
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1039, in _do_call
return fn(*args)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1021, in _run_fn
status, run_metadata)
File "/usr/lib/python3.5/contextlib.py", line 66, in __exit__
next(self.gen)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.CancelledError: Enqueue operation was cancelled
[[Node: input_producer/input_producer/input_producer_EnqueueMany = QueueEnqueueManyV2[Tcomponents=[DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](input_producer/input_producer, input_producer/input_producer/RandomShuffle)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/training/queue_runner_impl.py", line 234, in _run
sess.run(enqueue_op)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 778, in run
run_metadata_ptr)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 982, in _run
feed_dict_string, options, run_metadata)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1032, in _do_run
target_list, options, run_metadata)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1052, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.CancelledError: Enqueue operation was cancelled
[[Node: input_producer/input_producer/input_producer_EnqueueMany = QueueEnqueueManyV2[Tcomponents=[DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](input_producer/input_producer, input_producer/input_producer/RandomShuffle)]]
Caused by op 'input_producer/input_producer/input_producer_EnqueueMany', defined at:
File "<string>", line 1, in <module>
File "/usr/lib/python3.5/idlelib/run.py", line 124, in main
ret = method(*args, **kwargs)
File "/usr/lib/python3.5/idlelib/run.py", line 351, in runcode
exec(code, self.locals)
File "/home/joar/Documents/deep_learning/Deep-learning-MTFL/code/test3.py", line 58, in <module>
images, landmarks, attributes = data.read_images_from_disk()
File "/home/joar/Documents/deep_learning/Deep-learning-MTFL/code/test3.py", line 41, in read_images_from_disk
input_que = self.get_input_que()
File "/home/joar/Documents/deep_learning/Deep-learning-MTFL/code/test3.py", line 37, in get_input_que
inputQue = tf.train.slice_input_producer([images_tensor, landmark_tensor, attribute_tensor])
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/training/input.py", line 307, in slice_input_producer
shared_name=shared_name)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/training/input.py", line 264, in range_input_producer
shared_name, "fraction_of_%d_full" % capacity, name)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/training/input.py", line 164, in input_producer
enq = q.enqueue_many([input_tensor])
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/ops/data_flow_ops.py", line 367, in enqueue_many
self._queue_ref, vals, name=scope)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_data_flow_ops.py", line 1556, in _queue_enqueue_many_v2
name=name)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
op_def=op_def)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/joar/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
self._traceback = _extract_stack()
CancelledError (see above for traceback): Enqueue operation was cancelled
[[Node: input_producer/input_producer/input_producer_EnqueueMany = QueueEnqueueManyV2[Tcomponents=[DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](input_producer/input_producer, input_producer/input_producer/RandomShuffle)]]
我不会坚持这一点,所以任何帮助都会非常适合
答案 0 :(得分:1)
我通过更新
解决了这个问题tf.train.start_queue_runners()
到
tf.train.start_queue_runners(coord=coord)
答案 1 :(得分:0)
仅供参考,ListBox
API已被弃用,并由tf.data取代。