-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgsm.py
55 lines (45 loc) · 2.49 KB
/
gsm.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
# PyTorch GSM implementation by Swathikiran Sudhakaran
import torch
from torch import nn
from torch.cuda import FloatTensor as ftens
class gsmModule(nn.Module):
def __init__(self, fPlane, num_segments=3):
super(gsmModule, self).__init__()
self.conv3D = nn.Conv3d(fPlane, 2, (3, 3, 3), stride=1,
padding=(1, 1, 1), groups=2)
nn.init.constant_(self.conv3D.weight, 0)
nn.init.constant_(self.conv3D.bias, 0)
self.tanh = nn.Tanh()
self.fPlane = fPlane
self.num_segments = num_segments
self.bn = nn.BatchNorm3d(num_features=fPlane)
self.relu = nn.ReLU()
def lshift_zeroPad(self, x):
return torch.cat((x[:,:,1:], ftens(x.size(0), x.size(1), 1, x.size(3), x.size(4)).fill_(0)), dim=2)
def rshift_zeroPad(self, x):
return torch.cat((ftens(x.size(0), x.size(1), 1, x.size(3), x.size(4)).fill_(0), x[:,:,:-1]), dim=2)
def forward(self, x):
batchSize = x.size(0) // self.num_segments
shape = x.size(1), x.size(2), x.size(3)
assert shape[0] == self.fPlane
x = x.view(batchSize, self.num_segments, *shape).permute(0, 2, 1, 3, 4).contiguous()
x_bn = self.bn(x)
x_bn_relu = self.relu(x_bn)
gate = self.tanh(self.conv3D(x_bn_relu))
gate_group1 = gate[:, 0].unsqueeze(1)
gate_group2 = gate[:, 1].unsqueeze(1)
x_group1 = x[:, :self.fPlane // 2]
x_group2 = x[:, self.fPlane // 2:]
y_group1 = gate_group1 * x_group1
y_group2 = gate_group2 * x_group2
r_group1 = x_group1 - y_group1
r_group2 = x_group2 - y_group2
y_group1 = self.lshift_zeroPad(y_group1) + r_group1
y_group2 = self.rshift_zeroPad(y_group2) + r_group2
y_group1 = y_group1.view(batchSize, 2, self.fPlane // 4, self.num_segments, *shape[1:]).permute(0, 2, 1, 3, 4,
5)
y_group2 = y_group2.view(batchSize, 2, self.fPlane // 4, self.num_segments, *shape[1:]).permute(0, 2, 1, 3, 4,
5)
y = torch.cat((y_group1.contiguous().view(batchSize, self.fPlane//2, self.num_segments, *shape[1:]),
y_group2.contiguous().view(batchSize, self.fPlane//2, self.num_segments, *shape[1:])), dim=1)
return y.permute(0, 2, 1, 3, 4).contiguous().view(batchSize*self.num_segments, *shape)