The way to do pre-processing
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_dir,
batch_size=20,
target_size=(150, 150)
class_mode = 'binary'
)
for data_batch, labels_batch in train_generator:
print('data batch shape', data_batch)
print('labels batch shape', labels_batch.shape)
history = model.fit_generator(
train_generator,
steps_per_epoch=100,
epochs=30,
validation_data = validation_generator,
validation_steps = 50)
model.save('xxx.h5')
The way to augement the images with limited size of images samples
datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
# to call the instance
i=0
for batch in datagen.flow(x, batch_size=1):
plt.figure(i)
imgplot = plt.imshow(image.array_to_img(batch[0]))
i += 1
if i % 4 == 0:
break
plt.show()