python如何读取tfrecord_tensorflow读取tfrecords格式文件

dataset基本原理:

40e7049c9eb8

1.写入tfrecords文件:

|图片|文字 ->格式转换->example(tf.train.Example)生成 ->write(tf.python_io.TFRecordWriter.write)

2.读取tfrecords文件:

tf.data.dataset加载文件序列 ->dataset(example迭代器) -> tf.parse_single_sample(逐个解析example)

##代码实现:

to dataset:

dataset_b = tf.data.Dataset.list_files(files).shuffle(len(files))

dataset = dataset_b.apply(tf.contrib.data.parallel_interleave(\

tf.data.TFRecordDataset, cycle_length= num_threads))

dataset = dataset.map(decode_ex, num_parallel_calls=num_threads)

shapes = dataset._output_shapes

logging.info('dataset decode shapes', shapes)

dataset = dataset.shuffle(buffer_size=buffer_size)

#pipline

dataset = dataset.prefetch(num_prefetch_batches * batch_size)

#pad

dataset = dataset.padded_batch(batch_size, padded_shapes=(shapes))

###parse sigle example:

features_dict= {'id':tf.FixedLenFeature([], tf.string),\

'classes': tf.FixedLenFeature([NUM_CLASSES], tf.float32),\

'comment': tf.VarLenFeature(tf.int64),\

'title': tf.VarLenFeature(tf.int64),\

'comment_str': tf.FixedLenFeature([], tf.string),\

'title_str': tf.FixedLenFeature([], tf.string)

}

features = tf.parse_single_example(example, features = features_dict)

id = features['id']

classes = features['classes']

comment = None

comment = features['comment']

comment = tf.sparse_tensor_to_dense(comment)

comment = comment[:500]

#parser comment

comment_str = features['comment_str']

#comment_str= comment_str[:500]

#parser title

title = None

title = features['title']

参考教程:

https://www.yanshuo.me/p/305699(part6:如何使用 TensorFlow Eager 从 TFRecords 批量读取数据)

https://www.tensorflow.org/performance/datasets_performance(dataset的官方教程)

https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/data_reader.py(港科大的datareader代码)

##tensorflow并行数据抽取与预测

###1.基本知识

####cpu与gpu的分工:

layer是运行在gpu/tpu上的。也就是说,embedding_layer转化是在gpu上进行,但是prepare即word->id,是在cpu上进行的。parser_example是在cpu上;

####model训练性能比较:

TPU>GPU>CPU

TPU是google专门针对tensorflow开发的处理器,降低功耗,加大运算速率。alphago就是在TPU处理器上搭建的。

####加快cpu的预处理速度的方法

cpu做的工作有两个:1. 抽取(I/O)2.数据解析(map(parser)),故而,加快cpu预处理的速率的方法,有两个:

##1. 并行抽取;

##2.map方法并行

cpu与gpu结合的过程,还可以进行一步管道优化:也就是在batch的过程中,cpu的prepare与gpu的训练同时进行。

2.优化方法具体介绍

#一. batch流水线pipline

##原理流程图:

40e7049c9eb8

##代码实现:

dataset = dataset.batch(batch_size=FLAGS.batch_size)

dataset = dataset.prefetch(buffer_size=FLAGS.prefetch_buffer_size)

--------------------------------------------------------

##二.并行prepare(map):

##原理流程图:

40e7049c9eb8

##代码实现:

---------------------------------------------

dataset = dataset.map(map_func=parse_fn, num_parallel_calls=FLAGS.num_parallel_calls)

dataset = dataset.batch(batch_size=FLAGS.batch_size)

------------------------------------------------

#三.并行数据抽取

##原理流程图:

40e7049c9eb8

##代码实现:

------------------------

改代码:

dataset = files.interleave(tf.data.TFRecordDataset)

为:

dataset = files.apply(tf.contrib.data.parallel_interleave(

tf.data.TFRecordDataset, cycle_length=FLAGS.num_parallel_readers))

3.参考教程:

https://www.tensorflow.org/performance/datasets_performance

https://tensorflow.juejin.im/performance/datasets_performance.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python提供了多种读取二进制文件的方法,针对自定义格式的二进制文件,可以使用`struct`模块进行解析。 `struct`模块提供了一些函数,用于读取和写入二进制数据。在读取自定义格式的二进制文件时,首先需要了解文件的结构和每个字段的数据类型和长度。 下面是一个简单的例子,假设有一个自定义格式的二进制文件,每条记录包含两个字段:一个整数和一个浮点数,每个字段都占4个字节。 ```python import struct # 打开二进制文件 with open('custom.bin', 'rb') as file: # 读取整个文件内容 data = file.read() # 计算记录的个数 record_size = 8 # 4个字节整数 + 4个字节浮点数 record_count = len(data) // record_size # 解析每条记录 records = [] for i in range(record_count): offset = i * record_size record = struct.unpack('if', data[offset:offset+record_size]) records.append(record) # 打印解析结果 for record in records: print('整数: {}, 浮点数: {}'.format(record[0], record[1])) ``` 在上述代码中,首先使用`open`函数打开二进制文件,指定模式为`rb`,表示以二进制方式读取文件。然后使用`read`方法读取整个文件内容。 接下来,计算记录的个数,根据字段的数据类型和长度计算出每条记录的大小。使用`struct.unpack`函数按照指定的格式解析每条记录,并将解析结果存储在列表中。 最后,遍历解析结果,打印每条记录的字段值。 需要根据实际的自定义格式进行适当的修改。使用`struct`模块可以灵活解析各种自定义格式的二进制文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值