简体   繁体   English

python_keras_how一起增加图像和遮罩并将这些数据集另存为ndarray?

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

I have a question about a data augmentation in keras. 我对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. 我通常使用基于Keras示例的代码。 Now, I prepared training data as npy files(image.npy, mask.npy) instead of image files. 现在,我将训练数据准备为npy文件(image.npy,mask.npy)而不是图像文件。 So, I want to use datagen.flow instead of datagen.flow_from_directory. 因此,我想使用datagen.flow而不是datagen.flow_from_directory。 Also, I want to save augmented data as a nparray type instead of images. 另外,我想将增强数据保存为nparray类型而不是图像。 How I can solve this problem? 我该如何解决这个问题? Please comment to me, thanks. 请给我评论,谢谢。

You can just change flow_from_directory to flow then loop through the generated batch to store as .npy : 您可以将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)

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

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