2017-02-24 86 views
1

我正在執行Mohmad Havaei的this論文。它採用以下結構:關於keras中合併圖層的培訓

enter image description here

我已經修改了一些代碼here這樣做。

print 'Compiling two-path model...' 
#local pathway 
modle_l=Sequential() 
modle_l.add(Convolution2D(64,7,7, 
border_mode='valid',W_regularizer=l1l2(l1=0.01, l2=0.01), 
input_shape=(4,33,33))) 
modle_l.add(Activation('relu')) 
modle_l.add(BatchNormalization(mode=0,axis=1)) 
modle_l.add(MaxPooling2D(pool_size=(2,2),strides=(1,1))) 
modle_l.add(Dropout(0.5)) 
#Add second convolution 
modle_l.add(Convolution2D(64,3,3, 
border_mode='valid',W_regularizer=l1l2(l1=0.01, l2=0.01), 
input_shape=(4,33,33))) 
modle_l.add(BatchNormalization(mode=0,axis=1)) 
modle_l.add(MaxPooling2D(pool_size=(4,4), strides=(1,1))) 
modle_l.add(Dropout(0.5)) 
#global pathway 
modelg = Sequential() 
modelg.add(Convolution2D(160,12,12, 
border_mode='valid', W_regularizer=l1l2(l1=0.01, l2=0.01), 
input_shape=(self.n_chan,33,33))) 
modelg.add(Activation('relu')) 
modelg.add(BatchNormalization(mode=0, axis=1)) 
modelg.add(MaxPooling2D(pool_size=(2,2), strides=(1,1))) 
modelg.add(Dropout(0.5)) 

# merge local and global pathways 
merge= Sequential() 
merge.add(Merge([modle_l,modelg], mode='concat',concat_axis=1)) 
merge.add(Convolution2D(5,21,21, 
border_mode='valid', 
W_regularizer=l1l2(l1=0.01, l2=0.01), input_shape=(4,33,33))) 

# Flatten output of 5x1x1 to 1x5, perform softmax 
merge.add(Flatten()) 
merge.add(Dense(5)) 
merge.add(Activation('softmax')) 
sgd = SGD(lr=0.001, decay=0.01, momentum=0.9) 
merge.compile(loss='categorical_crossentropy', optimizer='sgd') 

print 'Done' 
return merge 

我已經使用這個替代方法爲圖模型在keras 1.0 我的問題是不贊成我怎麼現在訓練模式? 我已經使用這個訓練

merge.fit(X_train, Y_train, batch_size=self.batch_size, nb_epoch=self.n_epoch, validation_split=0.1, show_accuracy=True, verbose=1) 

在情況下,我需要訓練分開兩層,接着合併,我該怎麼做呢?

+0

只是一個改進,功能API是什麼取代了圖形API,它更容易使用,特別是這些類型的模型。 –

+0

的確,我還建議你去使用Functionnal API。它更加靈活。否則,我認爲你這樣做的方式應該是對兩種模型進行培訓。 –

回答

1
from keras.layers import * 
from keras.models import Model 

print 'Compiling two-path model...' 

# Input of the model 
input_model = Input(shape=(4,33,33)) 
# Local pathway 
#Add first convolution 
model_l = Convolution2D(64,7,7, 
          border_mode='valid', 
          activation='relu', 
          W_regularizer=l1l2(l1=0.01, l2=0.01))(input_model) 
model_l = BatchNormalization(mode=0,axis=1)(model_l) 
model_l = MaxPooling2D(pool_size=(2,2),strides=(1,1))(model_l) 
model_l = Dropout(0.5)(model_l) 
#Add second convolution 
model_l = Convolution2D(64,3,3, 
         border_mode='valid', 
         W_regularizer=l1l2(l1=0.01, l2=0.01), 
         input_shape=(4,33,33))(model_l) 
model_l = BatchNormalization(mode=0,axis=1)(model_l) 
model_l = MaxPooling2D(pool_size=(4,4),strides=(1,1))(model_l) 
model_l = Dropout(0.5)(model_l) 

#global pathway 
model_g = Convolution2D(160,12,12, 
         border_mode='valid', 
         activation='relu', 
         W_regularizer=l1l2(l1=0.01, l2=0.01))(input_model) 
model_g = BatchNormalization(mode=0,axis=1)(model_g) 
model_g = MaxPooling2D(pool_size=(2,2), strides=(1,1))(model_g) 
model_g = Dropout(0.5)(model_g) 

# merge local and global pathways 

merge = Merge(mode='concat', concat_axis=1)([model_l,model_g]) 
merge = Convolution2D(5,21,21, 
         border_mode='valid', 
         W_regularizer=l1l2(l1=0.01, l2=0.01))(merge) 
merge = Flatten()(merge) 
predictions = Dense(5, activation='softmax')(merge) 

model_merged = Model(input=input_model,output=predictions) 
sgd = SGD(lr=0.001, decay=0.01, momentum=0.9) 
model_merged.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) 

print('Done') 
return model_merged 

這是您發佈但與Functional API

定義。如可以看到網絡的等效,僅存在1輸入層,使用了兩次。然後你可以像你說的那樣訓練它:

model_merged.fit(X_train, Y_train, batch_size=self.batch_size, nb_epoch=self.n_epoch, validation_split=0.1, verbose=1) 

那有幫助嗎?

+0

是的模型是用一個小的改變合併(mode ='concat'...)而不是merge()來編譯的。然而,fit()拋出錯誤TypeError:fit()得到了一個意外的關鍵字參數'show_accuracy' –

+0

對不起,從我的手機做到了...我將相應地編輯 –

+0

現在應該會更好 –