deform_conv.py 1.34 KB
Newer Older
yhcao6's avatar
yhcao6 committed
1
2
3
4
import math

import torch
import torch.nn as nn
yhcao6's avatar
yhcao6 committed
5
from mmcv.cnn import uniform_init
yhcao6's avatar
yhcao6 committed
6
7
8
from torch.nn.modules.module import Module
from torch.nn.modules.utils import _pair

yhcao6's avatar
yhcao6 committed
9
from ..functions.deform_conv import deform_conv
yhcao6's avatar
yhcao6 committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40


class DeformConv(Module):

    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 num_deformable_groups=1):
        super(DeformConv, self).__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = _pair(kernel_size)
        self.stride = _pair(stride)
        self.padding = _pair(padding)
        self.dilation = _pair(dilation)
        self.num_deformable_groups = num_deformable_groups

        self.weight = nn.Parameter(
            torch.Tensor(out_channels, in_channels, *self.kernel_size))

        self.reset_parameters()

    def reset_parameters(self):
        n = self.in_channels
        for k in self.kernel_size:
            n *= k
        stdv = 1. / math.sqrt(n)
yhcao6's avatar
yhcao6 committed
41
        uniform_init(self, -stdv, stdv)
yhcao6's avatar
yhcao6 committed
42
43

    def forward(self, input, offset):
yhcao6's avatar
yhcao6 committed
44
45
46
        return deform_conv(input, offset, self.weight, self.stride,
                           self.padding, self.dilation,
                           self.num_deformable_groups)