张芷铭的个人博客

Deformable 卷积通过学习采样点偏移量,使感受野自适应调整,适应几何变形。

基本原理

传统卷积采样点固定,Deformable 卷积引入可学习的偏移量 $(\Delta x, \Delta y)$:

$$p = (x + \Delta x, y + \Delta y)$$

采样位置根据输入内容动态调整。

工作流程

  1. 偏移量学习:额外卷积层生成偏移量
  2. 重新采样:按偏移量从输入特征图采样
  3. 卷积计算:用新采样点进行卷积

优点

优点说明
灵活感受野根据局部结构动态调整
几何适应处理形变、旋转、尺度变化
丰富上下文提取更多细节信息
减少冗余计算仅在重要位置采样

应用场景

任务作用
目标检测适应不同尺度、形态目标
语义分割精细处理复杂边界
姿态估计捕捉姿态变化
视频分析处理动态场景

PyTorch 实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import torch.nn as nn

class DeformableConv2d(nn.Module):
    def __init__(self, in_ch, out_ch, kernel_size):
        super().__init__()
        self.offset_conv = nn.Conv2d(
            in_ch, 2 * kernel_size[0] * kernel_size[1],
            kernel_size, padding=kernel_size[0]//2
        )
        self.conv = nn.Conv2d(in_ch, out_ch, kernel_size, padding=kernel_size[0]//2)

    def forward(self, x):
        offset = self.offset_conv(x)
        x_offset = self.deformable_sampling(x, offset)
        return self.conv(x_offset)

    def deformable_sampling(self, x, offset):
        return F.grid_sample(x, offset)

局限性

问题说明
计算复杂需学习偏移量,开销较大
优化难度偏移量学习可能梯度不稳定
实现复杂需精细的重采样操作

Comments