tfrecords格式文件读取出坑

继上一篇博文,写到如何将非string类型的数组写入tfrecords文件,写入文件后读取有掉进坑里了,读出来的格式不正确,由于读frecords代码依然是照抄示例,将图片转成string写入tfrecords文件,然后再按照string的类型通过decode_raw将读取的内容转译回数组,

def _parse_function(example_proto):
  features = {'image':tf.FixedLenFeature([], tf.string),
              'label':tf.FixedLenFeature([], tf.int64)}
  
  parsed_features = tf.parse_single_example(example_proto, features)
  img = tf.decode_raw(parsed_features['image'], tf.uint8)
  img = tf.reshape(img, [128, 128, 1])
    # 在流中抛出img张量和label张量
  img = tf.cast(img, tf.float32) / 255
  label = tf.cast(parsed_features['label'], tf.int64)
  return img, label

但是我的图片数据写入并非按照string的格式写入的,而是以int64_list的格式写入的:

 example = tf.train.Example(features=tf.train.Features(feature={
                    "image": tf.train.Feature(int64_list=tf.train.Int64List(value=newDataFlat)),
                    "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))

读取的时候就有问题,继续查如何读写tfrecords文件,依然是我照抄的样例的方式,不能解决我的问题。那就根据调试器报错一步一步的解决错误,却发现已经有贤者遇到并解决此类问题,由于我对读写tfrecords文件的函数只是一知半解,根本不知道还有可能会存在错误,根据大神的提示,才明白用法,而查函数调用方法根本就没有任何帮助,

解决方法如下,

方案一:如果写入tfrecords文件时的数据数组是固定大小的,用tf.FixedLenFeature()读取的时候需要指定数据数组的长度,例如:

def _parse_function(example_proto):
  features = {'image':tf.FixedLenFeature([10000], tf.int64),
              'label':tf.FixedLenFeature([], tf.int64)}
  
 ...

这样就能够将相同长度写入tfrecords文件的数据读取出来。

方案二:

不用tf.FixedLenFeature(),而是用tf.VarLenFeature(tf.int64)

 

def _parse_function(example_proto):
  features = {'image':tf.VarLenFeature( tf.string),
              'label':tf.VarLenFeature(tf.int64)}
parsed_features = tf.parse_single_example(example_proto, features)
data = tf.sparse_tensor_to_dense(features['data'], default_value=0)

data = tf.reshape(data, [20,50])
...

根据这个方法,又查tf.sparse_tensor_to_dense()函数的作用,是将稀疏矩阵转换为稠密矩阵。

what?稀疏矩阵,函数接口没看到说明返回结果是个系数矩阵啊,然后继续查,有大神介绍说用该方法读取的tfrecords文件确实是稀疏矩阵,新手出坑挺难啊,处处是坑。

 

参考:

https://blog.csdn.net/qq_45391763/article/details/103562357

https://blog.csdn.net/huplion/article/details/80340310

https://blog.csdn.net/zxyhhjs2017/article/details/82774732

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TFRecords是TensorFlow中一种高效的数据格式,常用于存储大规模的训练数据。它是一种二进制格式,能够更加高效地存储和读取数据,特别是对于大规模数据集而言。 一个TFRecords文件由多个序列化的tf.train.Example组成,每个Example表示一个样本。每个Example由多个feature组成,其中每个feature可以是一个int、float、byte或者一个变长的数组。 下面是一个TFRecords文件的示例代码: ```python import tensorflow as tf # 创建一个TFRecords文件 writer = tf.io.TFRecordWriter("data.tfrecords") # 定义一个样本 feature = { 'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_bytes])), 'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label])), } # 将样本序列化并写入TFRecords文件 example = tf.train.Example(features=tf.train.Features(feature=feature)) writer.write(example.SerializeToString()) # 关闭TFRecords文件 writer.close() ``` 上面的代码中,我们首先创建了一个TFRecords文件,并定义了一个样本,其中包含一个图像和一个标签。将样本序列化后,我们使用TFRecordWriter将其写入TFRecords文件中。 在使用TFRecords文件进行模型训练时,需要先将原始数据集转换为TFRecords文件格式,然后使用tf.data API读取数据并进行相应的预处理操作,再将其传入模型进行训练。下面是读取TFRecords文件的示例代码: ```python import tensorflow as tf # 读取TFRecords文件 dataset = tf.data.TFRecordDataset("data.tfrecords") # 定义解析函数 def parse_example(example): feature_description = { 'image': tf.io.FixedLenFeature([], tf.string), 'label': tf.io.FixedLenFeature([], tf.int64), } features = tf.io.parse_single_example(example, feature_description) image = tf.io.decode_jpeg(features['image']) label = features['label'] return image, label # 对数据集进行解析和预处理 dataset = dataset.map(parse_example) dataset = dataset.shuffle(buffer_size=10000) dataset = dataset.batch(32) dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE) # 构建模型并进行训练 model.compile(...) model.fit(dataset, ...) ``` 上面的代码中,我们首先使用TFRecordDataset读取TFRecords文件,并定义了一个解析函数parse_example用于将序列化的样本解析成图像和标签。然后,我们对数据集进行了解析和预处理,并使用map、shuffle、batch和prefetch等函数对数据集进行相应的操作。最后,我们可以使用这个数据集进行模型的训练。 总之,TFRecords是TensorFlow中一种高效的数据格式,能够帮助我们更加高效地存储和读取大规模的训练数据。在使用TFRecords文件进行模型训练时,需要先将原始数据集转换为TFRecords文件格式,然后使用tf.data API读取数据并进行相应的预处理操作,再将其传入模型进行训练。这样可以加快数据的读取速度,提高训练效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值