Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the keras model compile but the fit_generator command throws a 'model not compiled runtime error'?

I've set up a custom CNN with K-fold cross-validation in keras with tensorflow back-end. The model.compile() function is called before starting the training, but the model.fit_generator() function call results in an runtime error: "You must compile your model before using it."

I do data augmentation with ImageDataGenerator and use the fit_generator function for training.

The only related issue I've found up untill now had to do with the tensorflow eager execution feature which seems to not be enabled in Keras.

Here is the code:

model definition:

model = Sequential()
model.add(Conv2D(24, (5, 5), 
          strides=(1, 1), 
          padding="valid", 
          data_format="channels_last",
          activation='relu', 
          use_bias=True,
          ))#out=96
model.add(Dropout(.25))
model.add(MaxPooling2D(pool_size=(2, 2)))#out=48
model.add(Conv2D(32, (3, 3), 
          strides=(1, 1), 
          padding="valid", 
          data_format="channels_last",
          activation='relu', 
          use_bias=True,
          ))#out=46
model.add(MaxPooling2D(pool_size=(2, 2)))#out=23
model.add(Conv2D(48, (3, 3), 
          strides=(1, 1), 
          padding="valid", 
          data_format="channels_last",
          activation='relu', 
          use_bias=True,
          ))#out=21
model.add(MaxPooling2D(pool_size=(2, 2)))#padding???
model.add(Flatten())
model.add(Dense(3, activation='softmax'))

...here would be the data initialization...

setting up the ImageGenerator:

datagen_training = ImageDataGenerator(
            rotation_range = 20,
            width_shift_range = 0.3,
            height_shift_range=0.3,
            zoom_range=0.2,
            fill_mode = "constant",
            cval = 0,
            vertical_flip = True,
            validation_split = 0.2
            )
datagen_training.fit(data)

model setup and training:

rmsprop = optimizers.RMSprop(lr=0.001)#docu says to only tune the learning rate
kf = KFold(n_splits=FOLDS, shuffle = True, random_state=78945)
model.compile(rmsprop, loss = losses.categorical_crossentropy, metrics=[metrics.categorical_accuracy])
acc_hist = []
while True:
    history = object()
    for train_idx, val_idx in kf.split(data, labels):
        x_train, y_train = data[train_idx], labels[train_idx]
        x_val, y_val = data[val_idx], labels[val_idx]
        data_iterator = datagen_training.flow(x_train, y_train, batch_size=BATCH_SIZE)
        history = model.fit_generator(data_iterator, steps_per_epoch=len(x_train) // BATCH_SIZE, epochs=1)
    acc_hist.append(history.history['categorical_accuracy'][0])
    #stop if accuracy doesn't change within 3 epochs
    if stopping_criterion_met:
        break
like image 606
Smyke Avatar asked Oct 17 '22 11:10

Smyke


1 Answers

The model isn't compiling because it isn't being built, and it isn't being built because the input_shape isn't being specified in the first layer. I'm not sure the input shape of your data, but something like this for your first layer would get the model to compile:

model = Sequential()
model.add(Conv2D(24, (5, 5), 
      strides=(1, 1), 
      padding="valid", 
      data_format="channels_last",
      activation='relu', 
      use_bias=True,
      input_shape=(100,100,1)
      ))#out=96
like image 113
A Kruger Avatar answered Nov 15 '22 05:11

A Kruger