Computer Vision · Advanced level

Advanced level · Module 5: Attention Mechanisms and Vision Transformers · Lesson 1 of 3

Attention in Convolutional Networks

Learners study squeeze-and-excitation style channel attention and spatial attention, and add a channel-attention block to a CNN.

In this lesson you will

  • Explain channel and spatial attention
  • Implement a channel-attention block

Key concepts

channel attentionglobal average poolinggating with sigmoidspatial attentionattention as re-weighting

The lesson

Attention re-weights features so that the network focuses on what matters. Channel attention (as in squeeze-and-excitation blocks) pools each feature map to one number, passes the vector through a small network with a sigmoid, and multiplies each channel by its weight.

Spatial attention computes a weight for each position, highlighting important regions. Many efficient architectures, including some MobileNet and EfficientNet versions, use channel attention.

Self-attention, covered next, goes further: every position compares itself with every other position.

Step by step

  1. Implement a channel-attention block in PyTorch.
  2. Insert it after a convolution block in the Intermediate CNN.
  3. Train both versions and compare accuracy and parameters.
  4. Visualise the channel weights for two images.

Practice activity

Add channel attention to a CNN and measure its effect.

Expected result: A working block, a measured comparison and a visualisation of channel weights.

Check your understanding

  1. What does channel attention multiply?

    Show answer

    Each feature map by a learned weight between 0 and 1.

  2. What does spatial attention highlight?

    Show answer

    Important positions in the image.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.