创建TF数据集时无法解析TFRecords

问题描述 投票:0回答:1

我正在尝试编写一个代码来解析TFRecords并创建TF数据集。我从图像列表创建TFRecords文件,并能够读回来并成功解码我的图像。我的代码基于此blog的示例。但是当我尝试读取我的TFRecords文件并创建TF数据集时,它会失败并显示以下错误:

ValueError: Argument must be a dense tensor: FixedLenFeature(shape=[], dtype=tf.int64, default_value=None) - got shape [3], but wanted [3, 0]

以下是尝试创建数据集的代码摘要:

 dataset = tf.data.TFRecordDataset(fnames)
 dataset = dataset.map(parse_tfrec)

其中parse_tfrec是一个解析单个proto记录的函数:

 def parse_tfrec(example_proto):
    features={
    'height': tf.FixedLenFeature([], tf.int64, default_value=IMG_SHAPE[0]),
    'width': tf.FixedLenFeature([], tf.int64, default_value=IMG_SHAPE[1]),
    'depth': tf.FixedLenFeature([], tf.int64, default_value=IMG_SHAPE[2]),
    'label': tf.FixedLenFeature([], tf.int64, default_value=0),
    'image': tf.FixedLenFeature([], tf.string, default_value=''),
    }
    parsed_features = tf.parse_single_example(example_proto, features)
    height = tf.cast(features['height'], tf.int32)
    width = tf.cast(features['width'], tf.int32)
    depth = tf.cast(features['depth'], tf.int32)
    label = tf.cast(features['label'], tf.int32)
    image = tf.decode_raw(features['image'], tf.uint8)
    image_shape = tf.pack([height, width, depth])
    image = tf.reshape(image, image_shape)
    return image, label

当代码尝试从TFRecords(或任何其他存储的整数)解析height时,代码失败。而且,我不确定我是否了解有关形状的失败信息。

有什么建议?

python tensorflow tensorflow-datasets
1个回答
1
投票

你能详细说明错误发生在哪一行吗?它出现在'parse_single_example'行吗?还是在后续的路线?

我注意到的一件事是,在你的演员陈述中,你使用的是features字典,而不是parsed_features

将代码更改为此类可能会解决您的问题:

height = tf.cast(parsed_features['height'], tf.int32)

如果问题仍然存在,请告诉我。我最近有一整天自己调试tfrecords :)他们起初可能很难掌握,但最终我的批量生成时间能够获得巨大的性能提升。

© www.soinside.com 2019 - 2024. All rights reserved.