使用keras實現(xiàn)densenet和Xception的模型融合
我正在參加天池上的一個競賽,剛開始用的是DenseNet121但是效果沒有達到預(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進行全局最大池化 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
如下進行調(diào)用:
if __name__=="__main__": weights_path=["./densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5", "xception_weights_tf_dim_ordering_tf_kernels_notop.h5"] 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進行全局最大池化 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)容了)

以上這篇使用keras實現(xiàn)densenet和Xception的模型融合就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python的tkinter布局之簡單的聊天窗口實現(xiàn)方法
這篇文章主要介紹了python的tkinter布局之簡單的聊天窗口實現(xiàn)方法,對于tkinter用法做了初步的介紹與應(yīng)用展示,需要的朋友可以參考下2014-09-09
在python中利用dict轉(zhuǎn)json按輸入順序輸出內(nèi)容方式
今天小編就為大家分享一篇在python中利用dict轉(zhuǎn)json按輸入順序輸出內(nèi)容方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python 列表(List)的底層實現(xiàn)原理分析
這篇文章主要介紹了Python 列表(List)的底層實現(xiàn)原理分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
python 常用日期處理-- datetime 模塊的使用
這篇文章主要介紹了python 如何對日期進行處理,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09
flask 框架操作MySQL數(shù)據(jù)庫簡單示例
這篇文章主要介紹了flask 框架操作MySQL數(shù)據(jù)庫,結(jié)合實例形式詳細分析了flask框架操作MySQL數(shù)據(jù)庫的連接、表格創(chuàng)建、數(shù)據(jù)增刪改查等相關(guān)使用技巧,需要的朋友可以參考下2020-02-02

