(ML) CA : Coordinate Attention for Efficient Mobile Network Design

CA (Coordinate Attention for Efficient Mobile Network Design)

YEN HUNG CHENG
4 min readMar 26, 2023

以下為 Coordinate Attention (Coordinate Attention for Efficient Mobile Network Design) 的網路架構

source

Attention Mechanisms

SENet (Squeeze-and-Excitation Networks)

source

CBAM (Convolutional Block Attention Module)

source

CA (Coordinate Attention for Efficient Mobile Network Design)

source

Revisit Squeeze-and-Excitation Attention

Squeeze

Squeeze 使用 Global Average Pooing 後,輸出的公式可以用下方公式表示

source

輸出維度:1 x 1 x c

Excitation and Scaling

Excitation 學習每一個 channel 之間的關係,也就是將重要資訊 feature map 權重調大,不重要的 feature map 權重調低

公式的步驟如下

  1. z 通過 T1 (Fully-connected layer) -> T1(z)
  2. 通過 ReLU -> ReLU(T1(z))
  3. 通過 T2 (Fully-connected layer) -> T2(ReLU(T1(z)))
  4. 通過 σ (Sigmoid) -> σ(z^)
  5. 再與 input tensor 進行通道間的相乘 (channel-wise multiplication) -> X^

Coordinate Information Embedding

高度為 h 的第 c 個通道的輸出可以用下方公式來表示

source
import torch
import torch.nn as nn

# 創建一個 4 維張量,形狀為 [batch_size, channels, height, width]
x = torch.randn(1, 1, 5, 5)

# 使用 nn.AdaptiveAvgPool2d 來對張量進行平均池化
pool_h = nn.AdaptiveAvgPool2d((None, 1))
pool_w = nn.AdaptiveAvgPool2d((1, None))
out_h = pool_h(x)
out_w = pool_w(x)

# 打印輸出張量的形狀
print(f"out_h shape: {out_h.shape}") # out_h shape: torch.Size([1, 1, 5, 1])
print(f"out_w shape: {out_w.shape}") # out_w shape: torch.Size([1, 1, 1, 5])
input tensor
output tensor

寬度為 w 的第 c 個通道的輸出可以用下方公式來表示

source
output tensor

Coordinate Attention Generation

source

公式步驟如下

  1. zh 與 zw 做 concatenate -> ([zh, zw])
  2. 通過 1 × 1 convolution -> F1([zh, zw])
  3. 通過 δ (a non-linear activation) -> δ(F1([zh, zw]))

zh 與 zw 做 concatenate 前,會先將 zw 的維度調整為 (c/r) x 1 x h

r:reduction ratio (r=32)

        x_h = self.pool_h(x)
x_w = self.pool_w(x).permute(0, 1, 3, 2)

y = torch.cat([x_h, x_w], dim=2)

Split

source

公式步驟如下

  1. f 做 split 得到 fh 與 fw
  2. 分別對 fh 與 fw 做 1 × 1 convolution 讓 fh 與 fw 得到相同 channel
  3. 最後分別通過 Sigmoid 可以得到 gh 與 gw

coordinate attention block

最終的輸出 y 可以用下方公式來表示

source

Network implementation for different network architectures

source

(a) Inverted residual block proposed in MobileNetV2

(b) Sandglass bottleneck block proposed in MobileNeXt

Result comparisons under different experiment settings

source

baseline result is based on the MobileNetV2 model

r : reduction ratio

可以從實驗的結果發現,不管是在水平 (X Attention) 或是 垂直 (Y Attention) 都能實現與 SE block 差不多的性能,當同時使用兩者的 Attention
(Coord )時,能夠得到最好的性能提升,當然為了降低複雜度,會採用較大的 reduction ratio

Experiments

Ablation Studies

Comparisons of different attention methods

source
source

考慮了 水平與垂直的 CA Block 能夠得到最佳的性能提升,所以從實驗可以得知,坐標信息嵌入對圖像分類是有幫助的

Comparisons of models equipped with different attention blocks under different reduction ratios r

source

The baseline result is based on the MobileNetV2 model

從實驗中可以得知,當減少比率降低,CA Block 仍然產生最佳的性能提升

Visualization of feature maps produced by models with different attention methods in the last building block

source

利用 Grad-CAM 可視化後,可以發現每一個同的 Attention Block ,注意放在圖片中的哪一個部分,而 CA Block 能夠更加精準地定位在圖片感興趣的地方

Experimental results when taking the powerful EfficientNet-b0

source

Object detection

Object detection results on the COCO validation set

source

Object detection results on the Pascal VOC 2007 test set

source

Semantic segmentation

Semantic segmentation results on the Pascal VOC 2012 validation set

source

Semantic segmentation results on the Cityscapes validation set

source

Performance of different attention methods on three classic vision tasks

source

Conclusions

CA (Coordinate Attention) 能夠比 SENet 更能捕捉位置的重要性,雖然 SENet 也許能夠知道每一個 channel 的重要性,卻忽略了水平與垂直位置的重要性,所以 CA (Coordinate Attention) 提出了新的注意力方法,不僅考慮了通道間的重要性,也考慮了水平與垂直位置的重要性,從最後的實驗結果來看,不論在分類、物件偵測或語意分割等任務上,都能表現的比其它 Attention 方法來的好

GitHub

--

--