首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

注意力模型CBAM

  • 25-03-03 21:22
  • 3939
  • 13527
blog.csdn.net

论文:CBAM: Convolutional Block Attention Module 

 

Convolutional Block Attention Module (CBAM) 表示卷积模块的注意力机制模块。是一种结合了空间(spatial)和通道(channel)的注意力机制模块。相比于senet只关注通道(channel)的注意力机制可以取得更好的效果。

 

基于传统VGG结构的CBAM模块。需要在每个卷积层后面加该模块。

基于shortcut结构的CBAM模块。例如resnet50,该模块在每个resnet的block后面加该模块。

 

Channel attention module:

 

将输入的featuremap,分别经过基于width和height的global max pooling 和global average pooling,然后分别经过MLP。将MLP输出的特征进行基于elementwise的加和操作,再经过sigmoid激活操作,生成最终的channel attention featuremap。将该channel attention featuremap和input featuremap做elementwise乘法操作,生成Spatial attention模块需要的输入特征。

其中,seigema为sigmoid操作,r表示减少率,其中W0后面需要接RELU激活。

 

Spatial attention module:

 

将Channel attention模块输出的特征图作为本模块的输入特征图。首先做一个基于channel的global max pooling 和global average pooling,然后将这2个结果基于channel 做concat操作。然后经过一个卷积操作,降维为1个channel。再经过sigmoid生成spatial attention feature。最后将该feature和该模块的输入feature做乘法,得到最终生成的特征。

其中,seigema为sigmoid操作,7*7表示卷积核的大小,7*7的卷积核比3*3的卷积核效果更好。

 

The code:

  1. def cbam_module(inputs,reduction_ratio=0.5,name=""):
  2. with tf.variable_scope("cbam_"+name, reuse=tf.AUTO_REUSE):
  3. batch_size,hidden_num=inputs.get_shape().as_list()[0],inputs.get_shape().as_list()[3]
  4. maxpool_channel=tf.reduce_max(tf.reduce_max(inputs,axis=1,keepdims=True),axis=2,keepdims=True)
  5. avgpool_channel=tf.reduce_mean(tf.reduce_mean(inputs,axis=1,keepdims=True),axis=2,keepdims=True)
  6. maxpool_channel = tf.layers.Flatten()(maxpool_channel)
  7. avgpool_channel = tf.layers.Flatten()(avgpool_channel)
  8. mlp_1_max=tf.layers.dense(inputs=maxpool_channel,units=int(hidden_num*reduction_ratio),name="mlp_1",reuse=None,activation=tf.nn.relu)
  9. mlp_2_max=tf.layers.dense(inputs=mlp_1_max,units=hidden_num,name="mlp_2",reuse=None)
  10. mlp_2_max=tf.reshape(mlp_2_max,[batch_size,1,1,hidden_num])
  11. mlp_1_avg=tf.layers.dense(inputs=avgpool_channel,units=int(hidden_num*reduction_ratio),name="mlp_1",reuse=True,activation=tf.nn.relu)
  12. mlp_2_avg=tf.layers.dense(inputs=mlp_1_avg,units=hidden_num,name="mlp_2",reuse=True)
  13. mlp_2_avg=tf.reshape(mlp_2_avg,[batch_size,1,1,hidden_num])
  14. channel_attention=tf.nn.sigmoid(mlp_2_max+mlp_2_avg)
  15. channel_refined_feature=inputs*channel_attention
  16. maxpool_spatial=tf.reduce_max(inputs,axis=3,keepdims=True)
  17. avgpool_spatial=tf.reduce_mean(inputs,axis=3,keepdims=True)
  18. max_avg_pool_spatial=tf.concat([maxpool_spatial,avgpool_spatial],axis=3)
  19. conv_layer=tf.layers.conv2d(inputs=max_avg_pool_spatial, filters=1, kernel_size=(7, 7), padding="same", activation=None)
  20. spatial_attention=tf.nn.sigmoid(conv_layer)
  21. refined_feature=channel_refined_feature*spatial_attention
  22. return refined_feature

 

注:本文转载自blog.csdn.net的watersink的文章"https://blog.csdn.net/qq_14845119/article/details/81393127"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top