Keras 显示来自数据生成器的图像

新手上路,请多包涵

我正在为 keras 使用图像生成器,如下所示:

 val_generator = datagen.flow_from_directory(
        path+'/valid',
        target_size=(224, 224),
        batch_size=batch_size,)

x,y = val_generator.next()
for i in range(0,1):
    image = x[i]
    plt.imshow(image.transpose(2,1,0))
    plt.show()

这显示了错误的颜色: 在此处输入图像描述

我有两个问题。

  1. 如何解决问题

  2. 如何获取文件的文件名(这样我就可以自己从 matplotlib 之类的东西中读取它)

编辑:这就是我的数据生成器的样子

datagen = ImageDataGenerator(
    rotation_range=3,
#     featurewise_std_normalization=True,
    fill_mode='nearest',
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True
)

编辑 2:

在遵循 Marcin 的回答之后:

 image = 255 - image

我得到正常的颜色,但仍然有一些奇怪的颜色:

在此处输入图像描述

原文由 harveyslash 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 342
2 个回答
  1. 至少有三种方法可以产生这种扭曲的颜色。所以:

    • 一种选择是您需要像这个 问题 一样切换颜色顺序。
    • 其次是您可能将图片制作成负片(每个通道都通过 255 - x 转换进行转换)这有时会在使用某些 GIS 库时发生。
    • 您还可以使用 score/255 转换。

您需要检查您的情况会发生哪些选项。

  1. 为了自己获取图像,我通常使用(当您的文件夹具有适合 Keras 的格式时 flow_from_directory )我通常混合使用 os.listdiros.path.join 作者:
    list_of_labels = os.listdir(path_to_dir_with_label_dirs)
   for label in list_of_labels:
       current_label_dir_path = os.path.join(path_to_dir_with_label_dirs, label
       list_of_images = os.listdir(current_label_dir_path)
       for image in list_of_images:
           current_image_path = os.path.join(current_label_dir_path, image)
           image = open(current_image_path) # use the function which you want.

原文由 Marcin Możejko 发布,翻译遵循 CC BY-SA 3.0 许可协议

图像数组的 dtype 是’float32’,只需将其转换为’uint8’:

 plt.imshow(image.astype('uint8'))

原文由 in3omnia 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题