Jac*_*ers 3 python keras tensorflow
使用 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:])
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,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")
Run Code Online (Sandbox Code Playgroud)
您可以test_data.take(1)从 test_data 中获取单个批次并将其可视化。