# example of zoom image augmentation from numpy import expand_dims from keras.preprocessing.image import load_img from keras.preprocessing.image import img_to_array from keras.preprocessing.image import ImageDataGenerator from matplotlib import pyplot # load the image #img = load_img('bird.jpg') # convert to numpy array #data = img_to_array(img) # expand dimension to one sample #samples = expand_dims(data, 0) # create image data augmentation generator #datagen = ImageDataGenerator(zoom_range=[0.5,0.8]) datagen = ImageDataGenerator( rotation_range=5, width_shift_range=0.1, height_shift_range=0.1, rescale=1./255, shear_range=0.1, zoom_range=0.1, horizontal_flip=False, fill_mode='nearest') # prepare iterator #it = datagen.flow(samples, batch_size=1) it = datagen.flow_from_directory('aug/', target_size=(128, 128), save_to_dir='trained/', save_prefix='gen_', batch_size=10, class_mode='binary') # generate samples and plot for i in range(1000): # define subplot #pyplot.subplot(330 + 1 + i) # generate batch of images batch = it.next() print('batch type:', type(batch)) # convert to unsigned integers for viewing #image = batch[0].astype('uint8') #print('image type:', type(image)) # plot raw pixel data #pyplot.plot(image) #pyplot.imshow(image) # show the figure #pyplot.show() #These are just a few of the options available (for more, see the documentation). Let's quickly go over what we just wrote: #rotation_range is a value in degrees (0-180), a range within which to randomly rotate pictures #width_shift and height_shift are ranges (as a fraction of total width or height) within which to randomly translate pictures vertically or horizontally #rescale is a value by which we will multiply the data before any other processing. Our original images consist in RGB coefficients in the 0-255, but such values would be too high for our models to process (given a typical learning rate), so we target values between 0 and 1 instead by scaling with a 1/255. factor. #shear_range is for randomly applying shearing transformations #zoom_range is for randomly zooming inside pictures #horizontal_flip is for randomly flipping half of the images horizontally --relevant when there are no assumptions of horizontal assymetry (e.g. real-world pictures). #fill_mode is the strategy used for filling in newly created pixels, which can appear after a rotation or a width/height shift.