-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconv_lstm.py
290 lines (178 loc) · 8.36 KB
/
conv_lstm.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import torch
import torch.nn as nn
from models import MBConv, DoubleConv, UpMB
import torch.nn.functional as F
# in_channels: int, out_features: int, MBC_type = "depthwise", expansion: int = 4
class ConvLSTM(nn.Module):
"""Adapted from: https://github.com/Atcold/pytorch-CortexNet/blob/master/model/ConvLSTMCell.py """
def __init__(self, input_size, hidden_size, kernel_size, MBC_type="conv", expansion=2):
super(ConvLSTM, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
pad = kernel_size // 2
# cache a tensor filled with zeros to avoid reallocating memory at each inference step if --no-recurrent is enabled
self.zero_tensors = {}
# self.Gates = nn.Conv2d(input_size + hidden_size, 4 * hidden_size, kernel_size, padding=pad)
if MBC_type == "conv":
self.Gates = nn.Conv2d(input_size + hidden_size, 4 * hidden_size, kernel_size, padding=pad)
else:
self.Gates = MBConv(input_size + hidden_size, 4 * hidden_size, MBC_type, expansion)
def forward(self, input_, prev_state=None):
# get batch and spatial sizes
batch_size = input_.data.size()[0]
spatial_size = input_.data.size()[2:]
# generate empty prev_state, if None is provided
if prev_state is None:
# create the zero tensor if it has not been created already
state_size = tuple([batch_size, self.hidden_size] + list(spatial_size))
if state_size not in self.zero_tensors:
# allocate a tensor with size `spatial_size`, filled with zero (if it has not been allocated already)
self.zero_tensors[state_size] = (
torch.zeros(state_size, dtype=input_.dtype).to(input_.device),
torch.zeros(state_size, dtype=input_.dtype).to(input_.device)
)
prev_state = self.zero_tensors[tuple(state_size)]
prev_hidden, prev_cell = prev_state
# data size is [batch, channel, height, width]
stacked_inputs = torch.cat((input_, prev_hidden), 1)
# print("stacked_inputs", input_.shape, prev_hidden.shape)
gates = self.Gates(stacked_inputs)
# chunk across channel dimension
in_gate, remember_gate, out_gate, cell_gate = gates.chunk(4, 1)
# apply sigmoid non linearity
in_gate = torch.sigmoid(in_gate)
remember_gate = torch.sigmoid(remember_gate)
out_gate = torch.sigmoid(out_gate)
# apply tanh non linearity
cell_gate = torch.tanh(cell_gate)
# compute current cell and hidden state
cell = (remember_gate * prev_cell) + (in_gate * cell_gate)
hidden = out_gate * torch.tanh(cell)
return hidden, cell
class DownConvLSTM(nn.Module):
"""Upscaling then double conv"""
def __init__(self, input_size, hidden_size, kernel_size, MBC_type="conv", expansion=2, pool_sz=2):
super().__init__()
self.mbd = ConvLSTM(input_size, hidden_size, kernel_size, MBC_type, expansion)
self.out = DoubleConv(hidden_size, hidden_size)
self.pool = nn.MaxPool2d(pool_sz)
def forward(self, x, p_state):
c_state = self.mbd(x, p_state)
x = self.out(c_state[0]) + c_state[0]
x = self.pool(x)
return x, c_state
class UpConvLSTM(nn.Module):
"""Upscaling then double conv"""
def __init__(self, in_channels, out_channels, MBC_type, expansion, n_repeats = 2, bilinear=True, scale_factor=2):
super().__init__()
# if bilinear, use the normal convolutions to reduce the number of channels
if bilinear:
self.up = nn.Upsample(scale_factor=scale_factor, mode='bilinear', align_corners=True)
# self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
else:
self.up = nn.ConvTranspose2d(in_channels, in_channels // 2, kernel_size=scale_factor, stride=scale_factor)
self.mbd = ConvLSTM(in_channels, out_channels, 3, MBC_type, expansion)
self.out = DoubleConv(out_channels, out_channels)
# print(in_channels, out_channels)
# self.mbd = torch.nn.Sequential()
# for i in range(n_repeats-1):
# self.mbd.add_module(f"convlstm_{i}",ConvLSTM(in_channels, in_channels, 3, MBC_type, expansion))
# self.mbd.add_module(f"convlstm_{n_repeats-1}", ConvLSTM(in_channels, out_channels, 3, MBC_type, expansion))
def forward(self, x1, x2, p_state):
x1 = self.up(x1)
# print(x1.shape)
# print(x2.shape)
# input is CHW
diffY = x2.size()[2] - x1.size()[2]
diffX = x2.size()[3] - x1.size()[3]
x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
diffY // 2, diffY - diffY // 2])
# if you have padding issues, see
# https://github.com/HaiyongJiang/U-Net-Pytorch-Unstructured-Buggy/commit/0e854509c2cea854e247a9c615f175f76fbb2e3a
# https://github.com/xiaopeng-liao/Pytorch-UNet/commit/8ebac70e633bac59fc22bb5195e513d5832fb3bd
# print(x2.shape)
x = torch.cat([x2, x1], dim=1)
# x = x2 + x1
c_states = self.mbd(x, p_state)
x = self.out(c_states[0]) + c_states[0]
return x, c_states
class UNetConvLSTM(nn.Module):
def __init__(self, input_size=5, output_size=1, n_lyr=3, decode_lstm=1):
super(UNetConvLSTM, self).__init__()
# Define the components of the UNet-like model here.
# This will depend on the specifics of your ConvLSTM.
self.input_size = input_size
self.output_size = input_size
self.n_lyr = n_lyr
self.decode_lstm = decode_lstm
kernel_sizes = [5, 3, 3, 3]
n_chs = [5, 10, 20, 40]
# 60, 20
# 30, 10
# 15, 5
# n_rep_dn = [2, 2, 4, 4, 6]
# lyr_ts = ["fused", "fused", "depthwise", "depthwise"]
lyr_ts = ["conv", "conv", "conv", "conv"]
# n_rep_up = [6, 4, 4, 2, 2]
expans = [1, 2, 4, 4, 6]
pool_szs = [3, 3, 2, 2, 5]
self.inc = DoubleConv(input_size, n_chs[0])
# self.down_convs = nn.ModuleList()
# self.down_pools = nn.ModuleList()
self.downs = nn.ModuleList()
# DownConvLSTM
i = 0
while i < n_lyr:
lyr = DownConvLSTM(n_chs[i], n_chs[i+1], kernel_sizes[i], lyr_ts[i], expans[i], pool_szs[i])
self.downs.append(lyr)
i += 1
self.ups = nn.ModuleList()
i = 0
while i < n_lyr:
rev_i = n_lyr-i
if decode_lstm:
in_ch = n_chs[rev_i] + n_chs[rev_i-1]
out_ch = n_chs[rev_i-1]
lyr = UpConvLSTM(in_ch, out_ch, lyr_ts[rev_i], expans[rev_i], 1, bilinear=True, scale_factor=pool_szs[rev_i])
else:
lyr = UpMB(n_chs[rev_i], n_chs[rev_i-1], lyr_ts[rev_i], expans[rev_i], 1, bilinear=True, scale_factor=pool_szs[rev_i])
self.ups.append(lyr)
i += 1
self.outc = DoubleConv(n_chs[0], output_size)
def forward(self, x, p_states):
# Define the forward pass for each layer.
# This will depend on the specifics of your ConvLSTM.
x0 = self.inc(x)
xs = [x0]
cur_states = []
i = 0
while i < self.n_lyr:
# print("xx")
tmp_x, c_state = self.downs[i](xs[-1], p_states[i])
# print(hidden.shape, cell.shape)
cur_states.append( c_state )
xs.append(tmp_x)
i += 1
x_m = xs[-1]
rev_xs = xs[::-1]
# print("after encoder")
if self.decode_lstm:
i = 0
while i < self.n_lyr:
up = self.ups[i]
x_r = rev_xs[i+1]
tmp_x, c_state = self.ups[i](x_m, x_r, p_states[i+self.n_lyr])
# print(hidden.shape, cell.shape)
cur_states.append( c_state )
x_m = tmp_x
i += 1
else:
for up, x_r in zip(self.ups, rev_xs[1:]):
# print("x_ie", x_ie.shape)
# print("xr", xr.shape)
x_m = up(x_m, x_r)
# print(x_ie.shape)
output = self.outc(x_m)
return output, cur_states
if __name__ == "__main__":
model = ConvLSTM(input_size=5, hidden_size=5, kernel_size=3)