Papers
Topics
Authors
Recent
Search
2000 character limit reached

Patchwise Self-Attention

Updated 19 December 2025
  • Patchwise self-attention is a neural mechanism that computes attention over localized image patches, enabling content-adaptive weighting for enhanced feature extraction.
  • It generalizes standard convolution by replacing fixed kernels with dynamic, per-channel weight vectors, resulting in improved accuracy and efficiency as shown in benchmark comparisons.
  • Its modular design integrates seamlessly into diverse architectures, offering increased robustness against geometric variations and adversarial attacks in visual recognition tasks.

Patchwise self-attention is a class of neural attention mechanisms designed for image recognition and related tasks, wherein attention is computed over local spatial patches, allowing the network to model adaptive, content-dependent weighting patterns within regions of the input feature map. As developed in multiple works, notably in the patchwise Self-Attention Network (SAN) module, patchwise attention generalizes local convolution by replacing translation-invariant weighting with content-adaptive, per-channel vector attention. This mechanism substantially increases the expressive power and flexibility of local aggregation in visual models, as evidenced by empirical and theoretical analyses (Zhao et al., 2020, Barkan, 2019).

1. Mathematical Definition and Mechanism

Patchwise self-attention operates on a feature map xRH×W×Cx \in \mathbb{R}^{H \times W \times C}, where HH, WW, and CC are spatial height, width, and channel dimensions respectively. For each spatial location ii, a local patch R(i){(u,v)}R(i) \subset \{(u, v)\} is selected (e.g., a 7×77 \times 7 window), yielding xR(i)RR×Cx_{R(i)} \in \mathbb{R}^{|R| \times C}. The new feature at position ii is computed as

yi=jR(i)α(xR(i))jβ(xj)y_i = \sum_{j \in R(i)} \alpha(x_{R(i)})_j \odot \beta(x_j)

where:

  • HH0 is a linear projection reducing dimensionality (bottleneck factor HH1).
  • HH2 comprises attention weight vectors (one per patch location HH3).
  • HH4 is parameterized as HH5, where:
    • HH6 aggregates the patch into a HH7-dimensional vector.
    • HH8 unfolds to yield attention weights.

Three forms for HH9 are employed:

  • Star-product: WW0
  • Clique-product: WW1
  • Concatenation: WW2

Here, WW3 are linear projections, with WW4 a bottleneck factor.

After attention, a final Linear layer restores the channel dimension, followed by batch normalization, ReLU activation, and a residual skip connection to the block input (Zhao et al., 2020).

2. The Expressive Power of Patchwise Self-Attention versus Convolution

Whereas standard convolution aggregates features using fixed, translation-invariant kernels WW5 with weights applied solely as a function of relative spatial offset, patchwise self-attention determines aggregation weights WW6 as an adaptive function of the local content WW7. The result is:

  • Local weighting adapts to structure such as edges, textures, or object parts.
  • Attention weights are per-channel vectors (length WW8), not scalars shared across all channels.
  • No translation invariance: the same offset WW9 can have different weights in different contexts.

Any given convolution can be exactly reproduced by patchwise attention by using fixed CC0. However, the converse is not true; patchwise attention can realize operators that are content-conditional, e.g., modulating or zeroing out specific channel groups based on patch context, which is beyond the scope of standard convolutional operations (Zhao et al., 2020).

3. Network Architecture and Integration Strategies

A standard patchwise SAN block constitutes two primary computational streams:

  • Attention-weight computation: CC1 convolutions (bottlenecked by CC2 by default) to compute CC3, CC4, followed by aggregation CC5 (with selectable relation function) and mapping CC6 (two LinearCC7ReLUCC8Linear layers, CC9).
  • Value projection: A ii0 convolution for ii1, reducing channels by ii2.

Fusion is executed via a Hadamard product of the output of ii3 and ii4, followed by batch normalization, ReLU, channel expansion, and residual addition.

The patchwise SAN architecture, exemplified by San15, comprises five stages (stride sequence ii5; channel widths ii6). Patch size is ii7 for the initial stage and ii8 afterwards. No explicit multihead splitting is applied; each attention vector is shared over 8-channel groups (Zhao et al., 2020). In the Self Attentive Convolution (SAC) formulation, the approach generalizes to arbitrary ii9 kernels with overlapping patches, sliding across the image without explicit partitioning, and supports both single- and multi-head extensions (Barkan, 2019).

4. Implementation Hyper-parameters and Complexity Analysis

The principal hyper-parameters for patchwise SAN include training for 100 epochs with batch size 256 (across 8 GPUs), SGD optimizer with momentum R(i){(u,v)}R(i) \subset \{(u, v)\}0, weight decay R(i){(u,v)}R(i) \subset \{(u, v)\}1, cosine-decayed learning rate (base R(i){(u,v)}R(i) \subset \{(u, v)\}2), label smoothing R(i){(u,v)}R(i) \subset \{(u, v)\}3, and standard data augmentations (resized R(i){(u,v)}R(i) \subset \{(u, v)\}4 crop, random horizontal flip, channel-wise normalization). By default, bottleneck factors R(i){(u,v)}R(i) \subset \{(u, v)\}5, R(i){(u,v)}R(i) \subset \{(u, v)\}6 control dimension reduction. Eight channels share the same attention weight vector (Zhao et al., 2020).

Computational complexity for patchwise operators is dominated by convolutional projection and score computation. For input spatial size R(i){(u,v)}R(i) \subset \{(u, v)\}7 and channels R(i){(u,v)}R(i) \subset \{(u, v)\}8:

  • R(i){(u,v)}R(i) \subset \{(u, v)\}9 for projections (where 7×77 \times 70),
  • 7×77 \times 71 for attention-weight computation and weighted sum,
  • Memory 7×77 \times 72. Stride or dilation can reduce the number of attended patch positions 7×77 \times 73 (Barkan, 2019).

5. Benchmark Results and Empirical Insights

Direct comparison of patchwise SANs with convolutional ResNets on ImageNet single-crop validation indicates:

Method Top-1 (%) Top-5 (%) Params (M) FLOPs (G)
ResNet26 73.6 91.7 13.7 2.4
SAN10 (patchwise) 77.1 93.5 11.8 1.9
ResNet38 76.0 93.0 19.6 3.2
SAN15 (patchwise) 78.0 93.9 16.2 2.6
ResNet50 76.9 93.5 25.6 4.1
SAN19 (patchwise) 78.2 93.9 20.5 3.3

Patchwise SAN models achieve 1–2% higher top-1 accuracy while requiring 20–40% fewer parameters and FLOPs relative to comparable convnets.

Patchwise SANs exhibit improved robustness: under 180° input rotation, SAN15 accuracy drops from 78.0% to 56.0% (−22.0 pp) versus ResNet38's drop from 76.0% to 52.2% (−23.8 pp); under a PGD 7×77 \times 74 adversarial attack (7×77 \times 75, 4 steps), ResNet50 top-1 falls to 11.8% (success rate 82.5%), SAN19 to 24.8% (success rate 62.0%). These results suggest patchwise self-attention confers additional robustness to geometric and white-box adversarial attacks (Zhao et al., 2020).

6. Ablations, Parameterizations, and Limitations

Multiple ablations reveal:

  • Among relation functions for 7×77 \times 76, concatenation yields the highest validation accuracy (79.3% top-1) compared to star (78.7%) or clique (79.1%).
  • Two layers for 7×77 \times 77 (Linear7×77 \times 78ReLU7×77 \times 79Linear) provide optimal depth.
  • Distinct xR(i)RR×Cx_{R(i)} \in \mathbb{R}^{|R| \times C}0, xR(i)RR×Cx_{R(i)} \in \mathbb{R}^{|R| \times C}1, xR(i)RR×Cx_{R(i)} \in \mathbb{R}^{|R| \times C}2 parameters perform better than tied versions.
  • Increasing patch (footprint) size from xR(i)RR×Cx_{R(i)} \in \mathbb{R}^{|R| \times C}3 to xR(i)RR×Cx_{R(i)} \in \mathbb{R}^{|R| \times C}4 improves performance then saturates, with limited added FLOPs.

A limitation is the additional implementation complexity and memory overhead incurred by large, fully-connected local attention maps, though modest compared to global pixelwise attention. The module does not employ explicit multihead splitting, relying instead on group sharing of attention weights (Zhao et al., 2020).

Patchwise attention subsumes classical convolution as a strict generalization, fully capturing stationary kernels as a limiting case while supporting content-conditioned adaptation. Self Attentive Convolutions (SAC) extend the paradigm further, showing that standard global self-attention is a 1×1 convolution, and that patchwise attention is equivalent to generalizing to xR(i)RR×Cx_{R(i)} \in \mathbb{R}^{|R| \times C}5 localities. Multiscale SAC (MSAC) computes parallel patchwise attentions over varying scales, laterally concatenating their outputs and fusing via xR(i)RR×Cx_{R(i)} \in \mathbb{R}^{|R| \times C}6 convolutions. This approach enables simultaneous modeling of local and non-local dependencies without explicit patch partitioning and can be integrated within ResNet- or DenseNet-style backbones. Preliminary experiments, though unpublished in detail, demonstrate that replacing convolutional layers with SAC/MSAC consistently improves classification and segmentation with comparable parameter budgets (Barkan, 2019).

Definition Search Book Streamline Icon: https://streamlinehq.com
References (2)

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Patchwise Self-Attention.