1

I am building a model which merges CNN and LSTM, but when I wanna train it, it pops out an Error: RuntimeError: You must compile your model before using it.

I have tried a lot of possible solution on StackOverflow, such as Keras Bidirectional "RuntimeError: You must compile your model before using it." after compilation completed & Keras encoder-decoder model RuntimeError: You must compile your model before using it

but none of them can work.

I have tried a lot of possible solution on StackOverflow, such as Keras Bidirectional "RuntimeError: You must compile your model before using it." after compilation completed & Keras encoder-decoder model RuntimeError: You must compile your model before using it

but none of them can work.

in creat_model.py

def create_model(self, ret_model = False):
    #base_model = VGG16(weights='imagenet', include_top=False, input_shape = (224, 224, 3))
    #base_model.trainable=False
    image_model = Sequential()
    #image_model.add(base_model)
    #image_model.add(Flatten())
    image_model.add(Dense(EMBEDDING_DIM, input_dim = 4096, activation='relu'))

    image_model.add(RepeatVector(self.max_cap_len))

    lang_model = Sequential()
    lang_model.add(Embedding(self.vocab_size, 256, input_length=self.max_cap_len))
    lang_model.add(LSTM(256,input_shape=(40,256), return_sequences=True))
    lang_model.add(TimeDistributed(Dense(EMBEDDING_DIM)))

    model = Sequential()
    model.add(Concatenate([image_model, lang_model]))
    model.add(LSTM(1000,input_shape=(40,128), return_sequences=False))
    model.add(Dense(self.vocab_size))
    model.add(Activation('softmax'))

    print("Model created!")

    if(ret_model==True):
        return model

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

...

in train.py

def train_model(weight = None, batch_size=32, epochs = 10):

    cg = caption_generator.CaptionGenerator()
    model = cg.create_model()

    if weight != None:
        model.load_weights(weight)

    counter = 0
    file_name = 'weights-improvement-{epoch:02d}.hdf5'
    checkpoint = ModelCheckpoint(file_name, monitor='loss', verbose=1, save_best_only=True, mode='min')
    callbacks_list = [checkpoint]
    model.fit_generator(cg.data_generator(batch_size=batch_size), steps_per_epoch=cg.total_samples/batch_size, epochs=epochs, verbose=2, callbacks=callbacks_list)
    try:
        model.save('Models/WholeModel.h5', overwrite=True)
        model.save_weights('Models/Weights.h5',overwrite=True)
    except:
        print("Error in saving model.")
    print("Training complete...\n")
Chason Fu
  • 11
  • 2
  • VGG16 is an functional model, so just adapt the functional API and it should work – ixeption Jan 28 '19 at 13:28
  • sorry, i still don't understand.. can you explain more details, please? – Chason Fu Jan 29 '19 at 01:22
  • Keras has two API styles, functional and sequential. VGG16 is functional, your API is sequential. It shoudl work, if you just rewrite your code to functional style. Keras documentation has some more information – ixeption Jan 29 '19 at 10:06

0 Answers0