AI01, Using image data augmentation in keras
Back to the previous page |page management
List of posts to read before reading this article
Contents
- Sample Image : bird.jpg
- Shift Augmentation
- Flip Augmentation
- Random Rotation Augmentation
- Random Brightness Augmentation
- Random Zoom Augmentation
Sample Image : bird.jpg
OUTPUT
Shift Augmentation
Horizontal shift
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(width_shift_range=0.9)
#datagen = ImageDataGenerator(width_shift_range=[-200,200])
# example of horizental shift 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')
data = img_to_array(img)
samples = expand_dims(data, 0)
# create image data augmentation generator
datagen = ImageDataGenerator(width_shift_range=0.9)
iterator = datagen.flow(samples, batch_size=1)
# generate samples and plot
fig , axes = pyplot.subplots(3,12,figsize=(20,3))
for i in range(3):
for j in range(12):
batch = iterator.next()
image = batch[0].astype('uint8')
axes[i,j].imshow(image)
pyplot.show()
OUTPUT
Vertical shift
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(height_shift_range=0.9)
#datagen = ImageDataGenerator(height_shift_range=[-200,200])
# example of vertical shift 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')
data = img_to_array(img)
samples = expand_dims(data, 0)
# create image data augmentation generator
datagen = ImageDataGenerator(height_shift_range=0.9)
iterator = datagen.flow(samples, batch_size=1)
# generate samples and plot
fig , axes = pyplot.subplots(3,12,figsize=(20,3))
for i in range(3):
for j in range(12):
batch = iterator.next()
image = batch[0].astype('uint8')
axes[i,j].imshow(image)
pyplot.show()
OUTPUT
Flip Augmentation
Horizontal flip
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(horizontal_flip=True)
# example of horizontal flip 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')
data = img_to_array(img)
samples = expand_dims(data, 0)
# create image data augmentation generator
datagen = ImageDataGenerator(horizontal_flip=True)
iterator = datagen.flow(samples, batch_size=1)
# generate samples and plot
fig , axes = pyplot.subplots(3,3,figsize=(10,10))
for i in range(3):
for j in range(3):
batch = iterator.next()
image = batch[0].astype('uint8')
axes[i,j].imshow(image)
pyplot.show()
OUTPUT
Vertical flip
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(vertical_flip=True)
# example of vertical flip 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')
data = img_to_array(img)
samples = expand_dims(data, 0)
# create image data augmentation generator
datagen = ImageDataGenerator(vertical_flip=True)
iterator = datagen.flow(samples, batch_size=1)
# generate samples and plot
fig , axes = pyplot.subplots(3,3,figsize=(10,10))
for i in range(3):
for j in range(3):
batch = iterator.next()
image = batch[0].astype('uint8')
axes[i,j].imshow(image)
pyplot.show()
OUTPUT
Random Rotation Augmentation
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(rotation_range=90)
# example of random rotation 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')
data = img_to_array(img)
samples = expand_dims(data, 0)
# create image data augmentation generator
datagen = ImageDataGenerator(rotation_range=90)
iterator = datagen.flow(samples, batch_size=1)
# generate samples and plot
fig , axes = pyplot.subplots(3,3,figsize=(9,9))
for i in range(3):
for j in range(3):
batch = iterator.next()
image = batch[0].astype('uint8')
axes[i,j].imshow(image)
pyplot.show()
OUTPUT
Random Brightness Augmentation
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(brightness_range=[0.2,1.0])
# example of brighting 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')
data = img_to_array(img)
samples = expand_dims(data, 0)
# create image data augmentation generator
datagen = ImageDataGenerator(brightness_range=[0.2,1.0])
iterator = datagen.flow(samples, batch_size=1)
# generate samples and plot
fig , axes = pyplot.subplots(3,3,figsize=(9,9))
for i in range(3):
for j in range(3):
batch = iterator.next()
image = batch[0].astype('uint8')
axes[i,j].imshow(image)
pyplot.show()
OUTPUT
Random Zoom Augmentation
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(zoom_range=[0.5,1.0])
# 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')
data = img_to_array(img)
samples = expand_dims(data, 0)
# create image data augmentation generator
datagen = ImageDataGenerator(zoom_range=[0.5,1.0])
iterator = datagen.flow(samples, batch_size=1)
# generate samples and plot
fig , axes = pyplot.subplots(3,3,figsize=(9,9))
for i in range(3):
for j in range(3):
batch = iterator.next()
image = batch[0].astype('uint8')
axes[i,j].imshow(image)
pyplot.show()
OUTPUT
List of posts followed by this article
Reference