在tensorflow 2 中制作和使用 tfrecord数据集

该博客详细介绍了如何利用TensorFlow库构建和解析TFRecord数据集。首先,定义了Float_Feature、Int64_Feature和Byte_Feature函数来创建不同类型的数据特征。接着,通过loadAndMakeTFRecord函数读取图片路径,将图片转换为二进制数据并写入TFRecord文件。然后,解析DataSets函数用于从TFRecord文件中提取并处理图片数据。最后,展示了如何使用处理后的数据集,并提供了一个简单的图像显示功能。整个过程涵盖了数据预处理、存储和读取的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 基本操作,先导入要使用的工具

import tensorflow as tf
import numpy as np
from PIL import Image
import os,glob

os.environ['CUDA_VISIBLE_DEVICES'] = '0' # 导入需要用到的包之后,启用一下GPU

第一步,构造需要用到的函数

#制作Featuer的数据,类型包括了Float,Int64和Bytes

def Float_Feature(value):
    return tf.train.Feature(float_list = tf.train.FloatList(value = [value]))

def Int64_Feature(value):
    return tf.train.Feature(int64_list = tf.train.Int64List(value = [value]))

def Byte_Feature(value):
    return tf.train.Feature(bytes_list = tf.train.BytesList(value = [value]))

第二步,读取图片路径并制作数据集

def loadAndMakeTFRecord(path):
    pictures = glob.glob(path)
    print("已准备好写入器!")
    tfrecordWriter = tf.io.TFRecordWriter("./K.tfrecord")

    for single_pic in pictures:
        print(single_pic)
        with open(single_pic,'rb') as f:
            binary_pic = f.read()
            pic_for_shape = tf.io.read_file(single_pic)
            picShape = tf.image.decode_jpeg(pic_for_shape,channels=3).shape

            examples = tf.train.Example(

                features = tf.train.Features(

                    feature = {

                        'width':Int64_Feature(picShape[0]),
                        'height':Int64_Feature(picShape[1]),
                        'mode':Int64_Feature(picShape[2]),
                        'raw_image':Byte_Feature(binary_pic)  #图片必须二进制写入,以字符串形式读出

                    }

                )

            )
            print("开始写入!")
            tfrecordWriter.write(examples.SerializeToString())
    tfrecordWriter.close()
    print("数据集写入完成!")

第三步,解析数据集

def parseDataSets(tfrecordData):

    feature = {
                'width':tf.io.FixedLenFeature([],tf.int64),
                'height':tf.io.FixedLenFeature([],tf.int64),
                'mode':tf.io.FixedLenFeature([],tf.int64),
                'raw_image':tf.io.FixedLenFeature([],tf.string)
    }

    single_example = tf.io.parse_single_example(tfrecordData,feature)
    raw_image = tf.image.decode_jpeg(single_example['raw_image'],channels=3)  #图片必须二进制写入,以字符串形式读出,是因为decode_jpeg需要“A Tensor of String”
    raw_image_tensor = tf.image.resize(raw_image,(64,64))
    return raw_image_tensor



def showImage(data):
    new_img = Image.new('RGB',(10*64,10*64))
    x = 0
    y = 0
    for singleBatch in data:
        for singlePic in singleBatch:
            images = Image.fromarray(np.uint8(singlePic),'RGB')
            new_img.paste(images,(x,y))
            x += 64
            if x >= 10*64:
                x = 0
                y += 64

    new_img.show()

第四步,测试程序

#主程序入口
if __name__ == '__main__':
    pictures_path = r"E:\data\single\*.jpg"
    repeateTime = 2

    loadAndMakeTFRecord(pictures_path)
    tfData = tf.data.TFRecordDataset("./K.tfrecord") #读取已经制作好的数据集

    tfData = tfData.map(parseDataSets).shuffle(100).batch(3,drop_remainder=True).repeat(repeateTime)

    for _ in range(repeateTime):
        showImage(tfData)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值