deform_conv.py 5.08 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
                 bias=False):
yhcao6's avatar
yhcao6 committed
22
        super(DeformConv, self).__init__()
23

Kai Chen's avatar
Kai Chen committed
24
        assert not bias
25
26
27
28
29
30
        assert in_channels % groups == 0, \
            'in_channels {} cannot be divisible by groups {}'.format(
                in_channels, groups)
        assert out_channels % groups == 0, \
            'out_channels {} cannot be divisible by groups {}'.format(
                out_channels, groups)
Kai Chen's avatar
Kai Chen committed
31

yhcao6's avatar
yhcao6 committed
32
33
34
35
36
37
        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
38
        self.groups = groups
39
        self.deformable_groups = deformable_groups
yhcao6's avatar
yhcao6 committed
40
41

        self.weight = nn.Parameter(
yhcao6's avatar
yhcao6 committed
42
43
            torch.Tensor(out_channels, in_channels // self.groups,
                         *self.kernel_size))
yhcao6's avatar
yhcao6 committed
44
45
46
47
48
49
50
51

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

Kai Chen's avatar
Kai Chen committed
54
55
56
57
58
59
60
61
    def forward(self, x, offset):
        return deform_conv(x, offset, self.weight, self.stride, self.padding,
                           self.dilation, self.groups, self.deformable_groups)


class DeformConvPack(DeformConv):

    def __init__(self, *args, **kwargs):
62
        super(DeformConvPack, self).__init__(*args, **kwargs)
Kai Chen's avatar
Kai Chen committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

        self.conv_offset = nn.Conv2d(
            self.in_channels,
            self.deformable_groups * 2 * self.kernel_size[0] *
            self.kernel_size[1],
            kernel_size=self.kernel_size,
            stride=_pair(self.stride),
            padding=_pair(self.padding),
            bias=True)
        self.init_offset()

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

    def forward(self, x):
        offset = self.conv_offset(x)
        return deform_conv(x, offset, self.weight, self.stride, self.padding,
                           self.dilation, self.groups, self.deformable_groups)
82
83
84
85
86
87
88
89


class ModulatedDeformConv(nn.Module):

    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
90
91
                 stride=1,
                 padding=0,
92
                 dilation=1,
yhcao6's avatar
yhcao6 committed
93
                 groups=1,
94
                 deformable_groups=1,
95
                 bias=True):
96
97
98
99
100
101
102
        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
103
        self.groups = groups
104
        self.deformable_groups = deformable_groups
105
        self.with_bias = bias
106
107

        self.weight = nn.Parameter(
yhcao6's avatar
yhcao6 committed
108
109
            torch.Tensor(out_channels, in_channels // groups,
                         *self.kernel_size))
110
111
112
        if bias:
            self.bias = nn.Parameter(torch.Tensor(out_channels))
        else:
113
            self.register_parameter('bias', None)
114
115
116
117
118
119
120
121
        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)
122
        if self.bias is not None:
123
            self.bias.data.zero_()
124

Kai Chen's avatar
Kai Chen committed
125
126
127
128
    def forward(self, x, offset, mask):
        return modulated_deform_conv(x, offset, mask, self.weight, self.bias,
                                     self.stride, self.padding, self.dilation,
                                     self.groups, self.deformable_groups)
129
130
131
132


class ModulatedDeformConvPack(ModulatedDeformConv):

Kai Chen's avatar
Kai Chen committed
133
134
    def __init__(self, *args, **kwargs):
        super(ModulatedDeformConvPack, self).__init__(*args, **kwargs)
135
136

        self.conv_offset_mask = nn.Conv2d(
Kai Chen's avatar
Kai Chen committed
137
            self.in_channels,
138
139
140
            self.deformable_groups * 3 * self.kernel_size[0] *
            self.kernel_size[1],
            kernel_size=self.kernel_size,
141
142
            stride=_pair(self.stride),
            padding=_pair(self.padding),
143
144
145
146
147
148
149
            bias=True)
        self.init_offset()

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

Kai Chen's avatar
Kai Chen committed
150
151
    def forward(self, x):
        out = self.conv_offset_mask(x)
152
153
154
        o1, o2, mask = torch.chunk(out, 3, dim=1)
        offset = torch.cat((o1, o2), dim=1)
        mask = torch.sigmoid(mask)
Kai Chen's avatar
Kai Chen committed
155
156
157
        return modulated_deform_conv(x, offset, mask, self.weight, self.bias,
                                     self.stride, self.padding, self.dilation,
                                     self.groups, self.deformable_groups)