使用ImageDataGenerator
看了一大堆的数据增强,也是用的这个函数,都是对图像分类的,但是没有针对语义分割的数据增强,所以写一下
语义分割:是像素级的分类,所以他的标签也是图片的形式
思路:
- 先生成一个生成器
- 在使用ImageDataGenerator这个函数进行增强设置
- 最后用另一个生成器封装
上代码,基于图像分类不说了,网上一搜一大把
调用包
from tensorflow.keras.preprocessing.image import ImageDataGenerator
def Generate(path="train", batch_size=10):
files_train = files[path]
while True:
imgs=[]
segs=[]
for i in range(batch_size):
# np.random.seed(17)
file = random.sample(files_train,1)[0]
#加载数据
img, seg = LoadImage(file)
#处理标签
seg = ColorsToClass(seg)
imgs.append(img)
segs.append(seg)
yield np.array(imgs),np.array(segs)
增强声明(增强的参数自己选择)
dg_args = dict(featurewise_center = False,
samplewise_center = False,
rotation_range = 30,
width_shift_range = 0.1,
height_shift_range = 0.1,
shear_range = 0.01,
zoom_range = [0.9, 1.25],
horizontal_flip = True,
vertical_flip = True,
fill_mode = 'reflect',
data_format = 'channels_last')
# brightness can be problematic since it seems to change the labels differently from the images
AUGMENT_BRIGHTNESS=True
if AUGMENT_BRIGHTNESS:
dg_args['brightness_range'] = [0.5, 1.5]
image_gen = ImageDataGenerator(**dg_args)
if AUGMENT_BRIGHTNESS:
dg_args.pop('brightness_range')
label_gen = ImageDataGenerator(**dg_args)
第二个生成器
def create_aug_gen(in_gen, seed = None):
np.random.seed(seed if seed is not None else np.random.choice(range(100)))
for in_x, in_y in in_gen:
seed = np.random.choice(range(100))
# keep the seeds syncronized otherwise the augmentation to the images is different from the masks
image_datagen.fit(in_x, augment=True, seed=seed)
mask_datagen.fit(in_y, augment=True, seed=seed)
g_x = image_gen.flow(255*in_x,
batch_size = 4, ##自己调节
seed = seed,
shuffle=True)
g_y = label_gen.flow(in_y,
batch_size = 4,
seed = seed,
shuffle=True)
yield next(g_x)/255.0, next(g_y)
上效果