python - 如何从 keras 中的图像批处理数据集中输出图像
问题描述
使用 keras 中的 image_dataset_from_directory 创建图像数据集后,如何以 numpy 格式从数据集中获取第一张图像,以便使用 pyplot.imshow 显示?
import tensorflow as tf
import matplotlib.pyplot as plt
test_data = tf.keras.preprocessing.image_dataset_from_directory(
"C:\\Users\\Admin\\Downloads\\kagglecatsanddogs_3367a",
validation_split=.1,
subset='validation',
seed=123)
for e in test_data.as_numpy_iterator():
print(e[1:])
解决方案
在上面的代码中,e 不是图像,而是包含图像和标签的元组。
代码:
plt.figure(figsize=(10, 10))
class_names = test_data.class_names
for images, labels in test_data.take(1):
for i in range(32):
ax = plt.subplot(6, 6, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])
plt.axis("off")
您可以使用test_data.take(1)
从 test_data 中获取单个批次并将其可视化。
推荐阅读
- typescript - 我希望得到这个功能。关于我如何使这项工作的任何建议
- r - 尝试安装 lm Ubuntu 18.04 时 RStudio 崩溃
- spring - Spring data JPA + Native query - 根据配置文件替换查询字符串
- asp.net-mvc - 在 AZURE 中通过安全透明方法“System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule()”尝试
- pandas - 有没有办法在 python 中优化 cumprod?
- ios - “id”类型如何在不强制转换的情况下理解方法的接收者?
- ghostscript - 使用 ghostscript 将 pdf 转换为具有颜色配置文件的其他格式
- alexa - Alexa notifications: The authentication token is not valid
- c# - System.AccessViolationException 发生在 System.Net.Http C#
- java - 从corda Flow返回HashMap到SpringBoot抛出:com.esotericsoftware.kryo.KryoException