空TfRecordDataset已创建

时间:2018-03-08 05:17:51

标签: python tensorflow machine-learning tfrecord

以下是我创建TFRecord的代码。

    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 create_tf_record(filename):
        writer = tf.python_io.TFRecordWriter(filename)

        subFoldersOfImages = glob.glob(PATH)
        for subFolder in subFoldersOfImages:
            image_paths = glob.glob(subFolder + '/*.jpg')

            for path in image_paths:
                img = cv2.imread(path)
                height = img.shape[0]
                width = img.shape[1]
                depth = img.shape[2]

                label = 1
                feature = {'label': _int64_feature(label),
                           'image': _bytes_feature(img.tostring()),
                           'height': _int64_feature(height),
                           'width': _int64_feature(width),
                           'depth': _int64_feature(depth)
                           }
                example = tf.train.Example(features=tf.train.Features(feature=feature))
                writer.write(example.SerializeToString())
        writer.close()


    def main():
        create_tf_record("image.tfrecords")
        dataset = tf.data.TFRecordDataset(["image.tfrecords"])
        print(dataset.output_shapes) 

当我打印出output_shapes时,它是空的,即()。当我检查.tfrecords文件时,它不是空的,大小为45 MB。

有人可以指出数据集为空的原因吗?

1 个答案:

答案 0 :(得分:0)

在不查看文件系统的情况下创建TFRecordDataset。每个tfrecord文件都是字符串列表,因此其数据集的形状是标量。您需要添加数据集解析操作(以反序列化示例等)并手动设置形状。