【问题标题】:How to make keras ImageGenerator load 16-bit images properly?如何让 keras ImageGenerator 正确加载 16 位图像?
【发布时间】:2020-01-26 01:39:39
【问题描述】:

我搜索了类似的问题,但没有找到解决我想要做的事情的方法。 我有 16 位灰度图像,我正在尝试将它们放入 keras ImageDataGenerator 中。当使用像 flow_from_dataframe 这样的函数时,它会产生所有具有相同像素值的图像(不正确)。

我尝试使用 keras preprocess_input,通过自定义预处理函数重新缩放到 [0,1],到 [-1,1],但这些都不起作用。我还在 ImageDataGenerator 中设置了 color_mode='grayscale'。

我进一步测试了转换为 8 位,并且成功了。我将通道数增加了三倍,这不是问题,因为它仍然适用于 8 位。我已经读过 keras 在这种情况下使用 PIL 库来读取图像,并且由于它不能正确处理 16 位,因此它会返回该错误。我看到我们可以设置一个不同的库来加载图像,但我不知道该怎么做。

有人知道使用 16 位图像的替代方法吗?在最后一种情况下,我会尝试自定义生成器,但我真的很想从为此目的构建和测试的函数中获利。

我的目标是使用这些图像来微调预训练的网络,因此我想标准化我使用的输入类型。

谢谢。

【问题讨论】:

  • 图片是以什么格式存储的?
  • .tiff。我也用 .png 图像进行了测试,它给出了同样的结果。

标签: keras python-imaging-library generator conv-neural-network 16-bit


【解决方案1】:

您可能想编写自己的ImageDataGenerator 并覆盖一些方法来按您的预期加载数据。

some great blog articles 可以解释您需要的一切。

然后,您必须确保覆盖的 __data_generation 方法将您的 .tiff 图像正确加载到 numpy 数组中,或者您事先将它们转换并加载它们。

测试你的生成器也很简单(去做吧!)并检查它是否产生了你期望的输出。

【讨论】:

    【解决方案2】:

    这是 keras_preprocessing 版本

    pip install git+git://github.com/keras-team/keras-preprocessing.git --upgrade --no-deps
    

    而且,使用 color_mode='grayscale',您的 ImageDataGenerator 将正确加载 16 位灰度图像。

    此错误已由 Rodrigo Agundez 修复(请参阅 his postpull request 了解更多详细信息)并应在下一个版本中发布。问题来自加载图像时的keras_preprocessing:

    # PIL library
    img = pil_image.open(io.BytesIO(f.read()))
    
    # This was the bug: converting with L mode will truncate all images to 8-bit.
    # All values > 255 will be set to 255
    if color_mode == 'grayscale':
        img = img.convert('L')
    
    # This is the fix
    if color_mode == 'grayscale':
        # if image is not already an 8-bit, 16-bit or 32-bit grayscale image
        # convert it to an 8-bit grayscale image.
        if img.mode not in ('L', 'I;16', 'I'):
            img = img.convert('L')
    

    请注意,如果您使用 color_mode='rgb' 加载 16 位灰度图像(假设您想将单个灰度通道转换为 3 通道图像),它将无法正常工作,因为 16 位灰度图像会转换为 8 位。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 2011-01-18
      • 2020-05-07
      • 2018-07-30
      • 2020-10-04
      • 2021-05-10
      相关资源
      最近更新 更多