Search code examples
keraslstmconv-neural-networkpre-trained-model

KERAS: Pretrained a CNN+Dense model. How to freeze CNN weights and substitute Dense with LSTM?


I trained and load a cnn+dense model:

# load model
cnn_model = load_model('my_cnn_model.h5')
cnn_model.summary()

The output is this (I have images dimension 2 X 3600):

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv2d_1 (Conv2D)            (None, 2, 3600, 32)       128
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 2, 1800, 32)       3104
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 2, 600, 32)        0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 2, 600, 64)        6208
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 2, 300, 64)        12352
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 2, 100, 64)        0
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 2, 100, 128)       24704
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 2, 50, 128)        49280
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 2, 16, 128)        0
_________________________________________________________________
flatten_1 (Flatten)          (None, 4096)              0
_________________________________________________________________
dense_1 (Dense)              (None, 1024)              4195328
_________________________________________________________________
dense_2 (Dense)              (None, 1024)              1049600
_________________________________________________________________
dense_3 (Dense)              (None, 3)                 3075
=================================================================
Total params: 5,343,779
Trainable params: 5,343,779
Non-trainable params: 0

Now, what I want is to leave weights up to flatten and replace dense layers with LSTM to train the added LSTM part.

I just wrote:

# freeze model
base_model = cnn_model(input_shape=(2, 3600, 1))

#base_model = cnn_model
base_model.trainable = False

# Adding the first lstm layer
x = LSTM(1024,activation='relu',return_sequences='True')(base_model.output)

# Adding the second lstm layer
x = LSTM(1024, activation='relu',return_sequences='False')(x)

# Adding the output
output = Dense(3,activation='linear')(x)

# Final model creation
model = Model(inputs=[base_model.input], outputs=[output])

But I obtained:

base_model = cnn_model(input_shape=(2, 3600, 1))
TypeError: __call__() missing 1 required positional argument: 'inputs'

I know I have to add TimeDistributed ideally in the Flatten layer, but I do not know how to do. Moreover I'm not sure about base_model.trainable = False if it do exactly what I want. Can you please help me to do the job?

Thank you very much!


Solution

  • You can't directly take the output from Flatten(), LSTM needs 2-d features (time, filters). You have to reshape your tensors.

    You can take the output from the layer before flatten (max-pooling), let's say this layer has index i in the model, we can take the output from that layer and reshape it based on our needs and pass it to LSTM.

    before_flatten = base_model.layers[i].output # i is the index of the layer from which you want to take the model output
    
    conv2lstm_reshape = Reshape((-1, 2))(before_flatten) # you have to select it, the temporal dim and filters
    
    # Adding the first lstm layer
    x = LSTM(1024,activation='relu',return_sequences='True')(conv2lstm_reshape)
    
    # Adding the second lstm layer
    x = LSTM(1024, activation='relu',return_sequences='False')(x)
    
    # Adding the output
    output = Dense(3,activation='linear')(before_flatten)
    
    # Final model creation
    model = Model(inputs=[base_model.input], outputs=[output])
    
    model.summary()