RepVGG: Making VGG-style ConvNets Great Again 是2021 CVPR的一篇论文,正如他的名字一样,应用structural re-parameterization的形式让类VGG的架构从新取得了最好的性能和更快的速度。在本文中首先对论文进行具体的介绍,而后再应用Pytorch复现RepVGG模型.

论文详解

1、多分支模型的问题

速度:

上图能够看到3×3 conv的实践计算密度大概是其余计算密度的4倍,这表明实践总FLOPs不是不同架构之间的理论速度的可比指标。例如,VGG-16比effentnet - b3大8.4×,但在1080Ti上运行却速度快1.8×。

在Inception的主动生成架构中,应用多个小的操作符,而不是几个大的操作符的多分支拓扑被宽泛采纳。

NASNet-A中的碎片化量为13,这对GPU等具备弱小并行计算能力的设施不敌对。

内存:

多分支的内存效率很低,因为每个分支的后果都须要保留到残差连贯或连贯为止,这会显著进步内存占用的峰值。上图显示,一个残差块的输出须要始终放弃到加法。假如块放弃feature map的大小,额定内存占用的峰值为为输出的2倍。

2、RepVGG

(a) ResNet:它在训练和推理过程中都失去了多路径拓扑,速度慢,内存效率低。

(b) RepVGG训练 :仅在训练时失去多路径拓扑。

(c) RepVGG推理:只在推理时失去单门路拓扑,推理工夫快。

对于多分支,ResNets胜利解释了这样的多分支架构使模型隐式地集成了许多较浅的模型。具体来说,当有n个块时,模型能够解释为2^n个模型的汇合,因为每个块都将流分支为两条门路。因为多分支拓扑在推理方面存在缺点,然而分支有利于训练,因而应用多分支来实现泛滥模型的集成只在训练时破费很多工夫。

repvgg应用相似于 identity层(尺寸匹配时,输出就是输入,不做操作)和1×1卷积,因而构建块的训练工夫信息流为y = x+g(x)+f(x),如上图的(b) 。所以模型变成了3^n个子模型的汇合,蕴含n个这样的块。

为一般推断工夫模型从新设置参数:

BN在每个分支中都在加法之前应用。

设大小为C2×C1×3×3的W(3)示意3×3 核,其C1输出通道和C2输入通道,而大小为C2×C1的W(1)示意1×1分支核

(3)、 (3)、(3)、(3)别离为3×3卷积后BN层的累积均值、标准差、学习尺度因子和偏差。

1×1 conv后的BN参数与(1)、 (1)、(1)、(1)类似,同分支的BN参数与(0)、(0)、(0)、(0)类似。

设M(1)的大小为N×C1×H1×W1, M(2)的大小为N×C2×H2×W2,别离为输出和输入,设*为卷积算子。

如果C1=C2, H1=H2, W1=W2,咱们失去:

式中bn为推理工夫bn函数:

BN与Conv合并:首先将每一个BN及其前一卷积层转换为带有偏置矢量的卷积。设{W ', b '}为转换后的核和偏置:

则推理时bn为:

所有分支合并:这种转换也实用于 identity分支,因为能够将 identity层视为1×1 conv,将单位矩阵作为核。在这些转换之后将领有一个3×3核、两个1×1内核和三个偏置向量。而后咱们将三个偏置向量相加,失去最终的偏置。最初是3×3核,将1×1核增加到3×3核的中心点上,这能够通过将两个1×1内核的零填充到3×3并将三个核相加来实现,如上图所示。

RepVGG架构如下

3×3层分为5个阶段,阶段的第一层则是stride= 2。为了进行图像分类,全局均匀合并后,而后将完连贯的层用作分类头。对于其余工作,特定于工作的部能够在任何一层产生的特色上应用(例如宰割、检测须要的多重特色)。

五个阶段别离具备1、2、4、14、1层,构建名称为RepVGG-B。

更深的RepVGG-B,在第2、3和4阶段中有2层。

也能够应用不同的a和b产生不同的变体。A用于缩放前四个阶段,而B用于最初阶段,然而要保障b> a。为了进一步缩小参数和计算量,采纳了interleave groupwise的3×3卷积层以换取效率。其中,RepVGG-A的第3、5、7、…、21层以及RepVGG-B额定的第23、25、27层设置组数g。为了简略起见,对于这些层,g被全局地设置为1、2或4,而没有进行分层调整。

3、试验后果

REPVGG-A0在准确性和速度方面比RESNET-18好1.25%和33%,REPVGGA1比RESNET-34好0.29%/64%,REPVGG-A2比Resnet-50好0.17%/83%。

通过分组层(g2/g4)的交织解决,RepVGG模型的速度进一步放慢,精度降落较为正当:RepVGG- b1g4比ResNet-101进步了0.37%/101%,RepVGGB1g2在精度雷同的状况下比ResNet-152进步了2.66倍。

尽管参数的数量不是次要问题,但能够看到以上所有的RepVGG模型都比ResNets更无效地利用参数。

与经典的VGG-16相比,RepVGG-B2的参数仅为58%,运行速度进步10%,准确率进步6.57%。

RepVGG模型在200个epoch的精度达到80%以上。RepVGG-A2比effecentnet - b0性能好1.37%/59%,RepVGG-B1比RegNetX-3.2GF性能好0.39%,运行速度也略快。

4、融化钻研

去除上图所示的这两个分支后,训练工夫模型进化为一般模型,准确率仅为72.39%。

应用仅应用1×1卷积和identity层精度都有所降落为 74.79%和73.15%

全功能RepVGGB0模型的准确率为75.14%,比一般一般模型高出2.75%。

宰割:

上图为应用批改后的PSPNET框架后果,批改后的PSPNET的运行速度比Resnet-50/101-backbone快得多。REPVGG 的backbone体现都优于Resnet-50和Resnet-101。

上面咱们开始应用Pytorch实现

Pytorch实现RepVGG

1、单与多分支模型

要实现RepVGG首先就要理解多分支,多分支就是其中输出通过不同的层,而后以某种形式汇总(通常是相加)。

论文中也提到了它使泛滥较浅模型的隐式汇合制作了多分支模型。更具体地说,该模型能够解释为2^n模型的汇合,因为每个块将流量分为两个门路。

多分支模型比单分支的模型更慢并且须要耗费更多的内存。咱们先创立一个经典的块来理解起因

import torchfrom torch import nn, Tensorfrom torchvision.ops import Conv2dNormActivationfrom typing import Dict, Listtorch.manual_seed(0)class ResNetBlock(nn.Module):    def __init__(self, in_channels: int, out_channels: int, stride: int = 1):        super().__init__()        self.weight = nn.Sequential(            Conv2dNormActivation(                in_channels, out_channels, kernel_size=3, stride=stride            ),            Conv2dNormActivation(                out_channels, out_channels, kernel_size=3, activation_layer=None            ),        )        self.shortcut = (            Conv2dNormActivation(                in_channels,                out_channels,                kernel_size=1,                stride=stride,                activation_layer=None,            )            if in_channels != out_channels            else nn.Identity()        )        self.act = nn.ReLU(inplace=True)    def forward(self, x):        res = self.shortcut(x)  # <- 2x memory        x = self.weight(x)        x += res        x = self.act(x)  # <- 1x memory        return x

存储残差会的有2倍的内存耗费。在上面的图像中,应用下面的图

多分支的构造仅在训练时才有用。因而,如果能够在预测工夫删除它,是能够改善模型速度和内存耗费的,咱们来看看代码怎么做:

2、从多分支到单分支

思考以下状况,有两个由两个3x3 Convs组成的分支

class TwoBranches(nn.Module):    def __init__(self, in_channels: int, out_channels: int):        super().__init__()        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3)        self.conv2 = nn.Conv2d(in_channels, out_channels, kernel_size=3)            def forward(self, x):        x1 = self.conv1(x)        x2 = self.conv2(x)        return x1 + x2

看看后果

two_branches = TwoBranches(8, 8)x = torch.randn((1, 8, 7, 7))two_branches(x).shapetorch.Size([1, 8, 5, 5])

当初,创立一个convv,咱们称其为“ conv_fused”,conv_fused(x) = conv1(x) + conv2(x)。咱们能够将两个卷积的权重和偏置求和,依据卷积的个性这是没问题的。

conv1 = two_branches.conv1conv2 = two_branches.conv2conv_fused = nn.Conv2d(conv1.in_channels, conv1.out_channels, kernel_size=conv1.kernel_size)conv_fused.weight = nn.Parameter(conv1.weight + conv2.weight)conv_fused.bias =  nn.Parameter(conv1.bias + conv2.bias)# check they give the same outputassert torch.allclose(two_branches(x), conv_fused(x), atol=1e-5)

让咱们对它的速度!

from time import perf_countertwo_branches.to("cuda")conv_fused.to("cuda")with torch.no_grad():    x = torch.randn((4, 8, 7, 7), device=torch.device("cuda"))        start = perf_counter()    two_branches(x)    print(f"conv1(x) + conv2(x) tooks {perf_counter() - start:.6f}s")        start = perf_counter()    conv_fused(x)    print(f"conv_fused(x) tooks {perf_counter() - start:.6f}s")

速度快了一倍

conv1(x) + conv2(x) tooks 0.000421sconv_fused(x) tooks 0.000215s

3、Fuse Conv和Batschorm

BATGNORM被用作卷积块之后层。论文中将它们交融在一起,即conv_fused(x) = batchnorm(conv(x))。

论文的2个公式解释这里截图在一起了,为了不便查看:

代码是这样的:

def get_fused_bn_to_conv_state_dict(    conv: nn.Conv2d, bn: nn.BatchNorm2d) -> Dict[str, Tensor]:    # in the paper, weights is gamma and bias is beta    bn_mean, bn_var, bn_gamma, bn_beta = (        bn.running_mean,        bn.running_var,        bn.weight,        bn.bias,    )    # we need the std!    bn_std = (bn_var + bn.eps).sqrt()    # eq (3)    conv_weight = nn.Parameter((bn_gamma / bn_std).reshape(-1, 1, 1, 1) * conv.weight)    # still eq (3)    conv_bias = nn.Parameter(bn_beta - bn_mean * bn_gamma / bn_std)    return {"weight": conv_weight, "bias": conv_bias}

让咱们看看它怎么工作:

conv_bn = nn.Sequential(    nn.Conv2d(8, 8, kernel_size=3, bias=False),    nn.BatchNorm2d(8))torch.nn.init.uniform_(conv_bn[1].weight)torch.nn.init.uniform_(conv_bn[1].bias)with torch.no_grad():    # be sure to switch to eval mode!!    conv_bn = conv_bn.eval()    conv_fused = nn.Conv2d(conv_bn[0].in_channels,                            conv_bn[0].out_channels,                            kernel_size=conv_bn[0].kernel_size)    conv_fused.load_state_dict(get_fused_bn_to_conv_state_dict(conv_bn[0], conv_bn[1]))    x = torch.randn((1, 8, 7, 7))        assert torch.allclose(conv_bn(x), conv_fused(x), atol=1e-5)

论文就是这样的形式交融了Conv2D和BatchRorm2D层。

其实能够看到论文的指标是一个:将整个模型交融成在一个繁多的数据流中(没有分支),使网络更快!

作者提出新的RepVgg块。与ResNet相似是有残差的,但通过identity层使其更快.

持续下面的图,pytorch的代码如下:

class RepVGGBlock(nn.Module):    def __init__(self, in_channels: int, out_channels: int, stride: int = 1):        super().__init__()        self.block = Conv2dNormActivation(            in_channels,            out_channels,            kernel_size=3,            padding=1,            bias=False,            stride=stride,            activation_layer=None,            # the original model may also have groups > 1        )        self.shortcut = Conv2dNormActivation(            in_channels,            out_channels,            kernel_size=1,            stride=stride,            activation_layer=None,        )        self.identity = (            nn.BatchNorm2d(out_channels) if in_channels == out_channels else None        )        self.relu = nn.ReLU(inplace=True)    def forward(self, x):        res = x  # <- 2x memory        x = self.block(x)        x += self.shortcut(res)        if self.identity:            x += self.identity(res)        x = self.relu(x)  # <- 1x memory        return x

4、参数的重塑

一个3x3 conv-> bn,一个1x1 conv-bn和(有时)一个batchnorm(identity分支)。要想将它们交融在一起,创立一个conv_fused,conv_fused

=

3x3conv-bn(x) + 1x1conv-bn(x) + bn(x),或者如果没有identity层,conv_fused

=

3x3conv-bn(x) + 1x1conv-bn(x)。

为了创立这个conv_fused,咱们须要做如下的操作:

  • 将3x3conv-bn(x)交融到一个3x3conv中
  • 1x1conv-bn(x),而后将其转换为3x3conv
  • 将identity的BN转换为3x3conv
  • 所有三个3x3convs相加

下图就是论文的总结:

第一步很容易,咱们能够在RepVGGBlock.block(主3x3 Conver-bn)上应用get_fused_bn_to_conv_state_dict。

第二步也相似的,在RepVGGBlock.shortcut上(1x1 cons-bn)应用get_fused_bn_to_conv_state_dict。这就是论文说的在每个维度上用1填充交融的1x1的核,造成一个3x3。

identity的bn比拟麻烦。论文的技巧(trick)是创立3x3 Conv来模仿identity,它将作为一个恒等函数,而后应用get_fused_bn_to_conv_state_dict将其与identity bn交融。还是通过在对应的内核核心为对应的通道的权重设置成1来实现。

Conv的权重是in_channels, out_channels, kernel_h, kernel_w。如果咱们要创立一个identity ,conv(x) = x,我只须要将权重设为1即可,代码如下:

with torch.no_grad():    x = torch.randn((1,2,3,3))    identity_conv = nn.Conv2d(2,2,kernel_size=3, padding=1, bias=False)    identity_conv.weight.zero_()    print(identity_conv.weight.shape)    in_channels = identity_conv.in_channels    for i in range(in_channels):        identity_conv.weight[i, i % in_channels, 1, 1] = 1    print(identity_conv.weight)        out = identity_conv(x)    assert torch.allclose(x, out)

后果

torch.Size([2, 2, 3, 3])Parameter containing:tensor([[[[0., 0., 0.],          [0., 1., 0.],          [0., 0., 0.]],         [[0., 0., 0.],          [0., 0., 0.],          [0., 0., 0.]]],        [[[0., 0., 0.],          [0., 0., 0.],          [0., 0., 0.]],         [[0., 0., 0.],          [0., 1., 0.],          [0., 0., 0.]]]], requires_grad=True)

咱们创立了一个Conv,它的作用就像一个恒等函数。把所有的货色放在一起,就是论文中的参数重塑。

def get_fused_conv_state_dict_from_block(block: RepVGGBlock) -> Dict[str, Tensor]:    fused_block_conv_state_dict = get_fused_bn_to_conv_state_dict(        block.block[0], block.block[1]    )    if block.shortcut:        # fuse the 1x1 shortcut        conv_1x1_state_dict = get_fused_bn_to_conv_state_dict(            block.shortcut[0], block.shortcut[1]        )        # we pad the 1x1 to a 3x3        conv_1x1_state_dict["weight"] = torch.nn.functional.pad(            conv_1x1_state_dict["weight"], [1, 1, 1, 1]        )        fused_block_conv_state_dict["weight"] += conv_1x1_state_dict["weight"]        fused_block_conv_state_dict["bias"] += conv_1x1_state_dict["bias"]    if block.identity:        # create our identity 3x3 conv kernel        identify_conv = nn.Conv2d(            block.block[0].in_channels,            block.block[0].in_channels,            kernel_size=3,            bias=True,            padding=1,        ).to(block.block[0].weight.device)        # set them to zero!        identify_conv.weight.zero_()        # set the middle element to zero for the right channel        in_channels = identify_conv.in_channels        for i in range(identify_conv.in_channels):            identify_conv.weight[i, i % in_channels, 1, 1] = 1        # fuse the 3x3 identity        identity_state_dict = get_fused_bn_to_conv_state_dict(            identify_conv, block.identity        )        fused_block_conv_state_dict["weight"] += identity_state_dict["weight"]        fused_block_conv_state_dict["bias"] += identity_state_dict["bias"]    fused_conv_state_dict = {        k: nn.Parameter(v) for k, v in fused_block_conv_state_dict.items()    }    return fused_conv_state_dict

最初定义一个RepVGGFastBlock。它只是由conv + relu组成

class RepVGGFastBlock(nn.Sequential):    def __init__(self, in_channels: int, out_channels: int, stride: int = 1):        super().__init__()        self.conv = nn.Conv2d(            in_channels, out_channels, kernel_size=3, stride=stride, padding=1        )        self.relu = nn.ReLU(inplace=True)

并在RepVGGBlock中增加to_fast办法来疾速创立RepVGGFastBlock

class RepVGGBlock(nn.Module):    def __init__(self, in_channels: int, out_channels: int, stride: int = 1):        super().__init__()        self.block = Conv2dNormActivation(            in_channels,            out_channels,            kernel_size=3,            padding=1,            bias=False,            stride=stride,            activation_layer=None,            # the original model may also have groups > 1        )        self.shortcut = Conv2dNormActivation(            in_channels,            out_channels,            kernel_size=1,            stride=stride,            activation_layer=None,        )        self.identity = (            nn.BatchNorm2d(out_channels) if in_channels == out_channels else None        )        self.relu = nn.ReLU(inplace=True)    def forward(self, x):        res = x  # <- 2x memory        x = self.block(x)        x += self.shortcut(res)        if self.identity:            x += self.identity(res)        x = self.relu(x)  # <- 1x memory        return x    def to_fast(self) -> RepVGGFastBlock:        fused_conv_state_dict = get_fused_conv_state_dict_from_block(self)        fast_block = RepVGGFastBlock(            self.block[0].in_channels,            self.block[0].out_channels,            stride=self.block[0].stride,        )        fast_block.conv.load_state_dict(fused_conv_state_dict)        return fast_block

5、RepVGG

switch_to_fast办法来定义RepVGGStage(块的汇合)和RepVGG:

class RepVGGStage(nn.Sequential):    def __init__(        self,        in_channels: int,        out_channels: int,        depth: int,    ):        super().__init__(            RepVGGBlock(in_channels, out_channels, stride=2),            *[RepVGGBlock(out_channels, out_channels) for _ in range(depth - 1)],        )class RepVGG(nn.Sequential):    def __init__(self, widths: List[int], depths: List[int], in_channels: int = 3):        super().__init__()        in_out_channels = zip(widths, widths[1:])        self.stages = nn.Sequential(            RepVGGStage(in_channels, widths[0], depth=1),            *[                RepVGGStage(in_channels, out_channels, depth)                for (in_channels, out_channels), depth in zip(in_out_channels, depths)            ],        )        # omit classification head for simplicity    def switch_to_fast(self):        for stage in self.stages:            for i, block in enumerate(stage):                stage[i] = block.to_fast()        return self

这样就实现了,上面咱们看看测试

6、模型测试

benchmark.py中曾经创立了一个基准,在gtx 1080ti上运行不同批处理大小的模型,这是后果:

模型每个阶段有两层,四个阶段,宽度为64,128,256,512。

在他们的论文中,他们将这些值按肯定的比例缩放(称为a和b),并应用分组卷积。因为对从新参数化局部更感兴趣,所以这里跳过了,因为这是一个调参的过程,能够应用超参数搜寻的办法得出。

基本上重塑参数的模型与一般模型相比在不同的时间尺度上晋升的还是很显著的

能够看到,对于batch_size=128,默认模型(多分支)占用1.45秒,而参数化模型(疾速)只占用0.0134秒。即108倍的晋升

总结

在本文中,首先具体的介绍了RepVGG的论文,而后逐渐理解了如何创立RepVGG,并且着重介绍了重塑权重的办法,并且用Pytorch复现了论文的模型,RepVGG这种重塑权重技术其实就是应用了过河拆桥的办法,白嫖了多分支的性能,并且还可能晋升,你说气不气人。这种“白嫖”的技术也能够移植到其余架构中。

代码在这里:

https://avoid.overfit.cn/post/f9263685607b40df80e5c4f949a28b42
谢谢浏览!