我正在尝试为Cancer Detection Kaggle Challenge构建一个图像分类器。这是我正在使用的代码。
`train_datagen = ImageDataGenerator(rescale=1./255,
validation_split=0.15
)
test_datagen = ImageDataGenerator(rescale=1./255)
train_path = MAIN_DIR + '/CancerTrain'
valid_path = MAIN_DIR + '/CancerTrain'
train_generator = train_datagen.flow_from_dataframe(
dataframe = train_labels,
directory=train_path,
x_col = 'id',
y_col = 'label',
has_ext=False,
subset='training',
target_size=(96, 96),
batch_size=64,
class_mode='binary'
)
validation_generator = train_datagen.flow_from_dataframe(
dataframe=df,
directory=valid_path,
x_col = 'id',
y_col = 'label',
has_ext=False,
subset='validation', # This is the trick to properly separate train and validation dataset
target_size=(96, 96),
batch_size=64,
shuffle=False,
class_mode='binary'
)`
但是,每当我运行它时,我都会收到此错误:
`AttributeError Traceback (most recent call last)
<ipython-input-22-eb9c70d0ad1c> in <module>()
15 )
16
---> 17 train_generator = train_datagen.flow_from_dataframe(
18 dataframe = train_labels,
19 directory=train_path,
AttributeError: 'ImageDataGenerator' object has no attribute 'flow_from_dataframe'`
我到处寻找,似乎无法找到解决方案。这个方法现在被称为不同的东西吗?
如果你想使用flow_from_dataframe()
方法,我建议你做以下事情:
卸载当前的keras-preprocessing模块:
pip uninstall keras-preprocessing
从以下git链接安装keras-preprocessing模块:
pip install git+https://github.com/keras-team/keras-preprocessing.git
(你可以看到方法可用in the source code here)
然后导入ImageDataGenerator
如下:
from keras_preprocessing.image import ImageDataGenerator
我使用Keras 2.1.4时遇到了同样的错误。我只是用pip install keras --upgrade
升级了。 Keras 2.2.4没有给出相同的错误。这一切现在都有效。