yolo系列文章目录
文章目录
一、可变形卷积是什么?
可变形卷积即DCN(缩写取自Deformable ConvNets)提出于ICCV 2017的paper:Deformable Convolutional Networks
codebase地址:(很多框架中都已实现,这里选择以pytorch的为例)https://github.com/4uiiurz1/pytorch-deform-conv-v2/blob/master/deform_conv_v2.py
DCN原文链接
代码链接
文章创新点(贡献):
本文提出了两个新模块:可变形卷积和可变形RoI池化
新模块可以很容易地取代现有CNN中的普通模块,并且可以通过标准反向传播轻松地进行端到端训练。
DCN目前也出到了v2,值得一提的是,DCN的思维也算一种可学习的自适应模块,跟注意力机制模块BAM/CBAM的思路有点像。
mmdetection里也有相关实现,可轻松移植进自己的项目,DCN对于大多数检测场景尤其是比赛都是有用的
二、使用步骤
1.在models/common.py文件添加
bash
class DCNv2(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=1, groups=1, act=True, dilation=1, deformable_groups=1):
super(DCNv2, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = (kernel_size, kernel_size)
self.stride = (stride, stride)
self.padding = (autopad(kernel_size, padding), autopad(kernel_size, padding))
self.dilation = (dilation, dilation)
self.groups = groups
self.deformable_groups = deformable_groups
self.weight = nn.Parameter(
torch.empty(out_channels, in_channels, *self.kernel_size)
)
self.bias = nn.Parameter(torch.empty(out_channels))
out_channels_offset_mask = (self.deformable_groups * 3 *
self.kernel_size[0] * self.kernel_size[1])
self.conv_offset_mask = nn.Conv2d(
self.in_channels,
out_channels_offset_mask,
kernel_size=self.kernel_size,
stride=self.stride,
padding=self.padding,
bias=True,
)
self.bn = nn.BatchNorm2d(out_channels)
self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
self.reset_parameters()
def forward(self, x):
offset_mask = self.conv_offset_mask(x)
o1, o2, mask = torch.chunk(offset_mask, 3, dim=1)
offset = torch.cat((o1, o2), dim=1)
mask = torch.sigmoid(mask)
x = torch.ops.torchvision.deform_conv2d(
x,
self.weight,
offset,
mask,
self.bias,
self.stride[0], self.stride[1],
self.padding[0], self.padding[1],
self.dilation[0], self.dilation[1],
self.groups,
self.deformable_groups,
True
)
x = self.bn(x)
x = self.act(x)
return x
def reset_parameters(self):
n = self.in_channels
for k in self.kernel_size:
n *= k
std = 1. / math.sqrt(n)
self.weight.data.uniform_(-std, std)
self.bias.data.zero_()
self.conv_offset_mask.weight.data.zero_()
self.conv_offset_mask.bias.data.zero_()
2.然后再yolo.py里面添加DCNv2
3.修改yolov7的yaml
添加到主干,只需要修改卷积核为3的,为1的没有必要修改,
总结
一般可变形卷积是添加到主干网上,如果想添加到head部分,自行尝试。
参考文章
[yolov5修改]在yolov5中加入可变形卷积模块
【YOLOv8/YOLOv7/YOLOv5/YOLOv4/Faster-rcnn系列算法改进NO.57】引入可形变卷积
可变形卷积:Deformable ConvNets