使用keras实现densenet和Xception的模型融合


Posted in Python onMay 23, 2020

我正在参加天池上的一个竞赛,刚开始用的是DenseNet121但是效果没有达到预期,因此开始尝试使用模型融合,将Desenet和Xception融合起来共同提取特征。

代码如下:

def Multimodel(cnn_weights_path=None,all_weights_path=None,class_num=5,cnn_no_vary=False):
	'''
	获取densent121,xinception并联的网络
	此处的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)
 
	#加载全部的参数
	if all_weights_path:
		model.load_weights(all_weights_path)
	return model

如下进行调用:

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")

最后生成的模型图如下:有点长,可以不看

使用keras实现densenet和Xception的模型融合

需要注意的一点是,如果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并联的网络
	此处的cnn_weights_path是个列表是densenet和xception的卷积部分的权值
	'''
	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)
 
	#加载全部的参数
	if all_weights_path:
		model.load_weights(all_weights_path)
	return model

这个bug我也是在服务器上跑的时候才出现的,找了半天,而实验室的cuda和cudnn又改不了,tensorflow无法升级,因此只能改代码了。

如下所示,是最后画出的模型图:(很长,底下没内容了)

使用keras实现densenet和Xception的模型融合

以上这篇使用keras实现densenet和Xception的模型融合就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现类的静态变量用法实例
May 08 Python
Python栈类实例分析
Jun 15 Python
python遍历 truple list dictionary的几种方法总结
Sep 11 Python
Python代码块批量添加Tab缩进的方法
Jun 25 Python
pyshp创建shp点文件的方法
Dec 31 Python
啥是佩奇?使用Python自动绘画小猪佩奇的代码实例
Feb 20 Python
解决python3.5 正常安装 却不能直接使用Tkinter包的问题
Feb 22 Python
python 实现将文件或文件夹用相对路径打包为 tar.gz 文件的方法
Jun 10 Python
TensorFlow tf.nn.max_pool实现池化操作方式
Jan 04 Python
Python如何绘制日历图和热力图
Aug 07 Python
Jupyter notebook 更改文件打开的默认路径操作
May 21 Python
Golang Web 框架Iris安装部署
Aug 14 Python
在keras下实现多个模型的融合方式
May 23 #Python
Keras使用ImageNet上预训练的模型方式
May 23 #Python
使用Keras预训练模型ResNet50进行图像分类方式
May 23 #Python
基于Python中random.sample()的替代方案
May 23 #Python
keras 自定义loss损失函数,sample在loss上的加权和metric详解
May 23 #Python
keras中模型训练class_weight,sample_weight区别说明
May 23 #Python
浅谈keras中的Merge层(实现层的相加、相减、相乘实例)
May 23 #Python
You might like
PHP实现时间轴函数代码
2011/10/08 PHP
php防注入及开发安全详细解析
2013/08/09 PHP
PHP文件锁定写入实例解析
2014/07/14 PHP
php中Y2K38的漏洞解决方法实例分析
2014/09/22 PHP
php调用新浪短链接API的方法
2014/11/08 PHP
php出租房数据管理及搜索页面
2017/05/23 PHP
JavaScript 一道字符串分解的题目
2011/08/03 Javascript
改变隐藏的input中value的值代码
2013/12/30 Javascript
jQuery实现dialog设置focus焦点的方法
2015/06/10 Javascript
第二次聊一聊JS require.js模块化工具的基础知识
2016/04/17 Javascript
jQuery中队列queue()函数的实例教程
2016/05/03 Javascript
用JS写的一个Ajax库(实例代码)
2016/08/06 Javascript
简单实现JS倒计时效果
2016/12/23 Javascript
原生js实现新闻列表展开/收起全文功能
2017/01/20 Javascript
JS创建Tag标签的方法详解
2017/06/09 Javascript
面包屑导航详解
2017/12/07 Javascript
javascript动态创建对象的属性详解
2018/11/07 Javascript
jQuery实现简易QQ聊天框
2020/02/10 jQuery
Vue-cli打包后如何本地查看的操作
2020/09/02 Javascript
React实现评论的添加和删除
2020/10/20 Javascript
Python中的defaultdict模块和namedtuple模块的简单入门指南
2015/04/01 Python
Python中二维列表如何获取子区域元素的组成
2017/01/19 Python
python实现决策树C4.5算法详解(在ID3基础上改进)
2017/05/31 Python
Python subprocess模块常见用法分析
2018/06/12 Python
Python Requests库基本用法示例
2018/08/20 Python
pygame实现贪吃蛇游戏(下)
2019/10/29 Python
python matplotlib折线图样式实现过程
2019/11/04 Python
python实现小世界网络生成
2019/11/21 Python
3种python调用其他脚本的方法
2020/01/06 Python
Python Pandas list列表数据列拆分成多行的方法实现
2020/12/14 Python
微信小程序之html5 canvas绘图并保存到系统相册
2019/06/20 HTML / CSS
Ever New美国:澳大利亚领先的女装时尚品牌
2019/11/28 全球购物
应届大学生简历中的自我评价
2014/01/15 职场文书
鲜花方阵解说词
2014/02/13 职场文书
企业办公室主任岗位职责
2014/02/19 职场文书
青岛海底世界导游词
2015/02/11 职场文书