高效性:TFRecord是一种二进制格式,可以提供更高的存储和读取效率。它可以更快地读取和解析数据,特别适用于大规模数据集
可压缩性:TFRecord可以使用压缩算法进行压缩,减小数据文件的大小。这对于需要传输或存储大量数据的场景非常有用,可以减少存储空间和网络传输带宽的消耗
灵活性:TFRecord可以存储多种类型的数据,包括数字、字符串、图像、音频等
数据读取效率高:使用TFRecord文件可以将数据预处理为模型所需的格式,并通过TensorFlow的数据读取API高效地读取和加载数据。这可以提高训练和推理的效率,并充分利用GPU等硬件资源
支持并行读取:TFRecord文件可以被并行读取,多个线程可以同时读取不同的TFRecord文件或不同的数据样本,提高数据加载的并行性和效率
将目标分类的数据存储成“.tfrecord”文件,
- import os, cv2, warnings
- import tensorflow as tf
- import numpy as np
- from tqdm import tqdm
-
-
- img_paths = []
- images = []
- labels = []
- for img_path in img_paths:
- img = cv2.imread(img_path)
-
- if img is None:
- print(f"Image at {img_path} is corrupted and will be skipped.")
- continue
-
- images.append(img)
- labels.append(1) ## 假设图片的label为1
-
-
- def _bytes_feature(value):
- if isinstance(value, type(tf.constant(0))):
- value = value.numpy()
- return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
-
-
- def _int64_feature(value):
- return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
-
-
- def create_example(img, label):
- img_bytes = img.tostring()
- feature = {
- 'height': _int64_feature(img.shape[0]),
- 'width': _int64_feature(img.shape[1]),
- 'depth': _int64_feature(img.shape[2]),
- 'label': _int64_feature(label),
- 'image_raw': _bytes_feature(img_bytes),
- }
- return tf.train.Example(features=tf.train.Features(feature=feature))
-
-
- def write_tfrecord(file_path, images, labels):
- with tf.io.TFRecordWriter(file_path) as writer:
- for i in tqdm(range(len(images))):
- example = create_example(images[i], labels[i])
- writer.write(example.SerializeToString())
-
-
- write_tfrecord("xxx.tfrecord", images, labels)