2

I am trying to use the ImageDataGenerator from Keras and I want to use model.fit instead of model.fit_generator , I want to get rid of the below statements i.e:- steps_per_epoch and validation_steps. Will it work and does it augment the data dynamically also or not ?

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory(train_path,
                                                 target_size = (224, 224),
                                                 batch_size = 32,
                                                 class_mode = 'categorical')
test_set = test_datagen.flow_from_directory(valid_path,
                                            target_size = (224, 224),
                                            batch_size = 32,
                                            class_mode = 'categorical')

#flow from directory is the method of imagedatagenerator
checkpoint_callback = ModelCheckpoint(filepath='CNN MobileNet.h5',monitor='val_accuracy', mode='max', save_best_only=True)
history=model.fit_generator(training_set,validation_data=(test_set),epochs=10,steps_per_epoch=len(training_set)
,validation_steps=len(test_set),callbacks=[checkpoint_callback])

# history=model.fit(training_set,validation_data=(test_set),epochs=10,callbacks=[checkpoint_callback])
Hamza
  • 530
  • 5
  • 27

2 Answers2

2

.fit is used when the entire training dataset can fit into the memory and no data augmentation is applied.

fit_generator is used when we have a huge dataset to fit into our memory or when data augmentation needs to be applied.

So, you need to use fit_generator when using ImageDataGenerator.

Hamza
  • 530
  • 5
  • 27
1

.fit is used when the entire training dataset can fit into the memory and no data augmentation is applied.

.fit_generator is used when either we have a huge dataset to fit into our memory or when data augmentation needs to be applied.

source: https://www.geeksforgeeks.org/keras-fit-and-keras-fit_generator/

So, you need to use fit_generator when using ImageDataGenerator.

Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
tekay
  • 11
  • 1
  • My dataset is not too large , it is very small but I have applied dynamic data augmentation – Hamza Jan 26 '21 at 05:03