Keras: “must compile model before using it” despite compile() is used

偶尔善良 提交于 2019-12-02 00:19:51

问题


I want to create and train a CNN model in Keras for classification of banknotes. Creating models works fine with simple tutorials but not with the architecture I adopt from this paper. Keras outputs: RuntimeError('You must compile your model before using it.') after fit_generator() is called.

I use the tensorflow backend if that is of relevance.


Model is defined in model.py:

from keras.layers import ...
model = Sequential() 
model.add(some_layer)

... #according to the paper

model.add(some_layer)
model.add(Dense(#output_classes, activation='softmax') #last layer

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

The model is then used from start_train.py:

from model import model as m

#some ImageGenerator stuff as input

m.fit_generator( #training on train_data
        train_pics,
        steps_per_epoch=#steps,
        epochs=#epochs,
        validation_data=test_pics,

As far as I understood it the process in Keras is as follows:

  1. Define model
  2. Compile model
  3. (If wanted evaluate() & summary() can be used now after the compilation)
  4. Fit model
  5. Evaluate model.

I tested if model.py is accessed before calling fit_generator() and it works properly. I'm out of ideas and wondering what I'm doing wrong especially since the same set-up works fine with a basic model/architecture.

Any help is highly appreciated! :)


回答1:


Found my mistake - explanation for future reference.

The Error origniates back in compile() where the first if-statement says:

if not self.built:
    # Model is not compilable because
    # it does not know its number of inputs
    # and outputs, nor their shapes and names.
    # We will compile after the first
    # time the model gets called on training data.
return

So I specified input_shape= and input_format=in the first Conv2D layer and everything works fine.




回答2:


If ever someone ends up with the same error code here is maybe a way to fix it. So I was using a generator and was getting the "Must compile" error even tho everything was fine. I was able to fix it by doing a model.fit(x,y) on a single batch before launching my fit_generator and everything worked fine after that. I don't know if this helps anyone but yeah!



来源:https://stackoverflow.com/questions/51792108/keras-must-compile-model-before-using-it-despite-compile-is-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!