Pytorch使用torchvision实现deform_conv2D

本文解析了Deformable Convolution的创新之处,介绍了如何在神经网络中加入可学习偏移量,以及其在卷积操作中的作用。通过实例展示了如何在PyTorch中应用,并指出在MNIST数据集上的实验优势:更快收敛和更高精度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章目录

含义

将feature map当作一个一个网格,其中输出结果y中,点p这个坐标的值,取决于,其中为权重,为论文中引入的modulation scalar factor。而需要根据三个参数之和作为输入,代表的是原坐标,是相对于坐标 的相对位移。例如,一个的卷积核,则,以上都是标准卷积。本文提出Deformable_Conv就在于加入了新的参数,即需要网络去学习的一个learnable offset。根据论文offset=和mask=都需要进行学习。

在这里插入图片描述

原文(The modulation scalar lies in the range [0,1], while is a real number with unconstrained range.)就是 是一个0到1的数,这也很容易理解,它是一个模型响应参数,随便取。

原文(the initial values of and are 0 and 0.5, respectively. )初始化。

操作

class net(nn.Module):
    def __init__(self):
        super(net, self).__init__()
        self.conv = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
 
    def forward(self, x):
        out = self.relu(self.conv(x))
        return out


class net(nn.Module):
    def __init__(self):
        super(dcn, self).__init__()
        self.conv = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1) #原卷积
 
        self.conv_offset = nn.Conv2d(3, 18, kernel_size=3, stride=1, padding=1)
        init_offset = torch.Tensor(np.zeros([18, 3, 3, 3]))
        self.conv_offset.weight = torch.nn.Parameter(init_offset) #初始化为0
 
        self.conv_mask = nn.Conv2d(3, 9, kernel_size=3, stride=1, padding=1)
        init_mask = torch.Tensor(np.zeros([9, 3, 3, 3])+np.array([0.5]))
        self.conv_mask.weight = torch.nn.Parameter(init_mask) #初始化为0.5
 
    def forward(self, x):
        offset = self.conv_offset(x)
        mask = torch.sigmoid(self.conv_mask(x)) #保证在0到1之间
        out = torchvision.ops.deform_conv2d(input=x, offset=offset, 
                                            weight=self.conv.weight, 
                                             mask=mask, padding=(1, 1))
        return out

应用

需要注意的点有deform_conv2d的stride默认为(1, 1),padding默认为(0, 0),dilation默认为(1, 1)。最后,随便用mnist数据集跑跑(训练集和验证集都加入了随机旋转,网络为四层卷积,三层全连接,加入了dropout,参数都一样,没有仔细调整)可以发现不仅收敛的更快,同时精度更高。deform_conv确实发挥了作用。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值