keras image generator

Jaabir
featurepreneur
Published in
2 min readJan 6, 2022

walkthrough on how to use keras image generator. Easy steps.

let’s use the dataset : https://www.kaggle.com/cactus3/basicshapes

it is the perfect dataset to practice keras image generator

  1. Create a dataframe:
base_path = '/kaggle/input/basicshapes/shapes/'
def get_filenames(shape):
path = os.path.join(base_path, shape)
return [os.path.join(path, img) for img in os.listdir(path)]
c = pd.DataFrame({
'img_path' : get_filenames('circles'),
'ishape' : 'circle'
})

s = pd.DataFrame({
'img_path' : get_filenames('squares'),
'ishape' : 'square'
})

t = pd.DataFrame({
'img_path' : get_filenames('triangles'),
'ishape' : 'triangle'
})

df = pd.concat([c, s, t]).reset_index(drop = True)
df.head()

make sure it outputs df similar to this

| img_path | ishape |
| --- | ----------- |
| /kaggle/input/basicshapes/shapes/circles/drawi... | circle |
| /kaggle/input/basicshapes/shapes/triangles/drawi... | triangle|
| /kaggle/input/basicshapes/shapes/squares/drawi... | square |

2. import the required modules

import tensorflow as tf 
import tensorflow.keras as keras
from tensorflow.keras import Sequential
from tensorflow.keras.utils import plot_model, to_categorical
from tensorflow.keras.layers import Dense, Input, Dropout, BatchNormalization, Flatten
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import tensorflow_addons as tfa

3. split the df into train and test so we can create a two different data generators.

x = df.drop(['ishape'], axis = 1)
y = df.ishape

x_train, x_dev, y_train, y_dev = sample(x, y, 0.3, True)

y_train.shape, y_dev.shape
((210,), (90,))

4. create a data generator function so its easy to create multiple different data generators easily in one line of code

def create_data_generators(df, batch_size = 16, datagen_args = {
'rotation_range' : 20,
'width_shift_range' : 0.2,
'height_shift_range' : 0.2,
'rescale' : 1./255,
'vertical_flip' : True,
'horizontal_flip' : True
}):

datagen = ImageDataGenerator(**datagen_args)
datagenerator = datagen.flow_from_dataframe(
df,
base_path,
x_col = 'img_path',
y_col = 'ishape',
target_size = (28,28),
class_mode='categorical',
batch_size = batch_size,
shuffle=True
)
return datagenerator

if you want to know more about the parameters look them up here : https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator

5. join the x_train and y_train into one df and x_dev and y_dev into another df in order to create a image data generator

train_df = pd.concat([x_train, y_train], axis = 1)
dev_df = pd.concat([x_dev, y_dev], axis = 1)


train_datagen_args = {
'rescale' : 1./255,
'zoom_range' : 0.1,
'vertical_flip' : True,
'horizontal_flip' : True
}
train_gen = create_data_generators(train_df, datagen_args = train_datagen_args)
dev_gen = create_data_generators(dev_df, datagen_args = {'rescale': 1./255})

if successful then it should log something like this

Found 210 validated image filenames belonging to 3 classes.
Found 90 validated image filenames belonging to 3 classes.

6. finally you can feed that train_gen into your model using model.fit_generator in keras

--

--