Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

machine learning - Can we use model.fit only instead of model.fit_generator?

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])
question from:https://stackoverflow.com/questions/65885923/can-we-use-model-fit-only-instead-of-model-fit-generator

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...