---
title: Hierarchical Convolutional Patch Embedding
url: https://www.emergentmind.com/topics/hierarchical-convolutional-patch-embedding-hcpe
type: topic
---

# Hierarchical Convolutional Patch Embedding

Hierarchical Convolutional Patch Embedding (HCPE) is a multi-layer convolutional embedding module designed to replace traditional patch embedding layers in hierarchical Vision Transformers (ViTs). HCPE is constructed to inject strong locality and inductive bias at each scale of the network, enhancing both the effective receptive field and semantic feature representation. It is primarily employed at each stage transition in hierarchical ViT backbones, resulting in measurable accuracy improvements across image classification, detection, and segmentation benchmarks without increasing FLOPs or parameter count in a significant manner [2207.13317].

## 1. Motivation and Conceptual Framework

Traditional ViT architectures utilize a single linear projection or a shallow convolution (often 1–2 layers) to perform patch embedding, typically lacking spatial locality and constrained effective receptive field (ERF). This design often limits data efficiency and the network’s local semantic representation. In contrast, hierarchical ViT designs (such as PVT and Swin) benefit from progressive spatial down-sampling and feature pyramid construction. HCPE generalizes the embedding module by introducing a compact stack of convolutional blocks—either MBConv (depthwise separable, pointwise expansion) or Fused-MBConv—at the beginning of each stage. This approach delivers:

- Stronger locality and increased ERF per scale,
- Inductive bias propagated to deeper stages, not just the initial input,
- Significant accuracy uplift with preserved computational budget.

## 2. Macro-Architecture and Stage Design

HCPEs are inserted at the start of each stage within a 5-stage hierarchical backbone. The progression of spatial resolutions and channel expansions for a $224 \times 224$ input image follows a regular pattern, as shown below:

| Stage   | Spatial Transformation        | Output Channels ($C^i$)    |
|---------|------------------------------|-----------------------------|
| S0      | $224 \to 56$                 | $C^0$ (conv stem)           |
| S1      | $56 \to 28$                  | $C^1$ (HCPE$^1$)            |
| S2      | $28 \to 14$                  | $C^2$ (HCPE$^2$)            |
| S3      | $14 \to 7$                   | $C^3$ (HCPE$^3$)            |
| S4      | $7 \to 7$                    | $C^4$ (HCPE$^4$)            |

The initial stem (S0) comprises four Fused-MBConv layers followed by a $1 \times 1$ convolution and LayerNorm, transforming RGB input into feature maps. Each subsequent HCPE takes feature maps, halves spatial resolution (except at S4) and increases channel dimension prior to transformer (ViT) blocks.

## 3. HCPE Layer Configuration and Forward Path

Each HCPE comprises $L=5$ convolutional blocks:

- The first block: MBConv (stages S2–S4) or Fused-MBConv (S0/S1), stride $=2$ for down-sampling.
- Remaining blocks: MBConv (or Fused-MBConv in S1), stride $=1$, maintaining spatial size.
- All blocks: 3×3 depthwise convolution with BatchNorm (BN) and GELU activation (ReLU for Fused-MBConv), sandwiched between pointwise ($1 \times 1$) expansion and projection.
- After 5 blocks, a terminal $1 \times 1$ convolution with LayerNorm maps to the target channel dimension.
- The final output is flattened spatially to produce a sequence of tokens for the transformer stage.

The typical configuration values for channels are $C^0$=64, $C^1$=128, $C^2$=256, $C^3$=512, $C^4$=1024 (for the “base” model).

## 4. Mathematical Formulation

Given input $X^0 \in \mathbb{R}^{H \times W \times C_\text{in}}$, the HCPE is a composition of L convolutional layers:

\[
X^l = \sigma \left( \text{Norm}_l \left( \text{Conv}_{k_l,s_l,p_l} (X^{l-1}) + b_l \right) \right)
\]

for $l = 1\ldots L$, with kernel sizes $k_l \in \{3,1\}$, strides $s_l \in \{1,2\}$ (down-sampling on $l=1$), and paddings $p_l = \lfloor k_l/2 \rfloor$. $\text{Norm}_l$ is BatchNorm for internal layers; the final $1 \times 1$ convolution’s output passes through LayerNorm. Activations $\sigma$ are GELU in MBConv, ReLU in Fused-MBConv. After $L$ layers, flattening across spatial dimensions yields token embeddings:

\[
E(X) = \text{FlattenSpatial}( X^L ) \in \mathbb{R}^{N \times D}
\]

where $N=H'W'$ and $D$ is the channel size after $L$ layers.

## 5. Integration into Vision Transformers

HCPE modules are integrated into the transformer backbone as follows:

- The input image passes through the convolutional stem (4 × Fused-MBConv + $1 \times 1$ conv + LayerNorm).
- At each subsequent stage, an HCPE module down-samples and re-embeds features, which are then tokenized.
- Tokens go through $N_i$ blocks of windowed (often shifted) self-attention + MLP transformer layers.
- The process is repeated for four hierarchical stages, forming a spatial feature pyramid for downstream tasks.
- Final tokens are processed by classification, detection, or segmentation heads.

The pseudocode for this process is:

```python
# X is input image of shape (H0,W0,3)
# S0: convolutional stem
X0 = ConvStem(X)  # 4 × Fused-MBConv + 1×1 + LN → (H0/4,W0/4,C0)
for i in 1…4:
    Xi = X{i-1}
    for l in 1…5:
        if l==1 and i<4:
            stride = 2  # downsample by 2
        else:
            stride = 1
        if i==1:
            Xi = FusedMBConv(Xi, stride=stride, out_channels=C_i)
        else:
            Xi = MBConv(Xi, stride=stride, expand_ratio=r_i, out_channels=C_i)
    Xi = Conv2D(Xi, kernel=1, stride=1, out_channels=C_i)
    Xi = LayerNorm(Xi)
    tokens = FlattenSpatial(Xi)   # shape (N_i, C_i)
    for blk in 1…N_i_blocks:
        tokens = TransformerBlock(tokens)
    X{i} = Unflatten(tokens, H_i, W_i, C_i)
# Final stage X4 tokens go to downstream task head
```

## 6. Comparison with Standard ViT Patch Embedding

The original ViT employs a single $16 \times 16$ convolution (or linear projection) with stride 16 to transform a $224\times224$ image directly into $14 \times 14$ tokens, leading to a lack of progressive spatial hierarchy. In contrast, HCPE introduces:

- **Multi-layer design:** Five small convolutional blocks per stage, instead of a single patchification layer.
- **Multi-scale pyramid:** Inserted at every stage, with progressive spatial reduction (56→28→14→7), forming hierarchical feature maps.
- **Enhanced inductive bias:** Depthwise convolutions enlarge the effective receptive field and embed locality, which a single projection cannot achieve.

## 7. Empirical Performance and Best Practices

Substituting original patch embedding/merging modules in hierarchical ViTs (CvT-13, PVT-S, Swin-T, CSWin-T) with HCPE leads to consistent accuracy increases on ImageNet-1K:

| Model       | Orig Top-1 | +HCPE Top-1 | Δ   |
|-------------|------------|-------------|-----|
| CvT-13      | 81.6%      | 82.1%       | +0.5|
| PVT-S       | 79.8%      | 81.1%       | +1.3|
| Swin-T      | 81.3%      | 82.5%       | +1.2|
| CSWin-T     | 82.7%      | 83.6%       | +0.9|

On COCO (Mask-R-CNN, $1\times$ schedule):

| Backbone      | box AP | mask AP |
|---------------|--------|---------|
| Swin-T        | 43.7   | 39.1    |
| HCPE-Swin-T   | 45.5   | 40.7    |

On ADE20K (UperNet, single-scale):

| Backbone    | mIoU |
|-------------|------|
| Swin-T      | 44.5 |
| HCPE-Swin-T | 46.5 |

For implementation, the following are recommended: use five conv blocks per HCPE (first with stride 2); prefer Fused-MBConv for S0/S1 and MBConv elsewhere; expansion ratios $r \in \{1,2\}$; always append LayerNorm after the final $1\times1$ conv; GELU activation for MBConv, ReLU for Fused-MBConv; AdamW optimizer with recommended hyperparameters (initial lr 1e–3, weight-decay 0.05, 300 epochs, batch 1024, input 224, 20-epoch warmup, cosine schedule), and fine-tuning at 384×384 resolution for 30 epochs with lr 2e–5. Maintaining windowed self-attention but projecting queries/keys via local depthwise convolution (LEWin) further strengthens inductive bias.

HCPE thus provides a lightweight, scalable, and empirically validated alternative to single-layer patch embeddings, enhancing both performance and feature hierarchy in modern hierarchical ViTs [2207.13317].

Source: https://www.emergentmind.com/topics/hierarchical-convolutional-patch-embedding-hcpe