医院网站可信认证必须做吗,高端婚恋网站排名,辽宁省建设工程招标协会网站,广州市官网网站建设报价以TFRecord方式存储的优点 高效性#xff1a;TFRecord是一种二进制格式#xff0c;可以提供更高的存储和读取效率。它可以更快地读取和解析数据#xff0c;特别适用于大规模数据集 可压缩性#xff1a;TFRecord可以使用压缩算法进行压缩#xff0c;减小数据文件的大小。这…以TFRecord方式存储的优点 高效性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 tqdmimg_paths []
images []
labels []
for img_path in img_paths:img cv2.imread(img_path)if img is None:print(fImage at {img_path} is corrupted and will be skipped.)continueimages.append(img)labels.append(1) ## 假设图片的label为1def _bytes_feature(value):if isinstance(value, type(tf.constant(0))):value value.numpy()return tf.train.Feature(bytes_listtf.train.BytesList(value[value]))def _int64_feature(value):return tf.train.Feature(int64_listtf.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(featurestf.train.Features(featurefeature))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)