-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_ops.py
77 lines (64 loc) · 1.93 KB
/
custom_ops.py
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
import torch
from torchvision.ops import deform_conv2d
import torch.nn as nn
class DeformableConv2d(nn.Module):
def __init__(self,
in_channels,
out_channels,
kernel = 3,
stride = 1,
padding = 1,
bias = False
) :
super(DeformableConv2d,self).__init__()
if type(stride) == tuple:
self.stride = stride
else:
self.stride = stride
self.padding = padding
#Offset Convolution
self.offset_conv = nn.Conv2d(
in_channels,
2 * kernel * kernel,
kernel_size = kernel,
stride = stride,
padding = self.padding,
bias = True
)
nn.init.constant_(self.offset_conv.weight,0.0)
nn.init.constant_(self.offset_conv.bias,0.0)
#Modulator Convolution
self.modulator_conv = nn.Conv2d(
in_channels,
1 * kernel * kernel,
kernel_size = kernel,
stride = stride,
padding = self.padding,
bias = True
)
nn.init.constant_(self.modulator_conv.weight,0.0)
nn.init.constant_(self.modulator_conv.bias,0.0)
#Regular Convolutions
self.regular_conv = nn.Conv2d(
in_channels = in_channels,
out_channels = out_channels,
kernel_size = kernel,
stride = stride,
padding = self.padding,
bias = bias
)
def forward(self,x):
offset = self.offset_conv(x)
modulator = 2.0 * torch.sigmoid(self.modulator_conv(x))
x = deform_conv2d(input=x,
offset = offset,
weight = self.regular_conv.weight,
bias = self.regular_conv.bias,
padding = self.padding,
mask = modulator,
stride = self.stride
)
return x
class EmptyLayer(nn.Module):
def __init__(self):
super(EmptyLayer, self).__init__()