使用Tensorflow如何将鲜花数据集分为训练集和验证集?
可以使用keras预处理API将鲜花数据集拆分为训练集和验证集,借助于’image_dataset_from_directory’,它要求指定验证集的百分比。
更多Python相关文章,请阅读:Python 教程
使用keras.Sequential模型创建图像分类器,并使用 preprocessing.image_dataset_from_directory 加载数据。数据从磁盘高效地加载。鉴别过度拟合并应用技术以减轻它。这些技术包括数据增强和dropout。此数据集包含3700朵花的图像。该数据集包含5个子目录,每个子目录表示一个类别。它们是:雏菊,蒲公英,玫瑰,向日葵和郁金香。
我们使用Google Colaboratory来运行下面的代码。Google Colab或Colaboratory帮助在浏览器上运行Python代码,不需要任何配置,并可免费访问GPU(图形处理单元)。Colaboratory是基于Jupyter Notebook构建的。
batch_size = 32
img_height = 180
img_width = 180
print("正在将数据拆分为训练集和验证集")
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
代码来源:https://www.tensorflow.org/tutorials/images/classification
输出
正在将数据拆分为训练集和验证集
Found 3670 files belonging to 5 classes.
Using 2936 files for training.
说明
- 使用’image_dataset_from_directory’实用程序从磁盘加载这些图像并创建tf.data.Dataset。
- 下载数据后,定义一些加载器参数。
- 将数据拆分为训练集和验证集。
极客教程