Search code examples
python-3.xkerasdeep-learninginfinite-loopdata-augmentation

How to save images after data augmentation in a new folder without looping


I am trying to save my augmented images in a folder, but the loop is executing infinite times. I have 5000 images in the folder, but the number of augmented images I am getting is infinity. My aim is to get the same number of augmented images i.e., 5000.

Thank you

import numpy as np
from keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(rotation_range=90)

image_path = 'C:/Users/1/Desktop/DEEP/Dataset/Train/1training_c10882.png'

image = np.expand_dims(imageio.imread(image_path), 0)

save_here = 'D:/Augmented DATASET/'

generator = datagen.flow_from_directory('C:/Users/1/Desktop/DEEP/Dataset/Train',target_size=(224,224),
                                    batch_size = 256, class_mode = 'binary')

for inputs,outputs in generator:
    pass

Solution

  • Maybe this will help you.

    from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
    datagen = ImageDataGenerator(shear_range=0.2, zoom_range=0.2) 
    
    for f in filenames:
        img = load_img(f)  
        x = img_to_array(img) 
        # Reshape the input image 
        x = x.reshape((1, ) + x.shape)  
        i = 0
    
        # generate 5 new augmented images 
        for batch in datagen.flow(x, batch_size = 1, 
                          save_to_dir ='aug',  
                          save_prefix ='car', save_format ='jpeg'):
            i += 1
            if i > 5: 
                break