使用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中使用mysql数据库详细介绍
Mar 27 Python
使用python绘制常用的图表
Aug 27 Python
基于python(urlparse)模板的使用方法总结
Oct 13 Python
Python利用itchat对微信中好友数据实现简单分析的方法
Nov 21 Python
python 队列基本定义与使用方法【初始化、赋值、判断等】
Oct 24 Python
Python argparse模块应用实例解析
Nov 15 Python
python的range和linspace使用详解
Nov 27 Python
scrapy数据存储在mysql数据库的两种方式(同步和异步)
Feb 18 Python
python获取栅格点和面值的实现
Mar 10 Python
Python类的继承super相关原理解析
Oct 22 Python
pandas map(),apply(),applymap()区别解析
Feb 24 Python
Python使用psutil库对系统数据进行采集监控的方法
Aug 23 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安全配置方法
2007/06/16 PHP
php实现文件下载实例分享
2014/06/02 PHP
网页中的图片的处理方法与代码
2009/11/26 Javascript
JS获取url链接字符串 location.href
2013/12/23 Javascript
完美兼容各大浏览器获取HTTP_REFERER方法总结
2014/06/24 Javascript
Node.js 去掉种子(torrent)文件里的邪恶信息
2015/03/27 Javascript
JavaScript调用客户端Java程序的方法
2015/07/27 Javascript
AngularJS 过滤器的简单实例
2016/07/27 Javascript
纯前端JavaScript实现Excel IO案例分享
2016/08/26 Javascript
vue-cli创建的项目,配置多页面的实现方法
2018/03/15 Javascript
JS封装的模仿qq右下角消息弹窗功能示例
2018/08/22 Javascript
深入浅析Vue.js 中的 v-for 列表渲染指令
2018/11/19 Javascript
微信小程序自定义带价格显示日历效果
2018/12/29 Javascript
Nodejs监控事件循环异常示例详解
2019/09/22 NodeJs
Layui表格监听行单双击事件讲解
2019/11/14 Javascript
vue使用map代替Aarry数组循环遍历的方法
2020/04/30 Javascript
node.js通过url读取文件
2020/10/16 Javascript
在Linux系统上通过uWSGI配置Nginx+Python环境的教程
2015/12/25 Python
Django分页查询并返回jsons数据(中文乱码解决方法)
2018/08/02 Python
[原创]Python入门教程3. 列表基本操作【定义、运算、常用函数】
2018/10/30 Python
在Python中调用Ping命令,批量IP的方法
2019/01/26 Python
Python3多目标赋值及共享引用注意事项
2019/05/27 Python
django框架自定义模板标签(template tag)操作示例
2019/06/24 Python
python tkinter图形界面代码统计工具
2019/09/18 Python
Python绘制组合图的示例
2020/09/18 Python
CSS3属性使网站设计增强同时不消弱可用性
2009/08/29 HTML / CSS
小天鹅官方商城:LittleSwan
2017/06/16 全球购物
Linux面试题LINUX系统类
2014/11/19 面试题
学徒工职责
2014/03/06 职场文书
代办委托书怎样写
2014/04/08 职场文书
活动总结格式
2014/08/30 职场文书
教师个人学习总结
2015/02/11 职场文书
小学生作文之《压岁钱的烦恼》
2019/09/27 职场文书
教你怎么用python爬取爱奇艺热门电影
2021/05/20 Python
Python内置数据结构列表与元组示例详解
2021/08/04 Python
MySQL RC事务隔离的实现
2022/03/31 MySQL