如何使用TensorFlow的Imagedatagener使用Scikit-Learn的随机搜索

发布于 2025-02-07 19:50:11 字数 2254 浏览 2 评论 0原文

我正在尝试使用Imagedatagenerators实现随机搜索。

这里是火车集使用的发电机的示例:

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]))

我对validation_generator进行了相同的操作,最后该模型已拟合为:

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)])

我想应用网格搜索(使用Sklearn RandomizedSearchCV)为了优化我的模型,因此,我使用了scikeras:

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

其中keras_regkerasclassifier它包装了Sklearn和param_distribs是带有字典的模型超参数值。

最后,我拟合了随机搜索对象,如下所示:

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)])

我有以下错误:

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'

您知道如何集成随机searchcv和imagedatagenerator对象吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2025-02-14 19:50:11

我不建议以下解决方案和征用。我强烈建议使用 kerastuner 但是,如果要输入 keras.preprocessing.image.image.image.directoryiterator 喜欢您的Train_generator to anturnizedsearchcv 代码>来自DirectoryIterator然后将它们输入到随机搜索中,如下所示:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文