您好,登錄后才能下訂單哦!
這篇文章主要講解了keras如何實現(xiàn)densenet和Xception的模型融合,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
我正在參加天池上的一個競賽,剛開始用的是DenseNet121但是效果沒有達(dá)到預(yù)期,因此開始嘗試使用模型融合,將Desenet和Xception融合起來共同提取特征。
代碼如下:
def Multimodel(cnn_weights_path=None,all_weights_path=None,class_num=5,cnn_no_vary=False): ''' 獲取densent121,xinception并聯(lián)的網(wǎng)絡(luò) 此處的cnn_weights_path是個列表是densenet和xception的卷積部分的權(quán)值 ''' input_layer=Input(shape=(224,224,3)) dense=DenseNet121(include_top=False,weights=None,input_shape=(224,224,3)) xception=Xception(include_top=False,weights=None,input_shape=(224,224,3)) #res=ResNet50(include_top=False,weights=None,input_shape=(224,224,3)) if cnn_no_vary: for i,layer in enumerate(dense.layers): dense.layers[i].trainable=False for i,layer in enumerate(xception.layers): xception.layers[i].trainable=False #for i,layer in enumerate(res.layers): # res.layers[i].trainable=False if cnn_weights_path!=None: dense.load_weights(cnn_weights_path[0]) xception.load_weights(cnn_weights_path[1]) #res.load_weights(cnn_weights_path[2]) dense=dense(input_layer) xception=xception(input_layer) #對dense_121和xception進(jìn)行全局最大池化 top1_model=GlobalMaxPooling2D(data_format='channels_last')(dense) top2_model=GlobalMaxPooling2D(data_format='channels_last')(xception) #top3_model=GlobalMaxPool2D(input_shape=res.output_shape)(res.outputs[0]) print(top1_model.shape,top2_model.shape) #把top1_model和top2_model連接起來 t=keras.layers.Concatenate(axis=1)([top1_model,top2_model]) #第一個全連接層 top_model=Dense(units=512,activation="relu")(t) top_model=Dropout(rate=0.5)(top_model) top_model=Dense(units=class_num,activation="softmax")(top_model) model=Model(inputs=input_layer,outputs=top_model) #加載全部的參數(shù) if all_weights_path: model.load_weights(all_weights_path) return model
如下進(jìn)行調(diào)用:
if __name__=="__main__": weights_path=["./densenet121_weights_tf_dim_ordering_tf_kernels_notop.h6", "xception_weights_tf_dim_ordering_tf_kernels_notop.h6"] model=Multimodel(cnn_weights_path=weights_path,class_num=6) plot_model(model,to_file="G:/model.png")
最后生成的模型圖如下:有點長,可以不看
需要注意的一點是,如果dense=dense(input_layer)這里報錯的話,說明你用的是tensorflow1.4以下的版本,解決的方法就是
1、升級tensorflow到1.4以上
2、改代碼:
def Multimodel(cnn_weights_path=None,all_weights_path=None,class_num=5,cnn_no_vary=False): ''' 獲取densent121,xinception并聯(lián)的網(wǎng)絡(luò) 此處的cnn_weights_path是個列表是densenet和xception的卷積部分的權(quán)值 ''' dir=os.getcwd() input_layer=Input(shape=(224,224,3)) dense=DenseNet121(include_top=False,weights=None,input_tensor=input_layer, input_shape=(224,224,3)) xception=Xception(include_top=False,weights=None,input_tensor=input_layer, input_shape=(224,224,3)) #res=ResNet50(include_top=False,weights=None,input_shape=(224,224,3)) if cnn_no_vary: for i,layer in enumerate(dense.layers): dense.layers[i].trainable=False for i,layer in enumerate(xception.layers): xception.layers[i].trainable=False #for i,layer in enumerate(res.layers): # res.layers[i].trainable=False if cnn_weights_path!=None: dense.load_weights(cnn_weights_path[0]) xception.load_weights(cnn_weights_path[1]) #print(dense.shape,xception.shape) #對dense_121和xception進(jìn)行全局最大池化 top1_model=GlobalMaxPooling2D(input_shape=(7,7,1024),data_format='channels_last')(dense.output) top2_model=GlobalMaxPooling2D(input_shape=(7,7,1024),data_format='channels_last')(xception.output) #top3_model=GlobalMaxPool2D(input_shape=res.output_shape)(res.outputs[0]) print(top1_model.shape,top2_model.shape) #把top1_model和top2_model連接起來 t=keras.layers.Concatenate(axis=1)([top1_model,top2_model]) #第一個全連接層 top_model=Dense(units=512,activation="relu")(t) top_model=Dropout(rate=0.5)(top_model) top_model=Dense(units=class_num,activation="softmax")(top_model) model=Model(inputs=input_layer,outputs=top_model) #加載全部的參數(shù) if all_weights_path: model.load_weights(all_weights_path) return model
這個bug我也是在服務(wù)器上跑的時候才出現(xiàn)的,找了半天,而實驗室的cuda和cudnn又改不了,tensorflow無法升級,因此只能改代碼了。
如下所示,是最后畫出的模型圖:(很長,底下沒內(nèi)容了)
看完上述內(nèi)容,是不是對keras如何實現(xiàn)densenet和Xception的模型融合有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。