最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用keras實(shí)現(xiàn)densenet和Xception的模型融合

 更新時(shí)間:2020年05月23日 16:26:53   作者:csliudh  
這篇文章主要介紹了使用keras實(shí)現(xiàn)densenet和Xception的模型融合,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

我正在參加天池上的一個競賽,剛開始用的是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.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")

最后生成的模型圖如下:有點(diǎn)長,可以不看

需要注意的一點(diǎn)是,如果dense=dense(input_layer)這里報(bào)錯的話,說明你用的是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ù)器上跑的時(shí)候才出現(xiàn)的,找了半天,而實(shí)驗(yàn)室的cuda和cudnn又改不了,tensorflow無法升級,因此只能改代碼了。

如下所示,是最后畫出的模型圖:(很長,底下沒內(nèi)容了)

以上這篇使用keras實(shí)現(xiàn)densenet和Xception的模型融合就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python的tkinter布局之簡單的聊天窗口實(shí)現(xiàn)方法

    python的tkinter布局之簡單的聊天窗口實(shí)現(xiàn)方法

    這篇文章主要介紹了python的tkinter布局之簡單的聊天窗口實(shí)現(xiàn)方法,對于tkinter用法做了初步的介紹與應(yīng)用展示,需要的朋友可以參考下
    2014-09-09
  • 在python中利用dict轉(zhuǎn)json按輸入順序輸出內(nèi)容方式

    在python中利用dict轉(zhuǎn)json按輸入順序輸出內(nèi)容方式

    今天小編就為大家分享一篇在python中利用dict轉(zhuǎn)json按輸入順序輸出內(nèi)容方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python 列表(List)的底層實(shí)現(xiàn)原理分析

    Python 列表(List)的底層實(shí)現(xiàn)原理分析

    這篇文章主要介紹了Python 列表(List)的底層實(shí)現(xiàn)原理分析,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • python 常用日期處理-- datetime 模塊的使用

    python 常用日期處理-- datetime 模塊的使用

    這篇文章主要介紹了python 如何對日期進(jìn)行處理,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-09-09
  • flask 框架操作MySQL數(shù)據(jù)庫簡單示例

    flask 框架操作MySQL數(shù)據(jù)庫簡單示例

    這篇文章主要介紹了flask 框架操作MySQL數(shù)據(jù)庫,結(jié)合實(shí)例形式詳細(xì)分析了flask框架操作MySQL數(shù)據(jù)庫的連接、表格創(chuàng)建、數(shù)據(jù)增刪改查等相關(guān)使用技巧,需要的朋友可以參考下
    2020-02-02
  • Python 正則模塊詳情

    Python 正則模塊詳情

    這篇文章主要介紹了Python 正則模塊,在Python中提供了操作正則表達(dá)式的模塊,即re模塊,文章詳細(xì)記錄了正則表達(dá)式的裝飾符的相關(guān)資料,需要的朋友可以參考一下
    2021-11-11
  • PyCharm添加python庫的方法步驟

    PyCharm添加python庫的方法步驟

    在使用PyCharm過程中,有時(shí)候需要添加需要的Python擴(kuò)展庫,本文主要介紹了PyCharm添加python庫的方法步驟,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • python實(shí)現(xiàn)簡單圖書管理系統(tǒng)

    python實(shí)現(xiàn)簡單圖書管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡單圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • 詳解Python文本操作相關(guān)模塊

    詳解Python文本操作相關(guān)模塊

    這篇文章主要介紹了詳解Python文本操作相關(guān)模塊的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Pytorch Tensor的索引與切片例子

    Pytorch Tensor的索引與切片例子

    今天小編就為大家分享一篇Pytorch Tensor的索引與切片例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08

最新評論

慈溪市| 昌图县| 上虞市| 连山| 弋阳县| 自治县| 抚远县| 乌苏市| 阜平县| 盖州市| 莱阳市| 思南县| 郁南县| 新晃| 鄱阳县| 万源市| 潍坊市| 萨嘎县| 兰西县| 太仓市| 麟游县| 和林格尔县| 南部县| 泗洪县| 宜丰县| 多伦县| 体育| 湖口县| 阳春市| 兴化市| 日照市| 灌云县| 如东县| 墨竹工卡县| 三门县| 乌拉特前旗| 余姚市| 鄯善县| 德州市| 南涧| 东兴市|