Search code examples
pythonmachine-learningkerastransfer-learning

Save Keras Model and Reuse It For Transfer Learning


I build a model in Keras for CNN. I want to save this model and reuse this trained model for transfer learning as a pre-trained model. I do not understand what is the proper way to save the model for reusing it for transfer learning. Again how to load my pre-trained model in Keras so that I can add some layers after load the previous model.

This is my model that I build

model_cnn = Sequential()      # initilaizing the Sequential nature for CNN model
# Adding the embedding layer which will take in maximum of 450 words as input and provide a 32 dimensional output of those words which belong in the top_words dictionary
model_cnn.add(Embedding(vocab_size, embedding_size, input_length=max_len))
model_cnn.add(Conv1D(32, 3, padding='same', activation='relu'))
model_cnn.add(Conv1D(64, 3, padding='same', activation='relu'))
model_cnn.add(MaxPooling1D())
model_cnn.add(Flatten())
model_cnn.add(Dense(250, activation='relu'))
model_cnn.add(Dense(2, activation='softmax'))

# optimizer = keras.optimizers.Adam(lr=0.001)
model_cnn.compile(
    loss='categorical_crossentropy', 
    optimizer=sgd, 
    metrics=['acc',Precision(),Recall(),]
)

history_cnn  = model_cnn.fit(
    X_train, y_train,
    validation_data=(X_val, y_val), 
    batch_size = 64,
    epochs=epochs,
    verbose=1
)

Solution

  • The recommended way to save model, is saving with SavedModel format:

    dir = "target_directory"
    model_cnn.save(dir) # it will save a .pb file with assets and variables folders 
    

    Then you can load it:

    model_cnn = tf.keras.models.load_model(dir)
    

    Now, you can add some layers and make another model. For example:

    input = tf.keras.Input(shape=(128,128,3))
    x = model_cnn(input)
    x = tf.keras.layers.Dense(1, activation='sigmoid')(x)
    new_model = tf.keras.models.Model(inputs=input, outputs=x)