這篇文章主要講解了keras如何實現(xiàn)densenet和Xception的模型融合,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
我正在參加天池上的一個競賽,剛開始用的是DenseNet121但是效果沒有達到預期,因此開始嘗試使用模型融合,將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)絡 此處的cnn_weights_path是個列表是densenet和xception的卷積部分的權值 ''' 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
本文題目:keras如何實現(xiàn)densenet和Xception的模型融合-創(chuàng)新互聯(lián)
轉載注明:http://www.chinadenli.net/article38/dgeppp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、小程序開發(fā)、營銷型網(wǎng)站建設、微信小程序、企業(yè)建站、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)