2017-07-18 7 views
0

こんにちは私は既に私が訓練した多くのモデルを組み合わせなければならない仕事があります。今私は、すべてのモデルを結合し、それ以上のトレーニングをせずに単一の出力を得たいと思っています。ありがとうございますケラスで異なるモデル出力を組み合わせる

答えて

0

タルケアMerge Layersを見てください。

次のおもちゃの例を考えてみましょう。ここでは、2つのモデルを作成し、それらの重みをロードし、それらを1つの結合モデルに結合します。

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) 
関連する問題