如何使用Python和Tensorflow可视化花卉数据集?
‘matplotlib’库可以帮助可视化花卉数据集。‘imshow’方法用于在控制台上显示图像。迭代整个数据集,仅显示前几个图像。
我们将使用花卉数据集,其中包含数千张花朵图像。 它包含5个子目录,每个类别一个子目录。
我们使用Google Colaboratory运行以下代码。Google Colab或Colaboratory可以通过浏览器运行Python代码,并且无需配置,可以免费访问GPU(图形处理器)。 Colaboratory是建立在Jupyter Notebook之上的。
import matplotlib.pyplot as plt
print("可视化花卉数据集")
plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
for i in range(6):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])
plt.axis("off")
print("迭代数据集")
print("检索图像的批次")
for image_batch, labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
break
代码来源:https://www.tensorflow.org/tutorials/load_data/images
更多Python相关文章,请阅读:Python 教程
输出
可视化花卉数据集
迭代数据集
检索图像的批次
(32, 180, 180, 3)
(32,)

说明
- 使用matplotlib库可视化花卉数据集。
- 迭代前6个图像,并在控制台上显示它们。
- 再次迭代数据集,并在控制台上显示图像的尺寸。
极客教程