2017-10-31 32 views
0

2種類のデータ(画像のボリュームと座標)があり、画像ボリュームデータに畳み込みニューラルネットワークを使いたいと思っています。情報(すなわち、ボリュームの座標)。Kerasで2種類の深い学習モデルを結合する

これは独立して、私の関数のかなりの予測変数を作成するはずです。どのようにKerasを使ってこれを実装できますか?

私がオンラインで見つけた唯一の回答はあいまいであるか、または私が仕事をしなければならない廃止された方法を使用しています。しかし、実際のAPIを使ってこれを実装して、後で使用するためにモデルをより簡単に保存できるようにしたいと思います。

model = Sequential() 
model.add(Conv3D(32, kernel_size=(3, 3, 3), 
       activation='relu', 
       input_shape=input_shape)) 
model.add(Conv3D(64, (3, 3, 3), activation='relu')) 
model.add(MaxPooling3D(pool_size=(2, 2, 2))) 
model.add(Dropout(0.25)) 
model.add(Flatten()) 
print(model.output_shape) 

# The additional data (the coordinates x,y,z) 
extra = Sequential() 
extra.add(Activation('sigmoid', input_shape=(3,))) 
print(extra.output_shape) 

merged = Concatenate([model, extra]) 

# New model should encompass the outputs of the convolutional network and the coordinates that have been merged. 
# But how? 
new_model = Sequential() 
new_model.add(Dense(128, activation='relu')) 
new_model.add(Dropout(0.8)) 
new_model.add(Dense(32, activation='sigmoid')) 
new_model.add(Dense(num_classes, activation='softmax')) 

new_model.compile(loss=keras.losses.categorical_crossentropy, 
       optimizer=keras.optimizers.Adadelta(), 
       metrics=['accuracy']) 

答えて

1

シーケンシャルモデルは支店を持つモデルを作成するには適していません。

2つの独立したモデルをシーケンシャルモデルとして使用できますが、Concatenateからは機能モデルAPIの使用を開始する必要があります。

考え方は、2つのモデルの出力テンソルを取得し、新しい出力テンソルを得るためにそれらを他のレイヤーに供給することです。

だから、あなたはmodelextraを持って考慮:

mergedOutput = Concatenate()([model.output, extra.output]) 

このmergetOutputはテンソルです。このテンソルを使用してモデルの最後の部分を作成するか、独立して最後の部分を作成して、このテンソルで呼び出すことができます。第2のアプローチは、あなたが各モデルを別々に訓練したい場合には良いかもしれません(あなたのケースではないようです)。今

、機能的なAPIモデルとして、新たなモデルを作成:

out = Dense(128, activation='relu')(mergetOutput) 
out = Dropout(0.8)(out) 
out = Dense(32, activation='sigmoid')(out) 
out = Dense(num_classes, activation='softmax')(out) 

new_model = Model(
    [model.input, extra.input], #model with two input tensors 
    out       #and one output tensor 
) 

より簡単なアプローチは、作成済みのすべての3つのモデルを取ると組み合わせたモデルを作成するためにそれらを使用することです:

model = Sequential() #your first model 
extra = Sequential() #your second model  
new_model = Sequential() #all these three exactly as you did 

#in this case, you just need to add an input shape to new_model, compatible with the concatenated output of the previous models. 
new_model.add(FirstNewModelLayer(...,input_shape=(someValue,))) 

このようにそれらに参加:

mergedOutput = Concatenate()([model.output, extra.output]) 
finalOutput = new_model(mergedOutput)  

fullModel = Model([model.input,extra.input],finalOutput) 
+0

APIの例を明確にしていただきありがとうございます。それは有り難いです! – JahKnows

+0

もう1つ質問がありますが、どうすればこの種のモデルを保存して読み込むことができますか? fullModelだけを保存すれば十分ですか? – JahKnows

+0

残念ながら私はケラでモデルを保存することはできませんでした。どうしてか分かりません。私がしているのは 'fullModel.save_weights(filename)'と 'fullModel.load_weights(filename)'です。これは、学習モデルを保存して読み込むのには十分ですが、トレーニングをやり直すときには問題が発生する可能性があります(このプロセスでオプティマイザが失われ、トレーニング中に再度調整する必要があります)。 –

0

Keras(https://keras.io/models/model/)の機能的APIを使用してください。 Kerasのマージレイヤーにレイヤーを適用するだけで済みます。機能的なAPIはこのように機能します。あなたはテンソルを持っており、このテンソルに関数を適用します。次にこれを再帰的に評価する。 Kerasではテンソルがかなり多いので、これはとてもうまく動作します。

このための例は次のとおりです。

activation = Dense(128, activation='relu')(merged) 
関連する問題