Computer Vision

CNN vs ViT: Inductive Bias

Comparing local inductive bias in ConvNets against global self-attention scaling in Vision Transformers.

🔴 advanced5 min readvision
Vision Transformers (ViT - Dosovitskiy et al., 2020) adapted self-attention mechanisms from NLP to computer vision by splitting images into non-overlapping 16x16 patch tokens. Convolutional Neural Networks (CNNs) possess strong Inductive Biases (Translation Invariance and Local Spatial Locality). Vision Transformers have minimal spatial inductive bias, requiring massive pre-training datasets (JFT-300M) or strong data augmentation, but scale significantly better with compute.

Structural Comparison

  CONVOLUTIONAL NEURAL NETWORK (CNN)              VISION TRANSFORMER (ViT)
  Local 3x3 Sliding Window Receptive Fields      Global 16x16 Patch Token Self-Attention
  ┌───┬───┬───┐                                  ┌──────┬──────┐
  │ . │ . │ . │  Local Locality                  │Patch1│Patch2│  Global Interactions
  ├───┼───┼───┤  Hard-Coded Inductive Bias!      ├──────┼──────┤  Learned Spatial Relationships!
  │ . │ . │ . │                                  │Patch3│Patch4│
  └───┴───┴───┘                                  └──────┴──────┘

Detailed Comparison Matrix

PropertyConvolutional Neural Network (ResNet / ConvNeXt)Vision Transformer (ViT / Swin)
Core OperationLocal 3x3 / 7x7 Sliding ConvolutionsGlobal / Windowed Multi-Head Self-Attention
Inductive BiasHigh (Local Locality & Translation Invariance)Minimal (Learns spatial layout from data)
Small Dataset PerformanceStrong (Works well on ImageNet-1K from scratch)Poor without heavy augmentation / pre-training
Large Scale Data ScalingSaturates earlierScales exceptionally with compute & data
Receptive Field GrowthLinear with depth ($O(L)$ layers)Global immediately in Layer 1 ($O(1)$)
Computational Complexity$O(H \cdot W \cdot C^2)$ (Linear with resolution)$O((HW)^2 \cdot C)$ (Quadratic with resolution)

Patch Tokenization Math in ViT

Given input image $X \in \mathbb{R}^{H \times W \times C}$ and patch size $P = 16$:

  1. Number of patch tokens $N$:

$$N = \frac{H \cdot W}{P^2}$$

  1. Each patch is flattened into vector $x_p \in \mathbb{R}^{P^2 \cdot C}$.
  2. Linear projection $E \in \mathbb{R}^{(P^2 \cdot C) \times D}$ maps patches to $D$-dimensional embeddings.
  3. Prepend learnable [CLS] token and add 1D Learnable Position Embeddings $E_{\text{pos}}$:

$$\mathbf{z}0 = [ x{\text{class}}; x_p^1 E; x_p^2 E; \dots; x_p^N E ] + E_{\text{pos}}$$

  1. Pass $\mathbf{z}_0$ into standard Transformer Encoder layers!

Hybrid & Swin Transformers

Standard ViT self-attention has quadratic $O(N^2)$ complexity with respect to image pixels, making high-resolution processing slow.

Say this out loud

CNNs use local sliding convolutions with strong spatial inductive biases (locality and translation invariance), excelling on smaller datasets. Vision Transformers (ViT) tokenize images into 16x16 patch tokens and use global self-attention. Lacking inductive bias, ViTs require massive pre-training data (JFT-300M) or Swin shifted windows, but scale significantly better with compute.

Follow-ups to expect

Check yourself

Question 1 of 3

What spatial Inductive Biases are hard-coded into Convolutional Neural Networks (CNNs) that Vision Transformers (ViT) lack?

More in Computer Vision

See all →
Image Augmentation Strategies5 minResizing, Normalization & Colour Spaces4 minResNet, EfficientNet & Friends5 min