-
Notifications
You must be signed in to change notification settings - Fork 4
/
conv4d.py
304 lines (255 loc) · 10.5 KB
/
conv4d.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
291
292
293
294
295
296
297
298
299
300
301
302
# -*- coding: UTF-8 -*-
import tensorflow as tf
# Credits to: https://github.com/funkey/conv4d
def conv4d(
input,
filters,
kernel_size,
strides=(1, 1, 1, 1),
padding='same',
data_format='channels_last',
dilation_rate=(1, 1, 1, 1),
activation=None,
use_bias=True,
kernel_initializer=None,
bias_initializer=tf.zeros_initializer(),
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
trainable=True,
name=None,
Stride_Temp=1,
Stride_Spat=1,
BATCH=10,
placeholders =None,
reuse=None):
# check arguments
assert len(input.get_shape().as_list()) == 6, (
"Tensor of shape (b, c, l, d, h, w) expected")
assert len(kernel_size) == 4, "4D kernel size expected"
assert data_format == 'channels_first', (
"Data format other than 'channels_first' not yet implemented")
assert dilation_rate == (1, 1, 1, 1), (
"Dilation rate other than 1 not yet implemented")
if not name:
name = 'conv4d'
# input, kernel, and output sizes
(b, c_i, l_i, d_i, h_i, w_i) = tuple(input.get_shape().as_list())
(l_k, d_k, h_k, w_k) = kernel_size
b=BATCH
# output size for 'valid' convolution
if padding == 'valid':
(l_o, d_o, h_o, w_o) = (
l_i - l_k + 1,
d_i - d_k + 1,
h_i - h_k + 1,
w_i - w_k + 1
)
else:
(l_o, d_o, h_o, w_o) = (l_i, d_i, h_i, w_i)
# output tensors for each 3D frame
frame_results = [ None ]*l_o
# convolve each kernel frame i with each input frame j
for i in range(l_k):
# reuse variables of previous 3D convolutions for the same kernel
# frame (or if the user indicated to have all variables reused)
reuse_kernel = reuse
Count = 0
for j in range(l_i):
# add results to this output frame
out_frame = j - (i - l_k/2) - (l_i - l_o)/2
if (out_frame < 0 or out_frame >= l_o):
continue
if (Count % Stride_Temp == 0): #Apply Temporal Stride here
# convolve input frame j with kernel frame i
frame_conv3d = tf.layers.conv3d(
tf.reshape(input[:,:,j,:], (b, c_i, d_i, h_i, w_i)),
filters,
kernel_size=(d_k, h_k, w_k),
strides=(Stride_Spat, Stride_Spat, Stride_Spat),
padding=padding,
data_format='channels_first',
activation=None,
use_bias=use_bias,
kernel_initializer=kernel_initializer,
bias_initializer=bias_initializer,
kernel_regularizer=kernel_regularizer,
bias_regularizer=bias_regularizer,
activity_regularizer=activity_regularizer,
trainable=trainable,
name=name + '_3dchan%d'%i,
reuse=reuse_kernel)
Count = Count + 1
# subsequent frame convolutions should use the same kernel
reuse_kernel = True
Frame_Result_check = ((frame_results[int(out_frame)])) #Check if its empty? (For first Iteration)
# Add Results of Convolutions with the same output frame
if Frame_Result_check is None:
frame_results[int(out_frame)] = frame_conv3d
else:
frame_results[int(out_frame)] += frame_conv3d
else:
Count = Count + 1
output = tf.stack(frame_results[0::Stride_Temp], axis=2) #Stack all Resuls into the temporal Dimension (axis=2)
if activation:
output = activation(output)
return output
def conv4d_BatchNorm(
input,
filters,
kernel_size,
strides=[1, 1, 1, 1],
padding='same',
data_format='channels_last',
dilation_rate=(1, 1, 1, 1),
activation=None,
use_bias=True,
kernel_initializer=None,
bias_initializer=tf.zeros_initializer(),
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
trainable=True,
name=None,
BATCH=20,
placeholders = None,
reuse=None):
# check arguments
assert len(input.get_shape().as_list()) == 6, (
"Tensor of shape (b, c, l, d, h, w) expected")
assert len(kernel_size) == 4, "4D kernel size expected"
assert data_format == 'channels_first', (
"Data format other than 'channels_first' not yet implemented")
assert dilation_rate == (1, 1, 1, 1), (
"Dilation rate other than 1 not yet implemented")
if not name:
name = 'conv4d'
#print("Input shape",input.get_shape())
# input, kernel, and output sizes
(b, c_i, l_i, d_i, h_i, w_i) = tuple(input.get_shape().as_list())
(l_k, d_k, h_k, w_k) = kernel_size
b=BATCH
#print("Input slice",input[:,:,0,:].get_shape())
#print("First iteration shape",tf.reshape(input[:,:,0,:], (b, c_i, d_i, h_i, w_i)).get_shape())
# output size for 'valid' convolution
if padding == 'valid':
(l_o, d_o, h_o, w_o) = (
l_i - l_k + 1,
d_i - d_k + 1,
h_i - h_k + 1,
w_i - w_k + 1
)
else:
(l_o, d_o, h_o, w_o) = (l_i, d_i, h_i, w_i)
# output tensors for each 3D frame
frame_results = [ None ]*l_o
# convolve each kernel frame i with each input frame j
for i in range(l_k):
# reuse variables of previous 3D convolutions for the same kernel
# frame (or if the user indicated to have all variables reused)
reuse_kernel = reuse
#Count = 0
for j in range(l_i):
# add results to this output frame
out_frame = j - (i - l_k/2) - (l_i - l_o)/2
if (out_frame < 0 or out_frame >= l_o) or (int(out_frame) % strides[0] != 0):
continue
#if (Count % strides[0] == 0): #Apply Temporal Stride here
# convolve input frame j with kernel frame i
#print("i",i,"j",j,"out frame",out_frame)
frame_conv3d = tf.layers.conv3d(
tf.reshape(input[:,:,j,:], (b, c_i, d_i, h_i, w_i)),
filters,
kernel_size=(d_k, h_k, w_k),
strides=(strides[1], strides[2], strides[3]),
padding=padding,
data_format='channels_first',
activation=None,
use_bias=use_bias,
kernel_initializer=kernel_initializer,
bias_initializer=bias_initializer,
kernel_regularizer=kernel_regularizer,
bias_regularizer=bias_regularizer,
activity_regularizer=activity_regularizer,
trainable=trainable,
name=name + '_3dchan%d'%i,
reuse=reuse_kernel)
#Count = Count + 1
# subsequent frame convolutions should use the same kernel
reuse_kernel = True
Frame_Result_check = ((frame_results[int(out_frame)])) #Check if its empty? (For first Iteration)
# Add Results of Convolutions with the same output frame
if Frame_Result_check is None:
frame_results[int(out_frame)] = frame_conv3d
else:
frame_results[int(out_frame)] += frame_conv3d
#print("fram res", frame_results)
#else:
# Count = Count + 1
output = tf.stack(frame_results[0::strides[0]], axis=2) #Stack all Resuls into the temporal Dimension (axis=2)
output = tf.contrib.layers.batch_norm(output, center=True, scale=True, is_training = placeholders['train_state'], epsilon=0.0001, decay=0.9, activation_fn=None, data_format='NCHW', updates_collections=tf.GraphKeys.UPDATE_OPS, fused=False)
if activation:
output = activation(output)
return output
def pool4d(
input,
kernel_size,
strides=(1, 1, 1, 1),
padding='SAME',
data_format= 'NCDHW',
name=None,
BATCH=10,
placeholders =None):
# check arguments
assert len(input.get_shape().as_list()) == 6, (
"Tensor of shape (b, c, l, d, h, w) expected")
assert len(kernel_size) == 4, "4D kernel size expected"
if not name:
name = 'pool3d'
# input, kernel, and output sizes
(b, c_i, l_i, d_i, h_i, w_i) = tuple(input.get_shape().as_list())
(l_k, d_k, h_k, w_k) = kernel_size
b=BATCH
# output size for 'valid' pooling
if padding == 'valid':
(l_o, d_o, h_o, w_o) = (
l_i - l_k + 1,
d_i - d_k + 1,
h_i - h_k + 1,
w_i - w_k + 1
)
else:
(l_o, d_o, h_o, w_o) = (l_i, d_i, h_i, w_i)
# output tensors for each 3D frame
frame_results = [ None ]*l_o
# convolve each kernel frame i with each input frame j
for i in range(l_k):
Count = 0 # counter for temporal Stride
for j in range(l_i):
# add results to this output frame
out_frame = j - (i - l_k/2) - (l_i - l_o)/2
if (out_frame < 0 or out_frame >= l_o):
continue
if (Count % strides[0] == 0): #Apply Temporal Stride here
# convolve input frame j with kernel frame i
frame_pool3d = tf.nn.pool(
tf.reshape(input[:,:,j,:], (b, c_i, d_i, h_i, w_i)),
window_shape= (d_k, h_k, w_k),
pooling_type = "AVG",
padding=padding,
strides= (strides[1], strides[2], strides[3]),
name= name + '_3dchan%d'%i,
data_format='NCDHW')
Count = Count + 1
# subsequent frame convolutions should use the same kernel
Frame_Result_check = ((frame_results[int(out_frame)])) #Check if its empty? (For first Iteration)
# Add Results of Convolutions with the same output frame
if Frame_Result_check is None:
frame_results[int(out_frame)] = frame_pool3d
else:
frame_results[int(out_frame)] += frame_pool3d
else:
Count = Count + 1
output = tf.stack(frame_results[0::strides[0]], axis=2) #Stack all Resuls into the temporal Dimension (axis=2)
output = output / l_k #Scale for Average for temporal Window
return output