欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年05月23日 16:26:53   作者:csliudh  
這篇文章主要介紹了使用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)絡(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如何爬取網(wǎng)站數(shù)據(jù)并進行數(shù)據(jù)可視化

    python如何爬取網(wǎng)站數(shù)據(jù)并進行數(shù)據(jù)可視化

    這篇文章主要介紹了python爬取拉勾網(wǎng)數(shù)據(jù)并進行數(shù)據(jù)可視化,爬取拉勾網(wǎng)關(guān)于python職位相關(guān)的數(shù)據(jù)信息,并將爬取的數(shù)據(jù)已csv各式存入文件,然后對csv文件相關(guān)字段的數(shù)據(jù)進行清洗,并對數(shù)據(jù)可視化展示,包括柱狀圖展示、直方圖展示,需要的朋友可以參考下
    2019-07-07
  • Python中使用支持向量機SVM實踐

    Python中使用支持向量機SVM實踐

    這篇文章主要為大家詳細介紹了Python中使用支持向量機SVM實踐,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 使用python編寫簡單的小程序編譯成exe跑在win10上

    使用python編寫簡單的小程序編譯成exe跑在win10上

    這篇文章主要介紹了使用python編寫簡單的小程序編譯成exe跑在win10上的相關(guān)資料,需要的朋友可以參考下
    2018-01-01
  • 基于python分析你的上網(wǎng)行為 看看你平時上網(wǎng)都在干嘛

    基于python分析你的上網(wǎng)行為 看看你平時上網(wǎng)都在干嘛

    這篇文章主要介紹了基于python分析你的上網(wǎng)行為 看看你平時上網(wǎng)都在干嘛,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • OpenCV3.3+Python3.6實現(xiàn)圖片高斯模糊

    OpenCV3.3+Python3.6實現(xiàn)圖片高斯模糊

    這篇文章主要為大家詳細介紹了OpenCV3.3+Python3.6實現(xiàn)圖片高斯模糊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • pycharm安裝opencv的實現(xiàn)

    pycharm安裝opencv的實現(xiàn)

    本文主要介紹了pycharm安裝opencv的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-09-09
  • 解決python3爬蟲無法顯示中文的問題

    解決python3爬蟲無法顯示中文的問題

    下面小編就為大家分享一篇解決python3爬蟲無法顯示中文的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python中計算一個列表中連續(xù)相同的元素個數(shù)方法

    python中計算一個列表中連續(xù)相同的元素個數(shù)方法

    今天小編就為大家分享一篇python中計算一個列表中連續(xù)相同的元素個數(shù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python ini文件常用操作方法解析

    Python ini文件常用操作方法解析

    這篇文章主要介紹了Python ini文件常用操作方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Django多數(shù)據(jù)庫聯(lián)用實現(xiàn)方法解析

    Django多數(shù)據(jù)庫聯(lián)用實現(xiàn)方法解析

    這篇文章主要介紹了Django多數(shù)據(jù)庫聯(lián)用實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11

最新評論