卜再回头 發表於 2026-5-4 15:01:00

深度学习进阶(十六) 混合注意力 CBAM

<p>上一篇我们介绍了 SE 模块,从通道维度引入了注意力机制,让网络能够自适应地调整每个通道的权重。<br>
再结合之前的相关内容,现在我们已经对通道维度和空间维度上的注意力逻辑都有所了解了,显然二者并不冲突,反而是相辅相成的,因此一个想法自然就出现了:</p>
<blockquote>
<p><strong>组合应用通道注意力和空间注意力,实现混合注意力机制。</strong></p>
</blockquote>
<p>沿着这个思路,18 年的论文:<strong><em>CBAM: Convolutional Block Attention Module</em></strong> 提出<strong>在 SE 的通道注意力基础上,进一步引入空间注意力模块</strong>,形成了"通道 + 空间"的混合注意力机制。</p>
<p>由于同时关注了"是什么"和"在哪里",CBAM 在图像分类、目标检测等任务上均取得了比 SE 更优的收益。</p>
<h1 id="1--cbam-的整体结构">1.CBAM 的整体结构</h1>
<p>在具体展开之前,我们先从宏观看看 CBAM 的架构思路。<br>
CBAM 由两个子模块串联而成:</p>
<ol>
<li><strong>通道注意力模块(Channel Attention Module,CAM)</strong></li>
<li><strong>空间注意力模块(Spatial Attention Module,SAM)</strong></li>
</ol>
<p><img src="https://img2024.cnblogs.com/blog/3708248/202605/3708248-20260504150025084-590746768.png" alt="image.png" loading="lazy"><br>
(很推荐去看原论文,几张传播图做的非常清晰,一目了然。)</p>
<p>如图,给定特征图 <span class="math inline">\(\mathbf{F} \in \mathbb{R}^{C \times H \times W}\)</span>,CBAM 依次应用两个模块:</p>
<ol>
<li>通道注意力得到 <span class="math inline">\(\mathbf{M}_c \in \mathbb{R}^{C \times 1 \times 1}\)</span>,与输入逐通道相乘。</li>
<li>空间注意力得到 <span class="math inline">\(\mathbf{M}_s \in \mathbb{R}^{1 \times H \times W}\)</span>,与上一步结果逐空间位置相乘。</li>
</ol>
<p>需要说明的是,这两个模块的顺序并不是随意的。作者在消融实验中测试了三种排列方式:</p>
<table>
<thead>
<tr>
<th>排列方式</th>
<th>Top-1 准确率</th>
</tr>
</thead>
<tbody>
<tr>
<td>无注意力(baseline)</td>
<td>93.04%</td>
</tr>
<tr>
<td>空间 → 通道</td>
<td>93.50%</td>
</tr>
<tr>
<td>通道 → 空间(CBAM)</td>
<td><strong>93.86%</strong></td>
</tr>
</tbody>
</table>
<p>结果表明,<strong>先通道后空间是最优排列</strong>。<br>
直觉上也说得通:先明确"哪个特征重要",再定位"特征位置在哪",逻辑上更加自然。</p>
<p>下面分别展开两个模块的细节。</p>
<h1 id="2-通道注意力模块-cam">2. 通道注意力模块 CAM</h1>
<p>CBAM 的通道注意力模块和 SE 模块的思路非常相似,<strong>核心差异在于池化方式</strong>。<br>
SE 只使用了 <strong>全局平均池化 GAP</strong> 来压缩空间信息。<br>
但 CBAM 的作者认为:<strong>仅靠平均值会丢失信息的"峰值"</strong>,而最大池化能够捕捉到特征图中最强烈的响应位置,这对注意力权重的估计是有益的。</p>
<p>因此,CBAM 同时使用了两种池化方式:</p>
<ol>
<li><strong>全局平均池化</strong>:反映通道的整体响应强度</li>
<li><strong>全局最大池化</strong>:反映通道的最强响应值</li>
</ol>
<p>二者并行,各自得到一个 <span class="math inline">\(1 \times 1 \times C\)</span> 的通道描述向量:</p>
<p></p><div class="math display">\[\mathbf{z}_{\text{avg}} = \text{GAP}(\mathbf{F}), \quad \mathbf{z}_{\text{max}} = \text{GMP}(\mathbf{F})
\]</div><p></p><p>这两个向量各自输入到一个<strong>和 SE 中结构相同的 MLP</strong> 中,得到:</p>
<p></p><div class="math display">\[\mathbf{s}_{\text{avg}} = \text{MLP}(\mathbf{z}_{\text{avg}}), \quad \mathbf{s}_{\text{max}} = \text{MLP}(\mathbf{z}_{\text{max}})
\]</div><p></p><p>然后将两个输出逐元素相加,再经过 Sigmoid 得到最终的通道权重:</p>
<p></p><div class="math display">\[\mathbf{M}_c(\mathbf{F}) = \sigma\Big(\text{MLP}(\text{GAP}(\mathbf{F})) + \text{MLP}(\text{GMP}(\mathbf{F}))\Big)
\]</div><p></p><p><img src="https://img2024.cnblogs.com/blog/3708248/202605/3708248-20260504150025633-1277696554.png" alt="image.png" loading="lazy"></p>
<p>两者结合,比 SE 单独用 GAP 的鲁棒性更强。</p>
<h1 id="3-空间注意力模块-sam">3. 空间注意力模块 SAM</h1>
<p>通道注意力解决了"该关注哪个通道"的问题,但特征图上不同的空间位置对任务的贡献显然也不一样。<br>
于是,CBAM 在对通道注意力进行改进的基础之上,继续提出空间注意力模块。</p>
<p>它的做法是<strong>先沿通道维度进行统计聚合</strong>,将 <span class="math inline">\(C\)</span> 个通道压缩为 1 个通道,这里也同时使用了平均池化和最大池化,但操作方式有所不同:</p>
<ol>
<li><strong>沿通道维度做平均池化</strong>:生成 <span class="math inline">\(1 \times H \times W\)</span> 的平均特征图。</li>
<li><strong>沿通道维度做最大池化</strong>:生成 <span class="math inline">\(1 \times H \times W\)</span> 的最大特征图。</li>
</ol>
<p>将这两张特征图在通道维度上拼接,得到一个 <span class="math inline">\(2 \times H \times W\)</span> 的张量:</p>
<p></p><div class="math display">\[\mathbf{F}_{\text{concat}} = \text{Concat}\big(\text{AvgPool}(\mathbf{F}); \text{MaxPool}(\mathbf{F})\big)
\]</div><p></p><p>然后经过一个 <span class="math inline">\(7 \times 7\)</span> 的卷积层,设计 <strong>padding = 3, stride = 1</strong> 保证空间尺寸不变,融合两图信息,降维到 <span class="math inline">\(1 \times H \times W\)</span>,最后通过 Sigmoid 得到空间权重:</p>
<p></p><div class="math display">\[\mathbf{M}_s(\mathbf{F}) = \sigma\big(f^{7 \times 7}([\text{AvgPool}(\mathbf{F}); \text{MaxPool}(\mathbf{F})])\big)
\]</div><p></p><p><img src="https://img2024.cnblogs.com/blog/3708248/202605/3708248-20260504150025297-1025473091.png" alt="image.png" loading="lazy"></p>
<p>这里选择 <span class="math inline">\(7 \times 7\)</span> 卷积而非 <span class="math inline">\(3 \times 3\)</span>,是因为<strong>空间注意力需要较大的感受野来确定哪个区域重要</strong>,较小的卷积核可能只能看到局部,难以感知全局的空间分布。</p>
<p>最终得到的 <span class="math inline">\(\mathbf{M}_s \in \mathbb{R}^{1 \times H \times W}\)</span> 就是空间注意力权重图,每个位置的数值表示该位置的重要性,然后与输入特征图逐元素相乘,实现空间维度上的特征重标定。</p>
<p>在原始特征图上依次应用两个模块得到的权重,就完成了 CBAM 的完整逻辑。</p>
<h1 id="4-cbam-和-se">4. CBAM 和 SE</h1>
<p>在 SE 中,我们提到它是一个<strong>轻量级模块</strong> ,我们先来看看 CBAM 会让其“变重多少”:<br>
首先,CBAM 的额外开销主要来自空间注意力中的 <span class="math inline">\(7 \times 7\)</span> 卷积,而通道注意力的参数量与 SE 完全相同。<br>
因此,对于一个 <span class="math inline">\(C\)</span> 通道的特征图,二者参数量对比为(忽略偏置):</p>
<ol>
<li>SE:两层 MLP</li>
</ol>
<p></p><div class="math display">\[2C^2 / r
\]</div><p></p><ol start="2">
<li>CBAM:两层 MLP + 一层 <span class="math inline">\(7 \times 7\)</span> 卷积</li>
</ol>
<p></p><div class="math display">\[2C^2 / r + 2 \times 7 \times 7\times 1 \approx 2C^2 / r + 98
\]</div><p></p><p>可以看到,<strong>相比于 SE,CBAM 增加的计算量几乎可以忽略不计。</strong><br>
因此,虽然 CBAM 已经是八年前的工作,但其同样简单高效、即插即用的特性,<strong>让其在许多轻量级网络(如 MobileNetV3)中仍然被广泛采用</strong>。</p><br><br>
来源:https://www.cnblogs.com/Goblinscholar/p/19971730
頁: [1]
查看完整版本: 深度学习进阶(十六) 混合注意力 CBAM