Engineering / Computer Vision

CNN vs Transformer in Computer Vision: A Complete Comparison

/16 min read

Introduction

For nearly a decade, CNNs were the undisputed architecture for computer vision. Then, in 2020, the Vision Transformer (ViT) demonstrated that a pure Transformer — with no convolutional operations — could match or exceed state-of-the-art CNNs on ImageNet classification. This sparked a fundamental debate: are CNNs still relevant, or have Transformers rendered them obsolete?

Five years later, the answer is nuanced. CNNs and Vision Transformers have different strengths, different scaling behaviours, and different deployment characteristics. The debate has largely been resolved not by one architecture winning, but by convergence — modern CNNs have adopted Transformer design elements (ConvNeXt), modern Transformers have adopted CNN design elements (Swin's local windows), and hybrid architectures combine both.

This post compares CNNs and Vision Transformers across the dimensions that matter for production deployment: inductive bias, data efficiency, scaling behaviour, computational efficiency, hardware utilisation, and practical trade-offs. We also examine the hybrid architectures that represent the current state of the art.

The Vision Transformer (ViT): A Primer

The Vision Transformer (Dosovitskiy et al., 2021) applies the standard Transformer architecture to images with minimal modification. An image is split into fixed-size patches (typically 16x16), each patch is linearly projected into a 1D embedding, and a learnable position embedding is added. The resulting sequence of patch embeddings is processed by a standard Transformer encoder.

ViT uses only the encoder (no decoder), prepends a class token to the sequence (whose final representation serves as the image representation), and adds no explicit image-specific inductive biases. The model must learn spatial relationships entirely from data through the attention mechanism.

class PatchEmbedding(nn.Module):
    """
    Vision Transformer patch embedding.
    Splits an image into patches and projects them.
    """
    def __init__(self, img_size=224, patch_size=16, in_chans=3, embed_dim=768):
        super().__init__()
        self.num_patches = (img_size // patch_size) ** 2
        # Conv layer with stride=patch_size acts as patch extraction
        self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
    
    def forward(self, x):
        x = self.proj(x)  # (B, embed_dim, H/patch, W/patch)
        x = x.flatten(2)  # (B, embed_dim, num_patches)
        x = x.transpose(1, 2)  # (B, num_patches, embed_dim)
        return x

class ViTBlock(nn.Module):
    def __init__(self, dim, num_heads, mlp_ratio=4.0):
        super().__init__()
        self.norm1 = nn.LayerNorm(dim)
        self.attn = nn.MultiheadAttention(dim, num_heads)
        self.norm2 = nn.LayerNorm(dim)
        self.mlp = nn.Sequential(
            nn.Linear(dim, int(dim * mlp_ratio)),
            nn.GELU(),
            nn.Linear(int(dim * mlp_ratio), dim),
        )
    
    def forward(self, x):
        x = x + self.attn(self.norm1(x), self.norm1(x), self.norm1(x))[0]
        x = x + self.mlp(self.norm2(x))
        return x

The critical finding: ViT requires large-scale pretraining (ImageNet-21K or JFT-300M) to perform well. When trained from scratch on ImageNet-1K, ViT underperforms ResNet. This is because Transformers lack the inductive biases that make CNNs data-efficient — specifically, locality (nearby pixels are related) and translation equivariance (a cat is a cat regardless of position).

Inductive Biases: The Fundamental Difference

The core difference between CNNs and Transformers lies in their inductive biases — the built-in assumptions about the structure of visual data.

  • Locality: CNNs assume that nearby pixels are more relevant than distant ones. A 3x3 kernel only looks at a 3x3 neighbourhood. Transformers make no such assumption — attention can connect any two patches regardless of distance, though in practice learned attention patterns are often local.
  • Translation equivariance: CNNs are naturally translation equivariant — shifting the input shifts the feature maps by the same amount. This makes CNNs robust to spatial translations without training. Transformers must learn translation invariance from data, which requires more examples.
  • Weight sharing: CNNs reuse the same kernel across all spatial positions, dramatically reducing parameters. Transformers use the same attention mechanism for all patch pairs, but the attention weights are computed dynamically based on content, not fixed.
  • Hierarchical structure: CNNs naturally build hierarchical representations through progressive down-sampling. Standard ViT maintains a single resolution throughout, though hierarchical ViTs (Swin, PVT) address this.

The practical implication: CNNs are more data-efficient — they achieve better performance with less training data. Transformers are more flexible — they can learn arbitrary spatial relationships if given enough data. This is why ViT needed JFT-300M (300 million images) to match BiT (a ResNet trained on the same data), but ViT outperforms CNNs when both are trained on sufficiently large datasets.

Scaling Behaviour

A landmark study by Zhai et al. (2022) compared CNNs and ViTs across multiple scales. The key finding: ViTs have better scaling properties. As model size, data size, and compute increase, ViT performance improves at a faster rate than CNN performance. This is the primary reason Transformers have been adopted for large-scale vision models.

The intuition: CNNs' strong inductive biases help when data is limited but become constraints when data is abundant. A fixed 3x3 kernel cannot learn to look at distant pixels, no matter how much data it sees. A Transformer has no such constraint — given enough data, it can learn optimal attention patterns.

However, this advantage only manifests at very large scales. At typical production scales (1K-100K images), CNNs often match or exceed ViTs, especially when combined with pretraining and data augmentation. The crossover point depends on the specific task and architecture but is generally in the range of 10-100 million training images.

Computational Efficiency and Hardware Utilisation

The computational characteristics of CNNs and ViTs differ significantly:

  • Attention complexity: Standard self-attention is O(n^2) in the number of patches, where n = (H * W) / patch_size^2. For a 224x224 image with 16x16 patches, n = 196 — manageable. For a 1024x1024 image with the same patch size, n = 4096, and attention becomes expensive. For high-resolution inputs, windowed attention (Swin) or linear attention is essential.
  • Convolution efficiency: Convolutions are O(k^2 * C_in * C_out * H_out * W_out). The constant factors are extremely well optimised through decades of work on cuDNN, MKL-DNN, and hardware-specific kernels. Convolutions map efficiently to GPU tensor cores and parallelise perfectly across the batch dimension.
  • Memory: ViTs must materialise the attention matrix (n x n), which for high-resolution inputs can exceed GPU memory. Flash Attention helps but adds complexity. CNNs have more predictable memory requirements that scale linearly with resolution.
  • Latency: On GPU, modern CNNs and ViTs have similar latency for equivalent accuracy. On CPU or edge devices, CNNs typically outperform ViTs because convolution kernels are more mature and require less memory bandwidth.

Swin Transformer: Bringing CNN Inductive Biases to Transformers

The Swin Transformer (Liu et al., 2021) addressed ViT's limitations by incorporating CNN-like design elements into the Transformer framework. Key innovations include:

  • Hierarchical feature maps: Like CNNs, Swin builds feature maps at multiple resolutions (4x, 8x, 16x, 32x down-sampled) through patch merging layers. This enables multi-scale processing and compatibility with existing architectures like FPN.
  • Windowed attention: Self-attention is computed within local windows (typically 7x7 patches), reducing complexity from O(n^2) to O(n * w^2) where w is the window size. This is analogous to convolution's local connectivity.
  • Shifted windows: Alternating between standard and shifted window partitions enables cross-window information flow without full global attention. This provides a path for long-range interactions while maintaining efficiency.
class SwinTransformerBlock(nn.Module):
    """
    Swin Transformer block with windowed attention and shifted windows.
    """
    def __init__(self, dim, num_heads, window_size=7, shift=False):
        super().__init__()
        self.window_size = window_size
        self.shift = shift
        self.norm1 = nn.LayerNorm(dim)
        self.attn = WindowAttention(dim, num_heads, window_size)
        self.norm2 = nn.LayerNorm(dim)
        self.mlp = nn.Sequential(
            nn.Linear(dim, 4 * dim), nn.GELU(), nn.Linear(4 * dim, dim)
        )
    
    def forward(self, x):
        H, W = x.shape[2], x.shape[3]
        x = x.flatten(2).transpose(1, 2)  # (B, N, C)
        shortcut = x
        x = self.norm1(x)
        
        # Window partition with optional cyclic shift
        if self.shift:
            x = torch.roll(x, shifts=(-self.window_size//2, -self.window_size//2), dims=(1,2))
        x = window_partition(x, self.window_size)
        x = self.attn(x)
        x = window_reverse(x, self.window_size, H, W)
        if self.shift:
            x = torch.roll(x, shifts=(self.window_size//2, self.window_size//2), dims=(1,2))
        
        x = shortcut + x
        x = x + self.mlp(self.norm2(x))
        return x.transpose(1, 2).reshape(-1, C, H, W)

Swin Transformer achieved state-of-the-art results on ImageNet (87.3% top-1 for Swin-L), COCO object detection, and ADE20K semantic segmentation. It demonstrated that Transformers could succeed in vision without requiring JFT-scale pretraining.

ConvNeXt: Bringing Transformer Design to CNNs

ConvNeXt (Liu et al., 2022) went in the opposite direction: starting from a standard ResNet, apply a series of targeted design modernisations inspired by Swin Transformer until performance matches. The result is a pure CNN that equals or exceeds Swin on ImageNet and downstream tasks.

Key changes from ResNet to ConvNeXt: larger kernel size (7x7 depthwise conv instead of 3x3), inverted bottleneck (hidden dim 4x input dim, like Transformers), fewer activation and normalisation layers, GELU activations instead of ReLU, LayerNorm instead of BatchNorm, patchify stem (4x4 stride-4 conv), and separate down-sampling layers. Remarkably, every change was motivated by corresponding Transformer design choices, but the final architecture uses only convolutions — no attention.

ConvNeXt's significance: it proved that CNNs are not fundamentally inferior to Transformers. The performance gap was largely due to design choices (small kernels, batch norm, ReLU) that had become outdated, not to fundamental limitations of the convolutional paradigm. ConvNeXt remains the preferred backbone for many production systems due to its efficient convolution kernels and well-understood behaviour.

Hybrid Architectures: Best of Both Worlds

Several hybrid architectures combine convolutional and attention-based processing:

  • ConvNeXt V2: Adds a convolutional-based masked autoencoder pretraining strategy (FCMAE) that significantly improves representation quality without changing the architecture.
  • MaxViT: Combines multi-axis attention (block local + dilated global) with convolutions in a hierarchical design, achieving strong performance across multiple scales.
  • CoAtNet: Systematically studies how to combine convolution and attention, finding that the optimal design uses convolution in early stages (for efficient local processing) and attention in later stages (for long-range dependencies).
  • Next-ViT: Uses a CNN-based next-generation vision backbone with decomposed attention (spatial and channel) to achieve both high accuracy and low latency on edge devices.

The trend is clear: the strict CNN vs Transformer dichotomy is giving way to a unified design space where the best architectures selectively combine elements from both paradigms based on the specific requirements of the task and deployment environment.

When to Use Each Architecture

Based on the current evidence, here are practical guidelines:

Choose CNNs (ConvNeXt, EfficientNet, ResNet) when:

  • You have limited data (less than 100K labelled images per task)
  • You need to deploy on edge devices, mobile phones, or CPUs
  • Latency is critical (real-time video processing, autonomous vehicles)
  • You need well-understood, predictable behaviour in production
  • Your inputs are high-resolution (convolution memory scales linearly)

Choose Transformers (ViT, Swin) when:

  • You have massive data (millions of labelled images or self-supervised pretraining)
  • You need the best possible accuracy and have abundant compute
  • Your task benefits from global context (document understanding, scene graphs)
  • You are fine-tuning a large pretrained model (ViT-Huge, Swin-V2-Giant)
  • You want to use multimodal architectures (CLIP, DALL-E, Flava)

For most production applications at moderate scale, the choice between a modern CNN (ConvNeXt) and a hierarchical Transformer (Swin) is unlikely to be the deciding factor in overall system performance. Data quality, annotation consistency, augmentations, and post-processing typically matter more.

Conclusion

The CNN vs Transformer debate in computer vision has largely converged: both architectures are viable, and the gap has narrowed as each camp adopted the other's best ideas. ConvNeXt is a CNN that performs like a Transformer. Swin is a Transformer that looks like a CNN. Hybrid architectures combine both.

The practical takeaway: choose ConvNeXt or EfficientNet for efficiency-constrained deployments, choose Swin or ViT for accuracy-maximising scenarios with abundant compute and data, and choose hybrid architectures when you need the best trade-off between the two. In all cases, the quality and quantity of your training data, the suitability of your data augmentations, and the rigour of your evaluation pipeline will have a larger impact on final performance than the architectural choice alone.

For teams deploying vision models in production, we offer infrastructure that supports both CNN and Transformer backends. For a deeper understanding of CNN architecture, see our guide on CNN architecture from the ground up and the evolution of CNN architectures.

References

  1. Dosovitskiy, A., et al. "An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale." ICLR 2021. arXiv:2010.11929
  2. Liu, Z., et al. "Swin Transformer: Hierarchical Vision Transformer using Shifted Windows." ICCV 2021. arXiv:2103.14030
  3. Liu, Z., et al. "A ConvNet for the 2020s." CVPR 2022. arXiv:2201.03545
  4. Touvron, H., et al. "Training Data-Efficient Image Transformers & Distillation Through Attention." ICML 2021. arXiv:2012.12877
  5. He, K., et al. "Masked Autoencoders Are Scalable Vision Learners." CVPR 2022. arXiv:2111.06377
  6. Dai, Z., et al. "CoAtNet: Marrying Convolution and Attention for All Data Sizes." NeurIPS 2021. arXiv:2106.04803
  7. Tu, Z., et al. "MaxViT: Multi-Axis Vision Transformer." ECCV 2022. arXiv:2204.01697
  8. Zhai, X., et al. "Scaling Vision Transformers." CVPR 2022. arXiv:2106.04560
  9. Wightman, R., et al. "ResNet Strikes Back: An Improved Training Procedure in TIMM." NeurIPS 2021. arXiv:2110.00476
  10. Brock, A., et al. "High-Performance Large-Scale Image Recognition Without Normalization." ICML 2021. arXiv:2102.06171
  11. Vaswani, A., et al. "Attention Is All You Need." NeurIPS 2017. arXiv:1706.03762
  12. Li, J., et al. "EVA: Exploring the Limits of Masked Visual Representation Learning at Scale." CVPR 2023. arXiv:2211.07636
Summarize with AI
Page