-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConvRNN.py
executable file
·187 lines (175 loc) · 7.45 KB
/
ConvRNN.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from torch import zeros, cat, tanh, stack, sigmoid, split
from torch.nn import Module, Sequential, Conv2d, GroupNorm, Dropout2d
class CGRU_cell(Module):
"""
ConvGRU Cell
"""
def __init__(self, shape, input_channels, filter_size, num_features, bias, no_bn, dropout, device):
"""
Initialize the ConvLSTM cell
:param shape: (int, int)
Height and width of input tensor as (height, width).
:param input_channels: int
Number of channels of input tensor.
:param filter_size: (int, int)
Size of the convolutional kernel.
:param num_features: int
Number of channels of hidden state.
:param bias: bool
Whether or not to add the bias.
:param dtype: str
Whether or not to use cuda.
"""
super(CGRU_cell, self).__init__()
self.height, self.width = shape
self.input_channels = input_channels
# kernel_size of input_to_state equals state_to_state
self.filter_size = filter_size
self.num_features = num_features
self.bias = bias
self.device = device
self.padding = (filter_size - 1) // 2
self.no_bn = no_bn
self.do_do = True if dropout > 0 else False
self.dropout = Dropout2d(p=dropout)
if self.no_bn:
self.conv_gates = Sequential(
Conv2d(in_channels=self.input_channels + self.num_features,
out_channels=2 * self.num_features,
kernel_size=self.filter_size,
stride=1,
padding=self.padding,
bias=self.bias),
)
self.conv_can = Sequential(
Conv2d(in_channels=self.input_channels + self.num_features,
out_channels=self.num_features,
kernel_size=self.filter_size,
stride=1,
padding=self.padding,
bias=self.bias),
)
else:
self.conv_gates = Sequential(
Conv2d(in_channels=self.input_channels + self.num_features,
out_channels=2 * self.num_features,
kernel_size=self.filter_size,
stride=1,
padding=self.padding,
bias=self.bias),
GroupNorm(num_groups=2 * self.num_features // 1,
num_channels=2 * self.num_features)
)
self.conv_can = Sequential(
Conv2d(in_channels=self.input_channels + self.num_features,
out_channels=self.num_features,
kernel_size=self.filter_size,
stride=1,
padding=self.padding,
bias=self.bias),
GroupNorm(num_groups=self.num_features // 1,
num_channels=self.num_features)
)
def move_device(self, device):
self.device = device
def forward(self, inputs=None, hidden_state=None, seq_len=10):
if hidden_state is None:
htprev = zeros(inputs.size(1), self.num_features,
self.height, self.width).to(self.device, non_blocking=True)
else:
htprev = hidden_state
output_inner = []
for index in range(seq_len):
if inputs is None:
x = zeros(htprev.size(0), self.input_channels,
self.height, self.width).to(self.device, non_blocking=True)
else:
x = inputs[index, ...]
# W * (X_t + H_t-1)
gates = self.conv_gates(cat([x, htprev], dim=1))
# activate gates
gamma, beta = split(gates, self.num_features, dim=1)
reset_gate = sigmoid(gamma)
update_gate = sigmoid(beta)
# h' = tanh(W*(x+reset_gate*H_t-1))
ht = tanh(self.conv_can(cat([x, reset_gate * htprev], dim=1)))
htnext = (1 - update_gate) * htprev + update_gate * ht
# drop out
if self.do_do:
htnext = self.dropout(htnext)
output_inner.append(htnext)
htprev = htnext
return stack(output_inner), htnext
class CLSTM_cell(Module):
"""ConvLSTMCell
"""
def __init__(self, shape, input_channels, filter_size, num_features, bias, no_bn, device):
"""
Initialize the ConvLSTM cell
:param shape: (int, int)
Height and width of input tensor as (height, width).
:param input_channels: int
Number of channels of input tensor.
:param kernel_size: (int, int)
Size of the convolutional kernel.
:param num_features: int
Number of channels of hidden state.
:param bias: bool
Whether or not to add the bias.
:param dtype: str
Whether or not to use cuda.
"""
super(CLSTM_cell, self).__init__()
self.height, self.width = shape
self.input_channels = input_channels
self.filter_size = filter_size
self.num_features = num_features
self.bias = bias
self.device = device
self.padding = (filter_size - 1) // 2
self.no_bn = no_bn
if self.no_bn:
self.conv = Sequential(
Conv2d(in_channels=self.input_channels + self.num_features,
out_channels=4 * self.num_features,
kernel_size=self.filter_size,
stride=1,
padding=self.padding,
bias=self.bias),
)
else:
self.conv = Sequential(
Conv2d(in_channels=self.input_channels + self.num_features,
out_channels=4 * self.num_features,
kernel_size=self.filter_size,
stride=1,
padding=self.padding,
bias=self.bias),
GroupNorm(num_groups=4 * self.num_features // 2,
num_channels=4 * self.num_features)
)
def move_device(self, device):
self.device = device
def forward(self, inputs=None, hidden_state=None, seq_len=10):
if hidden_state is None:
hx = zeros(inputs.size(1), self.num_features, self.height, self.width).to(self.device, non_blocking=True)
cx = zeros(inputs.size(1), self.num_features, self.height, self.width).to(self.device, non_blocking=True)
else:
hx, cx = hidden_state
output_inner = []
for index in range(seq_len):
if inputs is None:
x = zeros(hx.size(0), self.input_channels, self.height, self.width).to(self.device, non_blocking=True)
else:
x = inputs[index, ...]
ingate, forgetgate, cellgate, outgate = split(self.conv(cat([x, hx], dim=1)), self.num_features, dim=1)
ingate = sigmoid(ingate)
forgetgate = sigmoid(forgetgate)
cellgate = tanh(cellgate)
outgate = sigmoid(outgate)
cy = (forgetgate * cx) + (ingate * cellgate)
hy = outgate * tanh(cy)
output_inner.append(hy)
hx = hy
cx = cy
return stack(output_inner), (hy, cy)