使用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
0
投稿
猜你喜欢
- 每个产品诞生的背后都凝结着一位或是多位设计师的心血,在产品的诞生过程中文化、科技、环保、创意等这些方方面面的细节集结成一个绚丽的故事,因为有
- 在网页中经常见到两类不同的按钮。一类表示当前所示的状态,一类表示将要进行的动作。(如下图) 那么,同样是icon类的按钮,为什么有
- 这个效果本身难度不大,主要在程序结构和扩展中下了些功夫,务求用起来更方便,能用在更多的地方。程序特点 1,同一个提示框用在多个触发元素时,只
- 本文实例为大家分享了Python实现打砖块游戏的具体代码,供大家参考,具体内容如下#导入模块import pygamefrom pygame
- 一、asp.net中导出Execl的方法:在asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文
- 引言今天在使用Pytorch导入此前保存的模型进行测试,在过程中发现输出的结果与验证结果差距甚大,经过排查后发现是forward与eval(
- 写在前面的话:此篇还是asp相关的,相信玩ASP的都有这个感觉,当数据有5万多条时-------just like音乐网,要调用最新的10条
- 数据库的使用过程中由于程序方面的问题有时候会碰到重复数据,重复数据导致了数据库部分设置不能正确设置……方法一以下为引用的内容:declare
- flask多进程会引起重复加载,解决方法:把耗资源的加载挪到函数里面或者类里面,就不会重复加载资源了。测试发现,不是flask引起的,是多进
- 如下所示:>>> import numpy as np>>> a = np.array([[1, 2,
- 前言本文主要给大家介绍了利用django-suit模板在管理后台添加自定义的菜单和自定义的页面、设置访问权限的相关内容,分享出来供大家参考学
- 前因后果公司有人阳了,今天在家上班,突然小姨子就问我有没有baidu文库会员,想下载点东西,我心想这还要会员?用Python不是分分钟的事情
- 一:安装PyQt5pip install pyqt5如果你的系统没有安装pip请阅读我们的另一篇文章 windows下python安装pip
- 在ASP中,直接使用“Insert into” 语句与使用ADO中AddNew方法有什么区别?哪一种更好呢?AddNew方法的实质就是封装了
- 为新项目写的一份规范文档, 分享给大家. 我想前端开发过程中, 无论是团队开发, 还是单兵做站, 有一份开发文档做规范, 对开发工作都是很有
- 博主作为爬虫初学者,本次使用了requests和beautifulsoup库进行数据的爬取爬取网站:http://datachart.500
- 附上官网地址:https://pytorch.org/docs/stable/index.html1.torch.squeezesqueez
- 现在我们已经知道了,fixtures是一个非常强大的功能。那么有的时候,我们可能会写一个fixture,而这个fixture所有的测试函数都
- Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '+'.
- 业务需求我们需要一个微信小程序码,但是是需要提供给别人扫码的但是只有一个纯粹的小程序码是不好看的,所以需要推广的海报图片。再结合文字最终效果