with slim.arg_scope(hourglass_arg_scope_tf()):
# D1
net2 = slim.conv2d(inputs, 64, (7, 7), 2, scope='conv2-1')
net2 = bottleneck_module(net2, out_channel=128,
res=128, scope='bottleneck2-1')
net2 = slim.max_pool2d(net2, [2, 2], scope='pool2-1')
# D2
net2 = slim.stack(net2, bottleneck_module, [
(128, None), (128, None), (256, 256)], scope='conv2-2')
# hourglasses (D3,D4,D5)
with tf.variable_scope('hourglass2'):
net2 = hourglass_module(
net2, depth=2, deconv=deconv, bottleneck=bottleneck)
# final layers (D6, D7)
net2 = slim.stack(net2, slim.conv2d, [(512, [1, 1]), (256, [1, 1]),
(48, [1, 1])
], scope='conv2-3')
net2 = deconv_layer(net2, 4, 3, method=deconv) # 反卷积
net2 = slim.conv2d(net3, 3, 1, scope='conv2-last')
regression2 = slim.conv2d(
net2, 3, 1, activation_fn=None
) if 3 else None
def bottleneck_module(inputs, out_channel=256, res=None, scope=''):
with tf.variable_scope(scope):
net = slim.stack(inputs, slim.conv2d, [
(out_channel // 2, [1, 1]), (out_channel // 2, [3, 3]), (out_channel, [1, 1])], scope='conv')
if res:
inputs = slim.conv2d(inputs, res, (1, 1),
scope='bn_res'.format(scope))
net += inputs
return net
# recursive hourglass definition
def hourglass_module(inputs, depth=0, deconv='bilinear', bottleneck='bottleneck'):
bm_fn = globals()['%s_module' % bottleneck]
with tf.variable_scope('depth_{}'.format(depth)):
# buttom up layers
net = slim.max_pool2d(inputs, [2, 2], scope='pool')
net = slim.stack(net, bm_fn, [
(256, None), (256, None), (256, None)], scope='buttom_up')
# connecting layers
if depth > 0:
net = hourglass_module(net, depth=depth - 1, deconv=deconv)
else:
net = bm_fn(
net, out_channel=512, res=512, scope='connecting')
# top down layers
net = bm_fn(net, out_channel=512,
res=512, scope='top_down')
net = deconv_layer(net, 2, 512, method=deconv)
# residual layers
net += slim.stack(inputs, bm_fn,
[(256, None), (256, None), (512, 512)], scope='res')
return net