如何使用Tensorflow预处理花卉训练数据集?
可以使用Keras预处理API预处理花卉数据集。它有一个名为“image_dataset_from_directory”的方法,该方法接受验证集、数据存储的目录以及其他参数来处理数据集。
阅读更多: 什么是TensorFlow以及Keras如何与TensorFlow一起创建神经网络?
我们将使用Keras Sequential API,它有助于构建一个顺序模型,用于处理简单的层堆栈,其中每一层都只有一个输入张量和一个输出张量。图像分类器是使用keras.Sequential模型创建的,数据是使用preprocessing.image_dataset_from_directory加载的。
数据有效地从磁盘加载。识别过拟合并应用缓解技术。这些技术包括数据增强和dropout。有3700朵花的图像。此数据集包含5个子目录,每个类一个子目录。它们是:雏菊、蒲公英、玫瑰、向日葵和郁金香。
我们正在使用Google Colaboratory运行以下代码。Google Colab或Colaboratory帮助在浏览器上运行Python代码,无需任何配置即可免费访问GPU(图形处理单元)。Colaboratory构建在Jupyter Notebook之上。
print("Pre-processing the dataset using keras.preprocessing")
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
print("The class names are:")
print(class_names)代码来源: https://tensorflowcn.cn/tutorials/images/classification
输出
Pre-processing the dataset using keras.preprocessing Found 3670 files belonging to 5 classes. Using 734 files for validation. The class names are: ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
解释
- 使用keras.preprocessing方法处理数据集。
- 下一步是在控制台上显示类名。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP