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

时间:2018-01-09 06:22:04

标签: python image image-processing tensorflow

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))

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

`decoded_image.set_shape([656, 875, 3])` 

出现错误:ValueError:Shapes(?,)和(656,875,3)不兼容。 如果我只运行代码:

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

没关系,它可以告诉我1722000。所以我认为我的tfrecords文件没问题。怎么处理这个bug?

1 个答案:

答案 0 :(得分:0)

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

相反,请使用tf.reshape,如下所示:

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