2017-07-18 88 views

回答

0

德看看凯拉斯Merge Layers

请考虑以下玩具示例。这里我们创建两个模型,加载它们的权重并将它们合并成一个合并模型。

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) 
相关问题