首页 > 解决方案 > Tfrecords 错误:“无效参数:reshape 的输入是具有 71680 个值的张量,但请求的形状具有 8960”

问题描述

那些日子,当我运行代码读取“tfrecords”数据时,我得到了那个错误。问题是如何找到“具有 71680 个值的张量”或“形状有 8960”?

错误是:

I0807 09:54:11.147000 353860 coordinator.py:224] Error reported to Coordinator: <class 'tensorflow.python.framework.errors_impl.InvalidArgumentError'>, 2 root error(s) found.
 (0) Invalid argument: Input to reshape is a tensor with 71680 values, but the requested shape has 8960
 [[{{node Reshape}}]]
 (1) Invalid argument: Input to reshape is a tensor with 71680 values, but the requested shape has 8960
 [[{{node Reshape}}]]
 [[sub/_21]]
 0 successful operations.
 0 derived errors ignored.
 Traceback (most recent call last):
 File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1356, in _do_call
return fn(*args)
 File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1341, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
 File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1429, in _call_tf_sessionrun
run_metadata)
 tensorflow.python.framework.errors_impl.OutOfRangeError: 2 root error(s) found.
 (0) Out of range: RandomShuffleQueue '_2_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 4, current size 0)
 [[{{node shuffle_batch}}]]
 (1) Out of range: RandomShuffleQueue '_2_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 4, current size 0)
 [[{{node shuffle_batch}}]]
 [[CTCLoss/_121]]
 0 successful operations.
 0 derived errors ignored.

我阅读“tfrecords”数据的代码在这里:

def read_tfrecords():
  ......
  ......
  filename_queue = tf.train.string_input_producer([tfrecords_path])
  reader = tf.TFRecordReader()
  _,serizalized_example = reader.read(filename_queue)
  features = tf.parse_single_example(serizalized_example,
                                   features = {
                                       'image_raw':tf.FixedLenFeature([], tf.string),
                                       'label': tf.VarLenFeature(tf.int64),
                                       })
  images = tf.decode_raw(features['image_raw'],tf.uint8) # Maybe error is here
  images = tf.cast(images,dtype = tf.float32)
  images = tf.reshape(images, [32, 280, 1])
  labels = tf.cast(features['label'], tf.int32)
  images_batch, label_batch = tf.train.shuffle_batch([images,labels],batch_size=batch_size,num_threads=1,
                       capacity=5000, min_after_dequeue=1000)
  return images_batch, label_batch

我认为关键在于images = tf.decode_raw(features['image_raw'],tf.uint8),但我不知道为什么以及如何。在这里我给出我的代码来制作“tfrecords”数据。

def write2tfrecords():
....
....
image = cv2.imread(image_path,0)
image = cv2.resize(image, (280, 32))
image = image/255.0-0.5
image_raw = image.tobytes() ## I think the error is here
#image_raw = image.tostring() ## It also don't work
value_label = lable
example = tf.train.Example(features = tf.train.Features(
        feature={
            'image_raw':tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_raw])),
            'label':tf.train.Feature(int64_list=tf.train.Int64List(value=[value_label]))
        }))
    writer.write(example.SerializeToString())
    log.info('Finished image {:s}'.format(key))
writer.close()

当我PIL.Image用来替换时cv2,它可以工作!这里的图像 high=32,width=280 。有人知道为什么会这样cv2吗?或者我怎样才能tfrecords以正确的方式从数据中读取图像。

标签: pythontensorflow

解决方案


问题是奴隶。问题就在这里:image = image/255.0-0.5。有人知道为什么吗?


推荐阅读