赞
踩
本文介绍AK卷积,传统的卷积有2个缺陷:
1、卷积运算在固定大小的窗口运行、无法捕获其他窗口的信息,并且窗口的形状是固定的;
2、卷积核的尺寸固定为,窗口大小固定为k,随着k增加,参数会快速增加。
针对传统卷积的缺陷,作者提出了AK卷积,AK卷积拥有任意形状和任意的参数。作者在yolov5n和yolov8n上进行了测试,效果非常好。
论文地址:AKConv: Convolutional Kernel with Arbitrary Sampled Shapes and Arbitrary Number of Parameters
代码:https://github.com/cv-zhangxin/akconv
前面已经提到了传统卷积的2个缺陷,那么AKConv是怎么做的呢?
标准卷积是的矩形,而可变形卷积(Deformable Conv)是可以调整形状的,类似可变形卷积,AKConv也会学习偏移量,来改变卷积核的形状,如下图所示。
N是AKConv卷积参数的数量,特征图经过卷积运算得到卷积的位置偏移量,然后进行卷积运算,和可变形卷积一样。
可变形卷积可以通过学习偏移量改变卷积计算的位置,从而使得卷积核的形状不固定,但是可变形卷积有个缺陷:卷积核参数是固定的。(比如1,9, 27...)
AKConv的另一个特点就是参数数量是任意的(可以设置为1,2,3,4,5...任意值),如下图,这点是和传统卷积不一样的,摆脱了的参数限制。
除了参数数量可以任意选择,初始的卷积核形状也是可以任意选择,下图为5个卷积参数时,卷积核的初始形状设计方案。
AKConv是对可变形卷积的巨大改进,他的性能也是非常好的,在yolov5n上添加AKConv,可以看到在COCO2017数据集上的表现非常亮眼:
不同的卷积形状在yoloV8的测试(COCO2017数据集):
关于实验可以参考原论文,不多赘述。
官方的代码已经给出了在v5/7/8上的配置文件和代码,这里给出其核心代码,配置文件见源码:
- import math
- import torch.nn.functional as F
- from .conv import Conv
- import einops
-
- class AKConv(nn.Module):
- def __init__(self, inc, outc, num_param, stride=1, bias=None):
- super(AKConv, self).__init__()
- self.num_param = num_param
- self.stride = stride
- self.conv = nn.Sequential(nn.Conv2d(inc, outc, kernel_size=(num_param, 1), stride=(num_param, 1), bias=bias),nn.BatchNorm2d(outc),nn.SiLU()) # the conv adds the BN and SiLU to compare original Conv in YOLOv5.
- self.p_conv = nn.Conv2d(inc, 2 * num_param, kernel_size=3, padding=1, stride=stride)
- nn.init.constant_(self.p_conv.weight, 0)
- self.p_conv.register_full_backward_hook(self._set_lr)
-
- @staticmethod
- def _set_lr(module, grad_input, grad_output):
- grad_input = (grad_input[i] * 0.1 for i in range(len(grad_input)))
- grad_output = (grad_output[i] * 0.1 for i in range(len(grad_output)))
-
- def forward(self, x):
- # N is num_param.
- offset = self.p_conv(x)
- dtype = offset.data.type()
- N = offset.size(1) // 2
- # (b, 2N, h, w)
- p = self._get_p(offset, dtype)
-
- # (b, h, w, 2N)
- p = p.contiguous().permute(0, 2, 3, 1)
- q_lt = p.detach().floor()
- q_rb = q_lt + 1
-
- q_lt = torch.cat([torch.clamp(q_lt[..., :N], 0, x.size(2) - 1), torch.clamp(q_lt[..., N:], 0, x.size(3) - 1)],
- dim=-1).long()
- q_rb = torch.cat([torch.clamp(q_rb[..., :N], 0, x.size(2) - 1), torch.clamp(q_rb[..., N:], 0, x.size(3) - 1)],
- dim=-1).long()
- q_lb = torch.cat([q_lt[..., :N], q_rb[..., N:]], dim=-1)
- q_rt = torch.cat([q_rb[..., :N], q_lt[..., N:]], dim=-1)
-
- # clip p
- p = torch.cat([torch.clamp(p[..., :N], 0, x.size(2) - 1), torch.clamp(p[..., N:], 0, x.size(3) - 1)], dim=-1)
-
- # bilinear kernel (b, h, w, N)
- g_lt = (1 + (q_lt[..., :N].type_as(p) - p[..., :N])) * (1 + (q_lt[..., N:].type_as(p) - p[..., N:]))
- g_rb = (1 - (q_rb[..., :N].type_as(p) - p[..., :N])) * (1 - (q_rb[..., N:].type_as(p) - p[..., N:]))
- g_lb = (1 + (q_lb[..., :N].type_as(p) - p[..., :N])) * (1 - (q_lb[..., N:].type_as(p) - p[..., N:]))
- g_rt = (1 - (q_rt[..., :N].type_as(p) - p[..., :N])) * (1 + (q_rt[..., N:].type_as(p) - p[..., N:]))
-
- # resampling the features based on the modified coordinates.
- x_q_lt = self._get_x_q(x, q_lt, N)
- x_q_rb = self._get_x_q(x, q_rb, N)
- x_q_lb = self._get_x_q(x, q_lb, N)
- x_q_rt = self._get_x_q(x, q_rt, N)
-
- # bilinear
- x_offset = g_lt.unsqueeze(dim=1) * x_q_lt + \
- g_rb.unsqueeze(dim=1) * x_q_rb + \
- g_lb.unsqueeze(dim=1) * x_q_lb + \
- g_rt.unsqueeze(dim=1) * x_q_rt
-
- x_offset = self._reshape_x_offset(x_offset, self.num_param)
- out = self.conv(x_offset)
-
- return out
-
- # generating the inital sampled shapes for the AKConv with different sizes.
- def _get_p_n(self, N, dtype):
- base_int = round(math.sqrt(self.num_param))
- row_number = self.num_param // base_int
- mod_number = self.num_param % base_int
- p_n_x,p_n_y = torch.meshgrid(
- torch.arange(0, row_number),
- torch.arange(0,base_int))
- p_n_x = torch.flatten(p_n_x)
- p_n_y = torch.flatten(p_n_y)
- if mod_number > 0:
- mod_p_n_x,mod_p_n_y = torch.meshgrid(
- torch.arange(row_number,row_number+1),
- torch.arange(0,mod_number))
-
- mod_p_n_x = torch.flatten(mod_p_n_x)
- mod_p_n_y = torch.flatten(mod_p_n_y)
- p_n_x,p_n_y = torch.cat((p_n_x,mod_p_n_x)),torch.cat((p_n_y,mod_p_n_y))
- p_n = torch.cat([p_n_x,p_n_y], 0)
- p_n = p_n.view(1, 2 * N, 1, 1).type(dtype)
- return p_n
-
- # no zero-padding
- def _get_p_0(self, h, w, N, dtype):
- p_0_x, p_0_y = torch.meshgrid(
- torch.arange(0, h * self.stride, self.stride),
- torch.arange(0, w * self.stride, self.stride))
-
- p_0_x = torch.flatten(p_0_x).view(1, 1, h, w).repeat(1, N, 1, 1)
- p_0_y = torch.flatten(p_0_y).view(1, 1, h, w).repeat(1, N, 1, 1)
- p_0 = torch.cat([p_0_x, p_0_y], 1).type(dtype)
-
- return p_0
-
- def _get_p(self, offset, dtype):
- N, h, w = offset.size(1) // 2, offset.size(2), offset.size(3)
-
- # (1, 2N, 1, 1)
- p_n = self._get_p_n(N, dtype)
- # (1, 2N, h, w)
- p_0 = self._get_p_0(h, w, N, dtype)
- p = p_0 + p_n + offset
- return p
-
- def _get_x_q(self, x, q, N):
- b, h, w, _ = q.size()
- padded_w = x.size(3)
- c = x.size(1)
- # (b, c, h*w)
- x = x.contiguous().view(b, c, -1)
-
- # (b, h, w, N)
- index = q[..., :N] * padded_w + q[..., N:] # offset_x*w + offset_y
- # (b, c, h*w*N)
- index = index.contiguous().unsqueeze(dim=1).expand(-1, c, -1, -1, -1).contiguous().view(b, c, -1)
-
- x_offset = x.gather(dim=-1, index=index).contiguous().view(b, c, h, w, N)
-
- return x_offset
-
-
- # Stacking resampled features in the row direction.
- @staticmethod
- def _reshape_x_offset(x_offset, num_param):
- b, c, h, w, n = x_offset.size()
- # using Conv3d
- # x_offset = x_offset.permute(0,1,4,2,3), then Conv3d(c,c_out, kernel_size =(num_param,1,1),stride=(num_param,1,1),bias= False)
- # using 1 × 1 Conv
- # x_offset = x_offset.permute(0,1,4,2,3), then, x_offset.view(b,c×num_param,h,w) finally, Conv2d(c×num_param,c_out, kernel_size =1,stride=1,bias= False)
- # using the column conv as follow, then, Conv2d(inc, outc, kernel_size=(num_param, 1), stride=(num_param, 1), bias=bias)
-
- x_offset = rearrange(x_offset, 'b c h w n -> b c (h n) w')
- return x_offset
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。