Example #1
0
def rpn_head(featuremap, channel, num_anchors, tower_setup):
    with tf.variable_scope('rpn'):
        hidden = Conv(
            'conv0', [featuremap],
            channel,
            tower_setup,
            old_order=True,
            bias=True,
            W_initializer=tf.random_normal_initializer(stddev=0.01)).outputs[0]

        label_logits = Conv(
            'class', [hidden],
            num_anchors,
            tower_setup, (1, 1),
            old_order=True,
            bias=True,
            activation="linear",
            W_initializer=tf.random_normal_initializer(stddev=0.01)).outputs[0]
        box_logits = Conv(
            'box', [hidden],
            4 * num_anchors,
            tower_setup, (1, 1),
            old_order=True,
            bias=True,
            activation="linear",
            W_initializer=tf.random_normal_initializer(stddev=0.01)).outputs[0]
        shp = tf.shape(box_logits)
        box_logits = tf.reshape(
            box_logits, tf.stack([shp[0], shp[1], shp[2], num_anchors, 4]))
    return label_logits, box_logits
Example #2
0
def resnet_shortcut(l, n_out, stride, tower_setup):
    n_in = l.get_shape().as_list()[3]
    if n_in != n_out:  # change dimension when channel is not the same
        if stride == 2:
            l = l[:, :-1, :-1, :]
            return Conv(name='convshortcut',
                        inputs=[l],
                        n_features=n_out,
                        tower_setup=tower_setup,
                        filter_size=(1, 1),
                        strides=(stride, stride),
                        activation='linear',
                        padding='VALID',
                        batch_norm=True,
                        old_order=True).outputs[0]
        else:
            return Conv(name='convshortcut',
                        inputs=[l],
                        n_features=n_out,
                        tower_setup=tower_setup,
                        filter_size=(1, 1),
                        strides=(stride, stride),
                        activation='linear',
                        batch_norm=True,
                        old_order=True).outputs[0]
    else:
        return l
Example #3
0
def resnet_bottleneck(l, ch_out, stride, tower_setup):
  l, shortcut = l, l

  l = Conv(name='conv1', inputs=[l], n_features=ch_out, tower_setup=tower_setup, filter_size=(1, 1),
           activation='relu', batch_norm=True, old_order=True).outputs[0]
  if stride == 2:
    l = tf.pad(l, [[0, 0], [0, 1], [0, 1], [0, 0]])
    l = Conv(name='conv2', inputs=[l], n_features=ch_out, tower_setup=tower_setup, filter_size=(3, 3), strides=(2, 2),
             activation='relu', padding='VALID', batch_norm=True, old_order=True).outputs[0]
  else:
    l = Conv(name='conv2', inputs=[l], n_features=ch_out, tower_setup=tower_setup, filter_size=(3, 3),
             strides=(stride, stride), activation='relu', batch_norm=True, old_order=True).outputs[0]
  l = Conv(name='conv3', inputs=[l], n_features=ch_out * 4, tower_setup=tower_setup, filter_size=(1, 1),
           activation='linear', batch_norm=True, old_order=True).outputs[0]
  return l + resnet_shortcut(shortcut, ch_out * 4, stride, tower_setup)
Example #4
0
def maskrcnn_head(feature, num_class, tower_setup, class_agnostic_conv=False):
    with tf.variable_scope('maskrcnn'):
        # c2's MSRAFill is fan_out
        l = ConvTranspose('deconv', [feature],
                          256,
                          tower_setup, (2, 2),
                          strides=(2, 2),
                          bias=True,
                          W_initializer=tf.variance_scaling_initializer(
                              scale=2.0,
                              mode='fan_out',
                              distribution='truncated_normal')).outputs[0]
        if class_agnostic_conv:
            num_output_channels = 1
        else:
            num_output_channels = num_class - 1
        l = Conv('conv', [l],
                 num_output_channels,
                 tower_setup, (1, 1),
                 old_order=True,
                 bias=True,
                 activation="linear",
                 W_initializer=tf.variance_scaling_initializer(
                     scale=2.0,
                     mode='fan_out',
                     distribution='truncated_normal')).outputs[0]
    return l
Example #5
0
def pretrained_vgg16_2(image, num_blocks, tower_setup, freeze_c2=True):
  assert len(num_blocks) == 3
  l = tf.pad(image, [[0, 0], [2, 3], [2, 3], [0, 0]])

  l = Conv(name='conv0', inputs=[l], n_features=64, tower_setup=tower_setup, filter_size=(7, 7),
           activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]
  l = Conv(name='conv1', inputs=[l], n_features=64, tower_setup=tower_setup, filter_size=(7, 7),
           activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]

  l = tf.pad(l, [[0, 0], [0, 1], [0, 1], [0, 0]])
  l = max_pool(l, shape=[2, 2], strides=[2, 2], padding='VALID')

  l = Conv(name='conv2', inputs=[l], n_features=128, tower_setup=tower_setup, filter_size=(7, 7),
           activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]
  l = Conv(name='conv3', inputs=[l], n_features=128, tower_setup=tower_setup, filter_size=(7, 7),
           activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]

  l = tf.pad(l, [[0, 0], [0, 1], [0, 1], [0, 0]])
  l = max_pool(l, shape=[2, 2], strides=[2, 2], padding='VALID')

  l = Conv(name='conv4', inputs=[l], n_features=256, tower_setup=tower_setup, filter_size=(7, 7),
           activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]
  l = Conv(name='conv5', inputs=[l], n_features=256, tower_setup=tower_setup, filter_size=(7, 7),
           activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]
  l = Conv(name='conv6', inputs=[l], n_features=256, tower_setup=tower_setup, filter_size=(7, 7),
           activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]

  # l = tf.pad(l, [[0, 0], [0, 1], [0, 1], [0, 0]])
  # l = max_pool(l, shape=[2, 2], strides=[2, 2], padding='VALID')

  # l = Conv(name='conv7', inputs=[l], n_features=512, tower_setup=tower_setup, filter_size=(7, 7),
  #          activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]
  # l = Conv(name='conv8', inputs=[l], n_features=512, tower_setup=tower_setup, filter_size=(7, 7),
  #          activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]
  # l = Conv(name='conv9', inputs=[l], n_features=512, tower_setup=tower_setup, filter_size=(7, 7),
  #          activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]

  # l = tf.pad(l, [[0, 0], [0, 1], [0, 1], [0, 0]])
  # l = max_pool(l, shape=[2, 2], strides=[2, 2], padding='VALID')

  # l = Conv(name='conv10', inputs=[l], n_features=512, tower_setup=tower_setup, filter_size=(7, 7),
  #          activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]
  # l = Conv(name='conv11', inputs=[l], n_features=512, tower_setup=tower_setup, filter_size=(7, 7),
  #          activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]
  # l = Conv(name='conv12', inputs=[l], n_features=512, tower_setup=tower_setup, filter_size=(7, 7),
  #          activation='relu', padding='SAME', batch_norm=True, old_order=True).outputs[0]

  l = tf.pad(l, [[0, 0], [0, 1], [0, 1], [0, 0]])
  l = max_pool(l, shape=[2, 2], strides=[2, 2], padding='VALID')
  c2 = resnet_group(l, 'group0', 64, num_blocks[0], 1, tower_setup)
  if freeze_c2:
    c2 = tf.stop_gradient(c2)
  c3 = resnet_group(c2, 'group1', 128, num_blocks[1], 2, tower_setup)
  #if tower_setup.is_training:
  #  tf.add_to_collection('checkpoints', c3)
  c4 = resnet_group(c3, 'group2', 256, num_blocks[2], 2, tower_setup)

  return [c2, c3, c4]
Example #6
0
def maskrcnn_head(feature, num_class, tower_setup):
    with tf.variable_scope('maskrcnn'):
        # TODO both operations should have kernel_initializer=tf.variance_scaling_initializer(scale=2.0, mode='fan_out', distribution='normal')
        # c2's MSRAFill is fan_out
        l = ConvTranspose('deconv', [feature],
                          256,
                          tower_setup, (2, 2),
                          strides=(2, 2),
                          bias=True).outputs[0]
        l = Conv('conv', [l],
                 num_class - 1,
                 tower_setup, (1, 1),
                 old_order=True,
                 bias=True,
                 activation="linear").outputs[0]
    return l
Example #7
0
def pretrained_xception_done(image, num_blocks, tower_setup, freeze_c2=True):
    assert len(num_blocks) == 3
    l = tf.pad(image, [[0, 0], [2, 3], [2, 3], [0, 0]])

    l = Conv(name='conv0',
             inputs=[l],
             n_features=32,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]
    l = Conv(name='conv1',
             inputs=[l],
             n_features=64,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]

    l = Conv(name='conv2',
             inputs=[l],
             n_features=128,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]
    l = Conv(name='conv3',
             inputs=[l],
             n_features=128,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]

    l = max_pool(l, shape=[3, 3], strides=[2, 2], padding='SAME')

    l = Conv(name='conv4',
             inputs=[l],
             n_features=256,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]
    l = Conv(name='conv5',
             inputs=[l],
             n_features=256,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]

    l = max_pool(l, shape=[3, 3], strides=[2, 2], padding='SAME')

    l = Conv(name='conv6',
             inputs=[l],
             n_features=728,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]
    l = Conv(name='conv7',
             inputs=[l],
             n_features=728,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]

    l = max_pool(l, shape=[3, 3], strides=[2, 2], padding='SAME')

    l = Conv(name='conv8',
             inputs=[l],
             n_features=728,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]
    l = Conv(name='conv9',
             inputs=[l],
             n_features=728,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]
    l = Conv(name='conv10',
             inputs=[l],
             n_features=728,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]

    l = Conv(name='conv11',
             inputs=[l],
             n_features=728,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]
    l = Conv(name='conv12',
             inputs=[l],
             n_features=1024,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]

    l = max_pool(l, shape=[3, 3], strides=[2, 2], padding='SAME')

    l = Conv(name='conv13',
             inputs=[l],
             n_features=1536,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]
    l = Conv(name='conv14',
             inputs=[l],
             n_features=2048,
             tower_setup=tower_setup,
             filter_size=(3, 3),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]

    l = max_pool(l, shape=[3, 3], strides=[2, 2], padding='SAME')

    l = Conv(name='conv15',
             inputs=[l],
             n_features=2048,
             tower_setup=tower_setup,
             filter_size=(1, 1),
             activation='relu',
             padding='VALID',
             batch_norm=True,
             old_order=True).outputs[0]

    c2 = resnet_group(l, 'group0', 64, num_blocks[0], 1, tower_setup)
    if freeze_c2:
        c2 = tf.stop_gradient(c2)
    c3 = resnet_group(c2, 'group1', 128, num_blocks[1], 2, tower_setup)
    c4 = resnet_group(c3, 'group2', 256, num_blocks[2], 2, tower_setup)

    return [c2, c3, c4]