Esempio n. 1
0
def FCN_VGG19(input_shape, classes,
              trainable_encoder=True, weights='imagenet'):
    """Fully Convolutional Networks for semantic segmentation with VGG16.

    # Arguments
        input_shape: input image shape
        classes: number of classes
        trainable_encoder: Bool whether the weights of encoder are trainable
        weights: pre-trained weights to load (None for training from scratch)



    # Returns
        A Keras model instance

    """
    # input
    inputs = Input(shape=input_shape)

    # Get the feature pyramid [drop7, pool4, pool3] from the VGG16 encoder
    pyramid_layers = 3
    encoder = VGG19(inputs, weights='imagenet', trainable=trainable_encoder)
    feat_pyramid = encoder.outputs[:pyramid_layers]

    # Append image to the end of feature pyramid
    feat_pyramid.append(inputs)

    # Decode feature pyramid
    outputs = VGGUpsampler(feat_pyramid, scales=[1, 1e-2, 1e-4], classes=21)

    # Activation TODO{jihong} work only for channels_last
    outputs = Activation('softmax')(outputs)

    # return model
    return Model(inputs=inputs, outputs=outputs)
Esempio n. 2
0
def test_vgg_upsampler():
    if K.image_data_format() == 'channels_last':
        inputs = Input(shape=(500, 500, 3))
        pool3 = Input(shape=(63, 63, 256))
        pool4 = Input(shape=(32, 32, 512))
        drop7 = Input(shape=(16, 16, 4096))
        score_shape = (None, 500, 500, 21)
    else:
        inputs = Input(shape=(3, 500, 500))
        pool3 = Input(shape=(256, 63, 63))
        pool4 = Input(shape=(512, 32, 32))
        drop7 = Input(shape=(4096, 16, 16))
        score_shape = (None, 21, 500, 500)
    pyramid = [drop7, pool4, pool3, inputs]
    scales = [1., 1e-2, 1e-4]
    score = VGGUpsampler(pyramid, scales, classes=21)
    assert K.int_shape(score) == score_shape