RepVGG:訓練用分支,部署用直線網路
選讀・DLCV 01B・網路的積木與組合:分類與影像復原網路 的論文導讀(第 4/4 篇)。引用編號對應本頁底部的參考文獻。
導覽:本頁與本系列: 導覽:本頁與本系列
RepVGG [54] 把「容易訓練的網路」與「容易執行的網路」分開設計。它的重點是精確轉換已訓練的模組,而不是宣稱任意非線性分支都能用一個卷積取代。
挑戰
部署引擎可能很擅長執行一連串普通卷積,但這種簡單的推論圖,不一定是訓練時最容易最佳化的計算圖。殘差分支能提供額外的資訊與梯度路徑;執行時卻也需要保留中間激活值,並進行加法。RepVGG 要解決的問題是:能否保留分支對訓練的好處,卻不把分支帶進推論階段?
論文著重 GPU 吞吐量與可能的專用硬體。運算次數無法單獨描述記憶體存取、運算核支援與平行執行,因此它討論的是實際量測的部署取捨,而非一味減少參數。這些結果也不能證明密集卷積網路在所有行動處理器上都比較適合。
方法

圖片來源: [54], Published paper PDF, PDF 第 5 頁. 原始 PDF.
訓練模組。 一個模組把三條分支相加: 卷積接批次正規化(batch normalization,BN)、 卷積接 BN,以及恆等映射接 BN。只有在三者相加之後才套用一次 ReLU。恆等分支只用於步幅為一、輸入與輸出通道數相同的模組;降取樣模組只有兩條卷積分支。兩條卷積的步幅、填補與分組方式必須相容,讓輸出形狀可以相加。
非線性的位置是轉換能否成立的關鍵。BN 使用固定的推論統計量時,是一個仿射轉換,因此三條分支都能寫成仿射卷積。如果每條分支內各自放 ReLU,就無法用「一個線性卷積加一個 ReLU」取代。訓練時 BN 使用批次統計量,所以這個轉換並不表示分支模組與直線卷積具有相同的訓練動態。
把卷積與 BN 合併。 將一條分支的卷積寫成 。其中 為輸入, 是輸出通道索引, 是該輸出通道的卷積核, 表示卷積, 是可省略的偏差。令 與 為 BN 儲存的平均與變異數, 與 為學得的縮放與位移, 為數值穩定常數。評估模式的 BN 可合併為
其中 會縮放輸出通道 的所有權重; 與 是合併後的卷積核與偏差。原本沒有卷積偏差時,令 。這些都是使用儲存統計量的代數計算,合併運算本身不需要額外的校準批次。
把所有分支放進同一個卷積核空間。 將合併後的 卷積核放在全零 核的中心。對密集恆等分支,建立一個只有「相同輸入/輸出通道的中心位置」為一、其他位置全零的卷積核,再用同一公式合併它的 BN。分組模組必須在每一組內建立相對應的恆等映射,不能跨組連接;論文也讓兩條卷積分支使用相同的分組數。
令 表示把 核置中補零, 與 為合併 BN 後的卷積核, 為合併 BN 後的恆等核,部署參數就是
沒有恆等分支時,其卷積核與偏差都視為零。 與 定義一個 卷積;原本位於加法之後的 ReLU 仍保留在它後面。因此推論圖不再需要那些分支與 BN 運算。這不是近似,也不是額外的學習階段;浮點運算順序改變時,數值可能有微小差異。
重參數化折疊的逐步補充 已有完整三分支模組的檢查。下面只隔離一個容易漏掉的細節:恆等映射加上 BN,會變成通道縮放的中心卷積核以及偏差,不只是恆等核。
程式碼 · Python · 24 行
import numpy as np
from numpy.lib.stride_tricks import sliding_window_view
rng = np.random.default_rng(4)
x = rng.normal(size=(1, 2, 5, 5)) # batch, channels, height, width
gamma = np.array([1.2, -0.7])
beta = np.array([0.1, 0.3])
mean = np.array([0.2, -0.4])
variance = np.array([0.5, 1.3])
scale = gamma / np.sqrt(variance + 1e-5)
kernel = np.zeros((2, 2, 3, 3)) # output, input, kernel height/width
kernel[np.arange(2), np.arange(2), 1, 1] = scale
bias = beta - scale * mean
windows = sliding_window_view(
np.pad(x, ((0, 0), (0, 0), (1, 1), (1, 1))), (3, 3), axis=(2, 3)
)
fused = np.einsum("nchwkl,ockl->nohw", windows, kernel) + bias[None, :, None, None]
branch = (
gamma[None, :, None, None] * (x - mean[None, :, None, None])
/ np.sqrt(variance[None, :, None, None] + 1e-5)
+ beta[None, :, None, None]
)
print(f"max error: {np.max(np.abs(fused - branch)):.3e}")
assert np.allclose(fused, branch, rtol=1e-12, atol=1e-12)實際輸出為 max error: 4.441e-16。這是在合成輸入上驗證代數等價性的計算,不是訓練模型的準確率實驗。
整體架構。 主體分成五個階段,各階段第一個卷積以步幅二降取樣;後面接全域平均池化與分類器。表 2 列出 A 系列各階段的模組數為 ,B 系列則為 。若寬度倍率為 與 ,五個階段的通道數依序是 、、、、。第一階段設上限,避免在高解析度特徵上花太多計算;最後階段另用一個倍率,讓低解析度表示可以更寬。
表 3 對 A0/A1/A2 採用的 分別是 、、;B0/B1/B2/B3 則是 、、、。g2/g4 變體在特定的交替卷積層使用二組/四組分組卷積,並在中間保留密集卷積,讓不同通道仍能交換資訊。它們改變的是準確率與成本的平衡,不是合併原理。
訓練與計時。 簡單的 ImageNet 配方使用隨機裁切與水平翻轉、SGD、全域批次大小 256、初始學習率 0.1、120 個 epoch 的餘弦衰減、動量 0.9,以及對卷積/分類器卷積核施加 權重衰減。較強的 200-epoch 配方加入五個 epoch 暖身、AutoAugment [59]、標籤平滑與 mixup [60]。吞吐量在單張 GTX 1080 Ti 上,以 FP32 與批次大小 128 量測,先暖身 50 個批次,再計時 50 個批次。基準模型的卷積與 BN 也會先合併。解讀結果時,不能省略這些條件。
結果
表 4 與表 5 的 ImageNet top-1 準確率、吞吐量如下。表中保留訓練配方的差異;較強配方下的分數提升,不等於單純的架構改進。
| 模型 | 訓練 epoch 數 | Top-1(%) | 每秒影像數 |
|---|---|---|---|
| RepVGG-A0 | 120 | 72.41 | 3256 |
| ResNet-18 | 120 | 71.16 | 2442 |
| RepVGG-A2 | 120 | 76.48 | 1322 |
| ResNet-50 | 120 | 76.31 | 719 |
| RepVGG-B1g4 | 120 | 77.58 | 868 |
| ResNet-101 | 120 | 77.21 | 430 |
| RepVGG-B3 | 200 | 80.52 | 363 |
| RegNetX-12GF | 200 | 80.55 | 277 |
以上各列皆使用 的 ImageNet 輸入。這些是在論文批次 128 設定下的吞吐量,不是單張影像延遲,也不保證其他裝置有相同結果。Cityscapes [58] 驗證實驗的表 8 中,以 RepVGG-B1g2-fast 為骨幹的 PSPNet [57] 得到 78.88% mIoU、每秒 10.9 張影像;ResNet-50 骨幹則為 77.17%、每秒 10.4 張。這裡的計時改用 FP32、批次 16 與 輸入,GPU 相同。「fast」變體只在最後五層使用擴張卷積,因此比較中包含了明確的擴張設定,不能解讀成所有部署運算都一樣有效率。
消融
移除分支,表 6。 最終 B0 推論結構相同時,訓練不加兩條輔助分支的 top-1 為 72.39%;只加恆等–BN 為 74.79%,只加 –BN 為 73.15%,兩者都有則為 75.14%。完整模組在轉換前為每秒 1061 張影像,表 4 部署後的 B0 則為 1817 張。因此額外的訓練圖能幫助最佳化,訓練後移除分支則恢復吞吐量;準確率差異不能只用最終架構來解釋。
位置與非線性,表 7。 拿掉恆等分支的 BN 後得到 74.18%,把 BN 移到分支加法之後則為 73.52%,原模組為 75.14%。在每條分支內加入 ReLU 可達 75.69%,卻會破壞合併成一個卷積的條件。這項消融也提醒我們:若提升準確率的修改違反了部署限制,就不是可以直接替換的方案。
其他參數化方式,表 7 與第 4.2 節。 論文的 B0 比較得到:DiracNet 式參數化 [55] 為 73.97%,直接加入恆等核為 73.51%,非對稱卷積模組 [56] 為 73.58%,把各層重組成殘差模組為 74.56%。這些是該實驗配方下的比較,並非所有可能訓練方式的全面排名。把 ResNet-50 的卷積換成 RepVGG 模組後為 76.34%,基準為 76.31%。幾乎不變的結果,不支持把這個模組視為已具殘差路徑的網路都能使用的通用準確率升級。
結論
有用的設計原則是:先選一個容易訓練、而且推論線性分支能精確轉換成簡單計算圖的結構。RepVGG 在直線卷積分類器與分割骨幹上展示了這個原則。等價性證明、準確率比較與硬體計時分別回答不同問題;合理的部署決策需要把三者一起檢查。
問題
論文明確優先考慮 GPU 吞吐量,而非最小參數量。結果不能直接解決低功耗或單張影像部署的選型;訓練分支仍要花記憶體與計算。合併依賴固定且有意義的 BN 統計量,無法修復校準不良的模型。後續量化又會帶來捨入誤差,因此浮點合併等價,不代表整數推論也等價。量化與部署的獨立問題見 DLCV 01C;訓練與推論結構可能不同的偵測器則見 DLCV 03A。不能未經重新評估,就把論文的準確率或每秒影像數套用到那些任務。
參考文獻
-
K. He, X. Zhang, S. Ren and J. Sun, “Deep Residual Learning for Image Recognition,” in Proc. IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2016. CVF · arXiv
-
Z. Liu, H. Mao, C.-Y. Wu, C. Feichtenhofer, T. Darrell and S. Xie, “A ConvNet for the 2020s,” in Proc. IEEE/CVF CVPR, 2022. CVF · arXiv
-
A. Dosovitskiy, L. Beyer, A. Kolesnikov, D. Weissenborn, X. Zhai, T. Unterthiner, M. Dehghani, M. Minderer et al., “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale,” in International Conference on Learning Representations (ICLR), 2021. arXiv
-
J. Liang, J. Cao, G. Sun, K. Zhang, L. Van Gool and R. Timofte, “SwinIR: Image Restoration Using Swin Transformer,” in Proc. IEEE/CVF International Conference on Computer Vision Workshops (ICCVW), 2021. CVF · arXiv
-
T.-Y. Lin, P. Dollár, R. Girshick, K. He, B. Hariharan and S. Belongie, “Feature Pyramid Networks for Object Detection,” in Proc. IEEE CVPR, 2017. CVF · arXiv
-
Z. Liu, Y. Lin, Y. Cao, H. Hu, Y. Wei, Z. Zhang, S. Lin and B. Guo, “Swin Transformer: Hierarchical Vision Transformer Using Shifted Windows,” in Proc. IEEE/CVF International Conference on Computer Vision (ICCV), 2021. CVF · arXiv
-
O. Russakovsky, J. Deng, H. Su, J. Krause, S. Satheesh, S. Ma, Z. Huang, A. Karpathy et al., “ImageNet Large Scale Visual Recognition Challenge,” International Journal of Computer Vision, vol. 115, no. 3, pp. 211–252, 2015. doi
-
W. Yu, M. Luo, P. Zhou, C. Si, Y. Zhou, X. Wang, J. Feng and S. Yan, “MetaFormer Is Actually What You Need for Vision,” in Proc. IEEE/CVF CVPR, 2022. CVF · arXiv
-
A. G. Howard, M. Zhu, B. Chen, D. Kalenichenko, W. Wang, T. Weyand, M. Andreetto and H. Adam, “MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications,” arXiv:1704.04861, 2017. arXiv
-
M. Sandler, A. Howard, M. Zhu, A. Zhmoginov and L.-C. Chen, “MobileNetV2: Inverted Residuals and Linear Bottlenecks,” in Proc. IEEE/CVF CVPR, 2018. CVF · arXiv
-
K. He, X. Zhang, S. Ren and J. Sun, “Identity Mappings in Deep Residual Networks,” in Proc. European Conference on Computer Vision (ECCV), LNCS, Springer, 2016. doi · arXiv
-
B. Lim, S. Son, H. Kim, S. Nah and K. M. Lee, “Enhanced Deep Residual Networks for Single Image Super-Resolution,” in Proc. IEEE CVPR Workshops (CVPRW), 2017. CVF · arXiv
-
H. Touvron, M. Cord, A. Sablayrolles, G. Synnaeve and H. Jégou, “Going Deeper With Image Transformers,” in Proc. IEEE/CVF ICCV, 2021. CVF
-
L. Chen, X. Chu, X. Zhang and J. Sun, NAFNet official implementation,
basicsr/models/archs/NAFNet_arch.py, GitHub (megvii-research/NAFNet), accessed 2026. code -
Y. Zhang, K. Li, K. Li, L. Wang, B. Zhong and Y. Fu, “Image Super-Resolution Using Very Deep Residual Channel Attention Networks,” in Proc. ECCV, 2018. CVF · arXiv
-
I. Radosavovic, R. P. Kosaraju, R. Girshick, K. He and P. Dollár, “Designing Network Design Spaces,” in Proc. IEEE/CVF CVPR, 2020. CVF · arXiv
-
J. Hu, L. Shen and G. Sun, “Squeeze-and-Excitation Networks,” in Proc. IEEE/CVF CVPR, 2018. CVF · arXiv
-
S. Woo, J. Park, J.-Y. Lee and I. S. Kweon, “CBAM: Convolutional Block Attention Module,” in Proc. ECCV, 2018. CVF · arXiv
-
L. Chen, X. Chu, X. Zhang and J. Sun, “Simple Baselines for Image Restoration,” in Proc. ECCV, LNCS, Springer, 2022. doi · arXiv
-
M.-H. Guo, T.-X. Xu, J.-J. Liu, Z.-N. Liu, P.-T. Jiang, T.-J. Mu, S.-H. Zhang, R. R. Martin, M.-M. Cheng and S.-M. Hu, “Attention Mechanisms in Computer Vision: A Survey,” Computational Visual Media, vol. 8, no. 3, pp. 331–368, 2022. doi · arXiv
-
S. W. Zamir, A. Arora, S. Khan, M. Hayat, F. S. Khan and M.-H. Yang, “Restormer: Efficient Transformer for High-Resolution Image Restoration,” in Proc. IEEE/CVF CVPR, 2022. CVF · arXiv
-
X. Chen, Z. Li, Y. Pu, Y. Liu, J. Zhou, Y. Qiao and C. Dong, “A Comparative Study of Image Restoration Networks for General Backbone Network Design,” in Proc. ECCV 2024, LNCS, Springer, 2025. doi · arXiv
-
S. Ioffe and C. Szegedy, “Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift,” in Proc. International Conference on Machine Learning (ICML), PMLR 37, 2015. PMLR
-
J. L. Ba, J. R. Kiros and G. E. Hinton, “Layer Normalization,” arXiv:1607.06450, 2016. arXiv
-
D. Ulyanov, A. Vedaldi and V. Lempitsky, “Instance Normalization: The Missing Ingredient for Fast Stylization,” arXiv:1607.08022, 2016. arXiv
-
Y. Wu and K. He, “Group Normalization,” in Proc. ECCV, 2018. CVF · arXiv
-
C. Ledig, L. Theis, F. Huszár, J. Caballero, A. Cunningham, A. Acosta, A. Aitken, A. Tejani, J. Totz, Z. Wang and W. Shi, “Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network,” in Proc. IEEE CVPR, 2017. CVF
-
X. Wang, K. Yu, S. Wu, J. Gu, Y. Liu, C. Dong, Y. Qiao and C. C. Loy, “ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks,” in Proc. ECCV Workshops, 2018. CVF · arXiv
-
K. Zhang, W. Zuo, Y. Chen, D. Meng and L. Zhang, “Beyond a Gaussian Denoiser: Residual Learning of Deep CNN for Image Denoising,” IEEE Transactions on Image Processing, vol. 26, no. 7, pp. 3142–3155, 2017. doi · arXiv
-
Q. Huynh-Thu and M. Ghanbari, “Scope of Validity of PSNR in Image/Video Quality Assessment,” Electronics Letters, vol. 44, no. 13, pp. 800–801, 2008. doi
-
A. Abdelhamed, S. Lin and M. S. Brown, “A High-Quality Denoising Dataset for Smartphone Cameras,” in Proc. IEEE/CVF CVPR, 2018. CVF
-
S. Nah, T. H. Kim and K. M. Lee, “Deep Multi-Scale Convolutional Neural Network for Dynamic Scene Deblurring,” in Proc. IEEE CVPR, 2017. CVF
-
N. Shazeer, “GLU Variants Improve Transformer,” arXiv:2002.05202, 2020. arXiv
-
Y. N. Dauphin, A. Fan, M. Auli and D. Grangier, “Language Modeling with Gated Convolutional Networks,” in Proc. ICML, PMLR 70, 2017. PMLR
-
D. Hendrycks and K. Gimpel, “Gaussian Error Linear Units (GELUs),” arXiv:1606.08415, 2016. arXiv
-
W. Shi, J. Caballero, F. Huszár, J. Totz, A. P. Aitken, R. Bishop, D. Rueckert and Z. Wang, “Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network,” in Proc. IEEE CVPR, 2016. CVF · arXiv
-
A. Odena, V. Dumoulin and C. Olah, “Deconvolution and Checkerboard Artifacts,” Distill, vol. 1, no. 10, 2016. Distill · doi
-
A. Aitken, C. Ledig, L. Theis, J. Caballero, Z. Wang and W. Shi, “Checkerboard Artifact Free Sub-Pixel Convolution: A Note on Sub-Pixel Convolution, Resize Convolution and Convolution Resize,” arXiv:1707.02937, 2017. arXiv
-
O. Ronneberger, P. Fischer and T. Brox, “U-Net: Convolutional Networks for Biomedical Image Segmentation,” in Proc. Medical Image Computing and Computer-Assisted Intervention (MICCAI), LNCS, Springer, pp. 234–241, 2015. doi · arXiv
-
G. Huang, Z. Liu, L. van der Maaten and K. Q. Weinberger, “Densely Connected Convolutional Networks,” in Proc. IEEE CVPR, 2017. CVF
-
L.-C. Chen, G. Papandreou, I. Kokkinos, K. Murphy and A. L. Yuille, “DeepLab: Semantic Image Segmentation with Deep Convolutional Nets, Atrous Convolution, and Fully Connected CRFs,” IEEE Transactions on Pattern Analysis and Machine Intelligence, vol. 40, no. 4, pp. 834–848, 2018. doi · arXiv
-
W. Luo, Y. Li, R. Urtasun and R. Zemel, “Understanding the Effective Receptive Field in Deep Convolutional Neural Networks,” in Advances in Neural Information Processing Systems (NeurIPS), 2016. NeurIPS · arXiv
-
J. Kim, J. K. Lee and K. M. Lee, “Accurate Image Super-Resolution Using Very Deep Convolutional Networks,” in Proc. IEEE CVPR, 2016. CVF · arXiv
-
H. Guo, J. Li, T. Dai, Z. Ouyang, X. Ren and S.-T. Xia, “MambaIR: A Simple Baseline for Image Restoration with State-Space Model,” in Proc. ECCV 2024, LNCS, Springer, 2025. doi · arXiv
-
ITU-T, “Recommendation T.81: Information technology – Digital compression and coding of continuous-tone still images – Requirements and guidelines” (the JPEG standard), 1992. ITU
-
T. Elsken, J. H. Metzen and F. Hutter, “Neural Architecture Search: A Survey,” Journal of Machine Learning Research, vol. 20, 2019. JMLR · arXiv
-
A. Khan, A. Sohail, U. Zahoora and A. S. Qureshi, “A Survey of the Recent Architectures of Deep Convolutional Neural Networks,” Artificial Intelligence Review, vol. 53, no. 8, pp. 5455–5516, 2020. doi · arXiv
-
S. Khan, M. Naseer, M. Hayat, S. W. Zamir, F. S. Khan and M. Shah, “Transformers in Vision: A Survey,” ACM Computing Surveys, vol. 54, no. 10s, 2022. doi · arXiv
-
S. Woo, S. Debnath, R. Hu, X. Chen, Z. Liu, I. S. Kweon and S. Xie, “ConvNeXt V2: Co-Designing and Scaling ConvNets With Masked Autoencoders,” in Proc. IEEE/CVF CVPR, 2023. CVF · arXiv
-
M. Goldblum, H. Souri, R. Ni, M. Shu, V. Prabhu, G. Somepalli, P. Chattopadhyay, M. Ibrahim et al., “Battle of the Backbones: A Large-Scale Comparison of Pretrained Models across Computer Vision Tasks,” in Advances in Neural Information Processing Systems (NeurIPS), 2023. arXiv
-
X. Chen, X. Wang, J. Zhou, Y. Qiao and C. Dong, “Activating More Pixels in Image Super-Resolution Transformer,” in Proc. IEEE/CVF CVPR, 2023. CVF · arXiv
-
H. Guo, Y. Guo, Y. Zha, Y. Zhang, W. Li, T. Dai, S.-T. Xia and Y. Li, “MambaIRv2: Attentive State Space Restoration,” in Proc. IEEE/CVF CVPR, 2025. CVF · arXiv
-
A. Araujo, W. Norris and J. Sim, “Computing Receptive Fields of Convolutional Neural Networks,” Distill, vol. 4, no. 11, 2019. Distill · doi
-
X. Ding, X. Zhang, N. Ma, J. Han, G. Ding and J. Sun, “RepVGG: Making VGG-style ConvNets Great Again,” in Proc. IEEE/CVF CVPR, pp. 13733–13742, 2021. CVF paper
-
S. Zagoruyko and N. Komodakis, “DiracNets: Training Very Deep Neural Networks Without Skip-Connections,” arXiv:1706.00388, 2017. arXiv
-
X. Ding, Y. Guo, G. Ding and J. Han, “ACNet: Strengthening the Kernel Skeletons for Powerful CNN via Asymmetric Convolution Blocks,” in Proc. IEEE/CVF ICCV, 2019. CVF
-
H. Zhao, J. Shi, X. Qi, X. Wang and J. Jia, “Pyramid Scene Parsing Network,” in Proc. IEEE CVPR, 2017. CVF
-
M. Cordts, M. Omran, S. Ramos, T. Rehfeld, M. Enzweiler, R. Benenson, U. Franke, S. Roth and B. Schiele, “The Cityscapes Dataset for Semantic Urban Scene Understanding,” in Proc. IEEE CVPR, 2016. CVF
-
E. D. Cubuk, B. Zoph, D. Mane, V. Vasudevan and Q. V. Le, “AutoAugment: Learning Augmentation Strategies From Data,” in Proc. IEEE/CVF CVPR, 2019. CVF
-
H. Zhang, M. Cisse, Y. N. Dauphin and D. Lopez-Paz, “mixup: Beyond Empirical Risk Minimization,” in Proc. ICLR, 2018. arXiv