如何使用TensorFlow的Imagedatagener使用Scikit-Learn的随机搜索
我正在尝试使用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_reg
是kerasclassifier
它包装了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不建议以下解决方案和征用。我强烈建议使用
kerastuner
但是,如果要输入keras.preprocessing.image.image.image.directoryiterator
喜欢您的Train_generator
toanturnizedsearchcv
代码>来自DirectoryIterator
然后将它们输入到随机搜索中,如下所示:I don't recommend the below solution and approch. I highly recommend using
KerasTuner
but If you want to inputkeras.preprocessing.image.DirectoryIterator
like yourtrain_generator
toRandomizedSearchCV
you need to extractX
andY
fromDirectoryIterator
then input them to RandomizedSearchCV like below: