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

作者:csliudh 时间:2022-04-15 19:17:01 

我正在参加天池上的一个竞赛,刚开始用的是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的模型融合

来源:https://blog.csdn.net/qq_19332527/article/details/79829087

标签:keras,densenet,Xception,融合
0
投稿

猜你喜欢

  • python numpy实现多次循环读取文件 等间隔过滤数据示例

    2022-10-30 09:44:13
  • 浅析DW4中的站点管理

    2007-02-03 11:40:00
  • 细线表格的处理

    2008-08-06 12:53:00
  • Python TypeError: ‘float‘ object is not subscriptable错误解决

    2023-09-13 05:33:02
  • 百度编辑器复制微信图片无法保存

    2023-08-14 17:32:46
  • 分享整理的12条sql语句连同数据

    2012-07-11 16:14:59
  • asp连接MYSQL数据库的连接字符串(参数OPTION)

    2009-03-09 18:24:00
  • Python统计可散列的对象之容器Counter详解

    2023-09-23 18:30:50
  • python去除空格,tab制表符和\\n换行符的小技巧分享

    2022-05-12 14:20:39
  • 深入讲解Go语言中函数new与make的使用和区别

    2023-06-16 17:52:29
  • FSO无效的过程调用或参数问题

    2010-03-25 21:49:00
  • MySQL在命名用过程中所遇到的常见问题

    2008-12-05 16:03:00
  • asp使用shotgraph组件生成数字和字母验证码

    2007-09-26 12:26:00
  • python发送邮件脚本

    2022-01-28 03:04:58
  • 在SQL Server中使用CLR调用.NET方法

    2008-12-24 15:43:00
  • ASP所有的Session变量获取实现代码

    2011-03-11 10:44:00
  • 前端也应关注安全

    2009-03-16 17:02:00
  • golang中net的tcp服务使用

    2023-08-30 10:54:14
  • 利用sys.sysprocesses检查SqlServer的阻塞和死锁

    2011-11-03 17:24:11
  • 《JavaScript语言精粹》

    2009-04-03 11:27:00
  • asp之家 网络编程 m.aspxhome.com