Deformable convolution V2 Pytorch 实现

import torch
import torch.nn as nn
from torchvision.ops import DeformConv2d


class DCNConv(nn.Module):
    # Standard convolution
    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):  
        # ch_in, ch_out, kernel, stride, padding, groups
        super().__init__()
        self.conv1 = nn.Conv2d(c1, c2, 3, 2, 1, groups=g, bias=False)
        deformable_groups = 1
        offset_channels = 18
        self.conv2_offset = nn.Conv2d(c2, deformable_groups * offset_channels, kernel_size=3, padding=1)
        self.conv2 = DeformConv2d(c2, c2, kernel_size=3, padding=1, bias=False)

        # self.conv2 = DeformableConv2d(c2, c2, k, s, autopad(k, p), groups=g, bias=False)
        self.bn1 = nn.BatchNorm2d(c2)
        self.act1 = nn.Mish() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
        self.bn2 = nn.BatchNorm2d(c2)
        self.act2 = nn.Mish() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())

    def forward(self, x):
        x = self.act1(self.bn1(self.conv1(x)))
        offset = self.conv2_offset(x)
        x = self.act2(self.bn2(self.conv2(x, offset)))
        return x


# Parallel deformable convolutional network
class DCN(nn.Module):
    # ch_in, ch_out, kernel, stride, padding, groups
    def __init__(self, c1, c2):
        super().__init__()
        self.c_1 = int(c1)
        self.c_2 = int(c2)

        # deformable convolution
        self.dc = DCNConv(self.c_1, self.c_2)

    def forward(self, x):
        x = self.dc(x)
        return x 


def main(inputs):
    model_1 = DCN(3, 6)
    model_2 = DCN(3, 3)
    output_1 = model_1(inputs)
    output_2 = model_2(inputs)
    print(output_1.shape)      # torch.Size([1, 6, 2, 2])
    print(output_2.shape)      # torch.Size([1, 3, 2, 2])


if __name__ == "__main__":
    tensor = torch.rand(1, 3, 4, 4)
    main(tensor)

可以尝试更改DCN结构来调整下采样倍率(默认两倍下采样)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值