Tak ein Blick auf Keras Merge Layers.
Betrachten Sie das folgende Beispiel Spielzeug. Hier erstellen wir zwei Modelle, laden ihre Gewichte und kombinieren sie zu einem einzigen zusammengeführten Modell.
from keras.layers import Conv2D, MaxPooling2D, Input, AvgPool2D, concatenate
def get_model1(input_shape):
input_layer = Input(input_shape)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_layer)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), padding='same')(x)
model = Model(input_layer, x)
return model
def get_model2(input_shape):
input_layer = Input(input_shape)
x = Conv2D(16, (5, 5), activation='relu', padding='same')(input_layer)
x = Conv2D(16, (5, 5), activation='relu', padding='same')(x)
x = AvgPool2D((5, 5), strides=(2, 2), padding='same')(x)
model = Model(input_layer, x)
return model
model1 = get_model1(input_shape)
model1.load_weights('your_path_to_model1_weights')
model2 = get_model2(input_shape)
model2.load_weights('your_path_to_model2_weights')
# Combine two models
# concat_axis is the axis along which tensors are concatenated.
# If you are working with images, then it is usually the channel axis.
merged_model = concatenate([model1, model2], axis=concat_axis)