-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcnv2_up.py
More file actions
executable file
·158 lines (123 loc) · 5 KB
/
dcnv2_up.py
File metadata and controls
executable file
·158 lines (123 loc) · 5 KB
1
2
3
4
5
6
7
8
9
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import torch
from torch import nn
from torchvision.ops.deform_conv import deform_conv2d
import math
class DeformConvTranspose(nn.Module):
def __init__(self,
in_channels: int,
out_channels: int,
up_factor: int=2,
bias: bool=True):
super(DeformConvTranspose, self).__init__()
if up_factor % 2 != 0:
raise ValueError(f'up_factor must be an even, but got {up_factor}')
self.in_channels = in_channels
self.out_channels = out_channels
self.up_factor = up_factor
self.kernel_size = (up_factor * 2, up_factor * 2)
self.stride = up_factor
self.padding = up_factor // 2
self.weight = nn.Parameter(torch.empty(out_channels, in_channels,
self.kernel_size[0], self.kernel_size[1]))
if bias:
self.bias = nn.Parameter(torch.empty(out_channels))
else:
self.register_parameter('bias', None)
self.dcn = deform_conv2d
self.reset_parameters()
def reset_parameters(self) -> None:
nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5))
if self.bias is not None:
fan_in, _ = nn.init._calculate_fan_in_and_fan_out(self.weight)
bound = 1 / math.sqrt(fan_in)
nn.init.uniform_(self.bias, -bound, bound)
def forward(self,
input,
offset,
mask):
return self.dcn(input=input,
offset=offset,
weight=self.weight.flip(-1, -2),
bias=self.bias,
mask=mask)
def __repr__(self) -> str:
s = (
f"{self.__class__.__name__}("
f"{self.in_channels}"
f", {self.out_channels}"
)
s += f", up_factor={self.up_factor}" if self.up_factor != 2 else ""
s += ", bias=False" if not self.bias is None else ""
s += ")"
return s
class DCNTranspose(nn.Module):
def __init__(self,
in_channels: int,
out_channels: int,
up_factor: int=2,
bias: bool=True,
groups: int=1):
super(DCNTranspose, self).__init__()
if in_channels % groups != 0:
raise ValueError(f'in_channels must be divisible by groups, but got {in_channels} and {groups}')
if out_channels % groups != 0:
raise ValueError(f'out_channels must be divisible by groups, but got {out_channels} and {groups}')
self.in_channels = in_channels
self.out_channels = out_channels
self.up_factor = up_factor
self.kernel_size = (up_factor * 2, up_factor * 2)
self.stride = up_factor
self.padding = up_factor // 2
self.groups = groups
self.with_bias = bias
channels_ = self.groups * 3 * self.kernel_size[0] * self.kernel_size[1]
self.conv_offset_mask = nn.Conv2d(self.in_channels,
channels_,
kernel_size=self.kernel_size,
groups=groups,
bias=bias)
self.dcn = DeformConvTranspose(in_channels // groups, out_channels,
up_factor, bias)
self.init_offset()
def init_offset(self) -> None:
self.conv_offset_mask.weight.data.zero_()
self.conv_offset_mask.bias.data.zero_()
def input_transform(self, input):
b, c, h, w = input.shape
ip = self.stride - 1
ph = self.kernel_size[0] - self.padding - 1
pw = self.kernel_size[1] - self.padding - 1
result = torch.zeros(b, c, h + ip * (h - 1) + ph * 2,
w + ip * (w - 1) + pw * 2, device=input.device)
H, W = result.shape[-2:]
result[..., ph:H - ph:self.stride, pw:W - pw:self.stride] = input
return result
def forward(self, input):
input = self.input_transform(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.softmax(mask, dim=1)
return self.dcn(input=input, offset=offset, mask=mask)
def __repr__(self) -> str:
s = (
f"{self.__class__.__name__}("
f"{self.in_channels}"
f", {self.out_channels}"
)
s += f", up_factor={self.up_factor}" if self.up_factor != 2 else ""
s += ", bias=False" if not self.with_bias else ""
s += f", groups={self.groups}" if self.groups != 1 else ""
s += ")"
return s
if __name__ == '__main__':
a = torch.rand(1, 32, 7, 7)
#b = input_transform(a)
conv = DCNTranspose(32, 64, up_factor=4, groups=2)
c = conv(a)
print(c.shape)
print(conv)
print(conv.dcn)