如何使用 Keras 顺序 API 探索 TensorFlow 中的花卉数据集?


借助 'PIL' 包和 'Image.open' 方法,可以使用 Keras 顺序 API 探索花卉数据集。不同的子目录包含不同类型的花卉图像,这些图像可以被索引并在控制台上显示。

阅读更多: 什么是 TensorFlow 以及 Keras 如何与 TensorFlow 协作创建神经网络?

我们将使用 Keras 顺序 API,它有助于构建顺序模型,用于处理简单的层堆栈,其中每一层只有一个输入张量和一个输出张量。图像分类器使用 keras.Sequential 模型创建,数据使用 preprocessing.image_dataset_from_directory 加载。

数据可以高效地从磁盘加载。会识别过拟合并应用技术来减轻它。这些技术包括数据增强和 dropout。共有 3700 张花卉图像。此数据集包含 5 个子目录,每个类有一个子目录。它们是:雏菊、蒲公英、玫瑰、向日葵和郁金香。

我们使用 Google Colaboratory 来运行以下代码。Google Colab 或 Colaboratory 帮助在浏览器上运行 Python 代码,无需任何配置,并可免费访问 GPU(图形处理单元)。Colaboratory 建立在 Jupyter Notebook 之上。

image_count = len(list(data_dir.glob('*/*.jpg')))
print("The number of images in the dataset is:")
print(image_count)
print("A glimpse of the dataset")
print("ROSES")
roses = list(data_dir.glob('roses/*'))
PIL.Image.open(str(roses[1]))
print("TULIPS")
tulips = list(data_dir.glob('tulips/*'))
PIL.Image.open(str(tulips[0]))

代码来源:https://tensorflowcn.cn/tutorials/images/classification

输出

The number of images in the dataset is:
3670
A glimpse of the dataset
ROSES

TULIPS


解释

  • 数据样本显示在控制台上。

更新于: 2021年2月20日

95 次浏览

启动您的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.