我正在尝试为“癌症检测Kaggle挑战”构建图像分类器。这是我正在使用的代码。
`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'`
我到处都看过,似乎找不到解决方法。该方法现在是否有所不同?
答案 0 :(得分:0)
如果您想使用flow_from_dataframe()
方法,建议您执行以下操作:
卸载当前的keras预处理模块:
pip uninstall keras-preprocessing
通过以下git链接安装keras-预处理模块:
pip install git+https://github.com/keras-team/keras-preprocessing.git
(您可以看到该方法可用in the source code here)
,然后按如下所示导入ImageDataGenerator
:
from keras_preprocessing.image import ImageDataGenerator
答案 1 :(得分:0)
使用Keras 2.1.4时,我有同样的错误。我只是升级了pip install keras --upgrade
。 Keras 2.2.4没有给出相同的错误。现在一切正常。
答案 2 :(得分:0)
这也给了我Keras 2.2.2和keras-预处理1.0.2错误。使用该配置,在卸载keras-preprocessing(pip卸载keras-preprocessing)并重新安装(更新到1.1.2)之后,它将给出:
ERROR: keras 2.2.2 has requirement keras_preprocessing==1.0.2, but you'll have keras-preprocessing 1.1.2 which is incompatible.
它成功安装了1.1.2版,并且在使用flow_from_dataframe时不再出现“对象没有属性”错误。