Home > Back-end >  How to use RandomizedSearchCV of Scikit-Learn with ImageDataGenerator of Tensorflow
How to use RandomizedSearchCV of Scikit-Learn with ImageDataGenerator of Tensorflow

Time:06-17

I'm trying to implement a RandomizedSearchCV using the ImageDataGenerators.

Here an example of the generator used for the train set:

train_datagen = ImageDataGenerator(rotation_range=20,
                                        rescale=1./255,
                                        shear_range=0.2,
                                        zoom_range=0.25,
                                        horizontal_flip=True,
                                        width_shift_range=0.2,
                                        height_shift_range=0.2)

train_generator = train_datagen.flow_from_directory(paths["train_path"],
                                                batch_size=train_batch_size,
                                                class_mode='binary',
                                                target_size=(image_shape[0], image_shape[1]))

I've done the same for the validation_generator and finally the model has been fitted as:

model_history = model.fit(train_generator, 
                        epochs=200, 
                        steps_per_epoch=train_steps_per_epoch,
                        validation_data=validation_generator, 
                        validation_steps=valdation_steps_per_epoch,
                        callbacks=[callbacks.EarlyStopping(patience=20)])

I would like to apply a grid search (using sklearn RandomizedSearchCV) to optimize my model, for that reason I used SciKeras:

rnd_search_cv = RandomizedSearchCV(keras_reg, param_distribs, n_iter=10, cv=3)

where keras_reg is the KerasClassifier which wraps the model for sklearn and param_distribs is the dictionary with the hyperparameters values.

Finally I fitted the RandomizedSearchCV object as follows:

rnd_search_cv.fit(train_generator, 
                epochs=200, 
                steps_per_epoch=train_steps_per_epoch,
                validation_data=validation_generator, 
                validation_steps=valdation_steps_per_epoch,
                callbacks=[callbacks.EarlyStopping(patience=20)])

I have the following error:

Traceback (most recent call last):
File "/home/docker_user/.local/lib/python3.8/site-packages/sklearn/model_selection/_validation.py", line 684, in _fit_and_score
    estimator.fit(X_train, **fit_params)
TypeError: fit() missing 1 required positional argument: 'y'

Do you know how to integrate RandomizedSearchCV and ImageDataGenerator objects?

CodePudding user response:

I don't recommend the below solution and approch. I highly recommend using KerasTuner but If you want to input keras.preprocessing.image.DirectoryIterator like your train_generator to RandomizedSearchCV you need to extract X and Y from DirectoryIterator then input them to RandomizedSearchCV like below:

import numpy  as np

len_dataset = 100
batch_size = 32
image_size = (150,150)
channel_images = 3
num_clases = 5
x_train = np.empty((len_dataset*batch_size, 
              image_size[0], 
              image_size[1], 
              channel_images))
y_train = np.empty((len_dataset*batch_size, num_clases))

for idx in range(len_dataset):
    x_batch, y_batch = train_generator.__getitem__(idx)
    for i in range(x_batch.shape[0]):
        x_train[idx*batch_size i] = x_batch[i]
        y_train[idx*batch_size i] = y_batch[i]

rnd_search_cv.fit(x_train, y_train)
  • Related