Here is my architecture.
def model1():
inputs = layers.Input(shape=(x1.shape[-2],x1.shape[-1]), name='Input_layer')
x = layers.Reshape((x1.shape[-2], x1.shape[-1], 1), name='Input_reshape')(inputs)
#x= layers.Masking(mask_value=0.0, input_shape=(109, 80))(x)
x = layers.Conv2D(32, 3, strides=2,
activation=layers.LeakyReLU(alpha=0.2),
name='convolution_1')(x)
x = layers.BatchNormalization()(x)
x = layers.Dropout(0.2, name='dropout1_model1')(x)
x = layers.Conv2D(64, 3, strides=1,
activation=layers.LeakyReLU(alpha=0.2),
name='convolution_2')(x)
x = layers.BatchNormalization()(x)
x = layers.Dropout(0.2, name='dropout2_model1')(x)
x = layers.Reshape((-1, x.shape[-2] * x.shape[-1]), name='conv_reshpae')(x)
x = layers.Bidirectional(layers.GRU(64, return_sequences=True, activation=layers.LeakyReLU(alpha=0.2), name='GRU_1'))(x)
x = layers.Dropout(0.2, name='dropout3_model1')(x)
x = layers.Bidirectional(layers.GRU(64, return_sequences=True, activation=layers.LeakyReLU(alpha=0.2), name='GRU_2'))(x)
x = layers.Dropout(0.2, name='dropout4_model1')(x)
outputs=layers.Dense(128, activation=layers.LeakyReLU(alpha=0.2), name='output_1')(x)
model=keras.Model(inputs=inputs, outputs=outputs, name='audio_model')
return model
model_audio=model1()
print(model_audio.summary())
def model2():
inputs=layers.Input(shape=x2.shape[-1])
x=layers.Embedding(vocab_size, embedding_dim, mask_zero=True)(inputs)
x=layers.Bidirectional(LSTM(64, return_sequences=True, activation=layers.LeakyReLU(alpha=0.2), name='First_LSTM'))(x)
x = layers.Dropout(0.2, name='dropout1_model2')(x)
x=layers.Bidirectional(LSTM(64, return_sequences=True, activation=layers.LeakyReLU(alpha=0.2), name='Second_LSTM'))(x)
x = layers.Dropout(0.2, name='dropout2_model')(x)
outputs=layers.Dense(128, activation=layers.LeakyReLU(alpha=0.2), name='output_2')(x)
model=keras.Model(inputs=inputs,outputs=outputs, name='text_model')
return model
model_text=model2()
print(model_text.summary())
def model3(model_audio,model_text):
Input_1 = tf.keras.layers.Input(shape=(x1.shape[-2], x1.shape[-1]), name='Input_1')
Input_2 = tf.keras.layers.Input(shape=(x2.shape[-1]), name='Input_2')
output_1 = model_audio(Input_1)
output_2 = model_text(Input_2)
layer = layers.MultiHeadAttention(num_heads=2, key_dim=1, name='attention_layer')
context_vector, attention_weights = layer(output_1, output_2, return_attention_scores=True)
x = layers.GRU(128, return_sequences=False, activation=layers.LeakyReLU(alpha=0.2), name='GRU')(context_vector)
x = layers.Dropout(0.2, name='dropout1_modelFinal')(x)
x = layers.Dense(32, activation=layers.LeakyReLU(alpha=0.2))(x)
x = layers.Dropout(0.2, name='dropout2_modelFinal')(x)
outputs = layers.Dense(1,activation='sigmoid', name='final_layer')(x)
model = keras.Model(inputs=[Input_1, Input_2], outputs=outputs)
return model
model_final = model3(model_audio,model_text)
print(model_final.summary())
Here is the model3 (model final) summary
Layer (type) Output Shape Param # Connected to
Input_1 (InputLayer) [(None, 135, 40)] 0 []
Input_2 (InputLayer) [(None, 17)] 0 []
model (Functional) (None, 65, 128) 553344 ['Input_1[0][0]']
model_1 (Functional) (None, 17, 128) 287616 ['Input_2[0][0]']
attention_layer (MultiHead ((None, 65, 128), 1158 ['model[0][0]',
Attention) (None, 2, 65, 17)) 'model_1[0][0]']
GRU (GRU) (None, 128) 99072 ['attention_layer[0][0]']
dropout1_modelFinal (Dropo (None, 128) 0 ['GRU[0][0]']
ut)
dense (Dense) (None, 32) 4128 ['dropout1_modelFinal[0][0]']
dropout2_modelFinal (Dropo (None, 32) 0 ['dense[0][0]']
ut)
final_layer (Dense) (None, 1) 33 ['dropout2_modelFinal[0][0]']
================================================================================================== Total params: 945351 (3.61 MB) Trainable params: 945159 (3.61 MB) Non-trainable params: 192 (768.00 Byte)
Here I want to extract the model (Functional) output which is an another model (mode_audio) defined. It is a submodel fused in main model. actually model_audio output is used an input to model_final
Initially this is what tried
model_output = model.layers[-8].output # 'model' layer is at index -8 from the summary
model_layer_output = Model(inputs=model.input, outputs=model_output)
intermediate_output = model_layer_output.predict([test_input_1_data, test_input_2_data])
since layer[-8] is the model_audio layer .i used that as an output. But i was getting the below error
Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 135, 40), dtype=tf.float32, name='Input_layer'), name='Input_layer', description="created by layer 'Input_layer'") at layer "Input_reshape". The following previous layers were accessed without issue: []
So later i tried this. I got the expected output shape. but i am not sure whether it is right
#audio model
input_layer = Input(shape=(135, 40))
model_audio_output = model.get_layer('model')(input_layer)
submodel = Model(inputs=input_layer, outputs=model_audio_output)
audio_embed=submodel.predict(input_samples)
can someone suggest how to extract the model_audio output from my saved keras model