Sobes.tech
Junior — Senior

What is CBAM (Convolutional Block Attention Module)?

sobes.tech AI

Answer from AI

CBAM (Convolutional Block Attention Module) is an attention module for convolutional neural networks that helps the model focus on the most important features in spatial and channel dimensions. It consists of two sequential submodules:

  1. Channel Attention Module — computes the importance of each channel, emphasizing significant channels and suppressing less important ones.
  2. Spatial Attention Module — computes the importance of each spatial position (pixel) in the feature map, highlighting key areas.

CBAM easily integrates into existing convolutional networks and improves their quality by more effectively highlighting features.

Example usage in PyTorch:

import torch
import torch.nn as nn

class CBAM(nn.Module):
    def __init__(self, channels, reduction=16, kernel_size=7):
        super().__init__()
        # Channel Attention
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.max_pool = nn.AdaptiveMaxPool2d(1)
        self.fc = nn.Sequential(
            nn.Conv2d(channels, channels // reduction, 1, bias=False),
            nn.ReLU(),
            nn.Conv2d(channels // reduction, channels, 1, bias=False)
        )
        self.sigmoid_channel = nn.Sigmoid()

        # Spatial Attention
        self.conv_spatial = nn.Conv2d(2, 1, kernel_size, padding=kernel_size//2, bias=False)
        self.sigmoid_spatial = nn.Sigmoid()

    def forward(self, x):
        # Channel Attention
        avg_out = self.fc(self.avg_pool(x))
        max_out = self.fc(self.max_pool(x))
        channel_att = self.sigmoid_channel(avg_out + max_out)
        x = x * channel_att

        # Spatial Attention
        avg_out = torch.mean(x, dim=1, keepdim=True)
        max_out, _ = torch.max(x, dim=1, keepdim=True)
        spatial_att = self.sigmoid_spatial(self.conv_spatial(torch.cat([avg_out, max_out], dim=1)))
        x = x * spatial_att

        return x