首页 > 解决方案 > 如何使用 ImageDataGenerator 绘制验证图像?

问题描述

我正在尝试建立一个图像分类模型,但不知道如何绘制具有预测类别(和概率)的验证图像,如本指南中所示:

https://www.tensorflow.org/tutorials/images/hub_with_keras#check_the_predictions

当我使用 ImageDataGenerator 时,我可以获得有关预测类别和概率的信息,但我无法获得图像本身。

plt.figure(figsize=(10,9))
for n in range(30):
  plt.subplot(6,5,n+1)
  plt.imshow(image_batch[n])
  plt.title(labels_batch[n])
  plt.axis('off')
_ = plt.suptitle("Model predictions")

我得到 ValueError: could not broadcast input array from shape (400,80,80,3) into shape (400)

我知道问题出在 -> image_batch[n]

请告诉我如何将有关图像的信息传递给 imshow()。

我使用的图像生成器的 PS 代码:

img_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/255)

image_batch = img_generator.flow_from_directory(
    path,
    target_size=(size, size),
    color_mode='rgb',
    class_mode='categorical',
    batch_size=batch_size
)

标签: pythontensorflowkeras

解决方案


推荐阅读