2017-08-04 26 views
0

真的很感谢您阅读我的问题OutOfRangeError异常:RandomShuffleQueue '_7_shuffle_batch_1/random_shuffle_queue' 被关闭,没有足够的元素(请求200,当前大小为0)

我的数据集约为9779图像(.DCM)。有两个标签,一个是1,拥有5000个训练图像,另一个是0,拥有4779个图像。

我使用TFrecords来合并和建立一个数据集。然后将其提供给CNN模型。

writer = tf.python_io.TFRecordWriter("train.tfrecords") 
    for idx, img_path in enumerate(all_images):#all_images is a list containing all path of all images 
     img = dm.read_file(img_path) 

     pixel_bytes = img.PixelData 
     img_raw = pixel_bytes 
     if idx < len_all_cancer_images: 
      example = tf.train.Example(features=tf.train.Features(feature={ 
      "label": tf.train.Feature(int64_list = tf.train.Int64List(value = [1])), 
      'img_raw': tf.train.Feature(bytes_list = tf.train.BytesList(value = [img_raw]))})) 
      writer.write(example.SerializeToString()) 

     else: 
      example = tf.train.Example(features=tf.train.Features(feature={ 
      "label": tf.train.Feature(int64_list = tf.train.Int64List(value=[0])), 
      'img_raw': tf.train.Feature(bytes_list = tf.train.BytesList(value = [img_raw]))})) 
      writer.write(example.SerializeToString()) 

    writer.close() 

然后我用TFRecordReader读它

filename = '/Users/wuzhenglin/Python_nice/SAL_LUNG/train.tfrecords' 
    filename_queue = tf.train.string_input_producer([filename]) 

    reader = tf.TFRecordReader() 
    _, serialized_example = reader.read(filename_queue) 
    features = tf.parse_single_example(serialized_example, 
             features={ 
              'label': tf.FixedLenFeature([], tf.int64), 
              'img_raw' : tf.FixedLenFeature([], tf.string), 
             }) 

    print features['img_raw'] 
    print features['label'] 



    img = tf.decode_raw(features['img_raw'], tf.uint8) 
    img = tf.reshape(img, [512, 512, 1]) 
    img = tf.cast(img, tf.float32) * (1./255) 
    label = tf.cast(features['label'], tf.int32) 

然后,当我想读我的数据

img_batch, label_batch = tf.train.shuffle_batch([img, label], 
               batch_size = 200, capacity = 9779, 
               min_after_dequeue = 2000) 
init = tf.global_variables_initializer() 

    with tf.Session() as sess: 
     sess.run(init) 

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

     for i in xrange(1): 
      a, b = sess.run([img_batch, label_batch]) 
      a_ = a[0] 
      b_ = b[0] 
      img_1 = tf.reshape(a_[0, :, :, :], [512, 512]) 
      print img_1.shape 
      print b_.shape 
      print b_ 
      print '********************' 
      plt.imshow(sess.run(img_1), cmap='gray') 

我要挑一些数据,并打印出来,但错误.. 。

第一张:

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 524288 values, but the requested shape has 262144 
    [[Node: Reshape = Reshape[T=DT_UINT8, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](DecodeRaw, Reshape/shape)]] 

二:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile 
    execfile(filename, namespace) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile 
    builtins.execfile(filename, *where) 
    File "/Users/wuzhenglin/Python_nice/SAL_LUNG/test.py", line 96, in <module> 
    read_and_decode() 
    File "/Users/wuzhenglin/Python_nice/SAL_LUNG/test.py", line 82, in read_and_decode 
    a, b = sess.run([img_batch, label_batch]) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run 
run_metadata_ptr) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 965, in _run 
    feed_dict_string, options, run_metadata) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1015, in _do_run 
target_list, options, run_metadata) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1035, in _do_call 
    raise type(e)(node_def, op, message) 
tensorflow.python.framework.errors_impl.OutOfRangeError: RandomShuffleQueue '_7_shuffle_batch_1/random_shuffle_queue' is closed and has insufficient elements (requested 200, current size 0) 
    [[Node: shuffle_batch_1 = QueueDequeueManyV2[component_types=[DT_FLOAT, DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](shuffle_batch_1/random_shuffle_queue, shuffle_batch_1/n)]] 

Caused by op u'shuffle_batch_1', defined at: 
    File "<stdin>", line 1, in <module> 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile 
    execfile(filename, namespace) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile 
    builtins.execfile(filename, *where) 
    File "/Users/wuzhenglin/Python_nice/SAL_LUNG/test.py", line 96, in <module> 
read_and_decode() 
    File "/Users/wuzhenglin/Python_nice/SAL_LUNG/test.py", line 71, in read_and_decode 
min_after_dequeue = 2000) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/tensorflow/python/training/input.py", line 1165, in shuffle_batch 
name=name) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/tensorflow/python/training/input.py", line 739, in _shuffle_batch 
dequeued = queue.dequeue_many(batch_size, name=name) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/data_flow_ops.py", line 458, in dequeue_many 
    self._queue_ref, n=n, component_types=self._dtypes, name=name) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/gen_data_flow_ops.py", line 1310, in _queue_dequeue_many_v2 
    timeout_ms=timeout_ms, name=name) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op 
op_def=op_def) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2327, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "/Users/wuzhenglin/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1226, in __init__ 
    self._traceback = _extract_stack() 

OutOfRangeError (see above for traceback): RandomShuffleQueue '_7_shuffle_batch_1/random_shuffle_queue' is closed and has insufficient elements (requested 200, current size 0) 
    [[Node: shuffle_batch_1 = QueueDequeueManyV2[component_types=[DT_FLOAT, DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](shuffle_batch_1/random_shuffle_queue, shuffle_batch_1/n)]] 

而且一些有用的信息,该image.dcm为512 * 512,RGB

真的真的谢谢你:-)

回答

0

1错误InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 524288 values, but the requested shape has 262144意味着你输入重塑操作的大小是[512,512,2]

# this line causes the error,, you have to confirm that your image size 
img = tf.reshape(img, [512, 512, 1]) 
# should be 
img = tf.reshape(img, [512, 512, 2]) 
# another line with mkstake; here you dont need to use tensorflow reshape; you can use numpy reshape since a[0] is python object 
# img_1 = tf.reshape(a_[0, :, :, :], [512, 512]) 
img_1 = np.reshape(a_[0, :, :, :], [512, 512, 2]) 

第二个错误c由于第一个错误而失败。所以请确保你没有输入图像大小的错误。

+0

它仍然不起作用,InvalidArgumentError(参见上述用于回溯):输入重塑是与524288个值的张量,但请求的形状具有262144 \t [[节点:整形=整形[T = DT_UINT8,T形= DT_INT32 ,_device =“/ job:localhost/replica:0/task:0/cpu:0”](DecodeRaw,Reshape/shape)]] – JourneyWoo

+0

看到编辑,我的意思是你的输入图像大小不是512,512,1;它不知何故512,512,2 –

+0

但我该如何改变重塑值? – JourneyWoo

相关问题