Tensorflow Error:ValueError:形状 (?,) 和 (656, 875, 3) 不兼容


import tensorflow as tf 
reader = tf.TFRecordReader()
filename_queue = tf.train.string_input_producer(['output/output.tfrecords'])
_,serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example, features={
    'image_raw':tf.FixedLenFeature([], tf.string),
    'height':tf.FixedLenFeature([], tf.int64),
    'width':tf.FixedLenFeature([], tf.int64),
    'channel':tf.FixedLenFeature([], tf.int64)
    })
image = features['image_raw']
height, width = features['height'], features['width']
channel = features['channel']
decoded_image = tf.decode_raw(image, tf.uint8)
decoded_image.set_shape([656, 875, 3])
IMAGE_SIZE = 512
image = tf.image.convert_image_dtype(decoded_image, dtype=tf.float32)
destorted_image = tf.image.resize_image_with_crop_or_pad(image, IMAGE_SIZE, IMAGE_SIZE)
min_after_dequeue = 10000
batch_size = 100
capacity = min_after_dequeue + 3 * batch_size
image_batch = tf.train.shuffle_batch(
    [destorted_image], batch_size=batch_size,
    capacity = capacity, min_after_dequeue=min_after_dequeue)
with tf.Session() as sess:
    tf.initialize_all_variables().run()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    for i in range(10):
        image = sess.run([image_batch])
        print(len(image))

正在尝试运行张量流来处理我自己的 tfrecords 文件。 代码 :

`decoded_image.set_shape([656, 875, 3])` 
出现错误:

值错误:形状 (?,( 和 (656, 875, 3( 不兼容。如果我只运行代码:

print(sess.run(len(decoded_image)))

没关系,它可以告诉我 1722000.So 我认为我的 tfrecords 文件没有问题。如何处理错误?

tf.decode_raw返回一维张量,并且tf.set_shape无法更改张量的维数。

请改用 tf.reshape ,如下所示:

decoded_image = tf.reshape(decoded_image, shape=[656, 875, 3])

最新更新