Monica Heddneck
Monica Heddneck

Reputation: 3125

Error when checking model input: Found: <keras.preprocessing.image.DirectoryIterator... object> in Keras

I have batches of data in an iterator, here, using native Keras and nothing fancy:

batches = gen.flow_from_directory(path, target_size=(224,224), class_mode=class_mode, shuffle=shuffle, batch_size=batch_size) .

which appears fine:

print batches:

keras.preprocessing.image.DirectoryIterator object at 0x7f107c004210\

But now I've compiled and I'm ready to fit:

model.compile(optimizer=Adam(1e-5), loss='categorical_crossentropy', metrics=['accuracy']) .

model.fit(batches, val_batches, nb_epoch=1)

but I keep getting:

Exception: Error when checking model input: data should be a Numpy array, or list/dict of Numpy arrays. Found: <keras.preprocessing.image.DirectoryIterator object at 0x7f107c004210>...

No Keras doesn't like that I'm using an iterator? Why can't I fit using an iterator? I thought that's the whole point -- don't eat up all your memory, instead use some kind of batch iterator.

Upvotes: 2

Views: 2618

Answers (1)

Sergii Gryshkevych
Sergii Gryshkevych

Reputation: 4159

fit method expects its input to be a Numpy array or list of Numpy array. You should use fit_generator instead, which takes a generator as its argument.

model.fit_generator(generator=batches, 
                    validation_data=val_batches, 
                    nb_epoch=1)

Upvotes: 3

Related Questions