Python深度学习基于Tensorflow(4)Tensorflow 数据处理和数据可视化

ops/2024/10/10 21:38:47/

文章目录

      • 构建Tensorflow.data数据集
        • TFRecord数据底层
        • 生成TFRecord文件数据
        • 读取TFRecord文件数据
        • 图像增强
      • 数据可视化

构建Tensorflow.data数据集

tf.data.Dataset表示一串元素(element),其中每个元素包含一个或多个Tensor对象。例如:在一个图像流水线(pipeline)中,一个元素可以是单个训练样本,它们带有一个表示图像数据的张量和一个标签组成的数据对(pair)。有两种不同的方式构建一个数据集,具体如下。

  • 直接从 Tensor 创建数据集(例如 Dataset.from_tensor_slices());当然 NumPy 也是可以的,TensorFlow 会自动将其转换为 Tensor。
  • 通过对一个或多个 tf.data.Dataset 对象的变换(例如 Dataset.batch())来创建数据集。 这两类构建方法又可以进一步分为7种方法。如下所示:
数据格式读取方法备注
从NumPy数组读取tf.data.Dataset.from_tensor_slices当数据较小时
从Python Generator读取tf.data.Dataset.from_generator
从文本数据读取tf.data.TextLineDataset
从CSV数据读取tf.data.experimental.CsvDataset
从TFRecord data读取tf.data.TFRecordDatasetTFRecord 是TensorFlow中自带的,它是一种方便储存比较大的数据集的数据格式(二进制格式),当内存不足时,我们可以将数据集制作成TFRecord格式的再将其解压读取。
从二进制文件读取数据tf.data.FixedLengthRecordDataset
从文件集中读取数据tf.data.Dataset.list_files()
这里除了TFRecord以外的东西都很好理解,所以这里主要讲一下TFRecord数据集;

TFRecord 是 TensorFlow 自带的一种数据格式,是一种二进制文件。它是TensorFlow 官方推荐的数据保存格式,其数据的存储、读取操作更加高效。具体来说,TFRecord的优势可概括为:
1)支持多种数据格式;
2)更好的利用内存,方便复制和移动;
3)将二进制数据和标签(label)存储在同一个文件中。

TFRecord 格式文件的存储形式会很合理地帮我们存储数据。TFRecord 内部使用了 Protocol Buffer 二进制数据编码方案,它只占用一个内存块,只需要一次性加载一个二进制文件的方式即可,简单,快速,尤其对大型训练数据很友好。当我们的训练数据量比较大的时候,TFRecord可以将数据分成多个 TFRecord 文件,以提高处理效率。

假设有一万张图像, TFRecord 可以将其保存成 5 个.tfrecords 文件(具体保存成几个文件,要看文件大小),这样我们在读取数据时,只需要进行5 次数据读取。如果把这一万张图像保存为NumPy格式数据,则需要进行10000次数据读取。 我们可以使用tf.data.TFRecordDataset类读取TFRecord文件。

TFRecord数据底层

在数据转换过程中,Example是TFReocrd的核心,TFReocrd包含一系列Example,每个Example可以认为是一个样本。Example是Tensorflow的对象类型,可通过tf.train.example来使用。 特征指的是Example中输入向量的维度,有多少个维度就有多少个特征。

![[Pasted image 20240507123310.png]]

TFRecord,Example,features对应关系具体如下:

![[Pasted image 20240507124103.png]]

生成TFRecord文件数据

这里使用cat-dag数据集,数据下载连接如下:

链接:https://pan.baidu.com/s/1e9skHjPAzy9Bfd5Z7Xl70A?pwd=zynb 
提取码:zynb 

下载之后解压到当前目录的./data文件夹,然后读取文件位置和标签,最后依次写入TFRecord中

python">import tensorflow as tf
import os## 设置文件位置以及标签
data_dir = "./data/cat-dog"
train_cat_dir = data_dir + '/train/cats/'
train_dog_dir = data_dir + "/train/dogs/"test_cat_dir = data_dir + "/test/cats/"
test_dog_dir = data_dir + "/test/dogs/"train_cat_filenames = [train_cat_dir + filename for filename in os.listdir(train_cat_dir)]
train_dog_filenames = [train_dog_dir + filename for filename in os.listdir(train_dog_dir)]
train_filenames = train_cat_filenames + train_dog_filenames
train_labels = [0]*len(train_cat_filenames) + [1]*len(train_dog_filenames)test_cat_filenames = [test_cat_dir + filename for filename in os.listdir(test_cat_dir)]
test_dog_filenames = [test_dog_dir + filename for filename in os.listdir(test_dog_dir)]
test_filenames = test_cat_filenames + test_dog_filenames
test_labels = [0]*len(test_cat_filenames) + [1]*len(test_dog_filenames)## 创建生成TFRecord数据集函数
def encoder(filenames, labels, tfrecord_file):with tf.io.TFRecordWriter(tfrecord_file) as writer:for filename, label in zip(filenames, labels):with open(filename, 'rb') as f:image = f.read()## 将img,label转化为向量的形式  这里只能是普通的形式,不能np和tf,所以读取图片最好是直接读取字节,虽然np.array(Image.open(filename)) 很快,但是array.tolist() 很慢,这就导致效果很慢,所以这里还是读字节最后再在读取tfrecord数据的时候,使用tf.io.decode_jpeg对图片进行解码。image_feature = tf.train.Feature(bytes_list=tf.train.BytesList(value=[image]))label_feature = tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))## 建立feature字典feature = {'image': image_feature,'label': label_feature}# 通过字典创建example,example对象对label和image数据进行封装example = tf.train.Example(features=tf.train.Features(feature=feature))# 将example序列化并写入字典writer.write(example.SerializeToString())## 创建TFRecord
encoder(train_filenames, train_labels, 'train.tfrecords')
encoder(test_filenames, test_labels, 'test.tfrecords')

这里要注意的是,一共有三种类型,int64floatbytes只能是最原始的类型,不能np和tf

python">tf.train.Feature(bytes_list=tf.train.BytesList(value=[*]))
tf.train.Feature(int64_list=tf.train.Int64List(value=[*]))
tf.train.Feature(float_list=tf.train.FloatList(value=[*]))

可以看到当前目录下面有两个tfrecords文件。

读取TFRecord文件数据
python">def decoder(tfrecord_file, is_train_dataset=None):#构建datasetdataset = tf.data.TFRecordDataset(tfrecord_file)#说明特征的描述属性,为解吗每个example使用feature_discription = {'image': tf.io.FixedLenFeature([], tf.string),'label': tf.io.FixedLenFeature([], tf.int64)}def _parse_example(example_string): # 解码每一个example#将文件读入到队列中feature_dic = tf.io.parse_single_example(example_string, feature_discription)feature_dic['image'] = tf.io.decode_jpeg(feature_dic['image'])#对图片进行resize,属于数据处理的操作feature_dic['image'] = tf.image.resize(feature_dic['image'], [256, 256])/255.0return feature_dic['image'], feature_dic['label']batch_size = 4if is_train_dataset is not None:#tf.data.experimental.AUTOTUNE#根据计算机性能进行运算速度的调整dataset = dataset.map(_parse_example).shuffle(buffer_size=2000).batch(batch_size).prefetch(tf.data.experimental.AUTOTUNE)else:dataset = dataset.map(_parse_example)dataset = dataset.batch(batch_size)return datasettrain_data = decoder('train.tfrecords', is_train_dataset=True)
test_data = decoder('test.tfrecords')

最后得到的train_data抽取四个进行展示一下:

python">import matplotlib.pyplot as pltdef plot_img_label(elemtents):imgs, labels = elemtentsnum_imgs = labels.shape[0]for i in range(num_imgs):plt.subplot(1,num_imgs,i+1)plt.axis('off')plt.title(labels[i].numpy())plt.imshow(imgs[i].numpy())plt.show()plot_img_label(train_data.take(1).get_single_element())

![[Pasted image 20240507134554.png]]

图像增强
python"># 解码图片 转换图片数据类型 调整图片尺寸
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
image = tf.image.resize(image, (156, 156))# 对图片进行上下左右随机的翻转,调整明亮度最后旋转90度
images = tf.image.random_flip_left_right(images)
images = tf.image.random_flip_up_down(images)
images = tf.image.random_brightness(images, 1)
images = tf.image.rot90(images, 1)

数据可视化

数据可视化一般来说的库有 matplotlibtensorboard

更多可视化操作可以看这一个专栏:数据可视化 Python_Bigcrab__的博客-CSDN博客

matplotlib 设置中文

python"># windows
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False # mac
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False 

tensorboard

python">logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)model.fit(x=x_train, y=y_train, epochs=5, validation_data=(x_test, y_test), callbacks=[tensorboard_callback])

在Windows的命令行启动Tensorboard 服务,指定日志读写路径,如果是linux环境,请根据实际情况,修改logdir的值。 tensorboard --logdir=“C:\Users\wumg\jupyter-ipynb\tensorflow2-book\char-05\logs”

jupyter 中运行下列代码:

python">%load_ext tensorboard%tensorboard --logdir logsfrom tensorboard import notebook
notebook.list() # View open TensorBoard instances

![[Pasted image 20240507140028.png]]


http://www.ppmy.cn/ops/39117.html

相关文章

Mac 报错 Zsh: command not found :brew

Mac 安装其他命令时报错 Zsh: command not found :brew终于找到一个能行的,还能够配置国内下载源,记录一下 执行 /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"选择一个开始继续执行即可

使用Simulink Test进行单元测试

本文摘要:主要介绍如何利用Simulink Test工具箱,对模型进行单元测试。内容包括,如何创建Test Harness模型,如何自动生成excel格式的测试用例模板来创建测试用例,如何手动填写excel格式的测试用例模板来手动创建测试用例…

如何自动(定时/间隔/重复)执行 同步文件、备份打包加密压缩文件

参考下列两个教程结合使用即可: 快捷自由定时重启、注销、关机 如何从多个文件夹内转移全部文件(忽略文件夹的结构)(进行复制)(再打包) 就是先设定好 勾选对 来源路径’Zip打包,并…

ASP.NET学生成绩管理系统

摘要 本系统依据开发要求主要应用于教育系统,完成对日常的教育工作中学生成绩档案的数字化管理。开发本系统可使学院教职员工减轻工作压力,比较系统地对教务、教学上的各项服务和信息进行管理,同时,可以减少劳动力的使用&#xf…

(数据结构)快速了解时间复杂度和空间复杂度

一、时间复杂度 当计算时间复杂度时,通常需要考虑算法中的循环次数、递归深度等因素。以下是一些常见时间复杂度的示例: O(1):常数时间复杂度,表示算法的执行时间是固定的,与输入规模无关,比如直接访问数组…

C#常用关键字 收藏集

out 作为参数修饰符,它允许按引用而不是按值向方法传递参数。接口和委托的泛型类型参数声明中,该声明指定类型参数为协变。 public void Main(){double radiusValue 3.92781;//计算圆的周长和面积,并将结果返回给 Main().CalculateCircumf…

RabbiMQ-消息可靠性

RabbiMQ消息可靠性 生产者可靠性 生产者重试机制 问题:生产者发送消息时,出现了网络故障,导致与MQ的连接中断 解决: spring:rabbitmq:connection-timeout: 1s # 设置MQ的连接超时时间template:retry:enabled: true # 开启超时…

基于springboot实现可盈保险合同管理系统项目【项目源码+论文说明】

基于springboot实现可盈保险合同管理系统演示 摘要 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本可盈保险合同管理系统就是在这样的大环境下诞生,其…