keras之语义分割ImageDataGenerator with masks as labels

与普通的分类不同的是 ,语义分割出来的mask和label都是图片,要是有旋转,颠倒等增强数据集,要保持mask和label一一对应,需要对图片生成器进行处理,用随机种子seed去解决这个问题

 

1.

datagen = ImageDataGenerator( rotation_range=4) and then you could use
for batch in datagen.flow(x, batch_size=1,seed=1337 ): with random seed and use datagen.flow once on X and then on the mask y and save the batches. This should do the same rotations on both X and y, but maybe dont work with ZCA and other normalizations.

2. 

i dont know if you can use batchsize > 1. If you look at the example on the page, they treat x as 1 image. Ok they did this just for plotting reasons but i dont know if this would also work for batchsizes > 1. I would Loop over my imageset and set x to the current image, then apply the imagegenerator like in the example and then use the same generator again on the mask like
mask=[] img=[]
for batch in datagen.flow(x, batch_size=1, seed=1337): img.append(batch)

for batch in datagen.flow(ymask, batch_size=1, seed=1337): mask.append(batch)

3.目录方式

data_gen_args = dict(width_shift_range=0.2, height_shift_range=0.2)
image_datagen = ImageDataGenerator(**data_gen_args)
mask_datagen = ImageDataGenerator(**data_gen_args)
seed = 1
image_generator = image_datagen.flow_from_directory('raw/train_image', target_size=(img_rows,img_cols),
                                                        class_mode=None, seed=seed, batch_size=batchsize, color_mode='grayscale')
mask_generator = mask_datagen.flow_from_directory('raw/train_mask', target_size=(img_rows,img_cols),
                                                       class_mode=None, seed=seed, batch_size=batchsize, color_mode='grayscale')

train_generator = zip(image_generator, mask_generator)

model.fit_generator(train_generator, steps_per_epoch=5635/batchsize, epochs=100, verbose=1)

4.

def applyImageAugmentationAndRetrieveGenerator():
    from keras.preprocessing.image import ImageDataGenerator

# We create two instances with the same arguments
data_gen_args = dict(rotation_range=90.,
                     width_shift_range=0.1,
                     height_shift_range=0.1,
                     zoom_range=0.2
                     )
image_datagen = ImageDataGenerator(**data_gen_args)
mask_datagen = ImageDataGenerator(**data_gen_args)

# Provide the same seed and keyword arguments to the fit and flow methods
seed = 1

image_generator = image_datagen.flow_from_directory('dataset/train_images',
                                                    target_size=(360,480),    
                                                    class_mode=None,
                                                    seed=seed,
                                                    batch_size = 32)

mask_generator = mask_datagen.flow_from_directory('dataset/train_masks',
                                                  target_size=(360,480),  
                                                  class_mode=None,
                                                  seed=seed,
                                                  batch_size = 32)

#Combine generators into one which yields image and masks
#print(image_generator[0])
#print(mask_generator[0])
train_generator = zip(image_generator, mask_generator)
return train_generator




#引用

segmentation_model.fit_generator(training_generator,
                                 steps_per_epoch=186, 
                                 epochs=50,
                                 verbose=1
                                 )

5.   当获取到的是矩阵的mask和label

def train_generator(img_dir, label_dir, batch_size, input_size):
    list_images = os.listdir(img_dir)
    shuffle(list_images) #Randomize the choice of batches
    ids_train_split = range(len(list_images))
    while True:
         for start in range(0, len(ids_train_split), batch_size):
            x_batch = []
            y_batch = []
            end = min(start + batch_size, len(ids_train_split))
            ids_train_batch = ids_train_split[start:end]
            for id in ids_train_batch:
                img = cv2.imread(os.path.join(img_dir, list_images[id]))
                img = cv2.resize(img, (input_size[0], input_size[1]))
                mask = cv2.imread(os.path.join(label_dir, list_images[id].replace('jpg', 'png')), 0)
                mask = cv2.resize(mask, (input_size[0], input_size[1]))
                mask = np.expand_dims(mask, axis=2)
                x_batch.append(img)
                y_batch.append(mask)

            x_batch = np.array(x_batch, np.float32) / 255.
            y_batch = np.array(y_batch, np.float32)

            yield x_batch, y_batch

 

Without the hassle of organizing the folders for the case that one map matches on one mask as the answer above, we can use .flow()
my working code:
`def augmentationForTrainImageAndMask(imgs, masks):

data_gen_args = dict(rotation_range=40.,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)

image_datagen = ImageDataGenerator(**data_gen_args)
mask_datagen = ImageDataGenerator(**data_gen_args)

seed = 1
image_datagen.fit(imgs, augment=True, seed=seed)
mask_datagen.fit(masks, augment=True, seed=seed)

image_generator = image_datagen.flow(imgs,
                                     seed=seed,
                                     batch_size=batch_size,
                                     shuffle=False)

mask_generator = mask_datagen.flow(masks,
                                   seed=seed,
                                   batch_size=batch_size,
                                   shuffle=False)

return zip(image_generator, mask_generator)`

 

地址:https://github.com/keras-team/keras/issues/3059 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值