-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel_pose.py
165 lines (128 loc) · 7.18 KB
/
model_pose.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
import pdb
import tensorflow as tf
import re
slim = tf.contrib.slim
def residual(input, input_channels, output_channels, scope=None, reuse=None, counter=0):
name = ('residual_' + str(counter)) if counter else 'residual'
with tf.compat.v1.variable_scope(scope, name, [input], reuse=reuse):
with slim.arg_scope([slim.conv2d], stride=1):
with tf.compat.v1.variable_scope("convolution_path"):
conv = slim.conv2d(input, output_channels / 2, [1, 1], padding='VALID')
conv1 = slim.conv2d(conv, output_channels / 2, [3, 3], padding='SAME')
conv2 = slim.conv2d(conv1, output_channels, [1, 1], padding='VALID', activation_fn=None)
with tf.compat.v1.variable_scope("skip_path"):
if input_channels == output_channels:
skip = input
else:
skip = slim.conv2d(input, output_channels, [1, 1], padding='VALID', activation_fn=None)
res = conv2 + skip
return res
def hourglass(input, num_branches, input_channels, output_channels, num_res_modules=1, scope=None, reuse=None, id=0):
name = ('hourglass_' + str(id)) if id else 'hourglass'
with tf.compat.v1.variable_scope(scope, name, [input], reuse=reuse):
# Add the residual modules for the upper branch
with tf.compat.v1.variable_scope("upper_branch"):
# print(tf.get_default_graph().get_name_scope())
# print('- ^name Vvars')
# print(tf.get_variable_scope().name)
up1 = input
for i in range(num_res_modules):
up1 = residual(up1, input_channels, input_channels)
# Add the modules for the lower branch
# 1. Pool -> Residuals -> Hourglass -> Residuals -> Upsample
# 2. Pool -> Residuals -> Residuals -> Residuals -> Upsample
with tf.compat.v1.variable_scope("lower_branch"):
low1 = slim.max_pool2d(input, 2, stride=2, padding='VALID')
for i in range(num_res_modules):
low1 = residual(low1, input_channels, input_channels)
# Are we recursing?
if num_branches > 1:
low2 = hourglass(low1, num_branches - 1, input_channels, input_channels, num_res_modules, scope, reuse)
else:
low2 = low1
for i in range(num_res_modules):
low2 = residual(low2, input_channels, input_channels)
low3 = low2
for i in range(num_res_modules):
low3 = residual(low3, input_channels, input_channels)
low3_shape = low3.get_shape().as_list()
low3_height = low3_shape[1]
low3_width = low3_shape[2]
up2 = tf.compat.v1.image.resize_nearest_neighbor(images=low3, size=[low3_height * 2, low3_width * 2],
align_corners=False)
return up1 + up2
def build(input, num_parts, num_features=256, num_stacks=8, num_res_modules=1, reuse=None, scope='HourGlass'):
with tf.compat.v1.variable_scope(scope, 'StackedHourGlassNetwork', [input], reuse=reuse):
# Initial processing of the image
conv = slim.conv2d(input, 64, [7, 7], stride=2, padding='SAME')
r1 = residual(conv, 64, 128)
pool = slim.max_pool2d(r1, 2, stride=2, padding='VALID')
r2 = residual(pool, 128, 128)
r3 = residual(r2, 128, num_features)
intermediate_features = r3
heatmaps = []
for i in range(num_stacks):
# Build the hourglass
hg = hourglass(intermediate_features, num_branches=4, input_channels=num_features,
output_channels=num_features)
# Residual layers at the output resolution.
ll = hg
for j in range(num_res_modules):
ll = residual(ll, num_features, num_features)
with slim.arg_scope([slim.conv2d], kernel_size=[1, 1], stride=1, padding='VALID'):
# Linear layers to do simple projection.
ll = slim.conv2d(ll, num_features)
# Predicted heatmaps.
heatmap = slim.conv2d(ll, num_parts, activation_fn=None, normalizer_fn=None)
heatmaps.append(heatmap)
# Add the predictions and projections back.
if i < num_stacks - 1:
ll_ = slim.conv2d(ll, num_features, activation_fn=None, normalizer_fn=None)
heatmap_ = slim.conv2d(heatmap, num_features, activation_fn=None, normalizer_fn=None)
intermediate_features = ll_ + heatmap_ + intermediate_features
return heatmaps
def build_head(input, num_features=256, reuse=None, scope='HourGlass'):
with tf.compat.v1.variable_scope(scope, 'StackedHourGlassNetwork', [input], reuse=reuse):
conv = slim.conv2d(input, 64, [7, 7], stride=2, padding='SAME')
r1 = residual(conv, 64, 128)
pool = slim.max_pool2d(r1, 2, stride=2, padding='VALID')
r2 = residual(pool, 128, 128)
r3 = residual(r2, 128, num_features)
return r3
def build_hg(input, num_parts, stack_range, num_features=256, num_stacks=8, num_res_modules=1, reuse=None,
build_head=True, features=None, scope='HourGlass'):
with tf.compat.v1.variable_scope(scope, 'StackedHourGlassNetwork', [input], reuse=reuse):
# if build_head:
# conv = slim.conv2d(input, 64, [7, 7], stride=2, padding='SAME')
# r1 = residual(conv, 64, 128)
# pool = slim.max_pool2d(r1, 2, stride=2, padding='VALID')
# r2 = residual(pool, 128, 128)
# r3 = residual(r2, 128, num_features)
#
# intermediate_features = r3
# else:
intermediate_features = features
heatmaps = []
for i in stack_range:
# print('--')
# print(tf.get_default_graph().get_name_scope())
# print(tf.get_variable_scope().name)
# Build the hourglass
hg = hourglass(intermediate_features, num_branches=4, input_channels=num_features,
output_channels=num_features, id=i)
# Residual layers at the output resolution.
ll = hg
for j in range(num_res_modules):
ll = residual(ll, num_features, num_features, counter=3 + i*num_res_modules + j)
with slim.arg_scope([slim.conv2d], kernel_size=[1, 1], stride=1, padding='VALID'):
# Linear layers to do simple projection.
ll = slim.conv2d(ll, num_features, scope='Conv_'+str(i*4+1))
# Predicted heatmaps.
heatmap = slim.conv2d(ll, num_parts, activation_fn=None, normalizer_fn=None, scope='Conv_'+str(i*4+2))
heatmaps.append(heatmap)
# Add the predictions and projections back.
if i < num_stacks - 1:
ll_ = slim.conv2d(ll, num_features, activation_fn=None, normalizer_fn=None, scope='Conv_'+str(i*4+3))
heatmap_ = slim.conv2d(heatmap, num_features, activation_fn=None, normalizer_fn=None, scope='Conv_'+str(i*4+4))
intermediate_features = ll_ + heatmap_ + intermediate_features
return heatmaps, intermediate_features