forked from ForgottenOneNyx/Open-Pose-Keras
-
Notifications
You must be signed in to change notification settings - Fork 4
/
model.py
274 lines (207 loc) · 8.57 KB
/
model.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
from keras.models import Model
from keras.layers.merge import Concatenate
from keras.layers import Activation, Input, Lambda
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
from keras.layers.merge import Multiply
from keras.regularizers import l2
from keras.initializers import random_normal, constant
import re
#stages = 6
#np_branch1 = 38
#np_branch2 = 19
def relu(x): return Activation('relu')(x)
def conv(x, nf, ks, name, weight_decay):
kernel_reg = l2(weight_decay[0]) if weight_decay else None
bias_reg = l2(weight_decay[1]) if weight_decay else None
x = Conv2D(nf, (ks, ks), padding='same', name=name,
kernel_regularizer=kernel_reg,
bias_regularizer=bias_reg,
kernel_initializer=random_normal(stddev=0.01),
bias_initializer=constant(0.0))(x)
return x
def pooling(x, ks, st, name):
x = MaxPooling2D((ks, ks), strides=(st, st), name=name)(x)
return x
def vgg_block(x, weight_decay):
# Block 1
x = conv(x, 64, 3, "conv1_1", (weight_decay, 0))
x = relu(x)
x = conv(x, 64, 3, "conv1_2", (weight_decay, 0))
x = relu(x)
x = pooling(x, 2, 2, "pool1_1")
# Block 2
x = conv(x, 128, 3, "conv2_1", (weight_decay, 0))
x = relu(x)
x = conv(x, 128, 3, "conv2_2", (weight_decay, 0))
x = relu(x)
x = pooling(x, 2, 2, "pool2_1")
# Block 3
x = conv(x, 256, 3, "conv3_1", (weight_decay, 0))
x = relu(x)
x = conv(x, 256, 3, "conv3_2", (weight_decay, 0))
x = relu(x)
x = conv(x, 256, 3, "conv3_3", (weight_decay, 0))
x = relu(x)
x = conv(x, 256, 3, "conv3_4", (weight_decay, 0))
x = relu(x)
x = pooling(x, 2, 2, "pool3_1")
# Block 4
x = conv(x, 512, 3, "conv4_1", (weight_decay, 0))
x = relu(x)
x = conv(x, 512, 3, "conv4_2", (weight_decay, 0))
x = relu(x)
# Additional non vgg layers
x = conv(x, 256, 3, "conv4_3_CPM", (weight_decay, 0))
x = relu(x)
x = conv(x, 128, 3, "conv4_4_CPM", (weight_decay, 0))
x = relu(x)
return x
def stage1_block(x, num_p, branch, weight_decay):
# Block 1
x = conv(x, 128, 3, "Mconv1_stage1_L%d" % branch, (weight_decay, 0))
x = relu(x)
x = conv(x, 128, 3, "Mconv2_stage1_L%d" % branch, (weight_decay, 0))
x = relu(x)
x = conv(x, 128, 3, "Mconv3_stage1_L%d" % branch, (weight_decay, 0))
x = relu(x)
x = conv(x, 512, 1, "Mconv4_stage1_L%d" % branch, (weight_decay, 0))
x = relu(x)
x = conv(x, num_p, 1, "Mconv5_stage1_L%d" % branch, (weight_decay, 0))
return x
def stageT_block(x, num_p, stage, branch, weight_decay):
# Block 1
x = conv(x, 128, 7, "Mconv1_stage%d_L%d" % (stage, branch), (weight_decay, 0))
x = relu(x)
x = conv(x, 128, 7, "Mconv2_stage%d_L%d" % (stage, branch), (weight_decay, 0))
x = relu(x)
x = conv(x, 128, 7, "Mconv3_stage%d_L%d" % (stage, branch), (weight_decay, 0))
x = relu(x)
x = conv(x, 128, 7, "Mconv4_stage%d_L%d" % (stage, branch), (weight_decay, 0))
x = relu(x)
x = conv(x, 128, 7, "Mconv5_stage%d_L%d" % (stage, branch), (weight_decay, 0))
x = relu(x)
x = conv(x, 128, 1, "Mconv6_stage%d_L%d" % (stage, branch), (weight_decay, 0))
x = relu(x)
x = conv(x, num_p, 1, "Mconv7_stage%d_L%d" % (stage, branch), (weight_decay, 0))
return x
def apply_mask(x, mask1, mask2, num_p, stage, branch, np_branch1, np_branch2):
w_name = "weight_stage%d_L%d" % (stage, branch)
# TODO: we have branch number here why we made so strange check
assert np_branch1 != np_branch2 # we selecting branches by number of pafs, if they accidentally became the same it will be disaster
if num_p == np_branch1:
w = Multiply(name=w_name)([x, mask1]) # vec_weight
elif num_p == np_branch2:
w = Multiply(name=w_name)([x, mask2]) # vec_heat
else:
assert False, "wrong number of layers num_p=%d " % num_p
return w
def get_training_model(weight_decay, np_branch1, np_branch2, stages = 6, gpus = None):
img_input_shape = (None, None, 3)
vec_input_shape = (None, None, np_branch1)
heat_input_shape = (None, None, np_branch2)
inputs = []
outputs = []
img_input = Input(shape=img_input_shape)
vec_weight_input = Input(shape=vec_input_shape)
heat_weight_input = Input(shape=heat_input_shape)
inputs.append(img_input)
if np_branch1 > 0:
inputs.append(vec_weight_input)
if np_branch2 > 0:
inputs.append(heat_weight_input)
#img_normalized = Lambda(lambda x: x / 256 - 0.5)(img_input) # [-0.5, 0.5]
img_normalized = img_input # will be done on augmentation stage
# VGG
stage0_out = vgg_block(img_normalized, weight_decay)
# stage 1 - branch 1 (PAF)
new_x = []
if np_branch1 > 0:
stage1_branch1_out = stage1_block(stage0_out, np_branch1, 1, weight_decay)
w1 = apply_mask(stage1_branch1_out, vec_weight_input, heat_weight_input, np_branch1, 1, 1, np_branch1, np_branch2)
outputs.append(w1)
new_x.append(stage1_branch1_out)
# stage 1 - branch 2 (confidence maps)
if np_branch2 > 0:
stage1_branch2_out = stage1_block(stage0_out, np_branch2, 2, weight_decay)
w2 = apply_mask(stage1_branch2_out, vec_weight_input, heat_weight_input, np_branch2, 1, 2, np_branch1, np_branch2)
outputs.append(w2)
new_x.append(stage1_branch2_out)
new_x.append(stage0_out)
x = Concatenate()(new_x)
# stage sn >= 2
for sn in range(2, stages + 1):
new_x = []
# stage SN - branch 1 (PAF)
if np_branch1 > 0:
stageT_branch1_out = stageT_block(x, np_branch1, sn, 1, weight_decay)
w1 = apply_mask(stageT_branch1_out, vec_weight_input, heat_weight_input, np_branch1, sn, 1, np_branch1, np_branch2)
outputs.append(w1)
new_x.append(stageT_branch1_out)
# stage SN - branch 2 (confidence maps)
if np_branch2 > 0:
stageT_branch2_out = stageT_block(x, np_branch2, sn, 2, weight_decay)
w2 = apply_mask(stageT_branch2_out, vec_weight_input, heat_weight_input, np_branch2, sn, 2, np_branch1, np_branch2)
outputs.append(w2)
new_x.append(stageT_branch2_out)
new_x.append(stage0_out)
if sn < stages:
x = Concatenate()(new_x)
model = Model(inputs=inputs, outputs=outputs)
return model
def get_lrmult(model):
# setup lr multipliers for conv layers
lr_mult = dict()
for layer in model.layers:
if isinstance(layer, Conv2D):
# stage = 1
if re.match("Mconv\d_stage1.*", layer.name):
kernel_name = layer.weights[0].name
bias_name = layer.weights[1].name
lr_mult[kernel_name] = 1
lr_mult[bias_name] = 2
# stage > 1
elif re.match("Mconv\d_stage.*", layer.name):
kernel_name = layer.weights[0].name
bias_name = layer.weights[1].name
lr_mult[kernel_name] = 4
lr_mult[bias_name] = 8
# vgg
else:
print("matched as vgg layer", layer.name)
kernel_name = layer.weights[0].name
bias_name = layer.weights[1].name
lr_mult[kernel_name] = 1
lr_mult[bias_name] = 2
return lr_mult
def get_testing_model(np_branch1, np_branch2, stages = 6):
img_input_shape = (None, None, 3)
img_input = Input(shape=img_input_shape)
img_normalized = Lambda(lambda x: x / 256 - 0.5)(img_input) # [-0.5, 0.5]
# VGG
stage0_out = vgg_block(img_normalized, None)
stages_out = []
# stage 1 - branch 1 (PAF)
if np_branch1 > 0:
stage1_branch1_out = stage1_block(stage0_out, np_branch1, 1, None)
stages_out.append(stage1_branch1_out)
# stage 1 - branch 2 (confidence maps)
if np_branch2 > 0:
stage1_branch2_out = stage1_block(stage0_out, np_branch2, 2, None)
stages_out.append(stage1_branch2_out)
x = Concatenate()(stages_out + [stage0_out])
# stage t >= 2
stageT_branch1_out = None
stageT_branch2_out = None
for sn in range(2, stages + 1):
stages_out = []
if np_branch1 > 0:
stageT_branch1_out = stageT_block(x, np_branch1, sn, 1, None)
stages_out.append(stageT_branch1_out)
if np_branch2 > 0:
stageT_branch2_out = stageT_block(x, np_branch2, sn, 2, None)
stages_out.append(stageT_branch2_out)
if sn < stages:
x = Concatenate()(stages_out + [stage0_out])
model = Model(inputs=[img_input], outputs=[stageT_branch1_out, stageT_branch2_out])
return model