简体   繁体   English

投射 ImageDataGenerator 数据输出

[英]Cast ImageDataGenerator Data Output

I'm writing a network for Image Segmentation.我正在为图像分割编写一个网络。 I have my ImageDataGenerator for my masks (which are RGB images with only 0 and 255 as values, black and white) which is:我有我的 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)

And flow_from_directory:和 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)

The code works fine, the only problem is that, when i'm applying data augmentation to the masks, i won't have binary images anymore, but i get some values between 0 and 1 (normalized).代码工作正常,唯一的问题是,当我对掩码应用数据增强时,我将不再有二进制图像,但我得到一些 0 和 1 之间的值(标准化)。 For example, if i print my output matrix (the image) i get something like this:例如,如果我打印我的输出矩阵(图像),我会得到如下信息:

 [[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.        ]]

Which contains also those "extra" values due to augmentation.其中还包含由于增强而产生的那些“额外”值。 If i don't apply any augmentation i get binary images as i wanted.如果我不应用任何增强,我会得到我想要的二进制图像。

How can i embedd the casting to integer?我怎样才能将铸造嵌入到整数? (in order to get values which are only 0 or 1) I tried to use the field dtype=int in the ImageDataGenerator , but it doesn't do anything, i keep getting the same results. (为了获得只有 0 或 1 的值)我尝试在ImageDataGenerator使用字段dtype=int ,但它没有做任何事情,我一直得到相同的结果。

setting the dtype argument to 'uint8' worked for me:将 dtype 参数设置为 'uint8' 对我有用:

Original:原来的:

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

Output:输出:

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

New:新的:

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

output:输出:

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

The Keras docs do suggest that setting Dtype is the correct thing to do, so it may be a bug... One thing you could do is wrap the Keras generator yourself and cast it correctly: 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)

output:输出:

...
[[[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]]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM