how to concatenate two Pre trained models in keras?

Viewed 910

I have two pre-trained models and I want to concatenate them. model 1: model1.png

model 2: model2.png

and the result of concatenation that I want is :merged.png

here is the code put the weights are lost as i create the model from scratch.

i want some thing like that but without weights lose.

input_layer1= Input(shape=(20))

x=Dense(32,'relu')(input_layer)
x=Dense(16,'relu')(x)

out1=Dense(1,'sigmoid')(x)

input_layer2=Concatenate()([input_layer1,out1])

x=Dense(256,'relu')(input_layer2)
x=Dense(128,'relu')(x)
x=Dense(64,'relu')(x)
x=Dense(32,'relu')(x)

out2=Dense(y_train2.shape[1],'softmax')(x)

model = Model(inputs=input_layer1, outputs=out2)
1 Answers

With the functional model approach, a model can be called as a function, and can be used as a layer in another model. Something like this should work:

import tensorflow as tf
import tensorflow.keras.layers as L


def build_model1():
    input_layer = L.Input((20,))
    x = L.Dense(32)(input_layer)
    x = L.Dense(16)(x)
    output_layer = L.Dense(1)(x)
    model = tf.keras.models.Model(inputs=input_layer, outputs=output_layer)
    return model


def build_model2():
    input_layer = L.Input((21,))
    x = L.Dense(256)(input_layer)
    x = L.Dense(128)(x)
    x = L.Dense(64)(x)
    x = L.Dense(32)(x)
    output_layer = L.Dense(10)(x)
    model = tf.keras.models.Model(inputs=input_layer, outputs=output_layer)
    return model


def build_model_composite(model1, model2):
    input_layer = L.Input((20,))

    out1 = model1(input_layer)

    conc = L.Concatenate()([input_layer, out1])

    out2 = model2(conc)

    # xtrainshape = y_train2.shape[1]
    xtrainshape = 10
    output_layer = L.Dense(xtrainshape, "softmax")(out2)

    model = tf.keras.models.Model(inputs=input_layer, outputs=output_layer)
    return model


model1 = build_model1()
model2 = build_model2()
comp_model = build_model_composite(model1, model2)

comp_model.summary()
tf.keras.utils.plot_model(
    comp_model,
    show_shapes=True,
)

Where model1 and model2 can be trained independently.

The composite model looks like this:

composite_model

I never actually tried training something like this (I only used standard pre-trained models for transfer learning), so let me know if it worked as I expect.

Cheers!

Related