【问题标题】:Cast ImageDataGenerator Data Output投射 ImageDataGenerator 数据输出
【发布时间】:2019-12-07 11:30:58
【问题描述】:

我正在为图像分割编写一个网络。我有我的 ImageDataGenerator 用于我的面具(这是只有 0 和 255 作为值的 RGB 图像,黑色和白色),即:

train_mask_data_gen = ImageDataGenerator(rotation_range=10,
                                         width_shift_range=10,
                                         height_shift_range=10,
                                         zoom_range=0.3,
                                         horizontal_flip=True,
                                         vertical_flip=True,
                                         fill_mode='nearest',#interpolation used for augmenting the image
                                         cval=0,
                                         rescale=1./255)

和flow_from_directory:

train_mask_gen = train_mask_data_gen.flow_from_directory(os.path.join(training_dir, 'masks'),
                                                     target_size=(img_h, img_w),
                                                     batch_size=bs,
                                                     class_mode=None, # Because we have no class subfolders in this case
                                                     shuffle=True,
                                                     interpolation='nearest',#interpolation used for resizing
                                                     #color_mode='grayscale',
                                                     seed=SEED)

代码运行良好,唯一的问题是,当我对掩码应用数据增强时,我将不再有二进制图像,但我得到了一些介于 0 和 1 之间的值(标准化)。例如,如果我打印我的输出矩阵(图像),我会得到这样的结果:

 [[0.         0.         0.        ]


[0.         0.         0.        ]
   [0.         0.         0.        ]
   ...
   [1.         1.         1.        ]
   [1.         1.         1.        ]
   [1.         1.         1.        ]]

  ...

  [[0.         0.         0.        ]
   [0.3457849  0.3457849  0.3457849 ]
   [1.         1.         1.        ]
   ...
   [0.         0.         0.        ]
   [0.         0.         0.        ]
   [0.         0.         0.        ]]

其中还包含由于增强而产生的“额外”值。如果我不应用任何增强,我会得到我想要的二进制图像。

如何将转换嵌入整数? (为了得到只有 0 或 1 的值) 我尝试在ImageDataGenerator 中使用字段dtype=int,但它没有做任何事情,我一直得到相同的结果。

【问题讨论】:

    标签: python keras casting image-segmentation data-augmentation


    【解决方案1】:

    将 dtype 参数设置为“uint8”对我有用:

    原文:

    datagen = ImageDataGenerator(dtype = 'float32')
    val_set = datagen.flow_from_directory(data_dir, batch_size=1, target_size = (257,144))
    

    输出:

    [[[ 52.  58.  61.]
    [ 53.  53.  61.]
    [ 54.  57.  66.]
    ...
    [  5.  12.   0.]
    [ 19.  26.  12.]
    [  1.  15.   0.]]]
    

    新:

    datagen = ImageDataGenerator(dtype = 'uint8')
    val_set = datagen.flow_from_directory(data_dir, batch_size=1, target_size = (257,144))
    

    输出:

       [[[ 52  58  61]
       [ 53  53  61]
       [ 54  57  66]
       ...
       [  5  12   0]
       [ 19  26  12]
       [  1  15   0]]]
    

    【讨论】:

      【解决方案2】:

      Keras 文档确实建议设置 Dtype 是正确的做法,因此它可能是一个错误...您可以做的一件事是自己包装 Keras 生成器并正确转换:

      # quick stand in for a Keras image generator...
      def img_gen():
          for i in range(3):
              yield np.random.rand(1, 2, 3) + 0.5
      
      
      def int_gen(gen):
          for i in gen:
              yield i.astype(np.uint8)
      
      
      for i in img_gen():
          print(i)
      
      
      for i in int_gen(img_gen()):
          print(i)
      

      输出:

      ...
      [[[0.53385283 1.47129752 0.98338025]
        [0.56875012 1.19955292 0.90370756]]]
      [[[1.03524687 0.66555768 1.08211682]
        [1.23256381 0.84470396 0.53269755]]]
      [[[0.76095154 1.15223349 0.86353093]
        [0.63276903 0.74591046 0.50097586]]]
      
      
      [[[1 1 0]
        [0 0 1]]]
      [[[1 1 0]
        [1 1 1]]]
      [[[1 1 0]
        [1 1 0]]]
      

      【讨论】:

      • 我想过这个解决方案,但为什么 imageGenerator 不投射我的数据?我测试了数据类型,即使我写了 dtype=int,我也不断得到 numpy.float32
      • 我认为值得在 Keras Github 上提出一个问题来回答这个问题。如果我设置了那个标志,我也希望得到整数。尽管如此,如果这是您需要的,解决方案还是很简单的——您甚至可以通过理解在一行中完成。
      猜你喜欢
      • 1970-01-01
      • 2017-03-17
      • 2018-01-29
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      相关资源
      最近更新 更多