Remove last layer from a custom pre-trained model built with Functional API

I am building an emotion recogniton model that receives both text and audio features. I created an audio feature extractor, one that receives feature vectors from opensmile toolkit, in order to get better vectors.

def get_audio_model (lr):

    input_audio = Input(shape = (1, 1422))
    audio_hidden1 =  (Bidirectional(LSTM(526, return_sequences = False, input_shape = (1, 1422), recurrent_dropout=0.5)))(input_audio)
    drop1 = Dropout(0.3)(audio_hidden1)
    audio_hidden3 =  Dense(256, activation='relu')(drop1)
    final = Dense(3, activation='softmax')(audio_hidden3)
    model = keras.Model(inputs=input_audio, outputs=final, name="audio_extractor")

    model.compile(loss='categorical_crossentropy', optimizer = keras.optimizers.Adam(learning_rate = lr), metrics=['accuracy', precision, recall])
    return model

I have trained this model, saved it and loaded it in the following way'audio_feature_extractor.h5') loaded_audio_model = keras.models.load_model('audio_feature_extractor.h5')

I want to remove the top layer from the loaded model so that I can use the output from layer audio_hidden3 and concatenate it with other vector.
I tried pop() but it doesn’t do anything in functional models. I also tried

audioft_extractor = loaded_audio_model.layers[0:-1]

but it just creates a list with the layers. Any idea on how to just remove the last layer?

>Solution :

How about just creating that new model but specify as follows:

new_model = Model(inputs=..., outputs=loaded_audio_model.layers[-1].output

Leave a Reply