keras: keras 2 - fit_generator broken?
I’ve updated to keras v2 yesterday.
I adapted all my code from version 1 to the new API, following all the warnings I encountered.
However I’m having some very strange problems with fit_generator
method of Model
.
Using this toy example, wich worked totally fine in version 1:
from keras.models import Model
from keras.layers import Input, Dense, Flatten
from keras.optimizers import SGD
from keras.losses import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
gen = ImageDataGenerator()
train_batches = gen.flow_from_directory("D:/GitHub/Kaggle/redux/train/")
inp = Input(shape=(256,256,3))
l1 = Flatten()(inp)
out = Dense(2, activation="softmax")(l1)
model = Model(inp, out)
model.compile(loss=categorical_crossentropy, optimizer=SGD(lr=0.01))
model.fit_generator(train_batches, train_batches.samples / train_batches.batch_size)
The output in jupyter notebook is quite strange, printing a unknown symbol until the notebook crashes:
Epoch 1/1
23/718 [..............................] - ETA: 522s - loss: 8.4146
Running the code from the terminal don’t print those strange symbols.
The code works perfect when manually getting the batches from the generator to use with model.fit
:
n = 0
for imgs, labels in train_batches:
if n > 3:
break
X_train = np.array(imgs)
y_train = np.array(labels)
model.fit(X_train, y_train)
n += 1
Epoch 1/1
32/32 [==============================] - 0s - loss: 7.5555
Epoch 1/1
32/32 [==============================] - 0s - loss: 8.5627
Epoch 1/1
32/32 [==============================] - 0s - loss: 6.5480
Epoch 1/1
32/32 [==============================] - 0s - loss: 10.0738
Anyone is facing similar problems with fit_generator
and/or know something about it?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 25 (7 by maintainers)
For those who are looking at this issue now.
My solution: switch both steps_per_epoch and validation_steps to be the no Of samples/batch size.
I believe this is what @fchollet mentioned in his response. For me verbose=1 worked fine.
so my batch size is 32 , no of samples was 8000 and validation set size was 2000
As per the keras documentation the parameter steps_per_epoch = no of train samples/batch_size and validation_steps = no of validation samples/batch_size
steps_per_epoch = 8000/32 = 250
validation_steps = 2000/32 = 62.5
Following code tested on the day of this comment. 08 Sep 2017.
Old Code
classifier.fit_generator(training_set, samples_per_epoch=8000, epochs=25, verbose=1, validation_data=test_set, validation_steps=2000)
Changed Code
classifier.fit_generator(training_set, steps_per_epoch=250, epochs=25, verbose=1, validation_data=test_set, validation_steps=62.5)
You should all note that generator methods have switched from being sample-based (one epoch = defined number of samples) to being step-based (one epoch = defined number of batches). The conversion is handled automatically when possible, but if you are using custom generators then that may not be possible.
For more info, see the release notes:
https://github.com/fchollet/keras/wiki/Keras-2.0-release-notes
On 22 March 2017 at 12:36, jerpint notifications@github.com wrote:
I’m also having a problem with fit_generator after upgrading to Keras2. The model training time has gone up about 1000 times! Have not figured out yet why. I read that in Keras2 fit_generator number of samples has been replaced by the number of batches. I suspect this is the cause of the issue but don’t know for sure.
Ok so:
About loading images 1 by 1. The problem was that I was used to keras v1 where the number printed were the number of images. Now is the number of steps. the slowniness is just caused by the overhed of printing that strange symbol.
Setting verbose=0 on fit_generator avoids printing this strange thing at cost of not printing shit.
Lastly, this is a little embarrasing, but I closed the issue by mistake when closing issues from my repos. xD