Esempio n. 1
0
def build_deeplabv3(inputs, num_classes, preset_model='DeepLabV3', frontend="Res101", weight_decay=1e-5, is_training=True):
    """
    Builds the DeepLabV3 model. 

    Arguments:
      inputs: The input tensor= 
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes

    Returns:
      DeepLabV3 model
    """

    logits, end_points, frontend_scope, restore_variables = frontend_builder.build_frontend(
        inputs, frontend, is_training=is_training)

    label_size = tf.shape(inputs)[1:3]

    net = AtrousSpatialPyramidPoolingModule(end_points['pool4'])

    net = Upsampling(net, label_size)

    net = slim.conv2d(net, num_classes, [
                      1, 1], activation_fn=None, scope='logits')

    return net, restore_variables
Esempio n. 2
0
def build_custom(inputs,
                 num_classes,
                 frontend="ResNet101",
                 weight_decay=1e-5,
                 is_training=True,
                 pretrained_dir="models"):

    logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend(
        inputs, frontend, is_training=is_training)

    up_1 = conv_transpose_block(end_points["pool2"], strides=4, n_filters=64)
    up_2 = conv_transpose_block(end_points["pool3"], strides=8, n_filters=64)
    up_3 = conv_transpose_block(end_points["pool4"], strides=16, n_filters=64)
    up_4 = conv_transpose_block(end_points["pool5"], strides=32, n_filters=64)

    features = tf.concat([up_1, up_2, up_3, up_4], axis=-1)

    features = conv_block(inputs=features, n_filters=256, filter_size=[1, 1])

    features = conv_block(inputs=features, n_filters=64, filter_size=[3, 3])
    features = conv_block(inputs=features, n_filters=64, filter_size=[3, 3])
    features = conv_block(inputs=features, n_filters=64, filter_size=[3, 3])

    net = slim.conv2d(features, num_classes, [1, 1], scope='logits')
    return net
Esempio n. 3
0
def build_deeplabv3_plus(inputs,
                         num_classes,
                         preset_model='DeepLabV3+',
                         frontend="ResNet101",
                         weight_decay=1e-5,
                         is_training=True,
                         pretrained_dir="models"):
    """
    Builds the DeepLabV3 model. 

    Arguments:
      inputs: The input tensor= 
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes

    Returns:
      DeepLabV3 model
    """

    logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend(
        inputs, frontend, is_training=is_training)

    label_size = tf.shape(inputs)[1:3]

    encoder_features = end_points['pool2']

    net = AtrousSpatialPyramidPoolingModule(end_points['pool4'])
    net = slim.conv2d(net,
                      256, [1, 1],
                      scope="conv_1x1_output",
                      activation_fn=None)
    decoder_features = Upsampling(net, label_size / 4)

    encoder_features = slim.conv2d(encoder_features,
                                   48, [1, 1],
                                   activation_fn=tf.nn.relu,
                                   normalizer_fn=None)

    net = tf.concat((encoder_features, decoder_features), axis=3)

    net = slim.conv2d(net,
                      256, [3, 3],
                      activation_fn=tf.nn.relu,
                      normalizer_fn=None)
    net = slim.conv2d(net,
                      256, [3, 3],
                      activation_fn=tf.nn.relu,
                      normalizer_fn=None)

    net = Upsampling(net, label_size)

    net = slim.conv2d(net,
                      num_classes, [1, 1],
                      activation_fn=None,
                      scope='logits')

    return net, init_fn
Esempio n. 4
0
def build_refinenet(inputs, num_classes, preset_model='RefineNet', frontend="ResNet101", weight_decay=1e-5, upscaling_method="bilinear", is_training=True):
    """
    Builds the RefineNet model. 

    Arguments:
      inputs: The input tensor
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes

    Returns:
      RefineNet model
    """

    logits, end_points, frontend_scope, restore_variables = frontend_builder.build_frontend(
        inputs, frontend, is_training=is_training)

    high = [end_points['pool5'], end_points['pool4'],
            end_points['pool3'], end_points['pool2']]

    low = [None, None, None, None]

    # Get the feature maps to the proper size with bottleneck
    high[0] = slim.conv2d(high[0], 512, 1)
    high[1] = slim.conv2d(high[1], 256, 1)
    high[2] = slim.conv2d(high[2], 256, 1)
    high[3] = slim.conv2d(high[3], 256, 1)

    # RefineNet
    # Only input ResNet 1/32
    low[0] = RefineBlock(high_inputs=high[0], low_inputs=None)
    # High input = ResNet 1/16, Low input = Previous 1/16
    low[1] = RefineBlock(high[1], low[0])
    # High input = ResNet 1/8, Low input = Previous 1/8
    low[2] = RefineBlock(high[2], low[1])
    # High input = ResNet 1/4, Low input = Previous 1/4
    low[3] = RefineBlock(high[3], low[2])

    # g[3]=Upsampling(g[3],scale=4)

    net = low[3]

    net = ResidualConvUnit(net)
    net = ResidualConvUnit(net)

    if upscaling_method.lower() == "conv":
        net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 128)
        net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 64)
    elif upscaling_method.lower() == "bilinear":
        net = Upsampling(net, scale=4)

    net = slim.conv2d(net, num_classes, [
                      1, 1], activation_fn=None, scope='logits')

    return net, restore_variables
Esempio n. 5
0
def build_pspnet(inputs,
                 label_size,
                 num_classes,
                 preset_model='PSPNet',
                 frontend="ResNet101",
                 pooling_type="MAX",
                 weight_decay=1e-5,
                 upscaling_method="conv",
                 is_training=True,
                 pretrained_dir="models"):
    """
    Builds the PSPNet model. 

    Arguments:
      inputs: The input tensor
      label_size: Size of the final label tensor. We need to know this for proper upscaling 
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes
      pooling_type: Max or Average pooling

    Returns:
      PSPNet model
    """

    logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend(
        inputs, frontend, is_training=is_training)

    feature_map_shape = [int(x / 8.0) for x in label_size]
    print(feature_map_shape)
    psp = PyramidPoolingModule(end_points['pool3'],
                               feature_map_shape=feature_map_shape,
                               pooling_type=pooling_type)

    net = slim.conv2d(psp, 512, [3, 3], activation_fn=None)
    net = slim.batch_norm(net, fused=True)
    net = tf.nn.relu(net)

    if upscaling_method.lower() == "conv":
        net = ConvUpscaleBlock(net, 256, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 256)
        net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 128)
        net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 64)
    elif upscaling_method.lower() == "bilinear":
        net = Upsampling(net, label_size)

    net = slim.conv2d(net,
                      num_classes, [1, 1],
                      activation_fn=None,
                      scope='logits')

    return net, init_fn
Esempio n. 6
0
def build_dense_aspp(inputs,
                     num_classes,
                     preset_model='DenseASPP',
                     frontend="ResNet101",
                     weight_decay=1e-5,
                     is_training=True):

    logits, end_points, frontend_scope, restore_variables = frontend_builder.build_frontend(
        inputs, frontend, is_training=is_training)

    init_features = end_points['pool3']

    ### First block, rate = 3
    d_3_features = DilatedConvBlock(init_features,
                                    n_filters=256,
                                    kernel_size=[1, 1])
    d_3 = DilatedConvBlock(d_3_features,
                           n_filters=64,
                           rate=3,
                           kernel_size=[3, 3])

    ### Second block, rate = 6
    d_4 = tf.concat([init_features, d_3], axis=-1)
    d_4 = DilatedConvBlock(d_4, n_filters=256, kernel_size=[1, 1])
    d_4 = DilatedConvBlock(d_4, n_filters=64, rate=6, kernel_size=[3, 3])

    ### Third block, rate = 12
    d_5 = tf.concat([init_features, d_3, d_4], axis=-1)
    d_5 = DilatedConvBlock(d_5, n_filters=256, kernel_size=[1, 1])
    d_5 = DilatedConvBlock(d_5, n_filters=64, rate=12, kernel_size=[3, 3])

    ### Fourth block, rate = 18
    d_6 = tf.concat([init_features, d_3, d_4, d_5], axis=-1)
    d_6 = DilatedConvBlock(d_6, n_filters=256, kernel_size=[1, 1])
    d_6 = DilatedConvBlock(d_6, n_filters=64, rate=18, kernel_size=[3, 3])

    ### Fifth block, rate = 24
    d_7 = tf.concat([init_features, d_3, d_4, d_5, d_6], axis=-1)
    d_7 = DilatedConvBlock(d_7, n_filters=256, kernel_size=[1, 1])
    d_7 = DilatedConvBlock(d_7, n_filters=64, rate=24, kernel_size=[3, 3])

    full_block = tf.concat([init_features, d_3, d_4, d_5, d_6, d_7], axis=-1)

    net = slim.conv2d(full_block,
                      num_classes, [1, 1],
                      activation_fn=None,
                      scope='logits')

    net = Upsampling(net, scale=8)

    return net, restore_variables
def build_gcn(inputs,
              num_classes,
              preset_model='GCN',
              frontend="ResNet101",
              weight_decay=1e-5,
              is_training=True,
              upscaling_method="bilinear",
              pretrained_dir="models"):
    """
    Builds the GCN model. 

    Arguments:
      inputs: The input tensor
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes

    Returns:
      GCN model
    """

    logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend(
        inputs, frontend, is_training=is_training)

    res = [
        end_points['pool5'], end_points['pool4'], end_points['pool3'],
        end_points['pool2']
    ]

    down_5 = GlobalConvBlock(res[0], n_filters=21, size=3)
    down_5 = BoundaryRefinementBlock(down_5, n_filters=21, kernel_size=[3, 3])
    down_5 = ConvUpscaleBlock(down_5,
                              n_filters=21,
                              kernel_size=[3, 3],
                              scale=2)

    down_4 = GlobalConvBlock(res[1], n_filters=21, size=3)
    down_4 = BoundaryRefinementBlock(down_4, n_filters=21, kernel_size=[3, 3])
    down_4 = tf.add(down_4, down_5)
    down_4 = BoundaryRefinementBlock(down_4, n_filters=21, kernel_size=[3, 3])
    down_4 = ConvUpscaleBlock(down_4,
                              n_filters=21,
                              kernel_size=[3, 3],
                              scale=2)

    down_3 = GlobalConvBlock(res[2], n_filters=21, size=3)
    down_3 = BoundaryRefinementBlock(down_3, n_filters=21, kernel_size=[3, 3])
    down_3 = tf.add(down_3, down_4)
    down_3 = BoundaryRefinementBlock(down_3, n_filters=21, kernel_size=[3, 3])
    down_3 = ConvUpscaleBlock(down_3,
                              n_filters=21,
                              kernel_size=[3, 3],
                              scale=2)

    down_2 = GlobalConvBlock(res[3], n_filters=21, size=3)
    down_2 = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3])
    down_2 = tf.add(down_2, down_3)
    down_2 = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3])
    down_2 = ConvUpscaleBlock(down_2,
                              n_filters=21,
                              kernel_size=[3, 3],
                              scale=2)

    net = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3])
    net = ConvUpscaleBlock(net, n_filters=21, kernel_size=[3, 3], scale=2)
    net = BoundaryRefinementBlock(net, n_filters=21, kernel_size=[3, 3])

    net = slim.conv2d(net,
                      num_classes, [1, 1],
                      activation_fn=None,
                      scope='logits')

    return net, init_fn