A RepVGG block is the sum of three branches during training and a single 3×3 convolution at inference. The upper right of the figure at the top of the article shows the training-time and inference-time blocks.
Training-time block
For a block input x, the outputs of the following three branches are summed and passed through ReLU.
- 3×3 convolution + BN: an ordinary convolutional layer.
- 1×1 convolution + BN: corresponds to the 1×1 shortcut that ResNet uses when input and output shapes differ.
- Identity + BN: passes the input through unchanged and applies only BN. It is attached only to layers whose input and output shapes match (stride 1 and the same number of channels).
As an equation, the output M(2) is computed from the input M(1) as follows (Eq. 1 of the paper; ∗ is convolution, W(3) and W(1) are the 3×3 and 1×1 weights, and μ,σ,γ,β are BN's mean, standard deviation, scale, and shift).
M(2)=bn(M(1)∗W(3))+bn(M(1)∗W(1))+bn(M(1))
Each of the three branches has its own BN. This extends ResNet's residual connection y=x+f(x) to y=x+g(x)+f(x).
Conversion to the inference-time form
After training, the three branches are merged into a single 3×3 convolution with the following steps (Section 3.3 of the paper). The lower right of the figure at the top of the article shows these steps.
- Fold BN into the preceding convolution: at inference, BN is a linear expression (x−μi)γi/σi+βi for each channel i, so it can be absorbed into the weights and bias of the preceding convolution (Eq. 3 of the paper).
- Treat the identity as a 1×1 convolution: the identity is the same as a 1×1 convolution whose weight is the identity matrix. This lets step 1 apply to this branch as well.
- Pad the 1×1 weights to 3×3: surrounding the 1×1 weights with zeros gives 3×3 weights with a value only at the center.
- Add the three: adding the three 3×3 weights and the three biases yields a single 3×3 convolution.
The equation for step 1 is as follows. W′ and b′ are the weight and bias after folding in BN.
Wi,:,:,:′=σiγiWi,:,:,:,bi′=βi−σiμiγi
Because both convolution and inference-time BN are linear, the sum gives exactly the same result. The conversion is done once after training and does not need to be recomputed at each inference.
For the conversion to hold, the 3×3 and 1×1 layers must have the same stride, and the 1×1 layer's padding must be one pixel less than the 3×3 layer's. With the common "padding 1 for the 3×3 layer", the 1×1 layer uses padding 0.
Why training becomes easier
The paper explains this with the view that ResNet can be seen as "an implicit ensemble of many shallow models". A ResNet block splits the flow into two paths, so n blocks yield 2n paths. A RepVGG block splits it into three, giving 3n. For example, stage 4 of RepVGG-B0 has 2×315≈2.8×107 paths, far more than the same number of 3×3 convolutions arranged ResNet-style (28=256 paths).
Having the same structure at inference does not mean the training-time behavior is the same. Training-time BN uses per-mini-batch means and variances, so it is not linear, and training with a single convolution in place of the three-branch block does not give the same result. In the paper, placing a single BN after the addition lowered accuracy from 75.14% to 73.52% (Table 7).