Reputation: 11
How can I perform hyperparameter tuning when my image inputs are through ImageDataGenerator
? My training and test data are not in the form of arrays (X_train, Y_train etc). I want to tune my hyperparameters using GridSearchCV
from sklearn
and ImageDataGenerator
from keras
.
These are a few snippets from the code I've attempted!
#(5) Train
train_datagen = ImageDataGenerator(rescale=1./255)
validation_datagen = ImageDataGenerator(rescale=1./255)
train_batchsize = 15
val_batchsize = 10
train_generator = train_datagen.flow_from_directory(
train_dir,
batch_size=train_batchsize,
class_mode='categorical',
shuffle=False)
validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(image_size, image_size1),
batch_size=val_batchsize,
class_mode='categorical')
#Function for Creating Model
def create_model():
.....................
return model
model = KerasClassifier(build_fn=create_model, batch_size=1000, epochs=10, verbose = 1)
# Use scikit-learn to grid search
activation = ['relu', 'tanh', 'sigmoid', 'hard_sigmoid', 'linear'] # softmax, softplus, softsign
momentum = [0.0, 0.2, 0.4, 0.6, 0.8, 0.9]
neurons = [1, 5, 10, 15, 20, 25, 30]
init = ['uniform', 'lecun_uniform', 'normal', 'zero', 'glorot_normal', 'glorot_uniform', 'he_normal', 'he_uniform']
optimizer = [ 'SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']
param_grid = dict(epochs=epochs, batch_size=batch_size)
##############################################################
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit_generator(train_generator, validation_generator)
Getting an error in this line :
grid_result = grid.fit_generator(train_generator, validation_generator)
Upvotes: 1
Views: 1272
Reputation: 130
Sklearn GridSearchCV doesn't expose a fit_generator
method.
You're probably confusing it with Keras (now deprecated) fit_generator.
This means that it is non-trivial to gridsearch a Keras model if you get your training data from generators. I found two related questions on SO:
So for now, you have to resort to workarounds.
Upvotes: 0