Reputation: 503
I am trying to make a classifier which can classify cats and dogs using keras. I am just trying to create the tensor data from images using ImageDataGenerator.flow_from_directory() which are sorted and kept in the directories whose paths are given in train_path, test_path etc.
Here is my code:
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)
But I am getting the following error using 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'
What can I do to solve this problem?
Upvotes: 2
Views: 1954
Reputation: 914
Method flow_from_directory
of ImageDataGenerator
is not static. Therefore you first have to initialize an instance of class ImageDataGenerator
and then call this method.
This should work:
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)
Check documentation for adding more parameters.
Upvotes: 2