5
5
from keras .layers .pooling import MaxPooling2D
6
6
from keras .layers .merge import Multiply
7
7
from keras .regularizers import l2
8
+ from keras .initializers import random_normal ,constant
8
9
9
10
def relu (x ): return Activation ('relu' )(x )
10
11
@@ -14,7 +15,9 @@ def conv(x, nf, ks, name, weight_decay):
14
15
15
16
x = Conv2D (nf , (ks , ks ), padding = 'same' , name = name ,
16
17
kernel_regularizer = kernel_reg ,
17
- bias_regularizer = bias_reg )(x )
18
+ bias_regularizer = bias_reg ,
19
+ kernel_initializer = random_normal (stddev = 0.01 ),
20
+ bias_initializer = constant (0.0 ))(x )
18
21
return x
19
22
20
23
def pooling (x , ks , st , name ):
@@ -62,7 +65,7 @@ def vgg_block(x, weight_decay):
62
65
return x
63
66
64
67
65
- def stage1_block (x , x1 , x2 , num_p , branch , weight_decay ):
68
+ def stage1_block (x , num_p , branch , weight_decay ):
66
69
# Block 1
67
70
x = conv (x , 128 , 3 , "Mconv1_stage1_L%d" % branch , (weight_decay , 0 ))
68
71
x = relu (x )
@@ -74,17 +77,10 @@ def stage1_block(x, x1, x2, num_p, branch, weight_decay):
74
77
x = relu (x )
75
78
x = conv (x , num_p , 1 , "Mconv5_stage1_L%d" % branch , (weight_decay , 0 ))
76
79
77
- w_name = "weight_stage1_L%d" % branch
78
- if num_p == 38 :
79
- w = Multiply (name = w_name )([x , x1 ]) # vec_weight
80
-
81
- else :
82
- w = Multiply (name = w_name )([x , x2 ]) # vec_heat
83
-
84
- return x , w
80
+ return x
85
81
86
82
87
- def stageT_block (x , x1 , x2 , num_p , stage , branch , weight_decay ):
83
+ def stageT_block (x , num_p , stage , branch , weight_decay ):
88
84
# Block 1
89
85
x = conv (x , 128 , 7 , "Mconv1_stage%d_L%d" % (stage , branch ), (weight_decay , 0 ))
90
86
x = relu (x )
@@ -100,17 +96,20 @@ def stageT_block(x, x1, x2, num_p, stage, branch, weight_decay):
100
96
x = relu (x )
101
97
x = conv (x , num_p , 1 , "Mconv7_stage%d_L%d" % (stage , branch ), (weight_decay , 0 ))
102
98
99
+ return x
100
+
101
+
102
+ def apply_mask (x , mask1 , mask2 , num_p , stage , branch ):
103
103
w_name = "weight_stage%d_L%d" % (stage , branch )
104
104
if num_p == 38 :
105
- w = Multiply (name = w_name )([x , x1 ]) # vec_weight
105
+ w = Multiply (name = w_name )([x , mask1 ]) # vec_weight
106
106
107
107
else :
108
- w = Multiply (name = w_name )([x , x2 ]) # vec_heat
108
+ w = Multiply (name = w_name )([x , mask2 ]) # vec_heat
109
+ return w
109
110
110
- return x , w
111
111
112
-
113
- def get_model (training = True , weight_decay = None ):
112
+ def get_training_model (weight_decay ):
114
113
115
114
stages = 6
116
115
np_branch1 = 38
@@ -131,38 +130,77 @@ def get_model(training=True, weight_decay=None):
131
130
inputs .append (vec_weight_input )
132
131
inputs .append (heat_weight_input )
133
132
134
- img_normalized = Lambda (lambda x : x / 127.5 - 1.0 )(img_input )
133
+ img_normalized = Lambda (lambda x : x / 256 - 0.5 )(img_input )
135
134
136
135
# VGG
137
136
stage0_out = vgg_block (img_normalized , weight_decay )
138
137
139
- # stage 1
140
- stage1_branch1_out ,w1 = stage1_block (stage0_out , vec_weight_input ,
141
- heat_weight_input , np_branch1 , 1 , weight_decay )
142
- stage1_branch2_out ,w2 = stage1_block (stage0_out , vec_weight_input ,
143
- heat_weight_input , np_branch2 , 2 , weight_decay )
138
+ # stage 1 - branch 1 (PAF)
139
+ stage1_branch1_out = stage1_block (stage0_out , np_branch1 , 1 , weight_decay )
140
+ w1 = apply_mask (stage1_branch1_out , vec_weight_input , heat_weight_input , np_branch1 , 1 , 1 )
141
+
142
+ # stage 1 - branch 2 (confidence maps)
143
+ stage1_branch2_out = stage1_block (stage0_out , np_branch2 , 2 , weight_decay )
144
+ w2 = apply_mask (stage1_branch2_out , vec_weight_input , heat_weight_input , np_branch2 , 1 , 2 )
145
+
144
146
x = Concatenate ()([stage1_branch1_out , stage1_branch2_out , stage0_out ])
145
147
146
148
outputs .append (w1 )
147
149
outputs .append (w2 )
148
150
149
- # stage t >= 2
150
- #stageT_branch1_out = None
151
- #stageT_branch2_out = None
151
+ # stage sn >= 2
152
152
for sn in range (2 , stages + 1 ):
153
- stageT_branch1_out , w1 = stageT_block (x , vec_weight_input ,
154
- heat_weight_input , np_branch1 , sn , 1 , weight_decay )
155
- stageT_branch2_out , w2 = stageT_block (x , vec_weight_input ,
156
- heat_weight_input , np_branch2 , sn , 2 , weight_decay )
153
+ # stage SN - branch 1 (PAF)
154
+ stageT_branch1_out = stageT_block (x , np_branch1 , sn , 1 , weight_decay )
155
+ w1 = apply_mask (stageT_branch1_out , vec_weight_input , heat_weight_input , np_branch1 , sn , 1 )
156
+
157
+ # stage SN - branch 2 (confidence maps)
158
+ stageT_branch2_out = stageT_block (x , np_branch2 , sn , 2 , weight_decay )
159
+ w2 = apply_mask (stageT_branch2_out , vec_weight_input , heat_weight_input , np_branch2 , sn , 2 )
157
160
158
161
outputs .append (w1 )
159
162
outputs .append (w2 )
160
163
161
164
if (sn < stages ):
162
165
x = Concatenate ()([stageT_branch1_out , stageT_branch2_out , stage0_out ])
163
166
164
- #outputs.insert(0, stageT_branch1_out)
165
- #outputs.insert(1, stageT_branch2_out)
166
167
model = Model (inputs = inputs , outputs = outputs )
167
168
169
+ return model
170
+
171
+
172
+ def get_testing_model ():
173
+ stages = 6
174
+ np_branch1 = 38
175
+ np_branch2 = 19
176
+
177
+ img_input_shape = (None , None , 3 )
178
+
179
+ img_input = Input (shape = img_input_shape )
180
+
181
+ img_normalized = Lambda (lambda x : x / 256 - 0.5 )(img_input ) # [-0.5, 0.5]
182
+
183
+ # VGG
184
+ stage0_out = vgg_block (img_normalized , None )
185
+
186
+ # stage 1 - branch 1 (PAF)
187
+ stage1_branch1_out = stage1_block (stage0_out , np_branch1 , 1 , None )
188
+
189
+ # stage 1 - branch 2 (confidence maps)
190
+ stage1_branch2_out = stage1_block (stage0_out , np_branch2 , 2 , None )
191
+
192
+ x = Concatenate ()([stage1_branch1_out , stage1_branch2_out , stage0_out ])
193
+
194
+ # stage t >= 2
195
+ stageT_branch1_out = None
196
+ stageT_branch2_out = None
197
+ for sn in range (2 , stages + 1 ):
198
+ stageT_branch1_out = stageT_block (x , np_branch1 , sn , 1 , None )
199
+ stageT_branch2_out = stageT_block (x , np_branch2 , sn , 2 , None )
200
+
201
+ if (sn < stages ):
202
+ x = Concatenate ()([stageT_branch1_out , stageT_branch2_out , stage0_out ])
203
+
204
+ model = Model (inputs = [img_input ], outputs = [stageT_branch1_out , stageT_branch2_out ])
205
+
168
206
return model
0 commit comments