摘要
REFCONV是一种用于构建强大卷积神经网络的重新参数化的重新聚焦卷积(re-parameterized refocusing convolution)。它是一种特殊的卷积层,旨在通过重新参数化输入特征图来增强卷积神经网络的能力。
在传统的卷积神经网络中,卷积层通过将输入特征图与一组卷积核(filters)进行卷积运算,以检测和提取输入数据中的局部特征。然而,这种传统的卷积方式可能会在处理具有复杂结构和多样性的数据时遇到一些挑战。
链接:
https://jingjing.blog.csdn.net/article/details/134696974?spm=1001.2014.3001.5502
REFCONV通过重新参数化输入特征图来解决这些问题。它通过引入一个额外的可学习的参数化矩阵,将输入特征图重新参数化为一个新的特征图。这个新的特征图可以更好地适应卷积核的检测能力,从而增强网络对复杂和多样性的数据处理能力。
REFCONV的另一个重要特点是它能够重新聚焦卷积核的关注点。传统的卷积核通常只能捕捉到输入特征图的局部信息,而REFCONV通过重新参数化输入特征图,使得卷积核能够关注到更广泛的区域,从而更全面地捕捉输入数据的特征。
我们使用RefConv替换YoloV7中的卷积,既能提高精度,又能降低运算量,使得模型更加轻量化!
class RepConv(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, padding=None, groups=1,
map_k=3):
super(RepConv, self).__init__()
assert map_k <= kernel_size
self.origin_kernel_shape = (out_channels, in_channels // groups, kernel_size, kernel_size)
self.register_buffer('weight', torch.zeros(*self.origin_kernel_shape))
G = in_channels * out_channels // (groups ** 2)
self.num_2d_kernels = out_channels * in_channels // groups
self.kernel_size = kernel_size
self.convmap = nn.Conv2d(in_channels=self.num_2d_kernels,
out_channels=self.num_2d_kernels, kernel_size=map_k, stride=1, padding=map_k // 2,
groups=G, bias=False)
#nn.init.zeros_(self.convmap.weight)
self.bias = None#nn.Parameter(torch.zeros(out_channels), requires_grad=True) # must have a bias for identical initialization
self.stride = stride
self.groups = groups
if padding is None:
padding = kernel_size // 2
self.padding = padding
def forward(self, inputs):
origin_weight = self.weight.view(1, self.num_2d_kernels, self.kernel_size, self.kernel_size)
kernel = self.weight + self.convmap(origin_weight).view(*self.origin_kernel_shape)
return F.conv2d(inputs, kernel, stride=self.stride, padding=self.padding, dilation=1, groups=self.groups, bias=self.bias)
def conv_bn(inp, oup, stride, conv_layer=nn.Conv2d, norm_layer=nn.BatchNorm2d, nlin_layer=nn.ReLU):
return nn.Sequential(
RepConv(inp, oup, kernel_size=3, stride=stride, padding=None, groups=1, map_k=3),
#conv_layer(inp, oup, 3, stride, 1, bias=False),
norm_layer(oup),
nlin_layer(inplace=True)
)
Class Images Labels P R mAP@.5 mAP@.5:.95: 100%|██████████| 15/15 [00:02<00:00, 5.42it/s]
all 229 1407 0.979 0.977 0.993 0.736
c17 229 131 0.99 1 0.996 0.841
c5 229 68 0.962 1 0.996 0.839
helicopter 229 43 0.964 1 0.988 0.636
c130 229 85 1 0.997 0.997 0.671
f16 229 57 1 0.966 0.996 0.697
b2 229 2 0.948 1 0.995 0.647
other 229 86 1 0.914 0.994 0.562
b52 229 65 0.985 0.969 0.98 0.834
kc10 229 62 1 0.974 0.986 0.823
command 229 40 0.998 1 0.996 0.798
f15 229 123 1 0.979 0.997 0.659
kc135 229 91 0.988 0.989 0.987 0.679
a10 229 27 1 0.599 0.989 0.425
b1 229 20 0.994 1 0.997 0.741
aew 229 25 0.951 1 0.97 0.769
f22 229 17 0.993 1 0.996 0.742
p3 229 105 1 1 0.998 0.788
p8 229 1 0.921 1 0.995 0.498
f35 229 32 1 0.998 0.996 0.62
f18 229 125 0.987 0.992 0.995 0.816
v22 229 41 0.998 1 0.996 0.719
su-27 229 31 0.996 1 0.996 0.84
il-38 229 27 0.994 1 0.995 0.878
tu-134 229 1 0.866 1 0.995 0.995
su-33 229 2 0.965 1 0.995 0.647
an-70 229 2 0.94 1 0.995 0.896
tu-22 229 98 0.999 1 0.997 0.817
Class Images Labels P R mAP@.5 mAP@.5:.95: 100%|██████████| 15/15 [00:03<00:00, 4.97it/s]
all 229 1407 0.948 0.976 0.993 0.734
c17 229 131 0.97 1 0.996 0.818
c5 229 68 0.924 1 0.996 0.847
helicopter 229 43 0.92 1 0.987 0.645
c130 229 85 0.984 1 0.997 0.677
f16 229 57 0.977 0.965 0.994 0.695
b2 229 2 0.869 1 0.995 0.796
other 229 86 0.993 0.953 0.994 0.56
b52 229 65 0.981 0.969 0.983 0.826
kc10 229 62 0.978 0.984 0.986 0.845
command 229 40 0.989 1 0.997 0.838
f15 229 123 0.987 1 0.997 0.656
kc135 229 91 0.976 0.989 0.984 0.696
a10 229 27 0.989 1 0.996 0.5
b1 229 20 0.977 1 0.996 0.696
aew 229 25 0.939 1 0.959 0.739
f22 229 17 0.95 1 0.996 0.75
p3 229 105 0.979 1 0.998 0.791
p8 229 1 0.778 1 0.995 0.498
f35 229 32 0.986 1 0.996 0.594
f18 229 125 0.968 0.992 0.994 0.81
v22 229 41 0.989 1 0.996 0.676
su-27 229 31 0.954 1 0.996 0.808
il-38 229 27 0.982 1 0.996 0.842
tu-134 229 1 0.748 1 0.995 0.896
su-33 229 2 1 0.5 0.995 0.647
an-70 229 2 0.813 1 0.995 0.846
tu-22 229 98 0.985 1 0.997 0.818