How to Solve: 'str' object has no attribute 'data_format' in keras
我正在尝试制作一个分类器,可以使用 keras 对猫和狗进行分类。
我只是想使用 ImageDataGenerator.flow_from_directory() 从图像中创建张量数据,这些数据被排序并保存在其路径在 train_path、test_path 等中给出的目录中。
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import numpy as np import keras from keras import backend as K from keras.models import Sequential from keras.layers import Activation train_path = 'cats-and-dogs/train' test_path = 'cats-and-dogs/test' valid_path = 'cats-and-dogs/valid' train_dir = 'cats-and-dogs/' test_dir = 'cats-and-dogs/' valid_dir = 'cats-and-dogs/' train_batches = ImageDataGenerator.flow_from_directory(train_path, directory=train_dir, target_size=(200,200), classes=['dog','cat'], batch_size=10) test_batches = ImageDataGenerator.flow_from_directory(test_path, directory=test_dir, target_size=(200,200), classes=['dog','cat'], batch_size=5) valid_batches = ImageDataGenerator.flow_from_directory(valid_path, directory=valid_dir, target_size=(200,200), classes=['dog','cat'], batch_size=10) |
但我在使用 python 3.5 时遇到以下错误:
/usr/local/lib/python3.5/site-packages/h5py/init.py:36:
FutureWarning: Conversion of the second argument of issubdtype from
float tonp.floating is deprecated. In future, it will be treated
asnp.float64 == np.dtype(float).type . from ._conv import
register_converters as _register_converters Using TensorFlow backend.
Traceback (most recent call last): File"CNNFromScratch.py", line
29, in
train_batches = ImageDataGenerator.flow_from_directory(train_path, directory=train_dir, target_size=(200,200), classes=['dog','cat'],
batch_size=10) File
"/usr/local/lib/python3.5/site-packages/keras/preprocessing/image.py",
line 565, in flow_from_directory
data_format=self.data_format,AttributeError: 'str' object has no attribute 'data_format'
我能做些什么来解决这个问题?
方法
这应该可以工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import numpy as np import keras from keras import backend as K from keras.models import Sequential from keras.layers import Activation from keras.preprocessing.image import ImageDataGenerator train_path = 'cats-and-dogs/train' test_path = 'cats-and-dogs/test' valid_path = 'cats-and-dogs/valid' my_generator = ImageDataGenerator() train_batches = my_generator.flow_from_directory(directory=train_path, target_size=(200,200), classes=['dog','cat'], batch_size=10) test_batches = my_generator.flow_from_directory(directory=test_path, target_size=(200,200), classes=['dog','cat'], batch_size=5) valid_batches = my_generator.flow_from_directory(directory=valid_path, target_size=(200,200), classes=['dog','cat'], batch_size=10) |
查看文档以添加更多参数。