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

import torch
import torch.nn as nn
from torch.nn.modules.utils import _pair

7
from ..functions.deform_conv import deform_conv, modulated_deform_conv
yhcao6's avatar
yhcao6 committed
8
9


10
class DeformConv(nn.Module):
yhcao6's avatar
yhcao6 committed
11
12
13
14
15
16
17
18

    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
yhcao6's avatar
yhcao6 committed
19
                 groups=1,
20
                 deformable_groups=1,
Kai Chen's avatar
Kai Chen committed
21
22
                 bias=False):
        assert not bias
yhcao6's avatar
yhcao6 committed
23
24
25
26
27
28
29
        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)
yhcao6's avatar
yhcao6 committed
30
        self.groups = groups
31
        self.deformable_groups = deformable_groups
yhcao6's avatar
yhcao6 committed
32
33

        self.weight = nn.Parameter(
yhcao6's avatar
yhcao6 committed
34
35
            torch.Tensor(out_channels, in_channels // self.groups,
                         *self.kernel_size))
yhcao6's avatar
yhcao6 committed
36
37
38
39
40
41
42
43

        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
44
        self.weight.data.uniform_(-stdv, stdv)
yhcao6's avatar
yhcao6 committed
45
46

    def forward(self, input, offset):
yhcao6's avatar
yhcao6 committed
47
        return deform_conv(input, offset, self.weight, self.stride,
yhcao6's avatar
yhcao6 committed
48
49
                           self.padding, self.dilation, self.groups,
                           self.deformable_groups)
50
51
52
53
54
55
56
57


class ModulatedDeformConv(nn.Module):

    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
58
59
                 stride=1,
                 padding=0,
60
                 dilation=1,
yhcao6's avatar
yhcao6 committed
61
                 groups=1,
62
                 deformable_groups=1,
63
                 bias=True):
64
65
66
67
68
69
70
        super(ModulatedDeformConv, self).__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = _pair(kernel_size)
        self.stride = stride
        self.padding = padding
        self.dilation = dilation
yhcao6's avatar
yhcao6 committed
71
        self.groups = groups
72
        self.deformable_groups = deformable_groups
73
        self.with_bias = bias
74
75

        self.weight = nn.Parameter(
yhcao6's avatar
yhcao6 committed
76
77
            torch.Tensor(out_channels, in_channels // groups,
                         *self.kernel_size))
78
79
80
        if bias:
            self.bias = nn.Parameter(torch.Tensor(out_channels))
        else:
81
            self.register_parameter('bias', None)
82
83
84
85
86
87
88
89
        self.reset_parameters()

    def reset_parameters(self):
        n = self.in_channels
        for k in self.kernel_size:
            n *= k
        stdv = 1. / math.sqrt(n)
        self.weight.data.uniform_(-stdv, stdv)
90
        if self.bias is not None:
91
            self.bias.data.zero_()
92
93

    def forward(self, input, offset, mask):
yhcao6's avatar
yhcao6 committed
94
95
96
        return modulated_deform_conv(
            input, offset, mask, self.weight, self.bias, self.stride,
            self.padding, self.dilation, self.groups, self.deformable_groups)
97
98
99
100
101
102
103
104


class ModulatedDeformConvPack(ModulatedDeformConv):

    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
105
106
                 stride=1,
                 padding=0,
107
                 dilation=1,
yhcao6's avatar
yhcao6 committed
108
                 groups=1,
109
                 deformable_groups=1,
110
                 bias=True):
yhcao6's avatar
yhcao6 committed
111
112
113
        super(ModulatedDeformConvPack, self).__init__(
            in_channels, out_channels, kernel_size, stride, padding, dilation,
            groups, deformable_groups, bias)
114
115

        self.conv_offset_mask = nn.Conv2d(
yhcao6's avatar
yhcao6 committed
116
            self.in_channels // self.groups,
117
118
119
            self.deformable_groups * 3 * self.kernel_size[0] *
            self.kernel_size[1],
            kernel_size=self.kernel_size,
120
121
            stride=_pair(self.stride),
            padding=_pair(self.padding),
122
123
124
125
126
127
128
129
130
131
132
133
            bias=True)
        self.init_offset()

    def init_offset(self):
        self.conv_offset_mask.weight.data.zero_()
        self.conv_offset_mask.bias.data.zero_()

    def forward(self, input):
        out = self.conv_offset_mask(input)
        o1, o2, mask = torch.chunk(out, 3, dim=1)
        offset = torch.cat((o1, o2), dim=1)
        mask = torch.sigmoid(mask)
yhcao6's avatar
yhcao6 committed
134
135
136
        return modulated_deform_conv(
            input, offset, mask, self.weight, self.bias, self.stride,
            self.padding, self.dilation, self.groups, self.deformable_groups)