Exemple #1
0
def unet_road_detection(x, dropout, is_training, weight_decay, crop,
                        num_input_bands, num_classes, crop_size,
                        extract_features):
    x = tf.reshape(x, shape=[-1, crop, crop, num_input_bands])

    conv1_1 = _conv_layer(x, [3, 3, num_input_bands, 64],
                          "conv1_1",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME',
                          activation='elu')
    conv1_2 = _conv_layer(conv1_1, [3, 3, 64, 64],
                          "conv1_2",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME',
                          activation='elu')
    s_pool1 = crop_size
    pool1 = _max_pool(conv1_2, [1, 2, 2, 1], [1, 2, 2, 1], 'pool1', pad='SAME')

    conv2_1 = _conv_layer(pool1, [3, 3, 64, 128],
                          "conv2_1",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME',
                          activation='elu')
    conv2_2 = _conv_layer(conv2_1, [3, 3, 128, 128],
                          "conv2_2",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME',
                          activation='elu')
    s_pool2 = math.ceil(s_pool1 / float(2))
    # s_pool2 = math.ceil(float(s_pool1 - 2 + 1) / float(2))
    pool2 = _max_pool(conv2_2, [1, 2, 2, 1], [1, 2, 2, 1], 'pool2', pad='SAME')

    conv3_1 = _conv_layer(pool2, [3, 3, 128, 256],
                          "conv3_1",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME',
                          activation='elu')
    conv3_2 = _conv_layer(conv3_1, [3, 3, 256, 256],
                          "conv3_2",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME',
                          activation='elu')
    s_pool3 = math.ceil(s_pool2 / float(2))
    # s_pool3 = math.ceil(float(s_pool2 - 2 + 1) / float(2))
    pool3 = _max_pool(conv3_2, [1, 2, 2, 1], [1, 2, 2, 1], 'pool3', pad='SAME')

    conv4_1 = _conv_layer(pool3, [3, 3, 256, 512],
                          "conv4_1",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME',
                          activation='elu')
    conv4_2 = _conv_layer(conv4_1, [3, 3, 512, 512],
                          "conv4_2",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME',
                          activation='elu')
    # s_pool4 = math.ceil(s_pool3 / float(2))
    # pool4 = _max_pool(conv4_2, [1, 2, 2, 1], [1, 2, 2, 1], 'pool4', pad='SAME')
    #
    # conv5_1 = _conv_layer(pool4, [3, 3, 512, 1024], "conv5_1", weight_decay, is_training,
    #                       strides=[1, 1, 1, 1], pad='SAME', activation='elu')
    # conv5_2 = _conv_layer(conv5_1, [3, 3, 1024, 1024], "conv5_2", weight_decay, is_training,
    #                       strides=[1, 1, 1, 1], pad='SAME', activation='elu')

    # ---------------------------------End of encoder----------------------------------
    aspp = atrous_spatial_pyramid_pooling(conv4_2, [6, 12, 18], weight_decay,
                                          is_training)

    # deconv4_1 = _deconv_layer(aspp, [2, 2, 512, 1024], _get_shape(conv5_2), 'deconv4_1', weight_decay,
    #                           [1, 2, 2, 1], pad='SAME', has_bias=True)
    # # deconv4_2 = _crop_and_concat(conv2_2, deconv2_1, (s_pool2, s_pool2), (s_pool3*2, s_pool3*2))
    # deconv4_2 = tf.concat(values=[conv4_2, deconv4_1], axis=-1)
    # deconv4_3 = _conv_layer(deconv4_2, [3, 3, 1024, 512], "deconv4_3", weight_decay, is_training,
    #                         strides=[1, 1, 1, 1], pad='SAME')
    # deconv4_4 = _conv_layer(deconv4_3, [3, 3, 512, 512], "deconv4_4", weight_decay, is_training,
    #                         strides=[1, 1, 1, 1], pad='SAME')

    deconv3_1 = _deconv_layer(aspp, [2, 2, 256, 512],
                              _get_shape(conv4_2),
                              'deconv3_1',
                              weight_decay, [1, 2, 2, 1],
                              pad='SAME',
                              has_bias=True)
    # deconv3_2 = _crop_and_concat(conv2_2, deconv2_1, (s_pool2, s_pool2), (s_pool3*2, s_pool3*2))
    deconv3_2 = tf.concat(values=[conv3_2, deconv3_1], axis=-1)
    deconv3_3 = _conv_layer(deconv3_2, [3, 3, 512, 256],
                            "deconv3_3",
                            weight_decay,
                            is_training,
                            strides=[1, 1, 1, 1],
                            pad='SAME')
    deconv3_4 = _conv_layer(deconv3_3, [3, 3, 256, 256],
                            "deconv3_4",
                            weight_decay,
                            is_training,
                            strides=[1, 1, 1, 1],
                            pad='SAME')

    deconv2_1 = _deconv_layer(deconv3_4, [2, 2, 128, 256],
                              _get_shape(conv3_2),
                              'deconv2_1',
                              weight_decay, [1, 2, 2, 1],
                              pad='SAME',
                              has_bias=True)
    # deconv2_2 = _crop_and_concat(conv2_2, deconv2_1, (s_pool2, s_pool2), (s_pool3*2, s_pool3*2))
    deconv2_2 = tf.concat(values=[conv2_2, deconv2_1], axis=-1)
    deconv2_3 = _conv_layer(deconv2_2, [3, 3, 256, 128],
                            "deconv2_3",
                            weight_decay,
                            is_training,
                            strides=[1, 1, 1, 1],
                            pad='SAME')
    deconv2_4 = _conv_layer(deconv2_3, [3, 3, 128, 128],
                            "deconv2_4",
                            weight_decay,
                            is_training,
                            strides=[1, 1, 1, 1],
                            pad='SAME')

    deconv1_1 = _deconv_layer(deconv2_4, [2, 2, 64, 128],
                              _get_shape(deconv2_4),
                              'deconv1_1',
                              weight_decay, [1, 2, 2, 1],
                              pad='SAME',
                              has_bias=True)
    # deconv1_2 = _crop_and_concat(conv1_2, deconv1_1, (s_pool2, s_pool2), (s_pool2, s_pool2))
    deconv1_2 = tf.concat(values=[conv1_2, deconv1_1], axis=-1)
    deconv1_3 = _conv_layer(deconv1_2, [3, 3, 128, 64],
                            "deconv1_2",
                            weight_decay,
                            is_training,
                            strides=[1, 1, 1, 1],
                            pad='SAME')
    deconv1_4 = _conv_layer(deconv1_3, [3, 3, 64, 64],
                            "deconv1_3",
                            weight_decay,
                            is_training,
                            strides=[1, 1, 1, 1],
                            pad='SAME')

    with tf.variable_scope('conv_classifier') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[1, 1, 64, num_classes],
            ini=tf.contrib.layers.xavier_initializer_conv2d(dtype=tf.float32),
            weight_decay=weight_decay)
        biases = _variable_on_cpu('biases', [num_classes],
                                  tf.constant_initializer(0.0))

        conv = tf.nn.conv2d(deconv1_4, kernel, [1, 1, 1, 1], padding='SAME')
        conv_classifier = tf.nn.bias_add(conv, biases, name=scope.name)

    return conv_classifier
Exemple #2
0
def unet(x, dropout, is_training, weight_decay, crop, num_input_bands,
         num_classes, crop_size, extract_features):
    x = tf.reshape(x, shape=[-1, crop, crop, num_input_bands])

    conv1_1 = _conv_layer(x, [3, 3, num_input_bands, 64],
                          "conv1_1",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME')
    conv1_2 = _conv_layer(conv1_1, [3, 3, 64, 64],
                          "conv1_2",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME')
    s_pool1 = crop_size
    pool1 = _max_pool(conv1_2, [1, 2, 2, 1], [1, 2, 2, 1], 'pool1', pad='SAME')

    conv2_1 = _conv_layer(pool1, [3, 3, 64, 128],
                          "conv2_1",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME')
    conv2_2 = _conv_layer(conv2_1, [3, 3, 128, 128],
                          "conv2_2",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME')
    s_pool2 = math.ceil(s_pool1 / float(2))
    # s_pool2 = math.ceil(float(s_pool1 - 2 + 1) / float(2))
    pool2 = _max_pool(conv2_2, [1, 2, 2, 1], [1, 2, 2, 1], 'pool2', pad='SAME')

    conv3_1 = _conv_layer(pool2, [3, 3, 128, 256],
                          "conv3_1",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME')
    conv3_2 = _conv_layer(conv3_1, [3, 3, 256, 256],
                          "conv3_2",
                          weight_decay,
                          is_training,
                          strides=[1, 1, 1, 1],
                          pad='SAME')
    s_pool3 = math.ceil(s_pool2 / float(2))
    # s_pool3 = math.ceil(float(s_pool2 - 2 + 1) / float(2))
    print(s_pool1, s_pool2, s_pool3)

    # pool3 = _max_pool(conv3_2, [1, 2, 2, 1], [1, 2, 2, 1], 'pool3', pad='SAME')
    #
    # conv4_1 = _conv_layer(pool2, [3, 3, 128, 256], "conv3_1", weight_decay, is_training,
    #                       strides=[1, 1, 1, 1], pad='SAME')
    # conv4_2 = _conv_layer(conv3_1, [3, 3, 256, 256], "conv3_2", weight_decay, is_training,
    #                       strides=[1, 1, 1, 1], pad='SAME')

    # ------------------------End of encoder-----------------------------

    new_shape = [
        tf.shape(conv3_2)[0],
        tf.shape(conv3_2)[1] * 2,
        tf.shape(conv3_2)[2] * 2,
        tf.shape(conv3_2)[3] // 2
    ]
    try:
        output_shape = tf.pack(new_shape)
    except:
        output_shape = tf.stack(new_shape)
    deconv2_1 = _deconv_layer(conv3_2, [2, 2, 128, 256],
                              output_shape,
                              'deconv2_1',
                              weight_decay, [1, 2, 2, 1],
                              pad='SAME',
                              has_bias=True)
    # deconv2_2 = _crop_and_concat(conv2_2, deconv2_1, (s_pool2, s_pool2), (s_pool3*2, s_pool3*2))
    deconv2_2 = tf.concat(values=[conv2_2, deconv2_1], axis=-1)
    deconv2_3 = _conv_layer(deconv2_2, [3, 3, 256, 128],
                            "deconv2_3",
                            weight_decay,
                            is_training,
                            strides=[1, 1, 1, 1],
                            pad='SAME')
    deconv2_4 = _conv_layer(deconv2_3, [3, 3, 128, 128],
                            "deconv2_4",
                            weight_decay,
                            is_training,
                            strides=[1, 1, 1, 1],
                            pad='SAME')

    new_shape = [
        tf.shape(deconv2_4)[0],
        tf.shape(deconv2_4)[1] * 2,
        tf.shape(deconv2_4)[2] * 2,
        tf.shape(deconv2_4)[3] // 2
    ]
    try:
        output_shape = tf.pack(new_shape)
    except:
        output_shape = tf.stack(new_shape)
    deconv1_1 = _deconv_layer(deconv2_4, [2, 2, 64, 128],
                              output_shape,
                              'deconv1_1',
                              weight_decay, [1, 2, 2, 1],
                              pad='SAME',
                              has_bias=True)
    # deconv1_2 = _crop_and_concat(conv1_2, deconv1_1, (s_pool2, s_pool2), (s_pool2, s_pool2))
    deconv1_2 = tf.concat(values=[conv1_2, deconv1_1], axis=-1)
    deconv1_3 = _conv_layer(deconv1_2, [3, 3, 128, 64],
                            "deconv1_2",
                            weight_decay,
                            is_training,
                            strides=[1, 1, 1, 1],
                            pad='SAME')
    deconv1_4 = _conv_layer(deconv1_3, [3, 3, 64, 64],
                            "deconv1_3",
                            weight_decay,
                            is_training,
                            strides=[1, 1, 1, 1],
                            pad='SAME')

    with tf.variable_scope('conv_classifier') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[1, 1, 64, num_classes],
            ini=tf.contrib.layers.xavier_initializer_conv2d(dtype=tf.float32),
            weight_decay=weight_decay)
        biases = _variable_on_cpu('biases', [num_classes],
                                  tf.constant_initializer(0.0))

        conv = tf.nn.conv2d(deconv1_4, kernel, [1, 1, 1, 1], padding='SAME')
        conv_classifier = tf.nn.bias_add(conv, biases, name=scope.name)

    # resize to 32x32 or 64x64
    # save only the first 64 maps
    if extract_features is True:
        return [tf.image.resize_bilinear(conv1_1, [32, 32]), 64], \
               [tf.image.resize_bilinear(conv3_2, [32, 32]), 256], \
               [tf.image.resize_bilinear(deconv1_4, [32, 32]), 64], conv_classifier
    else:
        return conv_classifier
Exemple #3
0
def fcn_25_2_2x(x, dropout, is_training, crop_size, weight_decay,
                num_input_bands, num_classes, extract_features):
    # Reshape input picture
    x = tf.reshape(x, shape=[-1, crop_size, crop_size, num_input_bands])
    # print x.get_shape()

    conv1_1 = _conv_layer(x, [3, 3, num_input_bands, 64],
                          'conv1_1',
                          weight_decay,
                          is_training,
                          batch_norm=True)
    conv1_2 = _conv_layer(conv1_1, [3, 3, 64, 64],
                          'conv1_2',
                          weight_decay,
                          is_training,
                          batch_norm=True)
    pool1 = _max_pool(conv1_2,
                      kernel=[1, 2, 2, 1],
                      strides=[1, 2, 2, 1],
                      name='pool_1')

    conv2_1 = _conv_layer(pool1, [3, 3, 64, 128],
                          'conv2_1',
                          weight_decay,
                          is_training,
                          batch_norm=True)
    conv2_2 = _conv_layer(conv2_1, [3, 3, 128, 128],
                          'conv2_2',
                          weight_decay,
                          is_training,
                          batch_norm=True)
    pool2 = _max_pool(conv2_2,
                      kernel=[1, 2, 2, 1],
                      strides=[1, 2, 2, 1],
                      name='pool_2')

    # reshape = tf.reshape(pool3, [-1, 7*7*256])
    # fc1 = _fc_layer(pool2, layerShape=[3, 3, 128, 1024], name='fc1', weight_decay=weight_decay)
    fc1 = _conv_layer(pool2,
                      layer_shape=[3, 3, 128, 1024],
                      name='fc1',
                      weight_decay=weight_decay,
                      is_training=is_training,
                      strides=[1, 1, 1, 1],
                      pad='SAME',
                      activation='relu',
                      batch_norm=False,
                      has_activation=True)
    drop_fc1 = tf.nn.dropout(fc1, dropout)

    # fc2 = _fc_layer(drop_fc1, layerShape=[1, 1, 1024, 1024], name='fc2', weight_decay=weight_decay)
    fc2 = _conv_layer(drop_fc1,
                      layer_shape=[1, 1, 1024, 1024],
                      name='fc2',
                      weight_decay=weight_decay,
                      is_training=is_training,
                      strides=[1, 1, 1, 1],
                      pad='SAME',
                      activation='relu',
                      batch_norm=False,
                      has_activation=True)
    drop_fc2 = tf.nn.dropout(fc2, dropout)

    # score_fr = _score_layer(drop_fc2, 'score_fr', weight_decay)
    score_fr = _conv_layer(
        drop_fc2, [1, 1, drop_fc2.get_shape()[3].value, num_classes],
        'score_fr',
        weight_decay,
        is_training,
        strides=[1, 1, 1, 1],
        pad='SAME',
        batch_norm=False,
        has_activation=False)

    new_shape = [
        tf.shape(pool1)[0],
        tf.shape(pool1)[1],
        tf.shape(pool1)[2], num_classes
    ]
    try:
        output_shape = tf.pack(new_shape)
    except:
        output_shape = tf.stack(new_shape)
    # upscore2 = _upscore_layer(score_fr, layerShape=tf.shape(pool1), name='upscore2',
    # weight_decay=weight_decay, ksize=4, stride=2)
    upscore2 = _deconv_layer(
        score_fr,
        [4, 4, num_classes, score_fr.get_shape()[3].value],
        output_shape,
        'upscore2',
        weight_decay,
        strides=[1, 2, 2, 1],
        pad='SAME')

    # score_pool4 = _score_layer(pool1, "score_pool1", weight_decay)
    score_pool4 = _conv_layer(
        pool1, [1, 1, pool1.get_shape()[3].value, num_classes],
        'score_pool1',
        weight_decay,
        is_training,
        strides=[1, 1, 1, 1],
        pad='SAME',
        batch_norm=False,
        has_activation=False)
    fuse_pool4 = tf.add(upscore2, score_pool4)

    new_shape = [tf.shape(x)[0], tf.shape(x)[1], tf.shape(x)[2], num_classes]
    try:
        output_shape = tf.pack(new_shape)
    except:
        output_shape = tf.stack(new_shape)
    # upscore3 = _upscore_layer(fuse_pool4, layerShape=tf.shape(x), name='upscore3', weight_decay=weight_decay,
    #                           ksize=4, stride=2)
    upscore3 = _deconv_layer(
        fuse_pool4,
        [4, 4, num_classes, fuse_pool4.get_shape()[3].value],
        output_shape,
        'upscore3',
        weight_decay,
        strides=[1, 2, 2, 1],
        pad='SAME')

    return upscore3