我是靠谱客的博主 刻苦跳跳糖,最近开发中收集的这篇文章主要介绍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 

最后

以上就是刻苦跳跳糖为你收集整理的keras之语义分割ImageDataGenerator with masks as labels的全部内容,希望文章能够帮你解决keras之语义分割ImageDataGenerator with masks as labels所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部