Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.What to look at, and where
A squeeze-and-excitation block (E7-10) decides which channels matter. CBAM adds a second question: which positions matter. After rescaling the channels, it builds two maps (the average and the maximum over channels at each pixel), runs a 7 × 7 convolution over them and a sigmoid, and multiplies every channel by the resulting map. Positions that look important are kept; others are turned down.
Attention modules like CBAM are a cheap way to add context to detectors and segmenters used on drones, robots and satellites, but as exercise 2 shows, an accuracy gain is not proof that the module attends to the right thing.
- 1. CBAM applies…
- 2. The spatial gate is computed from…
- 3. Gates take values in…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Channel gate: Mc = σ(MLP(AvgPool(F)) + MLP(MaxPool(F))), with one MLP (C → C/r → C) shared by both descriptors. Then F′ = Mc ⊗ F.
- Spatial gate: Ms = σ(conv₇ₓ₇([AvgPoolc(F′); MaxPoolc(F′)])). Then F″ = Ms ⊗ F′.
- Channel pooling of one pixel: values (1, 3) across two channels give average 2 and maximum 3.
- Parameters:
- C = 256, r = 16: the MLP has 2·256·16 = 8,192 weights;
- the spatial convolution has 2·7·7 = 98;
- 8,290 in total.
- A zero spatial kernel gives gates of σ(0) = 0.5 everywhere.
- 1. Parameters of the 7 × 7 spatial convolution:
- 2. The channel MLP is…
- 3. Channel-pooled average of values (1, 3):
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.CBAM in code
Exercise 1.
- Both gates lay strictly between 0 and 1, and CBAM’s output equalled the channel-gated map times its own spatial gate.
- A zero spatial kernel gave gates of exactly 0.5.
- Changing one corner pixel changed the spatial gate near that corner but not at the far corner: the spatial gate is local, unlike SE’s.
- For C = 256 and r = 16 the module has 8,290 parameters.
Practice.
- Insert CBAM at the end of each residual branch, before the addition, as with SE.
- The paper found channel-then-spatial slightly better than the reverse or parallel order, and average plus max pooling better than either alone.
- Related lightweight modules:
- BAM applies the two gates in parallel at stage boundaries;
- ECA replaces the MLP with a 1-D convolution over channels;
- coordinate attention pools along rows and columns separately to keep position information.
class SpatialGate(nn.Module):
def __init__(self, k=7):
super().__init__(); self.conv = nn.Conv2d(2, 1, k, padding=k // 2, bias=False)
def forward(self, x):
m = torch.cat([x.mean(1, keepdim=True), x.amax(1, keepdim=True)], 1)
return x * torch.sigmoid(self.conv(m))- 1. The best order found in the paper:
- 2. ECA replaces the MLP with…
- 3. Changing one corner pixel changes the spatial gate…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Two complementary gates. The channel gate is a function of global statistics, so it rescales whole feature maps: it chooses what. The spatial gate is a function of local statistics across channels, so it rescales positions: it chooses where. Their product is a factorised approximation to a full C × H × W attention map, with far fewer parameters.
Why add max pooling. Average pooling summarises the whole map; max pooling keeps the strongest response, which marks distinctive features. Woo et al. found using both better than either. A max is sensitive to outliers, though: a single bright clutter pixel sets it. In exercise 2, with strong isolated clutter, the average-plus-max channel gate was the least stable configuration.
Attention is not explanation. A gate that improves accuracy need not highlight the object. Gates can saturate (all near 1), act as a learned rescaling that eases optimisation, or respond to whatever correlates with the label. Before calling a gate “attention”, measure where it is high: on labelled object pixels, on clutter and on background.
Cost. Parameters are tiny, but each gate needs a reduction over channels or positions, which adds memory traffic; on mobile hardware the latency increase can exceed what the parameter count suggests.
- 1. The channel gate chooses…
- 2. Max pooling's weakness under clutter:
- 3. To check that a gate attends to the object…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Does the attention attend?
History.
- J. Hu, L. Shen, G. Sun (2018) introduced SE channel attention.
- S. Woo, J. Park, J.-Y. Lee, I. S. Kweon (2018) introduced CBAM, and with the same authors BAM. CBAM lowered ResNet-50’s ImageNet top-1 error from about 24.6% to 22.7%.
- Q. Wang et al. (2020) introduced ECA-Net; Q. Hou, D. Zhou, J. Feng (2021) coordinate attention.
- S. Jain and B. Wallace (2019) argued that attention weights need not explain predictions.
Exercise 2. The task: 12 × 12 images of four small shapes (amplitude 1.5) on weak noise, plus 6 isolated clutter pixels of amplitude ±3 that are stronger than the shapes. The network is one 3 × 3 convolution with 8 filters, then ReLU, optional gates, global average pooling and a linear classifier. Each configuration is trained with Adam for 150 steps on 150 images. Test accuracy on 300 new images, mean of 2 seeds:
| network | parameters | test accuracy |
|---|---|---|
| no attention | 116 | 0.660 |
| channel gate (avg + max, as in CBAM) | 148 | 0.598 |
| channel gate (avg only, as in SE) | 148 | 0.647 |
| spatial gate only | 135 | 0.748 |
| CBAM (channel then spatial) | 167 | 0.677 |
Mean spatial gate of the two spatial-only models, on 300 new images:
| model | shape pixels | clutter pixels | background |
|---|---|---|---|
| seed 0 | 1.000 | 1.000 | 0.999 |
| seed 1 | 0.668 | 0.642 | 0.456 |
- The spatial gate raised accuracy, but not by telling shapes from clutter.
- The gates were about as high on clutter as on shapes.
- In seed 0 they saturated near 1 everywhere, so that model computes nearly the same function as the plain network. Its gain came from a different training path: the gate starts near 0.5, halving early activations.
- Neither channel gate helped, and putting one in front of the spatial gate (full CBAM) undid most of the spatial gain.
- With 2 seeds and 150 training images, differences of a few points are within noise. Three further seeds (2, 3, 4) gave the same ordering for spatial versus no attention in two of three.
- The lesson: measure what a gate does before calling it attention.
Attention modules in AI, robotics and aerospace
- Remote sensing: channel and spatial attention are common in ship, aircraft and building detection from satellite and aerial images, where small objects sit on large cluttered backgrounds.
- Robot perception: lightweight attention (ECA, coordinate attention) is added to mobile backbones for grasp and obstacle detection.
- Interpretability caution: attention maps are often shown as explanations; exercise 2 shows why they should be checked against labelled regions.
References
- S. Woo, J. Park, J.-Y. Lee, I. S. Kweon (2018). “CBAM: convolutional block attention module.” Proc. ECCV.
- J. Park, S. Woo, J.-Y. Lee, I. S. Kweon (2018). “BAM: bottleneck attention module.” Proc. BMVC.
- Q. Wang et al. (2020). “ECA-Net: efficient channel attention for deep convolutional neural networks.” Proc. CVPR.
- Q. Hou, D. Zhou, J. Feng (2021). “Coordinate attention for efficient mobile network design.” Proc. CVPR.
- S. Jain, B. C. Wallace (2019). “Attention is not explanation.” Proc. NAACL.