使用keras实现densenet和Xception的模型融合
作者:csliudh 发布时间:2022-04-15 19:17:01
标签:keras,densenet,Xception,融合
我正在参加天池上的一个竞赛,刚开始用的是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")
最后生成的模型图如下:有点长,可以不看
需要注意的一点是,如果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无法升级,因此只能改代码了。
如下所示,是最后画出的模型图:(很长,底下没内容了)
来源:https://blog.csdn.net/qq_19332527/article/details/79829087


猜你喜欢
- 来自巴西的设计师Roger Oddone的作品,通过此作品你可以了解到logo的设计的一些思路。
- 光线追迹得益于计算机的计算的能力,通过追踪具有代表性的光线的传播轨迹,可以更加精确地描述光学系统的性能,光线追迹方法也因此大展其能,诸如Ze
- 关于Django生成迁移文件,我是在虚拟机上完成的1.创建虚拟环境:在终端上输入创建python3的虚拟环境mkvirtualenv -p
- 本文实例讲述了Python实现对不原生支持比较操作的对象排序算法。分享给大家供大家参考,具体如下:问题:想在同一个类的实例之间做排序,但是它
- 一、创建数据库创建sqlite数据库的代码import sqlite3conn = sqlite3.connect("test.d
- 本文实例为大家分享了python绘制温度变化雷达图的具体代码,供大家参考,具体内容如下假设某天某地每三个小时取样的气温为针对温度变化趋势绘制
- 目录前言1. 准备工作2. 连接MongoDB3. 指定数据库4. 指定集合5. 插入数据6. 查询7. 计数8. 排序9. 偏移10. 更
- 在keras中,数据是以张量的形式表示的,不考虑动态特性,仅考虑shape的时候,可以把张量用类似矩阵的方式来理解。例如[[1],[2],[
- 在上篇文章给大家介绍了BootstrapTable与KnockoutJS相结合实现增删改查功能【一】,介绍了下knockout.js的一些基
- 一、安装apt-get install mysql-server 需要设置账号密码apt-get isntall mysql-clienta
- 本文不涉及分类器、训练识别器等算法原理,仅包含对其应用(未来我也会写自己对机器学习算法原理的一些观点和了解)首先我们需要知道的是利用现有框架
- 情景描述在项目开发过程中,不同项目阶段可能会有不同的分支,当创建好一个分支后,就需要将代码切换到这个分支上进行代码同步,例如将当前 orig
- SQL 多条件查询以后我们做多条件查询,一种是排列结合,另一种是动态拼接SQL如:我们要有两个条件,一个日期@addDate,一个是@nam
- 了解如何在sublime编辑器中安装python软件包,以实现自动完成等功能,并在sublime编辑器本身中运行build。安装Sublim
- CORS出于安全性,浏览器限制脚本内发起的跨源 HTTP 请求。例如,XMLHttpRequest 和 Fetch AP
- 前几天,Opera宣布其用户已经超过1亿——桌面版和手机版均超过5000万。Opera Mini是一个很优秀的手机浏览器,对手机用户而言,O
- 上一篇相关文章python_tkinter弹出对话框创建需要的可以参考一下1.fledialog对话框示例:askopenfilename(
- 最近做了一个项目,其中有项目需求涉及到手机号验证码,就是当用户点击获取验证码之后我们会发送一条信息到用户手机,然后就会出现一个倒计时按钮,很
- MySQL和MariaDB的关系MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。MariaDB
- 目录前言Tips - django版本区别路由匹配无名分组&有名分组无名分组有名分组小提示反向解析路由不涉及分组的反向解析有名分组&