使用tf.data.TFRecordDataset读取tfrecord时出现形状错误?

时间:2018-11-29 13:36:21

标签: python tensorflow reshape tfrecord

我用自己的图像创建了一个tfrecords文件,当我尝试使用tf.data.TFRecordDataset读取它时,出现形状错误: enter image description here 我用以下代码创建了tfrecords:

def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))

def img_to_tfrecord(data_path):
writer = tf.python_io.TFRecordWriter('test_imgs/test.tfrecords')

file = open('test_imgs/test.txt')
for line in file.readlines():
    img_name = line.split(' ')[0]
    label = int(line.split(' ')[1])
    img_path = data_path + '/test_imgs/' + img_name

    img = Image.open(img_path)
    img = img.resize((224, 224))
    img_bytes = img.tobytes()

    feature={'train_img': _bytes_feature(img_bytes),
             'train_label': _int64_feature(label)}
    example = tf.train.Example(features=tf.train.Features(feature=feature))
    writer.write(example.SerializeToString())

writer.close()

并用代码阅读:

def parser(record):
parsed = tf.parse_single_example(record, {'train_img': tf.FixedLenFeature((), tf.string),
                                          'train_label': tf.FixedLenFeature((), tf.int64)})

image = tf.decode_raw(parsed['train_img'], tf.uint8)
image = tf.reshape(image, [224, 224, 3])
label = tf.cast(parsed['train_label'], tf.int32)

return image, label

if __name__ == '__main__':
    os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
    tf.reset_default_graph()

    dataset = tf.data.TFRecordDataset('test_imgs/test.tfrecords')
    dataset = dataset.map(parser)
    dataset = dataset.shuffle(buffer_size=6).batch(4).repeat(2)
    iterator = dataset.make_one_shot_iterator()
    img, label = iterator.get_next()

    with tf.Session() as sess:
       a,b=sess.run([img, label])
       print(a.shape)

由于150528 = 224 * 224 * 3,200704是怎么来的?我已经阅读了很多教程,但仍然不能解决这个问题。我已经关注代码的解析类型:image = tf.decode_raw(parsed ['train_img'],tf.uint8)。任何人都可以帮助我,我快要崩溃了。 用于创建tfrecords的图像如下所示: enter image description here

1 个答案:

答案 0 :(得分:0)

好的,我解决了。天哪。关于图片本身。其中一张图片的位深为32。则其形状为(224,224,4)。

enter image description here