forked from dmckee5/BigVidGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvgru.py
executable file
·222 lines (189 loc) · 9.01 KB
/
convgru.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
import os
import torch
from torch import nn
from torch.autograd import Variable
class ConvGRUCell(nn.Module):
def __init__(self, input_size, input_dim, hidden_dim, kernel_size, bias, dtype):
"""
Initialize the ConvLSTM cell
:param input_size: (int, int)
Height and width of input tensor as (height, width).
:param input_dim: int
Number of channels of input tensor.
:param hidden_dim: int
Number of channels of hidden state.
:param kernel_size: (int, int)
Size of the convolutional kernel.
:param bias: bool
Whether or not to add the bias.
:param dtype: torch.cuda.FloatTensor or torch.FloatTensor
Whether or not to use cuda.
"""
super(ConvGRUCell, self).__init__()
self.height, self.width = input_size
self.padding = kernel_size[0] // 2, kernel_size[1] // 2
self.hidden_dim = hidden_dim
self.bias = bias
self.dtype = dtype
self.conv_gates = nn.Conv2d(in_channels=input_dim + hidden_dim,
out_channels=2*self.hidden_dim, # for update_gate,reset_gate respectively
kernel_size=kernel_size,
padding=self.padding,
bias=self.bias)
self.conv_can = nn.Conv2d(in_channels=input_dim+hidden_dim,
out_channels=self.hidden_dim, # for candidate neural memory
kernel_size=kernel_size,
padding=self.padding,
bias=self.bias)
def init_hidden(self, batch_size):
hidden_state = Variable(torch.zeros(batch_size, self.hidden_dim, self.height, self.width)).type(self.dtype)
# print("hidden state's device", hidden_state.get_device())
return hidden_state
def forward(self, input_tensor, h_cur):
"""
:param self:
:param input_tensor: (b, c, h, w)
input is actually the target_model
:param h_cur: (b, c_hidden, h, w)
current hidden and cell states respectively
:return: h_next,
next hidden state
"""
# print(input_tensor.shape,h_cur.shape)
combined = torch.cat([input_tensor, h_cur], dim=1)
combined_conv = self.conv_gates(combined)
gamma, beta = torch.split(combined_conv, self.hidden_dim, dim=1)
reset_gate = torch.sigmoid(gamma)
update_gate = torch.sigmoid(beta)
combined = torch.cat([input_tensor, reset_gate*h_cur], dim=1)
cc_cnm = self.conv_can(combined)
cnm = torch.tanh(cc_cnm)
h_next = (1 - update_gate) * h_cur + update_gate * cnm
return h_next
class ConvGRU(nn.Module):
def __init__(self, input_size, input_dim, hidden_dim, kernel_size, num_layers,
dtype, batch_first=False, bias=True, return_all_layers=False):
"""
:param input_size: (int, int)
Height and width of input tensor as (height, width).
:param input_dim: int e.g. 256
Number of channels of input tensor.
:param hidden_dim: int e.g. 1024
Number of channels of hidden state.
:param kernel_size: (int, int)
Size of the convolutional kernel.
:param num_layers: int
Number of ConvLSTM layers
:param dtype: torch.cuda.FloatTensor or torch.FloatTensor
Whether or not to use cuda.
:param alexnet_path: str
pretrained alexnet parameters
:param batch_first: bool
if the first position of array is batch or not
:param bias: bool
Whether or not to add the bias.
:param return_all_layers: bool
if return hidden and cell states for all layers
"""
super(ConvGRU, self).__init__()
# Make sure that both `kernel_size` and `hidden_dim` are lists having len == num_layers
kernel_size = self._extend_for_multilayer(kernel_size, num_layers)
hidden_dim = self._extend_for_multilayer(hidden_dim, num_layers)
if not len(kernel_size) == len(hidden_dim) == num_layers:
raise ValueError('Inconsistent list length.')
self.height, self.width = input_size
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.kernel_size = kernel_size
self.dtype = dtype
self.num_layers = num_layers
self.batch_first = batch_first
self.bias = bias
self.return_all_layers = return_all_layers
cell_list = []
for i in range(0, self.num_layers):
cur_input_dim = input_dim if i == 0 else hidden_dim[i - 1]
cell_list.append(ConvGRUCell(input_size=(self.height, self.width),
input_dim=cur_input_dim,
hidden_dim=self.hidden_dim[i],
kernel_size=self.kernel_size[i],
bias=self.bias,
dtype=self.dtype))
# convert python list to pytorch module
self.cell_list = nn.ModuleList(cell_list)
def forward(self, input_tensor, hidden_state=None):
"""
:param input_tensor: (b, t, c, h, w) or (t,b,c,h,w) depends on if batch first or not
extracted features from alexnet
:param hidden_state:
:return: layer_output_list, last_state_list
"""
if not self.batch_first:
# (t, b, c, h, w) -> (b, t, c, h, w)
input_tensor = input_tensor.permute(1, 0, 2, 3, 4)
# Implement stateful ConvLSTM
if hidden_state is not None:
raise NotImplementedError()
else:
hidden_state = self._init_hidden(batch_size=input_tensor.size(0))
layer_output_list = []
last_state_list = []
seq_len = input_tensor.size(1)
cur_layer_input = input_tensor
for layer_idx in range(self.num_layers):
h = hidden_state[layer_idx]
output_inner = []
for t in range(seq_len):
# input current hidden and cell state then compute the next hidden and cell state through ConvLSTMCell forward function
h = self.cell_list[layer_idx](input_tensor=cur_layer_input[:, t, :, :, :], # (b,t,c,h,w)
h_cur=h)
output_inner.append(h)
layer_output = torch.stack(output_inner, dim=1)
cur_layer_input = layer_output
# print("output tensor's device: ", layer_output.get_device())
layer_output_list.append(layer_output)
last_state_list.append([h])
if not self.return_all_layers:
layer_output_list = layer_output_list[-1:]
last_state_list = last_state_list[-1:]
return layer_output_list, last_state_list
def _init_hidden(self, batch_size):
init_states = []
for i in range(self.num_layers):
init_states.append(self.cell_list[i].init_hidden(batch_size))
return init_states
@staticmethod
def _check_kernel_size_consistency(kernel_size):
if not (isinstance(kernel_size, tuple) or
(isinstance(kernel_size, list) and all([isinstance(elem, tuple) for elem in kernel_size]))):
raise ValueError('`kernel_size` must be tuple or list of tuples')
@staticmethod
def _extend_for_multilayer(param, num_layers):
if not isinstance(param, list):
param = [param] * num_layers
return param
class ConvGRULinear(nn.Module):
def __init__(self, noise_size, ch0, time_steps, input_size, hidden_dim, kernel_size, num_layers,
dtype, batch_first=False, bias=True, return_all_layers=False, linearBias=True):
super(ConvGRULinear, self).__init__()
self.ch0=ch0
self.input_size=input_size
self.time=time_steps
self.convgru = ConvGRU(input_size=input_size,
input_dim=self.ch0,
hidden_dim=hidden_dim,
kernel_size=kernel_size,
num_layers=num_layers,
dtype=dtype,
batch_first=batch_first,
bias = bias,
return_all_layers = return_all_layers)
self.linear = nn.Linear(noise_size, self.ch0*self.input_size[0]*self.input_size[1],bias=linearBias)
def forward(self, input_vectors):
output_vectors=self.linear(input_vectors)
# print('ch0,input_size',self.ch0,self.input_size)
# print('output vectors shape', output_vectors.shape)
input_features=(output_vectors.view(-1,self.ch0,*self.input_size)).repeat(self.time,1,1,1,1).permute(1,0,2,3,4)
# print('input features shape', input_features.shape)
layer_output_list, last_state_list = self.convgru(input_features)
return layer_output_list, last_state_list