会话中的Tensor.eval()问题

时间:2018-10-30 20:53:47

标签: python tensorflow image-processing

我有一个非常简单的脚本,可以在张量流中获取图像,对其进行解码,然后尝试将该张量存储为numpy数组。但是,在下面的当前代码中,在评估会话中的张量时,tensorflow无限期挂起。

import tensorflow as tf

result = 0

#Get image into appropriately sized tensor
filename_queue = 

tf.train.string_input_producer(['C:\Python3\TensorflowExp\Pokemon_data\filename0.jpg'])
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
my_img = tf.image.decode_jpeg(value, channels=1)
resized_image = tf.image.resize_images(my_img, [299, 299])

#print out tensor
print(resized_image)

# Construct a `Session` to execute the graph.
sess = tf.Session()

#initialize Session
init = tf.global_variables_initializer()
sess.run(init)

#Print out numpy array in tensor 
with sess.as_default():
    result = resized_image.eval()

我尝试添加队列运行器,初始化全局变量并以各种方式运行会话。在这些情况下,我收到一个未知错误,无法调试也无法通过谷歌搜索出该问题是什么。

Traceback (most recent call last):
  File "C:\Python3\lib\site-packages\tensorflow\python\client\session.py", line 1350, in _do_call
    return fn(*args)
  File "C:\Python3\lib\site-packages\tensorflow\python\client\session.py", line 1329, in _run_fn
    status, run_metadata)
  File "C:\Python3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: NewRandomAccessFile failed to Create/Open: C:\Python3\TensorflowExp\Pokemon_datailename0.jpg : The filename, directory name, or volume label syntax is incorrect.
; Unknown error
         [[Node: ReaderReadV2 = ReaderReadV2[_device="/job:localhost/replica:0/task:0/device:CPU:0"](WholeFileReaderV2, input_producer)]]
         [[Node: DecodeJpeg/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_5_DecodeJpeg", tensor_type=DT_UINT8, _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".\ex5.py", line 27, in <module>
    print(resized_image.eval())
  File "C:\Python3\lib\site-packages\tensorflow\python\framework\ops.py", line 648, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "C:\Python3\lib\site-packages\tensorflow\python\framework\ops.py", line 4758, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "C:\Python3\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
    run_metadata_ptr)
  File "C:\Python3\lib\site-packages\tensorflow\python\client\session.py", line 1128, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Python3\lib\site-packages\tensorflow\python\client\session.py", line 1344, in _do_run
    options, run_metadata)
  File "C:\Python3\lib\site-packages\tensorflow\python\client\session.py", line 1363, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: NewRandomAccessFile failed to Create/Open: C:\Python3\TensorflowExp\Pokemon_datailename0.jpg : The filename, directory name, or volume label syntax is incorrect.
; Unknown error
         [[Node: ReaderReadV2 = ReaderReadV2[_device="/job:localhost/replica:0/task:0/device:CPU:0"](WholeFileReaderV2, input_producer)]]
         [[Node: DecodeJpeg/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_5_DecodeJpeg", tensor_type=DT_UINT8, _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]

Caused by op 'ReaderReadV2', defined at:
  File ".\ex5.py", line 8, in <module>
    key, value = reader.read(filename_queue)
  File "C:\Python3\lib\site-packages\tensorflow\python\ops\io_ops.py", line 209, in read
    return gen_io_ops._reader_read_v2(self._reader_ref, queue_ref, name=name)
  File "C:\Python3\lib\site-packages\tensorflow\python\ops\gen_io_ops.py", line 713, in _reader_read_v2
    queue_handle=queue_handle, name=name)
  File "C:\Python3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Python3\lib\site-packages\tensorflow\python\framework\ops.py", line 3160, in create_op
    op_def=op_def)
  File "C:\Python3\lib\site-packages\tensorflow\python\framework\ops.py", line 1625, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): NewRandomAccessFile failed to Create/Open: C:\Python3\TensorflowExp\Pokemon_datailename0.jpg : The filename, directory name, or volume label syntax is incorrect.
; Unknown error
         [[Node: ReaderReadV2 = ReaderReadV2[_device="/job:localhost/replica:0/task:0/device:CPU:0"](WholeFileReaderV2, input_producer)]]
         [[Node: DecodeJpeg/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_5_DecodeJpeg", tensor_type=DT_UINT8, _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]

任何指导将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

由于tf.train.string_input_producer正在从tf.Queuebase创建队列管道,因此可以预期这种行为。如果像您这样,执行操作时队列为空,它将一直阻塞,直到有一个要出队的元素为止。

with块更改为以下内容以终止:

with sess.as_default():

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    result = resized_image.eval()

    coord.request_stop()
    coord.join(threads)

请参见https://www.tensorflow.org/api_docs/python/tf/train/Coordinator