MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / fpn_model

Function fpn_model

examples/FasterRCNN/modeling/model_fpn.py:22–68  ·  view source on GitHub ↗

Args: features ([tf.Tensor]): ResNet features c2-c5 Returns: [tf.Tensor]: FPN features p2-p6

(features)

Source from the content-addressed store, hash-verified

20
21@layer_register(log_shape=True)
22def fpn_model(features):
23 """
24 Args:
25 features ([tf.Tensor]): ResNet features c2-c5
26
27 Returns:
28 [tf.Tensor]: FPN features p2-p6
29 """
30 assert len(features) == 4, features
31 num_channel = cfg.FPN.NUM_CHANNEL
32
33 use_gn = cfg.FPN.NORM == 'GN'
34
35 def upsample2x(name, x):
36 try:
37 resize = tf.compat.v2.image.resize_images
38 with tf.name_scope(name):
39 shp2d = tf.shape(x)[2:]
40 x = tf.transpose(x, [0, 2, 3, 1])
41 x = resize(x, shp2d * 2, 'nearest')
42 x = tf.transpose(x, [0, 3, 1, 2])
43 return x
44 except AttributeError:
45 return FixedUnPooling(
46 name, x, 2, unpool_mat=np.ones((2, 2), dtype='float32'),
47 data_format='channels_first')
48
49 with argscope(Conv2D, data_format='channels_first',
50 activation=tf.identity, use_bias=True,
51 kernel_initializer=tf.variance_scaling_initializer(scale=1.)):
52 lat_2345 = [Conv2D('lateral_1x1_c{}'.format(i + 2), c, num_channel, 1)
53 for i, c in enumerate(features)]
54 if use_gn:
55 lat_2345 = [GroupNorm('gn_c{}'.format(i + 2), c) for i, c in enumerate(lat_2345)]
56 lat_sum_5432 = []
57 for idx, lat in enumerate(lat_2345[::-1]):
58 if idx == 0:
59 lat_sum_5432.append(lat)
60 else:
61 lat = lat + upsample2x('upsample_lat{}'.format(6 - idx), lat_sum_5432[-1])
62 lat_sum_5432.append(lat)
63 p2345 = [Conv2D('posthoc_3x3_p{}'.format(i + 2), c, num_channel, 3)
64 for i, c in enumerate(lat_sum_5432[::-1])]
65 if use_gn:
66 p2345 = [GroupNorm('gn_p{}'.format(i + 2), c) for i, c in enumerate(p2345)]
67 p6 = MaxPooling('maxpool_p6', p2345[-1], pool_size=1, strides=2, data_format='channels_first', padding='VALID')
68 return p2345 + [p6]
69
70
71@under_name_scope()

Callers 1

backboneMethod · 0.85

Calls 7

argscopeFunction · 0.90
Conv2DFunction · 0.90
MaxPoolingFunction · 0.90
upsample2xFunction · 0.85
formatMethod · 0.80
appendMethod · 0.80
GroupNormFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…