Deformable Convolutional Networks
(可变性卷积)
大致介绍
作者在文章中提出了两个东西,第一个是可变性卷积,这个卷积结构可以根据物体的现状来改变卷积核,从而实现有针对性的卷积采样。第二个是可变性的ROI的池化层,这个也是利用改变采样形状的思想来完成的。本文的重点是可变性卷积,因为它的适用范围更为广阔。
可变性卷积
首先需要把握一个东西的概念,才能更好地把握重点。
可变性卷积中的可变性指的是卷积核的采样位置是可变性。就好比空洞卷积与普通卷积相比实在每个采样位置中间隔了一个空格来进行采样。而可变性卷积与空洞卷积相比,空洞卷积的采样是有规律的参数是相对固定的,而可变性卷积的采样位置则是借鉴了注意力机制的思想,需要网络自己去学习应该如何改变采样位置。
如上图所示,(a)是普通的卷积的采样方式,可以发现每个格子都被填满了,采样的形状是矩形。
(b),©,(d)则都是可变性卷积。可以观察到,可变性卷积的采样变化是自由度很大的,可以实现规律的采样类似空洞卷积,还能旋转进行采样,还在无序进行采样。
可变性卷积结构
了解大致结构之后,接下来就是观察作者如何实现的
整个可变性卷积的结构如下图
上图中,以3x3卷积为例子,首先需要先计算那些地方是需要进行采样的,所有先进行上面那一条分支的计算。输入的特征图经过一系列操作之后得到特征矩阵offsets。offsets是指导卷积核上的采样点如何移动的矩阵
可变性卷积的两个步骤:
- 使用正常采样矩阵R进行正常采样;
- 采样值的总和,以w加权; 定义感受野的大小和扩张。
那么搞清楚可变性卷积是什么了之后,我们需要知道这个东西可以放在backbone的哪个部位。
作者针对放在哪个位置设计了实验,所有的卷积核替换原来resnet中的3x3的卷积
在上述实验中几乎所有的可变性卷积都是加在最后几层里。
因此如果我们的网络需要使用可变性卷积则考虑加在最后几层效果比较好。
可变性卷积的代码
from torch.autograd import Variable, Function
import torch
from torch import nn
import numpy as np
class DeformConv2D(nn.Module):
def __init__(self, inc, outc, kernel_size=3, padding=1, bias=None):
super(DeformConv2D, self).__init__()
self.kernel_size = kernel_size
self.padding = padding
self.zero_padding = nn.ZeroPad2d(padding)
self.conv_kernel = nn.Conv2d(inc, outc, kernel_size=kernel_size, stride=kernel_size, bias=bias)
def forward(self, x, offset):
dtype = offset.data.type()
ks = self.kernel_size
N = offset.size(1) // 2
# Change offset's order from [x1, x2, ..., y1, y2, ...] to [x1, y1, x2, y2, ...]
# Codes below are written to make sure same results of MXNet implementation.
# You can remove them, and it won't influence the module's performance.
offsets_index = Variable(torch.cat([torch.arange(0, 2*N, 2), torch.arange(1, 2*N+1, 2)]), requires_grad=False).type_as(x).long()
offsets_index = offsets_index.unsqueeze(dim=0).unsqueeze(dim=-1).unsqueeze(dim=-1).expand(*offset.size())
offset = torch.gather(offset, dim=1, index=offsets_index)
# ------------------------------------------------------------------------
if self.padding:
x = self.zero_padding(x)
# (b, 2N, h, w)
p = self._get_p(offset, dtype)
# (b, h, w, 2N)
p = p.contiguous().permute(0, 2, 3, 1)
q_lt = Variable(p.data, requires_grad=False).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:]], -1)
q_rt = torch.cat([q_rb[..., :N], q_lt[..., N:]], -1)
# (b, h, w, N)
mask = torch.cat([p[..., :N].lt(self.padding)+p[..., :N].gt(x.size(2)-1-self.padding),
p[..., N:].lt(self.padding)+p[..., N:].gt(x.size(3)-1-self.padding)], dim=-1).type_as(p)
mask = mask.detach()
floor_p = p - (p - torch.floor(p))
p = p*(1-mask) + floor_p*mask
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:]))
# (b, c, h, w, N)
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)
# (b, c, h, w, N)
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, ks)
out = self.conv_kernel(x_offset)
return out
def _get_p_n(self, N, dtype):
p_n_x, p_n_y = np.meshgrid(range(-(self.kernel_size-1)//2, (self.kernel_size-1)//2+1),
range(-(self.kernel_size-1)//2, (self.kernel_size-1)//2+1), indexing='ij')
# (2N, 1)
p_n = np.concatenate((p_n_x.flatten(), p_n_y.flatten()))
p_n = np.reshape(p_n, (1, 2*N, 1, 1))
p_n = Variable(torch.from_numpy(p_n).type(dtype), requires_grad=False)
return p_n
@staticmethod
def _get_p_0(h, w, N, dtype):
p_0_x, p_0_y = np.meshgrid(range(1, h+1), range(1, w+1), indexing='ij')
p_0_x = p_0_x.flatten().reshape(1, 1, h, w).repeat(N, axis=1)
p_0_y = p_0_y.flatten().reshape(1, 1, h, w).repeat(N, axis=1)
p_0 = np.concatenate((p_0_x, p_0_y), axis=1)
p_0 = Variable(torch.from_numpy(p_0).type(dtype), requires_grad=False)
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
@staticmethod
def _reshape_x_offset(x_offset, ks):
b, c, h, w, N = x_offset.size()
x_offset = torch.cat([x_offset[..., s:s+ks].contiguous().view(b, c, h, w*ks) for s in range(0, N, ks)], dim=-1)
x_offset = x_offset.contiguous().view(b, c, h*ks, w*ks)
return x_offset