This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicConvLSTMCell.py
210 lines (181 loc) · 8.41 KB
/
BasicConvLSTMCell.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
# Oiginal file
# https://github.com/loliverhennigh/Convolutional-LSTM-in-Tensorflow/blob/master/BasicConvLSTMCell.py
import tensorflow as tf
class ConvRNNCell(object):
"""Abstract object representing an Convolutional RNN cell.
"""
def __call__(self, inputs, state, z, scope=None):
"""Run this RNN cell on inputs, starting from the given state.
"""
raise NotImplementedError("Abstract method")
@property
def state_size(self):
"""size(s) of state(s) used by this cell.
"""
raise NotImplementedError("Abstract method")
@property
def output_size(self):
"""Integer or TensorShape: size of outputs produced by this cell."""
raise NotImplementedError("Abstract method")
def zero_state(self, batch_size, dtype):
"""Return zero-filled state tensor(s).
Args:
batch_size: int, float, or unit Tensor representing the batch size.
dtype: the data type to use for the state.
Returns:
tensor of shape '[batch_size x shape[0] x shape[1] x num_features]
filled with zeros
"""
shape = self.shape
num_features = self.num_features
zeros = tf.zeros([batch_size, shape[0], shape[1], num_features * 2])
return zeros
class BasicConvLSTMCell(ConvRNNCell):
"""Basic Conv LSTM recurrent network cell. The
"""
def __init__(self, shape, filter_size, num_features, forget_bias=1.0, input_size=None,
state_is_tuple=True, activation=tf.nn.tanh):
"""Initialize the basic Conv LSTM cell.
Args:
shape: int tuple thats the height and width of the cell
filter_size: int tuple thats the height and width of the filter
num_features: int thats the depth of the cell
forget_bias: float, The bias added to forget gates (see above).
input_size: Deprecated and unused.
state_is_tuple: If True, accepted and returned states are 2-tuples of
the `c_state` and `m_state`. If False, they are concatenated
along the column axis. The latter behavior will soon be deprecated.
activation: Activation function of the inner states.
"""
# if not state_is_tuple:
# logging.warn("%s: Using a concatenated state is slower and will soon be "
# "deprecated. Use state_is_tuple=True.", self)
if input_size is not None:
logging.warn("%s: The input_size parameter is deprecated.", self)
self.shape = shape
self.filter_size = filter_size
self.num_features = num_features
self._forget_bias = forget_bias
self._state_is_tuple = state_is_tuple
self._activation = activation
@property
def state_size(self):
return (LSTMStateTuple(self._num_units, self._num_units)
if self._state_is_tuple else 2 * self._num_units)
@property
def output_size(self):
return self._num_units
def __call__(self, inputs, state, z, scope=None):
"""Long short-term memory cell (LSTM)."""
with tf.compat.v1.variable_scope(scope or type(self).__name__): # "BasicLSTMCell"
# Parameters of gates are concatenated into one multiply for efficiency.
if self._state_is_tuple:
c, h = state
else:
c, h = tf.split(axis=3, num_or_size_splits=2, value=state)
concat0 = _conv_linear_extended([inputs], self.filter_size, self.num_features * 4, False, strides_out=2, scope='inputs_calc')
concat1 = _conv_linear_extended([h, z], self.filter_size, self.num_features * 4, True, scope='internal_calc')
concat = concat0 + concat1
#concat = _conv_linear([inputs, h], self.filter_size, self.num_features * 4, True)
# i = input_gate, j = new_input, f = forget_gate, o = output_gate
i, j, f, o = tf.split(axis=3, num_or_size_splits=4, value=concat)
new_c = (c * tf.nn.sigmoid(f + self._forget_bias) + tf.nn.sigmoid(i) * self._activation(j))
new_h = self._activation(new_c) * tf.nn.sigmoid(o)
if self._state_is_tuple:
new_state = tf.tuple(tensors=[new_c, new_h])
else:
new_state = tf.concat(axis=3, values=[new_c, new_h])
# Also include gate values
gate_states = tf.concat(axis=0, values=[i, j, f, o])
return new_h, new_state, gate_states
def _conv_linear(args, filter_size, num_features, bias, bias_start=0.0, scope=None):
"""convolution:
Args:
args: a 4D Tensor or a list of 4D, batch x n, Tensors.
filter_size: int tuple of filter height and width.
num_features: int, number of features.
bias_start: starting value to initialize the bias; 0 by default.
scope: VariableScope for the created subgraph; defaults to "Linear".
Returns:
A 4D Tensor with shape [batch h w num_features]
Raises:
ValueError: if some of the arguments has unspecified or wrong shape.
"""
# Calculate the total size of arguments on dimension 1.
total_arg_size_depth = 0
shapes = [a.get_shape().as_list() for a in args]
for shape in shapes:
if len(shape) != 4:
raise ValueError("Linear is expecting 4D arguments: %s" % str(shapes))
if not shape[3]:
raise ValueError("Linear expects shape[4] of arguments: %s" % str(shapes))
else:
total_arg_size_depth += shape[3]
dtype = [a.dtype for a in args][0]
# Now the computation.
with tf.compat.v1.variable_scope(scope or "Conv"):
matrix = tf.compat.v1.get_variable(
"Matrix", [filter_size[0], filter_size[1], total_arg_size_depth, num_features], dtype=dtype)
if len(args) == 1:
res = tf.nn.conv2d(input=args[0], filters=matrix, strides=[1, 1, 1, 1], padding='SAME')
else:
res = tf.nn.conv2d(input=tf.concat(axis=3, values=args), filters=matrix, strides=[1, 1, 1, 1], padding='SAME')
if not bias:
return res
bias_term = tf.compat.v1.get_variable(
"Bias", [num_features],
dtype=dtype,
initializer=tf.compat.v1.constant_initializer(
bias_start, dtype=dtype))
return res + bias_term
def _conv_linear_extended(args, filter_size, num_features, bias, bias_start=0.0, padding_type='SAME', scope=None, strides_out=1.0):
"""
Extended convolution function 171010
:param args:
:param filter_size:
:param num_features:
:param bias:
:param bias_start:
:param scope:
:strides_out: select strides size
:return:
"""
"""convolution:
Args:
args: a 4D Tensor or a list of 4D, batch x n, Tensors.
filter_size: int tuple of filter height and width.
num_features: int, number of feature maps of current layer.
bias_start: starting value to initialize the bias; 0 by default.
scope: VariableScope for the created subgraph; defaults to "Linear".
Returns:
A 4D Tensor with shape [batch, FM_h, FM_w, num_features]
Raises:
ValueError: if some of the arguments has unspecified or wrong shape.
"""
# Calculate the total size of arguments on dimension 1.
total_arg_size_depth = 0
shapes = [a.get_shape().as_list() for a in args]
for shape in shapes:
if len(shape) != 4:
raise ValueError("Linear is expecting 4D arguments: %s" % str(shapes))
if not shape[3]:
raise ValueError("Linear expects shape[4] of arguments: %s" % str(shapes))
else:
total_arg_size_depth += shape[3] # the number of feature maps of input(args)
dtype = [a.dtype for a in args][0]
# Now the computation.
with tf.compat.v1.variable_scope(scope or "Conv"):
matrix = tf.compat.v1.get_variable(
"Matrix", [filter_size[0], filter_size[1], total_arg_size_depth, num_features], dtype=dtype)
if len(args) == 1:
res = tf.nn.conv2d(input=args[0], filters=matrix, strides=[1, strides_out, strides_out, 1], padding=padding_type)
else:
res = tf.nn.conv2d(input=tf.concat(args, 3), filters=matrix, strides=[1, strides_out, strides_out, 1], padding=padding_type)
if not bias:
return res
bias_term = tf.compat.v1.get_variable(
"Bias", [num_features],
dtype=dtype,
initializer=tf.compat.v1.constant_initializer(
bias_start, dtype=dtype))
return res + bias_term