I am a beginner in using the ImageDataGenerator from Keras and I accidentally used model.fit instead of model.fit_generator.

def gen_Image_data():
   gen = ImageDataGenerator(
   return gen

train_gen = gen_Image_data()
test_gen = ImageDataGenerator()

train_samples = train_gen.flow(X,y, batch_size=64)
test_samples = test_gen.flow(X_val, y_val, batch_size=64)

history = model.fit(train_samples, steps_per_epoch = np.ceil(len(X)/64),
              epochs=300, verbose=1, callbacks=[es])

Many thanks for every hint

  • $\begingroup$ What's the question? What are you trying to do? Please be more elaborate. $\endgroup$
    – Danny
    Mar 13, 2020 at 10:17
  • $\begingroup$ @Danny I want to do Data Augmentation during training. Has the data been augmented during the training when I only used fit instead of fit_generator? $\endgroup$
    – Code Now
    Mar 13, 2020 at 14:18

1 Answer 1


The data augmentations are defined when you instantiate your data generators. An example is as such:

train_datagen = ImageDataGenerator(

Other augmentation techniques can also be used by setting the correct parameters. Refer to this link: https://keras.io/preprocessing/image/

  • $\begingroup$ @VicentYong Does that mean that the data has been augmented with my code above and I don't have to train agian using fit_generator instead of just fit? $\endgroup$
    – Code Now
    Mar 13, 2020 at 17:07
  • $\begingroup$ The default arguments for ImageDataGenerator based on the link above shows that all the data augmentation techniques are not used if you did not define their parameters. For example, horizontal_flip by default is False. Hene you will need to state which of the techniques you would like to use and provide the necessary parameters. $\endgroup$ Mar 13, 2020 at 17:10
  • $\begingroup$ @VicentYong ok, I didn't really mean that. I only defined width_shift_range and horizontal_flip in the above code .My question would be whether I am doing data augmentation with model.fit(....) in the above code or whether it is absolutely necessary to use model.fit_generator(...)? $\endgroup$
    – Code Now
    Mar 13, 2020 at 17:34
  • $\begingroup$ It's actually the same as calling fit_generator. Check out line 1130 to 1147 from here: github.com/keras-team/keras/blob/master/keras/engine/…. If you gave a generator as your parameter in fit method, it will check if its a generator and call fit_generator if it is. And your generator produces the augmented image if you provided the necessary parameters. $\endgroup$ Mar 14, 2020 at 13:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.