Professional Documents
Culture Documents
How To Pass Image Datasets To CNN Models Using Image Data Generations - by MD Shahbaz Alam - Medium
How To Pass Image Datasets To CNN Models Using Image Data Generations - by MD Shahbaz Alam - Medium
ImageDataGenerator methods
(1) Flow_from_directory:
The flow_from_directory() method allows you to read the images directly from the
directory and augment them while the neural network model is learning on the
training data.
The method expects that images belonging to different classes are present in
different folders but are inside the same parent folder.
https://msalamiitd.medium.com/how-to-pass-image-datasets-to-cnn-models-using-image-data-generations-b2d9497c7a35 1/14
4/3/24, 11:19 PM How to Pass Image Datasets to CNN Models using Image Data Generations | by Md Shahbaz Alam | Medium
➊ The root directory contains at least two folders one for train and one for the test.
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
https://msalamiitd.medium.com/how-to-pass-image-datasets-to-cnn-models-using-image-data-generations-b2d9497c7a35 2/14
4/3/24, 11:19 PM How to Pass Image Datasets to CNN Models using Image Data Generations | by Md Shahbaz Alam | Medium
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'
)
val_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
In the above code, we’re applying various transformations like rotation, shifting,
shearing, zooming, and horizontal flipping to the training data. For validation and
test data, we only rescale the images.
train_generator = train_datagen.flow_from_directory(
'dataset/train',
target_size=(150, 150),
batch_size=32,
class_mode='binary'
)
val_generator = val_datagen.flow_from_directory(
'dataset/validation',
target_size=(150, 150),
batch_size=32,
class_mode='binary'
)
test_generator = test_datagen.flow_from_directory(
'dataset/test',
target_size=(150, 150),
batch_size=32,
class_mode='binary'
)
Ensure that target_size matches the input size expected by your CNN model.
With the data generators in place, you can now use them to train your CNN model.
model.fit(
train_generator,
epochs=10,
validation_data=val_generator,
verbose=1
)
By following these steps, you’ve successfully passed image datasets to a CNN model
using image data generation using flow from directory. This process not only helps
in efficient data handling but also enhances the model’s performance through data
augmentation.
(2) Flow_from_dataframe:
The flow_from_dataframe() method takes the Pandas DataFrame and the path to a
directory and generates batches of augmented/normalized data.The downloaded
dataset contains .csv file. we will use this .csv file with flow_from_dataframe()
function.
https://msalamiitd.medium.com/how-to-pass-image-datasets-to-cnn-models-using-image-data-generations-b2d9497c7a35 4/14
4/3/24, 11:19 PM How to Pass Image Datasets to CNN Models using Image Data Generations | by Md Shahbaz Alam | Medium
import pandas as pd
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import img_to_array, load_img
datagen = ImageDataGenerator(
rescale=1./255,
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'
)
https://msalamiitd.medium.com/how-to-pass-image-datasets-to-cnn-models-using-image-data-generations-b2d9497c7a35 5/14
4/3/24, 11:19 PM How to Pass Image Datasets to CNN Models using Image Data Generations | by Md Shahbaz Alam | Medium
generator = datagen.flow_from_dataframe(
df,
x_col='filename',
y_col='class',
target_size=(150, 150), # Adjust to match your model's input size
batch_size=32,
class_mode='categorical', # Change to 'binary' if you have binary classes
shuffle=True
)
model.fit(
generator,
epochs=10,
verbose=1
)
By following these steps, you’ve successfully passed image datasets to a CNN model
using image data generation flow from dataframe. This process not only helps in
efficient data handling but also enhances the model’s performance through data
augmentation.
https://msalamiitd.medium.com/how-to-pass-image-datasets-to-cnn-models-using-image-data-generations-b2d9497c7a35 6/14
4/3/24, 11:19 PM How to Pass Image Datasets to CNN Models using Image Data Generations | by Md Shahbaz Alam | Medium
Follow
😄
Data Scientist, ML/DL enthusiast, gamer. I share meaningful stories, hoping they bring value to you. If they
do, follow for more. 50 claps are free!
Md Shahbaz Alam
128
https://msalamiitd.medium.com/how-to-pass-image-datasets-to-cnn-models-using-image-data-generations-b2d9497c7a35 7/14
4/3/24, 11:19 PM How to Pass Image Datasets to CNN Models using Image Data Generations | by Md Shahbaz Alam | Medium
Md Shahbaz Alam
100
Md Shahbaz Alam
In this interconnected world, understanding and respecting diverse cultures is not just an
option, it’s a necessity
105 1
Md Shahbaz Alam
297 2
Sarka Pribylova
911 7
Lists
Vipul Sarode
https://msalamiitd.medium.com/how-to-pass-image-datasets-to-cnn-models-using-image-data-generations-b2d9497c7a35 11/14
4/3/24, 11:19 PM How to Pass Image Datasets to CNN Models using Image Data Generations | by Md Shahbaz Alam | Medium
186 1
368 1
https://msalamiitd.medium.com/how-to-pass-image-datasets-to-cnn-models-using-image-data-generations-b2d9497c7a35 12/14
4/3/24, 11:19 PM How to Pass Image Datasets to CNN Models using Image Data Generations | by Md Shahbaz Alam | Medium
Juan C Olamendy
Maahi Patel
Have you ever struggled with poor quality images in your machine learning or computer vision
projects? Images are the lifeblood of many Al…
143 3
https://msalamiitd.medium.com/how-to-pass-image-datasets-to-cnn-models-using-image-data-generations-b2d9497c7a35 14/14