python_keras_how to augment images and masks together and save these data sets as a ndarray?

时间:2018-03-25 19:40:27

标签: python keras

I have a question about a data augmentation in keras.

# we create two instances with the same arguments
data_gen_args = dict(featurewise_center=True,
                     featurewise_std_normalization=True,
                     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_datagen.fit(images, augment=True, seed=seed)
mask_datagen.fit(masks, augment=True, seed=seed)

image_generator = image_datagen.flow_from_directory(
    'data/images', target_size=(img_row, img_col), color_mode='grayscale', 
    class_mode=None,
    seed=seed, save_to_dir='data/aug_images')

mask_generator = mask_datagen.flow_from_directory(
    'data/masks', target_size=(img_row, img_col), color_mode='grayscale',
    class_mode=None,
    seed=seed, save_to_dir='data/aug_images')

# combine generators into one which yields image and masks
train_generator = zip(image_generator, mask_generator)

model.fit_generator(
    train_generator,
    steps_per_epoch=2000,
    epochs=50)

I usually used this code which is based on Keras example. Now, I prepared training data as npy files(image.npy, mask.npy) instead of image files. So, I want to use datagen.flow instead of datagen.flow_from_directory. Also, I want to save augmented data as a nparray type instead of images. How I can solve this problem? Please comment to me, thanks.

1 个答案:

答案 0 :(得分:0)

您可以将flow_from_directory更改为flow,然后循环遍历生成的批次以存储为.npy

# we create two instances with the same arguments
data_gen_args = dict(featurewise_center=True,
                     featurewise_std_normalization=True,
                     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_datagen.fit(images, augment=True, seed=seed)
mask_datagen.fit(masks, augment=True, seed=seed)

image_generator = image_datagen.flow(images, seed=seed)
mask_generator = mask_datagen.flow(masks, seed=seed)

# combine generators into one which yields image and masks
train_generator = zip(image_generator, mask_generator)

id = 0  # Use whatever method you wish for a name with no collision.
for images, masks in train_generator:
    for image, mask in zip(images, masks):
        np.save('data/aug_images/images/' + str(id), image)
        np.save('data/aug_images/masks/' + str(id), mask)
        id += 1

model.fit_generator(
    train_generator,
    steps_per_epoch=2000,
    epochs=50)