一、DCNv2简介
DCNv2是一种用于目标检测的网络结构,它的全称是deformable convolutional networks version 2,是对版本1进行改进后的产物。它主要解决的问题是传统卷积神经网络中的固定卷积核不能适应特定目标的形变,而DCNv2可以适应目标的形变,进而提高检测精度。它由卷积层、变形卷积层、group normalization(GN)和激活函数ReLU组成。
二、DCNv2与DCNv1的比较
DCNv2是基于DCNv1进行改进后的产物,主要的改进点在于引入了两个重要的模块:deformable convolutional offset(DCO)和modulated deformable convolutional offset(MDCO),进一步提升了模型的性能。相对于DCNv1,DCNv2在目标检测任务中有以下优势:
1、更少的计算量:DCNv2使用了轻量化的DCO和MDCO,可以减少计算量,提升速度;
2、更好的鲁棒性:DCNv2引入了GN层,使其对噪声和变化更加鲁棒;
3、更高的精度:DCNv2可以自适应目标形变,能够提高检测精度;
三、DCNv2的代码实现
import torch.nn as nn
import torch.nn.functional as F
from mmcv.cnn import kaiming_init, xavier_init, normal_init
from ..registry import CONV_LAYERS
from ..utils import bias_init_with_prob, constant_init, ConvModule
from ..weight_init import init_factorized_weight
@CONV_LAYERS.register_module
class DeformConv2dPack(ModulatedDeformConvPack):
"""A Deformable Conv Encapsulation that acts as normal conv layers.
Convs following this layer are not deformable and offsets are ignored.
"""
def __init__(self, *args, **kwargs):
super(DeformConv2dPack, self).__init__(*args, **kwargs)
def forward(self, x):
offset_mask = self.offset_mask.weight if self.with_mask else self.offset.weight
return F.conv2d(
x, self.weight, offset_mask, stride=self.stride,
padding=self.padding, dilation=self.dilation, groups=self.groups * self.deform_groups
)
class DCNv2(nn.Module):
"""Deformable Convolutional Networks with deformable group convolution.
https://arxiv.org/abs/1811.11168.
Args:
num_classes(int): the number of classes for classification.
conv_cfg(dict): dictionary to construct and config conv layer.
norm_cfg(dict): dictionary to construct and config norm layer.
backbone(nn.Module): backbone network used in the detector.
rpn_head(nn.Module): `rpn_head` for RPN targets generation.
bbox_roi_extractor(nn.Module): the feature extractor for bbox head.
bbox_head(nn.Module): head to predict bbox and cls.
train_cfg(dict): training config.
test_cfg(dict): testing config.
"""
def __init__(self,
num_classes,
conv_cfg,
norm_cfg,
backbone,
rpn_head,
bbox_roi_extractor,
bbox_head,
train_cfg=None,
test_cfg=None):
super(DCNv2, self).__init__()
self.backbone = nn.ModuleList(backbone)
self.rpn_head = nn.ModuleList(rpn_head)
self.train_cfg = train_cfg
self.test_cfg = test_cfg
def forward_train(self,
img,
img_metas,
gt_bboxes,
gt_labels,
gt_bboxes_ignore=None,
proposal_cfg=None):
x = self.extract_feat(img)
proposal_list = self.rpn_head(x)
rpn_losses = self.rpn_head.loss(proposal_list, gt_bboxes, gt_labels,
img_metas, proposal_cfg)
proposal_list = torch.cat(proposal_list, dim=0)
if self.with_bbox or self.with_mask:
rois = bbox2roi(proposal_list)
bbox_results = self._bbox_forward(x, rois)
else:
bbox_results = {}
losses = dict()
losses.update(rpn_losses)
losses.update(bbox_results['loss_bbox'])
if self.with_mask:
losses.update(bbox_results['loss_mask'])
return losses
def forward_test(self, img, img_metas, **kwargs):
x = self.extract_feat(img)
proposal_list = self.rpn_head(x)
proposal_list = torch.cat(proposal_list, dim=0)
if self.with_bbox or self.with_mask:
rois = bbox2roi(proposal_list)
bbox_results = self._bbox_forward(x, rois)
else:
bbox_results = {}
return bbox_results
四、DCNv2应用场景
DCNv2主要用于目标检测任务,尤其是在对形变较大的目标进行检测时比较有效。可以在海康威视的人脸识别、车辆识别等系统中应用。同时也可以通过微调等方法将其应用于其他图片分类任务。
五、总结
DCNv2是一种创新性的卷积神经网络结构,其引入的deformable convolutional offset和modulated deformable convolutional offset模块可以自适应目标形变,能够提高目标检测精度。在实际应用中,它可以用于对形变较大的目标进行检测,具有较好的鲁棒性和精度。在未来,我们可以将其应用于更多的计算机视觉任务中。
