-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathST3DNet.py
More file actions
198 lines (159 loc) · 6.59 KB
/
Copy pathST3DNet.py
File metadata and controls
198 lines (159 loc) · 6.59 KB
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
from keras.layers import (
Input,
Activation,
Dense,
Reshape
)
import keras
from keras.layers.convolutional import Convolution2D
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras.layers.convolutional import Conv3D
from keras.engine.topology import Layer
import numpy as np
from keras import backend as K
import tensorflow as tf
K.set_image_data_format('channels_first')
class iLayer(Layer):
'''
final weighted sum
'''
def __init__(self, **kwargs):
super(iLayer, self).__init__(**kwargs)
def build(self, input_shape):
initial_weight_value = np.random.random(input_shape[1:])
self.W = K.variable(initial_weight_value)
self.trainable_weights = [self.W]
def call(self, x, mask=None):
return x * self.W
def get_output_shape_for(self, input_shape):
return input_shape
class Recalibration(Layer):
'''
channel-wise recalibration for closeness component
'''
def __init__(self, **kwargs):
super(Recalibration, self).__init__(**kwargs)
def build(self, input_shape):
'''
input_shape: (batch, c,h,w)
'''
initial_weight_value = np.random.random((input_shape[1], 2, input_shape[2], input_shape[3])) # (c,2,h,w)
self.W = K.variable(initial_weight_value)
self.trainable_weights = [self.W]
super(Recalibration, self).build(input_shape)
def call(self, x):
'''
x: (batch, c, h,w)
'''
double_x = tf.stack([x,x], axis=2) # [(batch,c,h,w), (batch, c,h,w)] => (batch,c,2,h,w)
return tf.reduce_sum(double_x*self.W, 1) # (batch,2,h,w)
def compute_output_shape(self, input_shape):
return (input_shape[0], 2,input_shape[2],input_shape[3]) # (batch_size,2,h,w)
class Recalibration_T(Layer):
'''
channel-wise recalibration for weekly period component:
'''
def __init__(self,channel,**kwargs):
super(Recalibration_T, self).__init__(**kwargs)
self.channel = channel
def build(self, input_shape):
'''
input_shape: (batch, c, h, w)
'''
initial_weight_value = np.random.random(input_shape[1]*2) # [2c,]:because output 2 channel
self.W = K.variable(initial_weight_value)
self.trainable_weights = [self.W]
super(Recalibration_T, self).build(input_shape)
def call(self, x):
'''
x: (batch, c, h, w)
'''
nb_channel = self.channel
_, _, map_height, map_width = x.shape
W = tf.reshape(tf.tile(self.W, [map_height*map_width]),(nb_channel, 2, map_height, map_width)) # tile:sharing channel-wsie weight on different positions in the weekly-period recalibration block
double_x = tf.stack([x,x], axis=2) # stack [(batch, c,h, w)] = (batch, c, 2, h,w)
return tf.reduce_sum(double_x*W, 1) # (batch, 2, h, w)
def compute_output_shape(self, input_shape):
return (input_shape[0], 2,input_shape[2],input_shape[3]) # (batch_size,2,h,w)
def _shortcut(input, residual):
return keras.layers.Add()([input, residual])
def _bn_relu_conv(nb_filter, nb_row, nb_col, subsample=(1, 1), bn=False):
def f(input):
'''
input: (batch,c,h,w)
'''
if bn:
input = BatchNormalization(mode=0, axis=1)(input)
activation = Activation('relu')(input)
return Convolution2D(nb_filter=nb_filter, nb_row=nb_row, nb_col=nb_col, subsample=subsample, border_mode="same")(activation)
return f
def _residual_unit(nb_filter):
def f(input):
residual = _bn_relu_conv(nb_filter, 3, 3)(input)
residual = _bn_relu_conv(nb_filter, 3, 3)(residual)
return _shortcut(input, residual)
return f
def ResUnits(residual_unit, nb_filter, repetations=1):
def f(input):
for i in range(repetations):
input = residual_unit(nb_filter=nb_filter)(input)
return input
return f
def ST3DNet(c_conf=(6, 2, 16, 8), t_conf=(4, 2, 16, 8), external_dim=8, nb_residual_unit=4):
len_closeness, nb_flow, map_height, map_width = c_conf
# main input
main_inputs = []
outputs = []
if len_closeness > 0:
input = Input(shape=(nb_flow, len_closeness, map_height, map_width)) # (2,t_c,h,w)
main_inputs.append(input)
# Conv1 3D
conv = Conv3D(filters=64, kernel_size=(6, 3, 3), strides=(1, 1, 1), border_mode="same",
kernel_initializer='random_uniform')(input)
conv = Activation("relu")(conv)
# Conv2 3D
conv = Conv3D(filters=64, kernel_size=(3, 3, 3), strides=(3, 1, 1), border_mode="same")(conv)
conv = Activation("relu")(conv)
# Conv3 3D
conv = Conv3D(filters=64, kernel_size=(3, 3, 3), strides=(3, 1, 1), border_mode="same")(conv)
# (filter,1,height,width)
reshape = Reshape((64, map_height, map_width))(conv)
# Residual 2D [nb_residual_unit] Residual Units
residual_output = ResUnits(_residual_unit, nb_filter=64, repetations=nb_residual_unit)(reshape)
output_c = Recalibration()(residual_output)
outputs.append(output_c)
if t_conf is not None:
len_seq, nb_flow, map_height, map_width = t_conf
input = Input(shape=(nb_flow, len_seq, map_height, map_width))
main_inputs.append(input)
conv = Conv3D(nb_filter=8, kernel_dim1=len_seq, kernel_dim2=1, kernel_dim3=1, border_mode="valid")(input)
conv = Activation('relu')(conv)
output_t = Reshape((8, map_height, map_width))(conv)
output_t = Recalibration_T(8)(output_t)
outputs.append(output_t)
# parameter-matrix-based fusion
if len(outputs) == 1:
main_output = outputs[0]
else:
# from .iLayer import iLayer
new_outputs = []
for output in outputs:
new_outputs.append(iLayer()(output))
main_output = keras.layers.Add()(new_outputs)
# fusing with external component
if external_dim != None and external_dim > 0:
# external input
external_input = Input(shape=(external_dim,))
main_inputs.append(external_input)
embedding = Dense(output_dim=10)(external_input)
embedding = Activation('relu')(embedding)
h1 = Dense(output_dim=nb_flow * map_height * map_width)(embedding)
activation = Activation('relu')(h1)
external_output = Reshape((nb_flow, map_height, map_width))(activation)
main_output = keras.layers.Add()([main_output, external_output])
else:
print('external_dim:', external_dim)
main_output = Activation('relu')(main_output)
model = Model(input=main_inputs, output=main_output)
return model