Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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! :)

like image 977
very_interesting Avatar asked Aug 10 '18 18:08

very_interesting


People also ask

What is the model compile () method used for in keras?

compile method. Configures the model for training. optimizer: String (name of optimizer) or optimizer instance.

Is model compile necessary?

It has nothing to do with the weights and you can compile a model as many times as you want without causing any problem to pretrained weights. You need a compiled model to train (because training uses the loss function and the optimizer). But it's not necessary to compile a model for predicting.

Why do we need to compile the model?

During compilation, COMPILE checks for format errors, so you can use COMPILE to help debug your code before running a model. When you do not use COMPILE before you run the model, then the model is compiled automatically before it is solved.

What is compiling in keras?

The compilation is the final step in creating a model. Once the compilation is done, we can move on to training phase. Let us learn few concepts required to better understand the compilation process.


1 Answers

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.

like image 87
very_interesting Avatar answered Sep 26 '22 21:09

very_interesting