Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def preact_basicblock(l, ch_out, stride, preact):
l, shortcut = apply_preactivation(l, preact)
l = Conv2D('conv1', l, ch_out, 3, strides=stride, activation=BNReLU)
l = Conv2D('conv2', l, ch_out, 3)
return l + resnet_shortcut(shortcut, ch_out, stride)
return FixedUnPooling(
name, x, 2, unpool_mat=np.ones((2, 2), dtype='float32'),
data_format='channels_first')
# tf.image.resize is, again, not aligned.
# with tf.name_scope(name):
# shape2d = tf.shape(x)[2:]
# x = tf.transpose(x, [0, 2, 3, 1])
# x = tf.image.resize_nearest_neighbor(x, shape2d * 2, align_corners=True)
# x = tf.transpose(x, [0, 3, 1, 2])
# return x
with argscope(Conv2D, data_format='channels_first',
activation=tf.identity, use_bias=True,
kernel_initializer=tf.variance_scaling_initializer(scale=1.)):
lat_2345 = [Conv2D('lateral_1x1_c{}'.format(i + 2), c, num_channel, 1)
for i, c in enumerate(features)]
lat_sum_5432 = []
for idx, lat in enumerate(lat_2345[::-1]):
if idx == 0:
lat_sum_5432.append(lat)
else:
lat = lat + upsample2x('upsample_lat{}'.format(6 - idx), lat_sum_5432[-1])
lat_sum_5432.append(lat)
p2345 = [Conv2D('posthoc_3x3_p{}'.format(i + 2), c, num_channel, 3)
for i, c in enumerate(lat_sum_5432[::-1])]
p6 = MaxPooling('maxpool_p6', p2345[-1], pool_size=1, strides=2, data_format='channels_first')
return p2345 + [p6]
def resnet_shortcut(l, n_out, stride, activation=tf.identity):
data_format = get_arg_scope()['Conv2D']['data_format']
n_in = l.get_shape().as_list()[1 if data_format in ['NCHW', 'channels_first'] else 3]
if n_in != n_out: # change dimension when channel is not the same
return Conv2D('convshortcut', l, n_out, 1, strides=stride, activation=activation)
else:
return l
box logits: The encoded box logits from fast-rcnn paper https://arxiv.org/abs/1506.01497
page 5, in order to be consistent with the ground truth encoded boxes
Args:
featuremap: feature map for a single FPN layer, i.e. one from P23456, BS x NumChannel x H_feature x W_feature
channel: NumChannel of the feature map, scalar, default 256
num_anchors(NA): # of anchors for each pixel in the current feature map, scalar, default 3
Returns:
label_logits: BS x H_feature x W_feature x NA
box_logits: BS x (NA * 4) x H_feature x W_feature, encoded
"""
if fp16:
featuremap = tf.cast(featuremap, tf.float16)
with mixed_precision_scope(mixed=fp16):
with argscope(Conv2D, data_format='channels_first',
kernel_initializer=tf.random_normal_initializer(stddev=0.01, seed=seed_gen.next())):
hidden = Conv2D('conv0', featuremap, channel, 3, activation=tf.nn.relu, seed=seed_gen.next())
# BS x NumChannel x H_feature x W_feature
label_logits = Conv2D('class', hidden, num_anchors, 1, seed=seed_gen.next())
# BS x NA x H_feature x W_feature
box_logits = Conv2D('box', hidden, 4 * num_anchors, 1, seed=seed_gen.next())
# BS x (NA*4) x H_feature x W_feature
label_logits = tf.transpose(label_logits, [0, 2, 3, 1]) # BS x H_feature x W_feature x NA
if fp16:
label_logits = tf.cast(label_logits, tf.float32)
box_logits = tf.cast(box_logits, tf.float32)
return label_logits, box_logits
Returns:
mask_logits: Num_boxes x num_category x (2 * H_roi) x (2 * W_roi)
"""
assert norm in [None, 'GN'], norm
l = feature
if fp16:
l = tf.cast(l, tf.float16)
with mixed_precision_scope(mixed=fp16):
with argscope([Conv2D, Conv2DTranspose], data_format='channels_first',
kernel_initializer=tf.variance_scaling_initializer(
scale=2.0, mode='fan_out', seed=seed_gen.next(),
distribution='untruncated_normal' if get_tf_version_tuple() >= (1, 12) else 'normal')):
# c2's MSRAFill is fan_out
for k in range(num_convs):
l = Conv2D('fcn{}'.format(k), l, cfg.MRCNN.HEAD_DIM, 3, activation=tf.nn.relu, seed=seed_gen.next())
if norm is not None:
if fp16: l = tf.cast(l, tf.float32)
l = GroupNorm('gn{}'.format(k), l)
if fp16: l = tf.cast(l, tf.float16)
l = Conv2DTranspose('deconv', l, cfg.MRCNN.HEAD_DIM, 2, strides=2, activation=tf.nn.relu, seed=seed_gen.next()) # 2x upsampling
l = Conv2D('conv', l, num_category, 1, seed=seed_gen.next())
if fp16:
l = tf.cast(l, tf.float32)
return l
Returns:
mask_logits (N x num_category x 2s x 2s):
"""
assert norm in [None, 'GN'], norm
l = feature
with argscope([Conv2D, Conv2DTranspose], data_format='channels_first',
kernel_initializer=tf.variance_scaling_initializer(
scale=2.0, mode='fan_out', distribution='normal')):
# c2's MSRAFill is fan_out
for k in range(num_convs):
l = Conv2D('fcn{}'.format(k), l, cfg.MRCNN.HEAD_DIM, 3, activation=tf.nn.relu)
if norm is not None:
l = GroupNorm('gn{}'.format(k), l)
l = Conv2DTranspose('deconv', l, cfg.MRCNN.HEAD_DIM, 2, strides=2, activation=tf.nn.relu)
l = Conv2D('conv', l, num_category, 1)
return l
def resnet_bottleneck(l, ch_out, stride):
shortcut = l
if cfg.BACKBONE.STRIDE_1X1:
if stride == 2:
l = l[:, :, :-1, :-1]
l = Conv2D('conv1', l, ch_out, 1, strides=stride)
l = Conv2D('conv2', l, ch_out, 3, strides=1)
else:
l = Conv2D('conv1', l, ch_out, 1, strides=1)
if stride == 2:
l = tf.pad(l, [[0, 0], [0, 0], maybe_reverse_pad(0, 1), maybe_reverse_pad(0, 1)])
l = Conv2D('conv2', l, ch_out, 3, strides=2, padding='VALID')
else:
l = Conv2D('conv2', l, ch_out, 3, strides=stride)
l = Conv2D('conv3', l, ch_out * 4, 1, activation=get_norm(zero_init=True))
ret = l + resnet_shortcut(shortcut, ch_out * 4, stride, activation=get_norm(zero_init=False))
return tf.nn.relu(ret, name='output')
def preresnet_basicblock(l, ch_out, stride, preact):
l, shortcut = apply_preactivation(l, preact)
l = Conv2D('conv1', l, ch_out, 3, strides=stride, activation=BNReLU)
l = Conv2D('conv2', l, ch_out, 3)
return l + resnet_shortcut(shortcut, ch_out, stride)
shape = l.get_shape().as_list()
if ch_dim != 1:
shape = shape[1:3]
else:
shape = shape[2:4]
l = Conv2D(
'conv_flat', l, 768, shape, strides=1,
padding='valid', activation=BNReLU)
l = tf.layers.flatten(l)
else:
l = BNReLU('bnrelu_pred', l)
ch_in = _get_dim(l, ch_dim)
if prediction_feature == '1x1':
ch_out = ch_in
if n_dim == 4:
l = Conv2D('conv1x1', l, ch_out, 1)
else:
assert n_dim == 2, n_dim
l = FullyConnected('fc1x1', l, ch_out, activation=tf.identity)
l = BNReLU('bnrelu1x1', l)
elif prediction_feature == 'msdense':
assert n_dim == 2, n_dim
ch_inter = ch_in
l = Conv2D('conv1x1_0', l, ch_inter, 3, strides=2)
l = BNReLU('bnrelu1x1_0', l)
l = Conv2D('conv1x1_1', l, ch_inter, 3, strides=2)
l = BNReLU('bnrelu1x1_1', l)
elif prediction_feature == 'bn':
l = BatchNorm('bn', l)