神剑山庄资源网 Design By www.hcban.com
我正在参加天池上的一个竞赛,刚开始用的是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无法升级,因此只能改代码了。
如下所示,是最后画出的模型图:(很长,底下没内容了)
以上这篇使用keras实现densenet和Xception的模型融合就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无使用keras实现densenet和Xception的模型融合的评论...
更新日志
2024年09月30日
2024年09月30日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】