学术
模块更新于 2026-11-21· 浏览 0· 下载 0

DySample 超轻量动态上采样模块

原理创新:抛弃传统核基动态上采样范式,从点采样角度重构上采样逻辑,完全基于PyTorch内置函数实现,无需自定义CUDA包

计算机视觉计算机视觉上采样语义分割目标检测ICCV论文解读DySample

结构图

DySample 超轻量动态上采样模块结构图
DySample 超轻量动态上采样模块结构图

原理创新:抛弃传统核基动态上采样范式,从点采样角度重构上采样逻辑,完全基于PyTorch内置函数实现,无需自定义CUDA包

论文信息

  • 论文:Learning to Upsample by Learning to Sample
  • 出处:ICCV 2023

核心痛点

  1. 现有核基动态上采样器(如CARAFE、FADE)依赖动态卷积,计算量大、推理延迟高
  2. 部分上采样器需要额外高分辨率特征作为引导,适用场景受限
  3. 多数需要自定义CUDA算子,部署门槛高,适配不同框架难度大

模块亮点

  • 原理创新:抛弃传统核基动态上采样范式,从点采样角度重构上采样逻辑,完全基于PyTorch内置函数实现,无需自定义CUDA包
  • 性能优越:在语义分割、目标检测、实例分割、全景分割、单目深度估计5类稠密预测任务上全面超越同类SOTA上采样器,MaskFormer-SwinB基线上涨点幅度比CARAFE高46%
  • 极致轻量:参数量仅为CARAFE的3%,FLOPs仅为20%,推理延迟接近双线性插值,额外训练开销可忽略

适用场景

CV领域所有稠密预测任务,适配YOLO、Faster R-CNN、Mask R-CNN、SegFormer、MaskFormer等所有带FPN/上采样结构的模型,可直接替换原有最近邻/双线性插值上采样层

插入位置建议

  • 替换模型中所有需要上采样分辨率的算子,比如FPN结构中低分辨率特征向高分辨率特征对齐的上采样模块、检测头/分割头中恢复输出分辨率的上采样层

适用任务

  • CV领域所有稠密预测任务
  • 适配YOLO
  • Faster R-CNN

完整代码

dys.py
import torch
import torch.nn as nn
import torch.nn.functional as F

"""ICCV2023
DySample是一种超轻量级且有效的动态上采样器。虽然最近的基于内核的动态上采样器(如 CARAFE、FADE 和 SAPA)的性能提升令人印象深刻,但它们带来了大量工作量,主要是由于耗时的动态卷积和用于生成动态内核的额外子网络。
此外,FADE 和 SAPA 对高分辨率特征引导的需求在某种程度上限制了它们的应用场景。
为了解决这些问题,我们绕过动态卷积并从点采样的角度制定上采样,这更节省资源,并且可以使用 PyTorch 中的标准内置函数轻松实现。
我们首先展示了一个简单的设计,然后演示如何逐步加强其上采样行为以实现我们的新上采样器 DySample。
与以前基于内核的动态上采样器相比,DySample 不需要定制的 CUDA 包,并且具有更少的参数、FLOP、GPU 内存和延迟。
除了轻量级特性外,DySample 在五个密集预测任务中的表现优于其他上采样器,包括语义分割、对象检测、实例分割、全景分割和单目深度估计。
"""

def normal_init(module, mean=0, std=1, bias=0):
    if hasattr(module, 'weight') and module.weight is not None:
        nn.init.normal_(module.weight, mean, std)
    if hasattr(module, 'bias') and module.bias is not None:
        nn.init.constant_(module.bias, bias)


def constant_init(module, val, bias=0):
    if hasattr(module, 'weight') and module.weight is not None:
        nn.init.constant_(module.weight, val)
    if hasattr(module, 'bias') and module.bias is not None:
        nn.init.constant_(module.bias, bias)


class DySample(nn.Module):
    def __init__(self, in_channels, scale=2, style='lp', groups=4, dyscope=False):
        super().__init__()
        self.scale = scale
        self.style = style
        self.groups = groups
        assert style in ['lp', 'pl']
        if style == 'pl':
            assert in_channels >= scale ** 2 and in_channels % scale ** 2 == 0
        assert in_channels >= groups and in_channels % groups == 0

        if style == 'pl':
            in_channels = in_channels // scale ** 2
            out_channels = 2 * groups
        else:
            out_channels = 2 * groups * scale ** 2

        self.offset = nn.Conv2d(in_channels, out_channels, 1)
        normal_init(self.offset, std=0.001)
        if dyscope:
            self.scope = nn.Conv2d(in_channels, out_channels, 1, bias=False)
            constant_init(self.scope, val=0.)

        self.register_buffer('init_pos', self._init_pos())

    def _init_pos(self):
        h = torch.arange((-self.scale + 1) / 2, (self.scale - 1) / 2 + 1) / self.scale
        return torch.stack(torch.meshgrid([h, h])).transpose(1, 2).repeat(1, self.groups, 1).reshape(1, -1, 1, 1)

    def sample(self, x, offset):
        B, _, H, W = offset.shape
        offset = offset.view(B, 2, -1, H, W)
        coords_h = torch.arange(H) + 0.5
        coords_w = torch.arange(W) + 0.5
        coords = torch.stack(torch.meshgrid([coords_w, coords_h])
                             ).transpose(1, 2).unsqueeze(1).unsqueeze(0).type(x.dtype).to(x.device)
        normalizer = torch.tensor([W, H], dtype=x.dtype, device=x.device).view(1, 2, 1, 1, 1)
        coords = 2 * (coords + offset) / normalizer - 1
        coords = F.pixel_shuffle(coords.view(B, -1, H, W), self.scale).view(
            B, 2, -1, self.scale * H, self.scale * W).permute(0, 2, 3, 4, 1).contiguous().flatten(0, 1)
        return F.grid_sample(x.reshape(B * self.groups, -1, H, W), coords, mode='bilinear',
                             align_corners=False, padding_mode="border").view(B, -1, self.scale * H, self.scale * W)

    def forward_lp(self, x):
        if hasattr(self, 'scope'):
            offset = self.offset(x) * self.scope(x).sigmoid() * 0.5 + self.init_pos
        else:
            offset = self.offset(x) * 0.25 + self.init_pos
        return self.sample(x, offset)

    def forward_pl(self, x):
        x_ = F.pixel_shuffle(x, self.scale)
        if hasattr(self, 'scope'):
            offset = F.pixel_unshuffle(self.offset(x_) * self.scope(x_).sigmoid(), self.scale) * 0.5 + self.init_pos
        else:
            offset = F.pixel_unshuffle(self.offset(x_), self.scale) * 0.25 + self.init_pos
        return self.sample(x, offset)

    def forward(self, x):
        if self.style == 'pl':
            return self.forward_pl(x)
        return self.forward_lp(x)


if __name__ == '__main__':
    block = DySample(64)
    input = torch.rand(2, 64, 4, 7)
    output = block(input)
    print(input.size())
    print(output.size())

环境与依赖

Python 3.9+

继续浏览

下一篇FedTaxo 联邦概念重聚类用视觉原型与描述嵌入联合评分,在客户端不可合并约束下平均连接重聚类。外层保留所有新类证据并每轮重传,才能修订旧分配;源码不含层级关系与客户端标签映射。

相关资源