使用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编写web API的教程
Apr 30 Python
Python实现计算最小编辑距离
Mar 17 Python
Python数据分析之双色球中蓝红球分析统计示例
Feb 03 Python
python hook监听事件详解
Oct 25 Python
利用Python将数值型特征进行离散化操作的方法
Nov 06 Python
pandas的to_datetime时间转换使用及学习心得
Aug 11 Python
python实现查找所有程序的安装信息
Feb 18 Python
Django 如何使用日期时间选择器规范用户的时间输入示例代码详解
May 22 Python
python opencv实现简易画图板
Aug 27 Python
python推导式的使用方法实例
Feb 28 Python
Python中with上下文管理协议的作用及用法
Mar 18 Python
使用scrapy实现增量式爬取方式
Jun 21 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
详解:――如何将图片储存在数据库里
2006/12/05 PHP
百度站点地图(百度sitemap)生成方法分享
2014/01/09 PHP
php 实现一个字符串加密解密的函数实例代码
2016/11/01 PHP
phpmailer绑定邮箱的实现方法
2016/12/01 PHP
权威JavaScript 中的内存泄露模式
2007/08/13 Javascript
为jQuery增加join方法的实现代码
2010/11/28 Javascript
javascript错误的认识不用关心内存管理
2012/12/15 Javascript
两种不同的方法实现js对checkbox进行全选和反选
2014/05/13 Javascript
Javascript window对象详解
2014/11/12 Javascript
jQuery使用CSS()方法给指定元素同时设置多个样式
2015/03/26 Javascript
jQuery实现的鼠标滑过弹出放大图片特效
2016/01/08 Javascript
理解javascript封装
2016/02/23 Javascript
jQuery 弹出层插件(推荐)
2016/05/24 Javascript
详细谈谈javascript的对象
2016/07/31 Javascript
bootstrap模态框垂直居中效果
2016/12/03 Javascript
ES6新增的math,Number方法
2017/08/06 Javascript
JS中图片压缩的方法小结
2017/11/14 Javascript
详解React项目的服务端渲染改造(koa2+webpack3.11)
2018/03/19 Javascript
Vue 事件处理操作实例详解
2019/03/05 Javascript
微信小程序 简易计算器实现代码实例
2019/09/02 Javascript
微信小程序本地存储实现每日签到、连续签到功能
2019/10/09 Javascript
JS将指定的某个字符全部转换为其他字符实例代码
2020/10/13 Javascript
python中os操作文件及文件路径实例汇总
2015/01/15 Python
python的中异常处理机制
2018/08/30 Python
python字符串的拼接方法总结
2019/11/18 Python
三个python爬虫项目实例代码
2019/12/28 Python
PyTorch实现ResNet50、ResNet101和ResNet152示例
2020/01/14 Python
Python读入mnist二进制图像文件并显示实例
2020/04/24 Python
银行办理业务介绍信
2014/01/18 职场文书
社团招新策划书
2014/02/04 职场文书
函授大学生自我鉴定
2014/02/05 职场文书
物业消防安全责任书
2014/07/23 职场文书
大学生各类奖学金申请书
2019/06/24 职场文书
在校大学生才艺比赛策划书怎么写?
2019/08/26 职场文书
详细聊聊浏览器是如何看闭包的
2021/11/11 Javascript
关于的python五子棋的算法
2022/05/02 Python