示例#1
0
def SegNet_basic(input_shape, n_classes, w_decay):
    """
    Create the SegNET Model for the semgmentation approach. Encoding and Decoding Layers.
    The SegNET uses a AutoEncoder Class() in order to create the Encoding and Decoding part for the network.
    :param input_shape: input shape of the images
    :param n_classes: number of classes of the dataset
    :param w_decay: weight_decay
    
    :return: SegNet Basic Model 
    """

    # K.set_image_dim_ordering('tf')
    inputs = Input(input_shape)

    layers = AutoEncoder_Layers(n_classes=n_classes,
                                w_decay=w_decay,
                                border_mode='valid')

    encoding_layers = layers.EncodingLayers(inputs)
    decoding_layers = layers.DecodingLayers(encoding_layers)

    x = Activation('relu')(decoding_layers)
    # x = CropLayer2D(inputs, name='score')(x)
    softmax_segnet = NdSoftmax()(x)
    # softmax_segnet = Activation('softmax')(x)

    model = Model(input=inputs, output=softmax_segnet)

    return model
示例#2
0
def build_segnet_vgg(inputs, n_classes, l2_reg=0.):

    """ encoding layers """
    enc1 = downsampling_block_vgg(inputs, 2, 64, 3, 1, l2_reg)
    enc2 = downsampling_block_vgg(enc1, 2, 128, 3, 2, l2_reg)
    enc3 = downsampling_block_vgg(enc2, 3, 256, 3, 3, l2_reg)
    enc4 = downsampling_block_vgg(enc3, 3, 512, 3, 4, l2_reg)
    enc5 = downsampling_block_vgg(enc4, 3, 512, 3, 5, l2_reg)

    """ decoding layers """
    dec5 = upsampling_block_vgg(enc5, 3, 512, 3, 5, l2_reg, enc5)
    dec4 = upsampling_block_vgg(dec5, 3, 512, 3, 4, l2_reg, enc4)
    dec3 = upsampling_block_vgg(dec4, 3, 256, 3, 3, l2_reg, enc3)
    dec2 = upsampling_block_vgg(dec3, 2, 128, 3, 2, l2_reg, enc2)
    dec1 = upsampling_block_vgg(dec2, 2, 64, 3, 1, l2_reg, enc1)

    """ logits """
    l1 = Convolution2D(n_classes, 1, 1, border_mode='valid')(dec1)
    score = CropLayer2D(inputs, name='score')(l1)
    softmax_segnet = NdSoftmax()(score)

    # Complete model
    model = Model(input=inputs, output=softmax_segnet)

    return model
示例#3
0
def build_segnet_basic(inputs, n_classes, depths=[64, 64, 64, 64],
                       filter_size=7, l2_reg=0.):
    """ encoding layers """
    enc1 = downsampling_block_basic(inputs, depths[0], filter_size, l2(l2_reg))
    enc2 = downsampling_block_basic(enc1, depths[1], filter_size, l2(l2_reg))
    enc3 = downsampling_block_basic(enc2, depths[2], filter_size, l2(l2_reg))
    enc4 = downsampling_block_basic(enc3, depths[3], filter_size, l2(l2_reg))

    """ decoding layers """
    dec1 = upsampling_block_basic(enc4, depths[3], filter_size, enc4,
                                  l2(l2_reg))
    dec2 = upsampling_block_basic(dec1, depths[2], filter_size, enc3,
                                  l2(l2_reg))
    dec3 = upsampling_block_basic(dec2, depths[1], filter_size, enc2,
                                  l2(l2_reg))
    dec4 = upsampling_block_basic(dec3, depths[0], filter_size, enc1,
                                  l2(l2_reg))

    """ logits """
    l1 = Convolution2D(n_classes, 1, 1, border_mode='valid')(dec4)
    score = CropLayer2D(inputs, name='score')(l1)
    softmax_segnet = NdSoftmax()(score)

    # Complete model
    model = Model(input=inputs, output=softmax_segnet)

    return model
示例#4
0
def build_segnet_basic(inputs,
                       n_classes,
                       depths=[64, 64, 64, 64],
                       filter_size=7,
                       l2_reg=0.):
    """ encoding layers """
    print('******++++*** ENTRE AQUI ++++++*******')
    enc1 = downsampling_block_basic(inputs, depths[0], filter_size, l2(l2_reg))
    enc2 = downsampling_block_basic(enc1, depths[1], filter_size, l2(l2_reg))
    enc3 = downsampling_block_basic(enc2, depths[2], filter_size, l2(l2_reg))
    enc4 = downsampling_block_basic(enc3, depths[3], filter_size, l2(l2_reg))
    """ decoding layers """
    dec1 = upsampling_block_basic(enc4, depths[3], filter_size, enc4,
                                  l2(l2_reg))
    dec2 = upsampling_block_basic(dec1, depths[2], filter_size, enc3,
                                  l2(l2_reg))
    dec3 = upsampling_block_basic(dec2, depths[1], filter_size, enc2,
                                  l2(l2_reg))
    dec4 = upsampling_block_basic(dec3, depths[0], filter_size, enc1,
                                  l2(l2_reg))
    """ logits """
    l1 = Conv2D(n_classes, (1, 1), padding='valid')(dec4)
    score = CropLayer2D(inputs, name='score')(l1)

    print('******++++*** score shape  ++++++*******', str(score.shape))
    softmax_segnet = NdSoftmax()(score)

    # Complete model
    model = Model(input=inputs, output=softmax_segnet)

    return model
示例#5
0
def build_segnet_basic(img_shape=(3, None, None), nclasses=8, l2_reg=0.,\
                       freeze_layers_from=None, path_weights=None,\
                       depths=[64, 64, 64, 64],filter_size=7):

    # Regularization warning
    if l2_reg > 0.:
        print("Regularizing the weights: " + str(l2_reg))

    # Build network

    # CONTRACTING PATH

    # Input layer
    inputs = Input(img_shape)
    padded1 = ZeroPadding2D(padding=(1, 1), name='pad1')(inputs)

    enc1 = downsampling_block_basic(padded1, depths[0], filter_size,
                                    l2(l2_reg))
    enc2 = downsampling_block_basic(enc1, depths[1], filter_size, l2(l2_reg))
    enc3 = downsampling_block_basic(enc2, depths[2], filter_size, l2(l2_reg))
    enc4 = downsampling_block_basic(enc3, depths[3], filter_size, l2(l2_reg))

    # ##### decoding layers

    dec1 = upsampling_block_basic(enc4, depths[3], filter_size, enc4,
                                  l2(l2_reg))
    dec2 = upsampling_block_basic(dec1, depths[2], filter_size, enc3,
                                  l2(l2_reg))
    dec3 = upsampling_block_basic(dec2, depths[1], filter_size, enc2,
                                  l2(l2_reg))
    dec4 = upsampling_block_basic(dec3, depths[0], filter_size, enc1,
                                  l2(l2_reg))

    l1 = Convolution2D(nclasses, 1, 1, border_mode='valid')(dec4)
    score = CropLayer2D(inputs, name='score')(l1)
    # Softmax
    softmax_segnet = NdSoftmax()(score)

    # Complete model
    model = Model(input=inputs, output=softmax_segnet)

    # Load pretrained Model
    if path_weights:
        load_matcovnet(model, n_classes=nclasses)

    # Freeze some layers
    if freeze_layers_from is not None:
        freeze_layers(model, freeze_layers_from)

    return model
示例#6
0
def segnet_basic(inp, kernel, concat_axis, n_classes, l2r):

    # Encoding layers
    enc1 = downsampling_block_basic(inp, 64, kernel, concat_axis, l2(l2r))
    enc2 = downsampling_block_basic(enc1, 64, kernel, concat_axis, l2(l2r))
    enc3 = downsampling_block_basic(enc2, 64, kernel, concat_axis, l2(l2r))
    enc4 = downsampling_block_basic(enc3, 64, kernel, concat_axis, l2(l2r))

    # Decoding layers
    dec1 = upsampling_block_basic(enc4, 64, kernel, enc4, concat_axis, l2(l2r))
    dec2 = upsampling_block_basic(dec1, 64, kernel, enc3, concat_axis, l2(l2r))
    dec3 = upsampling_block_basic(dec2, 64, kernel, enc2, concat_axis, l2(l2r))
    dec4 = upsampling_block_basic(dec3, 64, kernel, enc1, concat_axis, l2(l2r))

    l1 = Convolution2D(n_classes, 1, 1, border_mode='valid')(dec4)
    score = CropLayer2D(inp, name='score')(l1)
    predictions = NdSoftmax()(score)

    return predictions
示例#7
0
def create_classifier(body, data, n_classes, l2_reg=0.):
    # Include last layers
    top = BatchNormalization(mode=0, axis=channel_idx, name="bn7")(body)
    top = Activation('relu', name="relu7")(top)
    top = AtrousConvolution2D(512,
                              3,
                              3,
                              'he_normal',
                              atrous_rate=(12, 12),
                              border_mode='same',
                              name="conv6a",
                              W_regularizer=l2(l2_reg))(top)
    top = Activation('relu', name="conv6a_relu")(top)
    name = "hyperplane_num_cls_%d_branch_%d" % (n_classes, 12)

    def my_init(shape, name=None, dim_ordering='th'):
        return initializations.normal(shape, scale=0.01, name=name)

    top = AtrousConvolution2D(n_classes,
                              3,
                              3,
                              my_init,
                              atrous_rate=(12, 12),
                              border_mode='same',
                              name=name,
                              W_regularizer=l2(l2_reg))(top)

    top = Deconvolution2D(n_classes,
                          16,
                          16,
                          top._keras_shape,
                          bilinear_init,
                          'linear',
                          border_mode='valid',
                          subsample=(8, 8),
                          bias=False,
                          name="upscaling_" + str(n_classes),
                          W_regularizer=l2(l2_reg))(top)

    top = CropLayer2D(data, name='score')(top)
    top = NdSoftmax()(top)

    return top
示例#8
0
def build_unet(img_shape=(3, None, None),
               nclasses=8,
               l2_reg=0.,
               init='glorot_uniform',
               freeze_layers_from=None,
               path_weights=None):

    # Regularization warning
    if l2_reg > 0.:
        print("Regularizing the weights: " + str(l2_reg))

    # Build network

    # CONTRACTING PATH

    # Input layer
    inputs = Input(img_shape)
    padded1 = ZeroPadding2D(padding=(100, 100), name='padded1')(inputs)

    # Block 1
    conv1_1 = Convolution2D(64,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv1_1',
                            W_regularizer=l2(l2_reg))(padded1)
    conv1_2 = Convolution2D(64,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv1_2',
                            W_regularizer=l2(l2_reg))(conv1_1)
    pool1 = MaxPooling2D(pool_size=(2, 2), name='pool1')(conv1_2)

    # Block 2
    conv2_1 = Convolution2D(128,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv2_1',
                            W_regularizer=l2(l2_reg))(pool1)
    conv2_2 = Convolution2D(128,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv2_2',
                            W_regularizer=l2(l2_reg))(conv2_1)
    pool2 = MaxPooling2D(pool_size=(2, 2), name='pool2')(conv2_2)

    # Block 3
    conv3_1 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv3_1',
                            W_regularizer=l2(l2_reg))(pool2)
    conv3_2 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv3_2',
                            W_regularizer=l2(l2_reg))(conv3_1)
    pool3 = MaxPooling2D(pool_size=(2, 2), name='pool3')(conv3_2)

    # Block 4
    conv4_1 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv4_1',
                            W_regularizer=l2(l2_reg))(pool3)
    conv4_2 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv4_2',
                            W_regularizer=l2(l2_reg))(conv4_1)
    conv4_drop = Dropout(0.5, name='conv4_drop')(conv4_2)
    pool4 = MaxPooling2D(pool_size=(2, 2), name='pool4')(conv4_drop)

    # Block 5
    bottom_conv1 = Convolution2D(1024,
                                 3,
                                 3,
                                 init,
                                 'relu',
                                 border_mode='valid',
                                 name='bottom_conv1',
                                 W_regularizer=l2(l2_reg))(pool4)
    bottom_conv2 = Convolution2D(1024,
                                 3,
                                 3,
                                 init,
                                 'relu',
                                 border_mode='valid',
                                 name='bottom_conv2',
                                 W_regularizer=l2(l2_reg))(bottom_conv1)
    bottom_drop = Dropout(0.5, name='bottom_drop')(bottom_conv2)

    #uncoder blocks

    # Block 6
    deconv4 = Deconvolution2D(512,
                              2,
                              2,
                              bottom_drop._keras_shape,
                              init,
                              'linear',
                              border_mode='valid',
                              subsample=(2, 2),
                              name='deconv4',
                              W_regularizer=l2(l2_reg))(bottom_drop)
    conv4_crop = CropLayer2D(deconv4, name='conv4_crop')(conv4_drop)
    deconv4_crop = CropLayer2D(deconv4, name='deconv4_crop')(deconv4)
    deconv4_concat = merge([deconv4_crop, conv4_crop],
                           mode='concat',
                           concat_axis=3,
                           name='deconv4_concat')
    deconv4_1 = Convolution2D(512,
                              3,
                              3,
                              init,
                              'relu',
                              border_mode='valid',
                              name='deconv4_1',
                              W_regularizer=l2(l2_reg))(deconv4_concat)
    deconv4_2 = Convolution2D(512,
                              3,
                              3,
                              init,
                              'relu',
                              border_mode='valid',
                              name='deconv4_2',
                              W_regularizer=l2(l2_reg))(deconv4_1)

    # Block 7
    deconv3 = Deconvolution2D(256,
                              2,
                              2,
                              deconv4_2._keras_shape,
                              init,
                              'linear',
                              border_mode='valid',
                              subsample=(2, 2),
                              name='deconv3',
                              W_regularizer=l2(l2_reg))(deconv4_2)
    deconv3_crop = CropLayer2D(deconv3, name='deconv3_crop')(conv3_2)
    deconv3_concat = merge([deconv3_crop, deconv3],
                           mode='concat',
                           concat_axis=3,
                           name='deconv3_concat')
    deconv3_1 = Convolution2D(256,
                              3,
                              3,
                              init,
                              'relu',
                              border_mode='valid',
                              name='deconv3_1',
                              W_regularizer=l2(l2_reg))(deconv3_concat)
    deconv3_2 = Convolution2D(256,
                              3,
                              3,
                              init,
                              'relu',
                              border_mode='valid',
                              name='deconv3_2',
                              W_regularizer=l2(l2_reg))(deconv3_1)

    # Block 8
    deconv2 = Deconvolution2D(128,
                              2,
                              2,
                              deconv3_2._keras_shape,
                              init,
                              'linear',
                              border_mode='valid',
                              subsample=(2, 2),
                              name='deconv2',
                              W_regularizer=l2(l2_reg))(deconv3_2)
    deconv2_crop = CropLayer2D(deconv2, name='deconv2_crop')(conv2_2)
    deconv2_concat = merge([deconv2_crop, deconv2],
                           mode='concat',
                           concat_axis=3,
                           name='deconv2_concat')
    deconv2_1 = Convolution2D(128,
                              3,
                              3,
                              init,
                              'relu',
                              border_mode='valid',
                              name='deconv2_1',
                              W_regularizer=l2(l2_reg))(deconv2_concat)
    deconv2_2 = Convolution2D(128,
                              3,
                              3,
                              init,
                              'relu',
                              border_mode='valid',
                              name='deconv2_2',
                              W_regularizer=l2(l2_reg))(deconv2_1)

    # Block 9
    deconv1 = Deconvolution2D(64,
                              2,
                              2,
                              deconv2_2._keras_shape,
                              init,
                              'linear',
                              border_mode='valid',
                              subsample=(2, 2),
                              name='deconv1',
                              W_regularizer=l2(l2_reg))(deconv2_2)
    deconv1_crop = CropLayer2D(deconv1, name='deconv1_crop')(conv1_2)
    deconv1_concat = merge([deconv1_crop, deconv1],
                           mode='concat',
                           concat_axis=3,
                           name='deconv1_concat')
    deconv1_1 = Convolution2D(64,
                              3,
                              3,
                              init,
                              'relu',
                              border_mode='valid',
                              name='deconv1_1',
                              W_regularizer=l2(l2_reg))(deconv1_concat)
    deconv1_2 = Convolution2D(64,
                              3,
                              3,
                              init,
                              'relu',
                              border_mode='valid',
                              name='deconv1_2',
                              W_regularizer=l2(l2_reg))(deconv1_1)

    l1 = Convolution2D(nclasses, 1, 1, border_mode='valid',
                       name='logits')(deconv1_2)
    score = CropLayer2D(inputs, name='score')(l1)
    # Softmax
    softmax_segnet = NdSoftmax()(score)

    # Complete model
    model = Model(input=inputs, output=softmax_segnet)

    # Load pretrained Model
    if path_weights:
        load_matcovnet(model, n_classes=nclasses)

    # Freeze some layers
    if freeze_layers_from is not None:
        freeze_layers(model, freeze_layers_from)

    return model
示例#9
0
def build_unet(img_shape=(3, None, None),
               nclasses=8,
               l2_reg=0.,
               init='glorot_uniform',
               path_weights=None,
               freeze_layers_from=None,
               padding=100,
               dropout=True):

    # Regularization warning
    if l2_reg > 0.:
        print("Regularizing the weights: " + str(l2_reg))

    # Input
    inputs = Input(img_shape, name='input')
    padded = ZeroPadding2D(padding=(padding, padding), name='padded')(inputs)

    # Block 1
    conv1_1 = Convolution2D(64,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv1_1',
                            W_regularizer=l2(l2_reg))(padded)
    conv1_2 = Convolution2D(64,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv1_2',
                            W_regularizer=l2(l2_reg))(conv1_1)
    pool1 = MaxPooling2D((2, 2), (2, 2), name='pool1')(conv1_2)

    # Block 2
    conv2_1 = Convolution2D(128,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv2_1',
                            W_regularizer=l2(l2_reg))(pool1)
    conv2_2 = Convolution2D(128,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv2_2',
                            W_regularizer=l2(l2_reg))(conv2_1)
    pool2 = MaxPooling2D((2, 2), (2, 2), name='pool2')(conv2_2)

    # Block 3
    conv3_1 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv3_1',
                            W_regularizer=l2(l2_reg))(pool2)
    conv3_2 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv3_2',
                            W_regularizer=l2(l2_reg))(conv3_1)
    pool3 = MaxPooling2D((2, 2), (2, 2), name='pool3')(conv3_2)

    # Block 4
    conv4_1 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv4_1',
                            W_regularizer=l2(l2_reg))(pool3)
    conv4_2 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv4_2',
                            W_regularizer=l2(l2_reg))(conv4_1)
    if dropout:
        conv4_2 = Dropout(0.5, name='drop1')(conv4_2)
    pool4 = MaxPooling2D((2, 2), (2, 2), name='pool4')(conv4_2)

    # Block 5
    conv5_1 = Convolution2D(1024,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv5_1',
                            W_regularizer=l2(l2_reg))(pool4)
    conv5_2 = Convolution2D(1024,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv5_2',
                            W_regularizer=l2(l2_reg))(conv5_1)
    if dropout:
        conv5_2 = Dropout(0.5, name='drop2')(conv5_2)
    # pool5 = MaxPooling2D((2, 2), (2, 2), name='pool4')(conv5_2)

    # Upsampling 1
    upconv4 = Deconvolution2D(512,
                              2,
                              2,
                              conv5_2._keras_shape,
                              init,
                              'linear',
                              border_mode='valid',
                              subsample=(2, 2),
                              name='upconv4',
                              W_regularizer=l2(l2_reg))(conv5_2)
    conv4_2_crop = CropLayer2D(upconv4, name='conv4_2_crop')(conv4_2)
    upconv4_crop = CropLayer2D(upconv4, name='upconv4_crop')(upconv4)
    Concat_4 = merge([conv4_2_crop, upconv4_crop],
                     mode='concat',
                     concat_axis=3,
                     name='Concat_4')
    conv6_1 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv6_1',
                            W_regularizer=l2(l2_reg))(Concat_4)
    conv6_2 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv6_2',
                            W_regularizer=l2(l2_reg))(conv6_1)

    # Upsampling 2
    upconv3 = Deconvolution2D(256,
                              2,
                              2,
                              conv6_2._keras_shape,
                              init,
                              'linear',
                              border_mode='valid',
                              subsample=(2, 2),
                              name='upconv3',
                              W_regularizer=l2(l2_reg))(conv6_2)
    conv3_2_crop = CropLayer2D(upconv3, name='conv3_2_crop')(conv3_2)
    Concat_3 = merge([conv3_2_crop, upconv3], mode='concat', name='Concat_3')
    conv7_1 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv7_1',
                            W_regularizer=l2(l2_reg))(Concat_3)
    conv7_2 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv7_2',
                            W_regularizer=l2(l2_reg))(conv7_1)

    # Upsampling 3
    upconv2 = Deconvolution2D(128,
                              2,
                              2,
                              conv7_2._keras_shape,
                              init,
                              'linear',
                              border_mode='valid',
                              subsample=(2, 2),
                              name='upconv2',
                              W_regularizer=l2(l2_reg))(conv7_2)
    conv2_2_crop = CropLayer2D(upconv2, name='conv2_2_crop')(conv2_2)
    Concat_2 = merge([conv2_2_crop, upconv2], mode='concat', name='Concat_2')
    conv8_1 = Convolution2D(128,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv8_1',
                            W_regularizer=l2(l2_reg))(Concat_2)
    conv8_2 = Convolution2D(128,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv8_2',
                            W_regularizer=l2(l2_reg))(conv8_1)

    # Upsampling 4
    upconv1 = Deconvolution2D(64,
                              2,
                              2,
                              conv8_2._keras_shape,
                              init,
                              'linear',
                              border_mode='valid',
                              subsample=(2, 2),
                              name='upconv1',
                              W_regularizer=l2(l2_reg))(conv8_2)
    conv1_2_crop = CropLayer2D(upconv1, name='conv1_2_crop')(conv1_2)
    Concat_1 = merge([conv1_2_crop, upconv1], mode='concat', name='Concat_1')
    conv9_1 = Convolution2D(64,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv9_1',
                            W_regularizer=l2(l2_reg))(Concat_1)
    conv9_2 = Convolution2D(64,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv9_2',
                            W_regularizer=l2(l2_reg))(conv9_1)

    conv10 = Convolution2D(nclasses,
                           1,
                           1,
                           init,
                           'linear',
                           border_mode='valid',
                           name='conv10',
                           W_regularizer=l2(l2_reg))(conv9_2)

    # Crop
    final_crop = CropLayer2D(inputs, name='final_crop')(conv10)

    # Softmax
    softmax_unet = NdSoftmax()(final_crop)

    # Complete model
    model = Model(input=inputs, output=softmax_unet)

    # Load pretrained Model
    if path_weights:
        pass

    # Freeze some layers
    if freeze_layers_from is not None:
        freeze_layers(model, freeze_layers_from)

    return model
示例#10
0
def build_segnet(img_shape=(3, None, None),
                 nclasses=8,
                 l2_reg=0.,
                 init='glorot_uniform',
                 path_weights=None,
                 freeze_layers_from=None):

    # Regularization warning
    if l2_reg > 0.:
        print("Regularizing the weights: " + str(l2_reg))

    # Build network:

    #Encoding Block

    # Input layer
    inputs = Input(shape=img_shape)
    padded = ZeroPadding2D(padding=(10, 10), name='pad100')(inputs)

    #Enc Block 1
    X = batched_conv(padded, 64, 3, 3, l2_reg, 1, 'Enc')
    X = batched_conv(X, 64, 3, 3, l2_reg, 2, 'Enc')
    X = MaxPooling2D(name='Enc_MaxP_1')(X)

    #Enc Block 2
    X = batched_conv(X, 128, 3, 3, l2_reg, 3, 'Enc')
    X = batched_conv(X, 128, 3, 3, l2_reg, 4, 'Enc')
    X = MaxPooling2D(name='Enc_MaxP_2')(X)

    #Enc Block 3
    X = batched_conv(X, 256, 3, 3, l2_reg, 5, 'Enc')
    X = batched_conv(X, 256, 3, 3, l2_reg, 6, 'Enc')
    X = batched_conv(X, 256, 3, 3, l2_reg, 7, 'Enc')
    X = MaxPooling2D(name='Enc_MaxP_3')(X)

    #Enc Block 4
    X = batched_conv(X, 512, 3, 3, l2_reg, 8, 'Enc')
    X = batched_conv(X, 515, 3, 3, l2_reg, 9, 'Enc')
    X = batched_conv(X, 512, 3, 3, l2_reg, 10, 'Enc')
    X = MaxPooling2D(name='Enc_MaxP_4')(X)

    #Enc Block 5
    X = batched_conv(X, 512, 3, 3, l2_reg, 11, 'Enc')
    X = batched_conv(X, 512, 3, 3, l2_reg, 12, 'Enc')
    X = batched_conv(X, 512, 3, 3, l2_reg, 13, 'Enc')
    X = MaxPooling2D(name='Enc_MaxP_5')(X)

    #Dec Block 1
    X = UpSampling2D(name='Dec_Ups_1')(X)
    X = batched_conv(X, 512, 3, 3, l2_reg, 1, 'Dec')
    X = batched_conv(X, 512, 3, 3, l2_reg, 2, 'Dec')
    X = batched_conv(X, 512, 3, 3, l2_reg, 3, 'Dec')

    #Dec Block 2
    X = UpSampling2D(name='Dec_Ups_2')(X)
    X = batched_conv(X, 512, 3, 3, l2_reg, 4, 'Dec')
    X = batched_conv(X, 512, 3, 3, l2_reg, 5, 'Dec')
    X = batched_conv(X, 512, 3, 3, l2_reg, 6, 'Dec')

    #Dec Block 3
    X = UpSampling2D(name='Dec_Ups_3')(X)
    X = batched_conv(X, 256, 3, 3, l2_reg, 7, 'Dec')
    X = batched_conv(X, 256, 3, 3, l2_reg, 8, 'Dec')
    X = batched_conv(X, 256, 3, 3, l2_reg, 9, 'Dec')

    #Dec Block 4
    X = UpSampling2D(name='Dec_Ups_4')(X)
    X = batched_conv(X, 128, 3, 3, l2_reg, 10, 'Dec')
    X = batched_conv(X, 128, 3, 3, l2_reg, 11, 'Dec')

    #Dec Block 5
    X = UpSampling2D(name='Dec_Ups_5')(X)
    X = batched_conv(X, 128, 3, 3, l2_reg, 12, 'Dec')
    X = batched_conv(X, 128, 3, 3, l2_reg, 13, 'Dec')

    # Softmax
    X = CropLayer2D(inputs, name='Crop')(X)
    #X = Reshape(img_shape)(X)

    softmax_segnet = NdSoftmax()(X)

    #print img_shape

    #X = Reshape((5, 288 * 384))(X)
    #X = Permute((2, 1))(X)
    #softmax_segnet = Activation('softmax')(X)

    # Complete model
    model = Model(input=inputs, output=softmax_segnet)

    # Load pretrained Model
    if path_weights:
        load_matcovnet(model, path_weights, n_classes=nclasses)

    # Freeze some layers
    if freeze_layers_from is not None:
        freeze_layers(model, freeze_layers_from)

    return model
示例#11
0
def build_fcn8(img_shape=(3, None, None),
               nclasses=8,
               l2_reg=0.,
               init='glorot_uniform',
               path_weights=None,
               freeze_layers_from=None):

    # Regularization warning
    if l2_reg > 0.:
        print("Regularizing the weights: " + str(l2_reg))

    # Build network

    # CONTRACTING PATH

    # Input layer
    inputs = Input(img_shape)
    padded = ZeroPadding2D(padding=(100, 100), name='pad100')(inputs)

    # Block 1
    conv1_1 = Convolution2D(64,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='valid',
                            name='conv1_1',
                            W_regularizer=l2(l2_reg))(padded)
    conv1_2 = Convolution2D(64,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv1_2',
                            W_regularizer=l2(l2_reg))(conv1_1)
    pool1 = MaxPooling2D((2, 2), (2, 2), name='pool1')(conv1_2)

    # Block 2
    conv2_1 = Convolution2D(128,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv2_1',
                            W_regularizer=l2(l2_reg))(pool1)
    conv2_2 = Convolution2D(128,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv2_2',
                            W_regularizer=l2(l2_reg))(conv2_1)
    pool2 = MaxPooling2D((2, 2), (2, 2), name='pool2')(conv2_2)

    # Block 3
    conv3_1 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv3_1',
                            W_regularizer=l2(l2_reg))(pool2)
    conv3_2 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv3_2',
                            W_regularizer=l2(l2_reg))(conv3_1)
    conv3_3 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv3_3',
                            W_regularizer=l2(l2_reg))(conv3_2)
    pool3 = MaxPooling2D((2, 2), (2, 2), name='pool3')(conv3_3)

    # Block 4
    conv4_1 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv4_1',
                            W_regularizer=l2(l2_reg))(pool3)
    conv4_2 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv4_2',
                            W_regularizer=l2(l2_reg))(conv4_1)
    conv4_3 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv4_3',
                            W_regularizer=l2(l2_reg))(conv4_2)
    pool4 = MaxPooling2D((2, 2), (2, 2), name='pool4')(conv4_3)

    # Block 5
    conv5_1 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv5_1',
                            W_regularizer=l2(l2_reg))(pool4)
    conv5_2 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv5_2',
                            W_regularizer=l2(l2_reg))(conv5_1)
    conv5_3 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='conv5_3',
                            W_regularizer=l2(l2_reg))(conv5_2)
    pool5 = MaxPooling2D((2, 2), (2, 2), name='pool5')(conv5_3)

    # Block 6 (fully conv)
    fc6 = Convolution2D(4096,
                        7,
                        7,
                        init,
                        'relu',
                        border_mode='valid',
                        name='fc6',
                        W_regularizer=l2(l2_reg))(pool5)
    fc6 = Dropout(0.5)(fc6)

    # Block 7 (fully conv)
    fc7 = Convolution2D(
        4096,
        1,
        1,
        init,
        'relu',
        border_mode='valid',
        name='fc7',
        W_regularizer=l2(l2_reg),
    )(fc6)
    fc7 = Dropout(0.5)(fc7)

    score_fr = Convolution2D(nclasses,
                             1,
                             1,
                             init,
                             'relu',
                             border_mode='valid',
                             name='score_fr')(fc7)

    # DECONTRACTING PATH
    # Unpool 1
    score_pool4 = Convolution2D(nclasses,
                                1,
                                1,
                                init,
                                'relu',
                                border_mode='same',
                                name='score_pool4',
                                W_regularizer=l2(l2_reg))(pool4)
    score2 = Deconvolution2D(nclasses,
                             4,
                             4,
                             score_fr._keras_shape,
                             init,
                             'linear',
                             border_mode='valid',
                             subsample=(2, 2),
                             name='score2',
                             W_regularizer=l2(l2_reg))(score_fr)

    score_pool4_crop = CropLayer2D(score2,
                                   name='score_pool4_crop')(score_pool4)
    score_fused = merge([score_pool4_crop, score2],
                        mode=custom_sum,
                        output_shape=custom_sum_shape,
                        name='score_fused')

    # Unpool 2
    score_pool3 = Convolution2D(nclasses,
                                1,
                                1,
                                init,
                                'relu',
                                border_mode='valid',
                                name='score_pool3',
                                W_regularizer=l2(l2_reg))(pool3)

    score4 = Deconvolution2D(
        nclasses,
        4,
        4,
        score_fused._keras_shape,
        init,
        'linear',
        border_mode='valid',
        subsample=(2, 2),
        bias=True,  # TODO: No bias??
        name='score4',
        W_regularizer=l2(l2_reg))(score_fused)

    score_pool3_crop = CropLayer2D(score4,
                                   name='score_pool3_crop')(score_pool3)
    score_final = merge([score_pool3_crop, score4],
                        mode=custom_sum,
                        output_shape=custom_sum_shape,
                        name='score_final')

    upsample = Deconvolution2D(
        nclasses,
        16,
        16,
        score_final._keras_shape,
        init,
        'linear',
        border_mode='valid',
        subsample=(8, 8),
        bias=False,  # TODO: No bias??
        name='upsample',
        W_regularizer=l2(l2_reg))(score_final)

    score = CropLayer2D(inputs, name='score')(upsample)

    # Softmax
    softmax_fcn8 = NdSoftmax()(score)

    # Complete model
    model = Model(input=inputs, output=softmax_fcn8)

    # Load pretrained Model
    if path_weights:
        load_matcovnet(model, n_classes=nclasses)

    # Freeze some layers
    if freeze_layers_from is not None:
        freeze_layers(model, freeze_layers_from)

    return model
示例#12
0
def inceptionSeg(inp, kernel, concat_axis, n_classes):
    # Encoding layers: inception  layers
#    print('Entra1')
#    base_model = InceptionV3(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)
#    print('Entra2')
#    x = base_model.layers[-2].output


    conv1_7x7_s2 = Convolution2D(64,7,7,subsample=(2,2),border_mode='same',activation='relu',name='conv1/7x7_s2')(inp)
    
#    conv1_zero_pad = ZeroPadding2D(padding=(1, 1))(conv1_7x7_s2)
#    
#    pool1_helper = PoolHelper()(conv1_zero_pad)
    
#    pool1_3x3_s2 = MaxPooling2D(pool_size=(3,3),strides=(2,2),border_mode='valid',name='pool1/3x3_s2')(pool1_helper)
    pool1_3x3_s2 = MaxPooling2D(pool_size=(3,3),strides=(2,2),border_mode='valid',name='pool1/3x3_s2')(conv1_7x7_s2)    
    #pool1_norm1 = LRN(name='pool1/norm1')(pool1_3x3_s2)
    
    conv2_3x3_reduce = Convolution2D(64,1,1,border_mode='same',activation='relu',name='conv2/3x3_reduce')(pool1_3x3_s2)
    
    conv2_3x3 = Convolution2D(192,3,3,border_mode='same',activation='relu',name='conv2/3x3')(conv2_3x3_reduce)
    
    #conv2_norm2 = LRN(name='conv2/norm2')(conv2_3x3)
    
    conv2_zero_pad = ZeroPadding2D(padding=(1, 1))(conv2_3x3)
    
    pool2_helper = PoolHelper()(conv2_zero_pad)
    
    pool2_3x3_s2 = MaxPooling2D(pool_size=(3,3),strides=(2,2),border_mode='valid',name='pool2/3x3_s2')(pool2_helper)
    
    # First Inception module
    inception_3a_1x1 = Convolution2D(64,1,1,border_mode='same',activation='relu',name='inception_3a/1x1')(pool2_3x3_s2)
    
    inception_3a_3x3_reduce = Convolution2D(96,1,1,border_mode='same',activation='relu',name='inception_3a/3x3_reduce')(pool2_3x3_s2)
    
    inception_3a_3x3 = Convolution2D(128,3,3,border_mode='same',activation='relu',name='inception_3a/3x3')(inception_3a_3x3_reduce)
    
    inception_3a_5x5_reduce = Convolution2D(16,1,1,border_mode='same',activation='relu',name='inception_3a/5x5_reduce')(pool2_3x3_s2)
    
    inception_3a_5x5 = Convolution2D(32,5,5,border_mode='same',activation='relu',name='inception_3a/5x5')(inception_3a_5x5_reduce)
    
    inception_3a_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode='same',name='inception_3a/pool')(pool2_3x3_s2)
    
    inception_3a_pool_proj = Convolution2D(32,1,1,border_mode='same',activation='relu',name='inception_3a/pool_proj')(inception_3a_pool)
    
    inception_3a_output = merge([inception_3a_1x1,inception_3a_3x3,inception_3a_5x5,inception_3a_pool_proj],mode='concat',concat_axis=1,name='inception_3a/output')
    
		# Second Inception module
    inception_3b_1x1 = Convolution2D(128,1,1,border_mode='same',activation='relu',name='inception_3b/1x1')(inception_3a_output)
    
    inception_3b_3x3_reduce = Convolution2D(128,1,1,border_mode='same',activation='relu',name='inception_3b/3x3_reduce')(inception_3a_output)
    
    inception_3b_3x3 = Convolution2D(192,3,3,border_mode='same',activation='relu',name='inception_3b/3x3')(inception_3b_3x3_reduce)
    
    inception_3b_5x5_reduce = Convolution2D(32,1,1,border_mode='same',activation='relu',name='inception_3b/5x5_reduce' )(inception_3a_output)
    
    inception_3b_5x5 = Convolution2D(96,5,5,border_mode='same',activation='relu',name='inception_3b/5x5' )(inception_3b_5x5_reduce)
    
    inception_3b_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode='same',name='inception_3b/pool')(inception_3a_output)
    
    inception_3b_pool_proj = Convolution2D(64,1,1,border_mode='same',activation='relu',name='inception_3b/pool_proj' )(inception_3b_pool)
    
    inception_3b_output = merge([inception_3b_1x1,inception_3b_3x3,inception_3b_5x5,inception_3b_pool_proj],mode='concat',concat_axis=1,name='inception_3b/output')
    
    
    inception_3b_output_zero_pad = ZeroPadding2D(padding=(1, 1))(inception_3b_output)
    
    pool3_helper = PoolHelper()(inception_3b_output_zero_pad)
    
    pool3_3x3_s2 = MaxPooling2D(pool_size=(3,3),strides=(2,2),border_mode='valid',name='pool3/3x3_s2')(pool3_helper)
    
		# Third Inception module
    inception_4a_1x1 = Convolution2D(192,1,1,border_mode='same',activation='relu',name='inception_4a/1x1')(pool3_3x3_s2)
    
    inception_4a_3x3_reduce = Convolution2D(96,1,1,border_mode='same',activation='relu',name='inception_4a/3x3_reduce')(pool3_3x3_s2)
    
    inception_4a_3x3 = Convolution2D(208,3,3,border_mode='same',activation='relu',name='inception_4a/3x3')(inception_4a_3x3_reduce)
    
    inception_4a_5x5_reduce = Convolution2D(16,1,1,border_mode='same',activation='relu',name='inception_4a/5x5_reduce')(pool3_3x3_s2)
    
    inception_4a_5x5 = Convolution2D(48,5,5,border_mode='same',activation='relu',name='inception_4a/5x5' )(inception_4a_5x5_reduce)
    
    inception_4a_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode='same',name='inception_4a/pool')(pool3_3x3_s2)
    
    inception_4a_pool_proj = Convolution2D(64,1,1,border_mode='same',activation='relu',name='inception_4a/pool_proj')(inception_4a_pool)
    
    inception_4a_output = merge([inception_4a_1x1,inception_4a_3x3,inception_4a_5x5,inception_4a_pool_proj],mode='concat',concat_axis=1,name='inception_4a/output')
    
    
    loss1_ave_pool = AveragePooling2D(pool_size=(5,5),strides=(3,3),name='loss1/ave_pool')(inception_4a_output)
    
    loss1_conv = Convolution2D(128,1,1,border_mode='same',activation='relu',name='loss1/conv')(loss1_ave_pool)
    
    loss1_flat = Flatten()(loss1_conv)
    
    loss1_fc = Dense(1024,activation='relu',name='loss1/fc')(loss1_flat)
    
    loss1_drop_fc = Dropout(0.7)(loss1_fc)
    
    loss1_classifier = Dense(1000,name='loss1/classifier')(loss1_drop_fc)
    
    loss1_classifier_act = Activation('softmax')(loss1_classifier)
    
		# Fourth Inception module
    inception_4b_1x1 = Convolution2D(160,1,1,border_mode='same',activation='relu',name='inception_4b/1x1')(inception_4a_output)
    
    inception_4b_3x3_reduce = Convolution2D(112,1,1,border_mode='same',activation='relu',name='inception_4b/3x3_reduce')(inception_4a_output)
    
    inception_4b_3x3 = Convolution2D(224,3,3,border_mode='same',activation='relu',name='inception_4b/3x3' )(inception_4b_3x3_reduce)
    
    inception_4b_5x5_reduce = Convolution2D(24,1,1,border_mode='same',activation='relu',name='inception_4b/5x5_reduce')(inception_4a_output)
    
    inception_4b_5x5 = Convolution2D(64,5,5,border_mode='same',activation='relu',name='inception_4b/5x5')(inception_4b_5x5_reduce)
    
    inception_4b_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode='same',name='inception_4b/pool')(inception_4a_output)
    
    inception_4b_pool_proj = Convolution2D(64,1,1,border_mode='same',activation='relu',name='inception_4b/pool_proj')(inception_4b_pool)
    
    inception_4b_output = merge([inception_4b_1x1,inception_4b_3x3,inception_4b_5x5,inception_4b_pool_proj],mode='concat',concat_axis=1,name='inception_4b_output')
    
		# Fifth Inception module
    inception_4c_1x1 = Convolution2D(128,1,1,border_mode='same',activation='relu',name='inception_4c/1x1')(inception_4b_output)
    
    inception_4c_3x3_reduce = Convolution2D(128,1,1,border_mode='same',activation='relu',name='inception_4c/3x3_reduce')(inception_4b_output)
    
    inception_4c_3x3 = Convolution2D(256,3,3,border_mode='same',activation='relu',name='inception_4c/3x3')(inception_4c_3x3_reduce)
    
    inception_4c_5x5_reduce = Convolution2D(24,1,1,border_mode='same',activation='relu',name='inception_4c/5x5_reduce')(inception_4b_output)
    
    inception_4c_5x5 = Convolution2D(64,5,5,border_mode='same',activation='relu',name='inception_4c/5x5')(inception_4c_5x5_reduce)
    
    inception_4c_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode='same',name='inception_4c/pool')(inception_4b_output)
    
    inception_4c_pool_proj = Convolution2D(64,1,1,border_mode='same',activation='relu',name='inception_4c/pool_proj')(inception_4c_pool)
    
    inception_4c_output = merge([inception_4c_1x1,inception_4c_3x3,inception_4c_5x5,inception_4c_pool_proj],mode='concat',concat_axis=1,name='inception_4c/output')
    
		# Sixth Inception module
    inception_4d_1x1 = Convolution2D(112,1,1,border_mode='same',activation='relu',name='inception_4d/1x1')(inception_4c_output)
    
    inception_4d_3x3_reduce = Convolution2D(144,1,1,border_mode='same',activation='relu',name='inception_4d/3x3_reduce')(inception_4c_output)
    
    inception_4d_3x3 = Convolution2D(288,3,3,border_mode='same',activation='relu',name='inception_4d/3x3' )(inception_4d_3x3_reduce)
    
    inception_4d_5x5_reduce = Convolution2D(32,1,1,border_mode='same',activation='relu',name='inception_4d/5x5_reduce')(inception_4c_output)
    
    inception_4d_5x5 = Convolution2D(64,5,5,border_mode='same',activation='relu',name='inception_4d/5x5')(inception_4d_5x5_reduce)
    
    inception_4d_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode='same',name='inception_4d/pool')(inception_4c_output)
    
    inception_4d_pool_proj = Convolution2D(64,1,1,border_mode='same',activation='relu',name='inception_4d/pool_proj')(inception_4d_pool)
    
    inception_4d_output = merge([inception_4d_1x1,inception_4d_3x3,inception_4d_5x5,inception_4d_pool_proj],mode='concat',concat_axis=1,name='inception_4d/output')
    
    
    loss2_ave_pool = AveragePooling2D(pool_size=(5,5),strides=(3,3),name='loss2/ave_pool')(inception_4d_output)
    
    loss2_conv = Convolution2D(128,1,1,border_mode='same',activation='relu',name='loss2/conv')(loss2_ave_pool)
    
    loss2_flat = Flatten()(loss2_conv)
    
    loss2_fc = Dense(1024,activation='relu',name='loss2/fc' )(loss2_flat)
    
    loss2_drop_fc = Dropout(0.7)(loss2_fc)
    
    loss2_classifier = Dense(1000,name='loss2/classifier' )(loss2_drop_fc)
    
    loss2_classifier_act = Activation('softmax')(loss2_classifier)
    
		# Seventh Inception module
    inception_4e_1x1 = Convolution2D(256,1,1,border_mode='same',activation='relu',name='inception_4e/1x1' )(inception_4d_output)
    
    inception_4e_3x3_reduce = Convolution2D(160,1,1,border_mode='same',activation='relu',name='inception_4e/3x3_reduce' )(inception_4d_output)
    
    inception_4e_3x3 = Convolution2D(320,3,3,border_mode='same',activation='relu',name='inception_4e/3x3')(inception_4e_3x3_reduce)
    
    inception_4e_5x5_reduce = Convolution2D(32,1,1,border_mode='same',activation='relu',name='inception_4e/5x5_reduce' )(inception_4d_output)
    
    inception_4e_5x5 = Convolution2D(128,5,5,border_mode='same',activation='relu',name='inception_4e/5x5' )(inception_4e_5x5_reduce)
    
    inception_4e_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode='same',name='inception_4e/pool')(inception_4d_output)
    
    inception_4e_pool_proj = Convolution2D(128,1,1,border_mode='same',activation='relu',name='inception_4e/pool_proj')(inception_4e_pool)
    
    inception_4e_output = merge([inception_4e_1x1,inception_4e_3x3,inception_4e_5x5,inception_4e_pool_proj],mode='concat',concat_axis=1,name='inception_4e/output')
    
    
    inception_4e_output_zero_pad = ZeroPadding2D(padding=(1, 1))(inception_4e_output)
    
    pool4_helper = PoolHelper()(inception_4e_output_zero_pad)
    
    pool4_3x3_s2 = MaxPooling2D(pool_size=(3,3),strides=(2,2),border_mode='valid',name='pool4/3x3_s2')(pool4_helper)
    
		# Eighth Inception module
    inception_5a_1x1 = Convolution2D(256,1,1,border_mode='same',activation='relu',name='inception_5a/1x1' )(pool4_3x3_s2)
    
    inception_5a_3x3_reduce = Convolution2D(160,1,1,border_mode='same',activation='relu',name='inception_5a/3x3_reduce' )(pool4_3x3_s2)
    
    inception_5a_3x3 = Convolution2D(320,3,3,border_mode='same',activation='relu',name='inception_5a/3x3' )(inception_5a_3x3_reduce)
    
    inception_5a_5x5_reduce = Convolution2D(32,1,1,border_mode='same',activation='relu',name='inception_5a/5x5_reduce' )(pool4_3x3_s2)
    
    inception_5a_5x5 = Convolution2D(128,5,5,border_mode='same',activation='relu',name='inception_5a/5x5' )(inception_5a_5x5_reduce)
    
    inception_5a_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode='same',name='inception_5a/pool')(pool4_3x3_s2)
    
    inception_5a_pool_proj = Convolution2D(128,1,1,border_mode='same',activation='relu',name='inception_5a/pool_proj' )(inception_5a_pool)
    
    inception_5a_output = merge([inception_5a_1x1,inception_5a_3x3,inception_5a_5x5,inception_5a_pool_proj],mode='concat',concat_axis=1,name='inception_5a/output')
    
		# Nineth Inception module
    inception_5b_1x1 = Convolution2D(384,1,1,border_mode='same',activation='relu',name='inception_5b/1x1' )(inception_5a_output)
    
    inception_5b_3x3_reduce = Convolution2D(192,1,1,border_mode='same',activation='relu',name='inception_5b/3x3_reduce' )(inception_5a_output)
    
    inception_5b_3x3 = Convolution2D(384,3,3,border_mode='same',activation='relu',name='inception_5b/3x3' )(inception_5b_3x3_reduce)
    
    inception_5b_5x5_reduce = Convolution2D(48,1,1,border_mode='same',activation='relu',name='inception_5b/5x5_reduce' )(inception_5a_output)
    
    inception_5b_5x5 = Convolution2D(128,5,5,border_mode='same',activation='relu',name='inception_5b/5x5' )(inception_5b_5x5_reduce)
    
    inception_5b_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode='same',name='inception_5b/pool')(inception_5a_output)
    
    inception_5b_pool_proj = Convolution2D(128,1,1,border_mode='same',activation='relu',name='inception_5b/pool_proj' )(inception_5b_pool)
    
    inception_5b_output = merge([inception_5b_1x1,inception_5b_3x3,inception_5b_5x5,inception_5b_pool_proj],mode='concat',concat_axis=1,name='inception_5b/output')    
      # DECONTRACTING PATH
    # Unpool 1
    score_pool4 = Convolution2D(nclasses, 1, 1, init, 'relu', border_mode='same',
                                name='score_pool4',
                                W_regularizer=l2(l2_reg))(x)
    score2 = Deconvolution2D(nclasses, 4, 4, score_fr._keras_shape, init,
                             'linear', border_mode='valid', subsample=(2, 2),
                             name='score2', W_regularizer=l2(l2_reg))(score_fr)

    score_pool4_crop = CropLayer2D(score2,
                                   name='score_pool4_crop')(score_pool4)
    score_fused = merge([score_pool4_crop, score2], mode=custom_sum,
                        output_shape=custom_sum_shape, name='score_fused')

    # Unpool 2
    score_pool3 = Convolution2D(nclasses, 1, 1, init, 'relu', border_mode='valid',
                                name='score_pool3',
                                W_regularizer=l2(l2_reg))(pool3)

    score4 = Deconvolution2D(nclasses, 4, 4, score_fused._keras_shape, init,
                             'linear', border_mode='valid', subsample=(2, 2),
                             bias=True,     # TODO: No bias??
                             name='score4', W_regularizer=l2(l2_reg))(score_fused)

    score_pool3_crop = CropLayer2D(score4, name='score_pool3_crop')(score_pool3)
    score_final = merge([score_pool3_crop, score4], mode=custom_sum,
                        output_shape=custom_sum_shape, name='score_final')

    upsample = Deconvolution2D(nclasses, 16, 16, score_final._keras_shape, init,
                               'linear', border_mode='valid', subsample=(8, 8),
                               bias=False,     # TODO: No bias??
                               name='upsample', W_regularizer=l2(l2_reg))(score_final)

    score = CropLayer2D(inputs, name='score')(upsample)

    # Softmax
    predictions = NdSoftmax()(score)
    
	
	## Decoder part ##
	#First decoding block
	
	
		#Final block
    # pool5_7x7_s1 = AveragePooling2D(pool_size=(7,7),strides=(1,1),name='pool5/7x7_s2')(inception_5b_output)
    
    # loss3_flat = Flatten()(pool5_7x7_s1)
    
    # pool5_drop_7x7_s1 = Dropout(0.4)(loss3_flat)
    
    # loss3_classifier = Dense(1000,name='loss3/classifier' )(pool5_drop_7x7_s1)
    
    # predictions = Activation('softmax',name='prob')(loss3_classifier)

	#googlenet = Model(input=input, output=[loss1_classifier_act,loss2_classifier_act,loss3_classifier_act])
	
    return predictions
示例#13
0
def segnet_VGG(inp, kernel, concat_axis, n_classes):
    # Encoding layers: VGG 13 convolutional layers
    x = Convolution2D(64, kernel, kernel, border_mode='same')(inp)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    x = Convolution2D(64, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    pool1 = MaxPooling2D((2, 2), strides=(2, 2))(x)

    x = Convolution2D(128, kernel, kernel, border_mode='same')(pool1)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    x = Convolution2D(128, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    pool2 = MaxPooling2D((2, 2), strides=(2, 2))(x)

    x = Convolution2D(256, kernel, kernel, border_mode='same')(pool2)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    x = Convolution2D(256, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    x = Convolution2D(256, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    pool3 = MaxPooling2D((2, 2), strides=(2, 2))(x)

    x = Convolution2D(512, kernel, kernel, border_mode='same')(pool3)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    x = Convolution2D(512, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    x = Convolution2D(512, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    pool4 = MaxPooling2D((2, 2), strides=(2, 2))(x)

    x = Convolution2D(512, kernel, kernel, border_mode='same')(pool4)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    x = Convolution2D(512, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    x = Convolution2D(512, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Activation('relu')(x)
    pool5 = MaxPooling2D((2, 2), strides=(2, 2))(x)

    # Decoding layers
    x = DePool2D(pool2d_layer=pool5, size=(2, 2))(pool5)
    x = Convolution2D(512, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Convolution2D(512, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Convolution2D(512, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)

    x = DePool2D(pool2d_layer=pool4, size=(2, 2))(x)
    x = Convolution2D(512, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Convolution2D(512, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Convolution2D(256, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)

    x = DePool2D(pool2d_layer=pool3, size=(2, 2))(x)
    x = Convolution2D(256, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Convolution2D(256, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Convolution2D(128, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)

    x = DePool2D(pool2d_layer=pool2, size=(2, 2))(x)
    x = Convolution2D(128, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Convolution2D(64, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)

    x = DePool2D(pool2d_layer=pool1, size=(2, 2))(x)
    x = Convolution2D(64, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)
    x = Convolution2D(64, kernel, kernel, border_mode='same')(x)
    x = BatchNormalization(mode=0, axis=concat_axis)(x)

    x = Convolution2D(n_classes, 1, 1, border_mode='valid')(x)

    score = CropLayer2D(inp, name='score')(x)
    predictions = NdSoftmax()(score)

    return predictions
示例#14
0
def build_densenet_segmentation(in_shape=(3, 224, 224),
                                n_classes=1000,
                                weight_decay=0.,
                                freeze_layers_from='base_model',
                                path_weights=None):

    #####################
    # First Convolution #
    #####################
    print('Input shape:' + str(in_shape))
    inp = Input(shape=in_shape)
    n_filter = 48
    x = Convolution2D(n_filter, 3, 3, subsample=(1, 1),
                      border_mode='same')(inp)

    #####################
    # Downsampling path #
    #####################

    growth_rate = 16
    dropout_fraction = 0.2
    n_layers_down = [4, 5, 7, 10, 12]
    skip_connection_list = []
    for i in range(len(n_layers_down)):
        #Dense block
        x, n_filter = denseBlock_down(x, n_layers_down[i], growth_rate,
                                      n_filter, dropout_fraction)

        print('number of filters = ' + str(x._keras_shape[-1]))

        x = transition_down_Layer(x, n_filter, dropout_fraction)
        # At the end of the dense block, the current output is stored in the skip_connections list
        skip_connection_list.append(x)


#        print('Shape: ' + str(x._keras_shape))
    skip_connection_list = skip_connection_list[::-1]

    #####################
    #     Bottleneck    #
    #####################
    n_layers = 15
    # We store now the output of the next dense block in a list(block_to_upsample).
    # We will only upsample these new feature maps
    x, n_filter, block_to_upsample = denseBlock_up(x, n_layers, growth_rate,
                                                   n_filter, dropout_fraction)
    print('number of filters = ' + str(x._keras_shape[-1]))

    # Add dense blocks of upsampling path

    n_layers_up = [15, 12, 10, 7, 5, 4]
    for j in range(1, len(n_layers_up)):

        # Transition Up ( Upsampling + concatenation with the skip connection)
        n_filters_keep = growth_rate * n_layers_up[j - 1]

        x = transition_up_Layer(skip_connection_list[j - 1], block_to_upsample,
                                n_filters_keep)
        x, n_filter, block_to_upsample = denseBlock_up(x, n_layers_up[j],
                                                       growth_rate, n_filter,
                                                       dropout_fraction)
        print('number of filters = ' + str(x._keras_shape[-1]))
    x = Deconvolution2D(n_filters_keep,
                        3,
                        3,
                        input_shape=x._keras_shape,
                        activation='linear',
                        border_mode='valid',
                        subsample=(2, 2))(x)
    x = CropLayer2D(inp)(x)
    #Last convolution
    x = Convolution2D(n_classes, 1, 1, subsample=(1, 1), border_mode='same')(x)

    #####################
    #      Softmax      #
    #####################
    predictions = NdSoftmax()(x)
    print('Predictions_shape: ' + str(predictions._keras_shape))
    # This is the model we will train
    model = Model(input=inp, output=predictions)
    model.summary()
    # Freeze some layers
    if freeze_layers_from is not None:
        if freeze_layers_from == 'base_model':
            print('   Freezing base model layers')
            for layer in model.layers:
                layer.trainable = False
        else:
            for i, layer in enumerate(model.layers):
                print(i, layer.name)
            print('   Freezing from layer 0 to ' + str(freeze_layers_from))
            for layer in model.layers[:freeze_layers_from]:
                layer.trainable = False
            for layer in model.layers[freeze_layers_from:]:
                layer.trainable = True

    return model
示例#15
0
文件: fcn8.py 项目: vt0311/bmi_test
def build_fcn8(img_shape,
               x_shape=None,
               dim_ordering='th',
               l2_reg=0.,
               nclasses=8,
               x_test_val=None,
               weights_file=False,
               **kwargs):

    # For Theano debug prouposes
    if x_test_val is not None:
        inputs.tag.test_value = x_test_val
        theano.config.compute_test_value = "warn"

    do = dim_ordering

    # Regularization warning
    if l2_reg > 0.:
        print("Regularizing the weights: " + str(l2_reg))

    # Build network

    # CONTRACTING PATH

    # Input layer
    inputs = Input(img_shape)
    sh = inputs._keras_shape
    padded = ZeroPadding2D(padding=(100, 100), dim_ordering=do,
                           name='pad100')(inputs)

    # Block 1
    conv1_1 = Convolution2D(64,
                            3,
                            3,
                            activation='relu',
                            border_mode='valid',
                            dim_ordering=do,
                            name='conv1_1',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(padded)
    conv1_2 = Convolution2D(64,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv1_2',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(conv1_1)
    pool1 = MaxPooling2D(pool_size=(2, 2),
                         strides=(2, 2),
                         dim_ordering=do,
                         name='pool1')(conv1_2)

    # Block 2
    conv2_1 = Convolution2D(128,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv2_1',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(pool1)
    conv2_2 = Convolution2D(128,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv2_2',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(conv2_1)
    pool2 = MaxPooling2D(pool_size=(2, 2),
                         strides=(2, 2),
                         dim_ordering=do,
                         name='pool2')(conv2_2)

    # Block 3
    conv3_1 = Convolution2D(256,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv3_1',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(pool2)
    conv3_2 = Convolution2D(256,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv3_2',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(conv3_1)
    conv3_3 = Convolution2D(256,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv3_3',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(conv3_2)
    pool3 = MaxPooling2D(pool_size=(2, 2),
                         strides=(2, 2),
                         dim_ordering=do,
                         name='pool3')(conv3_3)

    # Block 4
    conv4_1 = Convolution2D(512,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv4_1',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(pool3)
    conv4_2 = Convolution2D(512,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv4_2',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(conv4_1)
    conv4_3 = Convolution2D(512,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv4_3',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(conv4_2)
    pool4 = MaxPooling2D(pool_size=(2, 2),
                         strides=(2, 2),
                         dim_ordering=do,
                         name='pool4')(conv4_3)

    # Block 5
    conv5_1 = Convolution2D(512,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv5_1',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(pool4)
    conv5_2 = Convolution2D(512,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv5_2',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(conv5_1)
    conv5_3 = Convolution2D(512,
                            3,
                            3,
                            activation='relu',
                            border_mode='same',
                            dim_ordering=do,
                            name='conv5_3',
                            W_regularizer=l2(l2_reg),
                            trainable=True)(conv5_2)
    pool5 = MaxPooling2D(pool_size=(2, 2),
                         strides=(2, 2),
                         dim_ordering=do,
                         name='pool5')(conv5_3)

    # Block 6 (fully conv)
    fc6 = Convolution2D(4096,
                        7,
                        7,
                        activation='relu',
                        border_mode='valid',
                        dim_ordering=do,
                        name='fc6',
                        W_regularizer=l2(l2_reg),
                        trainable=True)(pool5)
    fc6 = Dropout(0.5)(fc6)

    # Block 7 (fully conv)
    fc7 = Convolution2D(4096,
                        1,
                        1,
                        activation='relu',
                        border_mode='valid',
                        dim_ordering=do,
                        name='fc7',
                        W_regularizer=l2(l2_reg),
                        trainable=True)(fc6)
    fc7 = Dropout(0.5)(fc7)

    score_fr = Convolution2D(nclasses,
                             1,
                             1,
                             activation='relu',
                             border_mode='valid',
                             dim_ordering=do,
                             name='score_fr')(fc7)

    # DECONTRACTING PATH
    # Unpool 1
    score_pool4 = Convolution2D(nclasses,
                                1,
                                1,
                                activation='relu',
                                border_mode='same',
                                dim_ordering=do,
                                name='score_pool4',
                                W_regularizer=l2(l2_reg),
                                trainable=True)(pool4)
    score2 = Deconvolution2D(nb_filter=nclasses,
                             nb_row=4,
                             nb_col=4,
                             input_shape=score_fr._keras_shape,
                             subsample=(2, 2),
                             border_mode='valid',
                             activation='linear',
                             W_regularizer=l2(l2_reg),
                             dim_ordering=do,
                             trainable=True,
                             name='score2')(score_fr)
    score_pool4_crop = CropLayer2D(score2,
                                   dim_ordering=do,
                                   name='score_pool4_crop')(score_pool4)
    score_fused = merge([score_pool4_crop, score2],
                        mode=custom_sum,
                        output_shape=custom_sum_shape,
                        name='score_fused')

    # Unpool 2
    score_pool3 = Convolution2D(nclasses,
                                1,
                                1,
                                activation='relu',
                                border_mode='valid',
                                dim_ordering=do,
                                W_regularizer=l2(l2_reg),
                                trainable=True,
                                name='score_pool3')(pool3)
    score4 = Deconvolution2D(nb_filter=nclasses,
                             nb_row=4,
                             nb_col=4,
                             input_shape=score_fused._keras_shape,
                             subsample=(2, 2),
                             border_mode='valid',
                             activation='linear',
                             W_regularizer=l2(l2_reg),
                             dim_ordering=do,
                             trainable=True,
                             name='score4',
                             bias=False)(score_fused)  # TODO: No bias??
    score_pool3_crop = CropLayer2D(score4,
                                   dim_ordering=do,
                                   name='score_pool3_crop')(score_pool3)
    score_final = merge([score_pool3_crop, score4],
                        mode=custom_sum,
                        output_shape=custom_sum_shape,
                        name='score_final')

    # Unpool 3
    upsample = Deconvolution2D(nb_filter=nclasses,
                               nb_row=16,
                               nb_col=16,
                               input_shape=score_final._keras_shape,
                               subsample=(8, 8),
                               border_mode='valid',
                               activation='linear',
                               W_regularizer=l2(l2_reg),
                               dim_ordering=do,
                               trainable=True,
                               name='upsample',
                               bias=False)(score_final)  # TODO: No bias??
    score = CropLayer2D(inputs, dim_ordering=do, name='score')(upsample)

    # Softmax
    if do == 'th':
        softmax_fcn8 = NdSoftmax(1)(score)
    else:
        softmax_fcn8 = NdSoftmax(3)(score)

    # Complete model
    net = Model(input=inputs, output=softmax_fcn8)

    # Load weights
    if weights_file:
        print(' > Loading weights from pretrained model: ' + weights_file)
        net.load_weights(weights_file)

    return net
示例#16
0
def build_dilation(img_shape=(3, None, None),
                   nclasses=11,
                   l2_reg=0.,
                   init='glorot_uniform',
                   path_weights=None,
                   load_pretrained=False,
                   freeze_layers_from=None,
                   vgg_weights=True):
    # Build network
    if K.image_dim_ordering() == 'tf':
        bn_axis = 3
    else:
        bn_axis = 1

    # CONTRACTING PATH

    # Input layer
    inputs = Input(img_shape)
    # padded = ZeroPadding2D(padding=(100, 100), name='pad100')(inputs)

    # Block1
    conv1_1 = Convolution2D(64,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='block1_conv1',
                            W_regularizer=l2(l2_reg))(inputs)
    conv1_2 = Convolution2D(64,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='block1_conv2',
                            W_regularizer=l2(l2_reg))(conv1_1)
    pool1 = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(conv1_2)

    # Block2
    conv2_1 = Convolution2D(128,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='block2_conv1',
                            W_regularizer=l2(l2_reg))(pool1)
    conv2_2 = Convolution2D(128,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='block2_conv2',
                            W_regularizer=l2(l2_reg))(conv2_1)
    pool2 = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(conv2_2)

    # Block3
    conv3_1 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='block3_conv1',
                            W_regularizer=l2(l2_reg))(pool2)
    conv3_2 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='block3_conv2',
                            W_regularizer=l2(l2_reg))(conv3_1)
    conv3_3 = Convolution2D(256,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='block3_conv3',
                            W_regularizer=l2(l2_reg))(conv3_2)
    pool3 = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(conv3_3)

    # Block4
    conv4_1 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='block4_conv1',
                            W_regularizer=l2(l2_reg))(pool3)
    conv4_2 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='block4_conv2',
                            W_regularizer=l2(l2_reg))(conv4_1)
    conv4_3 = Convolution2D(512,
                            3,
                            3,
                            init,
                            'relu',
                            border_mode='same',
                            name='block4_conv3',
                            W_regularizer=l2(l2_reg))(conv4_2)

    vgg_base_model = Model(input=inputs, output=conv4_3)

    vgg_base_in = vgg_base_model.output
    # Block5
    conv5_bn = BatchNormalization(axis=bn_axis, name='block5_bn')(vgg_base_in)

    conv5_1 = AtrousConvolution2D(512,
                                  3,
                                  3,
                                  atrous_rate=(2, 2),
                                  name='atrous_conv_5_1',
                                  border_mode='same',
                                  dim_ordering=dim_ordering,
                                  init=identity_init)(conv5_bn)

    conv5_1_relu = Activation('relu')(conv5_1)
    conv5_bn_2 = BatchNormalization(axis=bn_axis,
                                    name='block5_bn2')(conv5_1_relu)

    conv5_2 = AtrousConvolution2D(512,
                                  3,
                                  3,
                                  atrous_rate=(2, 2),
                                  name='atrous_conv_5_2',
                                  border_mode='same',
                                  dim_ordering=dim_ordering,
                                  init=identity_init)(conv5_bn_2)

    conv5_2_relu = Activation('relu')(conv5_2)
    conv5_bn3 = BatchNormalization(axis=bn_axis,
                                   name='block5_bn3')(conv5_2_relu)

    conv5_3 = AtrousConvolution2D(512,
                                  3,
                                  3,
                                  atrous_rate=(2, 2),
                                  name='atrous_conv_5_3',
                                  border_mode='same',
                                  dim_ordering=dim_ordering,
                                  init=identity_init)(conv5_bn3)

    conv5_3_relu = Activation('relu')(conv5_3)

    # Block6
    conv6_bn = BatchNormalization(axis=bn_axis, name='block6_bn')(conv5_3_relu)

    conv6 = AtrousConvolution2D(1024,
                                7,
                                7,
                                atrous_rate=(4, 4),
                                name='atrous_conv_6',
                                border_mode='same',
                                dim_ordering=dim_ordering,
                                init=identity_init)(conv6_bn)

    conv6_relu = Activation('relu')(conv6)
    conv6_relu = Dropout(0.5)(conv6_relu)

    # Block7
    conv7_bn = BatchNormalization(axis=bn_axis, name='block7_bn')(conv6_relu)

    conv7 = AtrousConvolution2D(4096,
                                1,
                                1,
                                atrous_rate=(1, 1),
                                name='atrous_conv_7',
                                border_mode='same',
                                dim_ordering=dim_ordering,
                                init=identity_init)(conv7_bn)

    conv7_relu = Activation('relu')(conv7)
    conv7_relu = Dropout(0.5)(conv7_relu)

    # Final block
    convf_bn = BatchNormalization(axis=bn_axis, name='blockf_bn')(conv7_relu)

    x = AtrousConvolution2D(nclasses,
                            1,
                            1,
                            atrous_rate=(1, 1),
                            name='final_block',
                            border_mode='same',
                            dim_ordering=dim_ordering,
                            init=identity_init)(convf_bn)

    # Appending context block
    upsampling = 8
    context_out = context_block(x, [1, 1, 2, 4, 8, 16, 1],
                                nclasses,
                                init=identity_init)
    deconv_out = Deconvolution2D(
        nclasses,
        upsampling,
        upsampling,
        init=bilinear_init,
        subsample=(upsampling, upsampling),
        input_shape=context_out._keras_shape)(context_out)
    # Softmax
    prob = NdSoftmax()(deconv_out)

    # Complete model
    model = Model(input=vgg_base_model.input, output=prob)

    # Load pretrained weights VGG part of the model
    if vgg_weights == True:
        if K.image_dim_ordering() == 'th':
            weights_path = get_file(
                'vgg19_weights_th_dim_ordering_th_kernels_notop.h5',
                TH_WEIGHTS_PATH_NO_TOP,
                cache_subdir='models')
        else:

            weights_path = get_file(
                'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5',
                TF_WEIGHTS_PATH_NO_TOP,
                cache_subdir='models')

        model.load_weights(weights_path, by_name=True)
        print('Loaded pre-trained VGG weights')

    if path_weights:
        print('Loaded pre-trained weights for the full network')
        model.load_weights(weights_path, by_name=True)
        #  load_matcovnet(model, path_weights, n_classes=nclasses)

    # Freeze some layers
    if freeze_layers_from is not None:
        freeze_layers(model, freeze_layers_from)

    return model
示例#17
0
def build_segnet_vgg(img_shape=(3, None, None),
                     nclasses=8,
                     l2_reg=0.,
                     init='glorot_uniform',
                     freeze_layers_from=None,
                     path_weights=None):

    # Regularization warning
    if l2_reg > 0.:
        print("Regularizing the weights: " + str(l2_reg))

    # Build network

    # CONTRACTING PATH

    # Input layer
    inputs = Input(img_shape)
    padded1 = ZeroPadding2D(padding=(1, 1), name='pad1')(inputs)

    # Block 1
    conv1_1 = segnet_conv2D(padded1,
                            64,
                            3,
                            3,
                            init,
                            block='1',
                            layer='1',
                            l2_reg=l2_reg)
    conv1_2 = segnet_conv2D(conv1_1,
                            64,
                            3,
                            3,
                            init,
                            block='1',
                            layer='2',
                            l2_reg=l2_reg)
    pool1 = MaxPooling2D((2, 2), (2, 2), name='pool1')(conv1_2)

    # Block 2
    padded2 = ZeroPadding2D(padding=(1, 1), name='pad2')(pool1)
    conv2_1 = segnet_conv2D(padded2,
                            128,
                            3,
                            3,
                            init,
                            block='2',
                            layer='1',
                            l2_reg=l2_reg)
    conv2_2 = segnet_conv2D(conv2_1,
                            128,
                            3,
                            3,
                            init,
                            block='2',
                            layer='2',
                            l2_reg=l2_reg)
    pool2 = MaxPooling2D((2, 2), (2, 2), name='pool2')(conv2_2)

    # Block 3
    padded3 = ZeroPadding2D(padding=(1, 1), name='pad3')(pool2)
    conv3_1 = segnet_conv2D(padded3,
                            256,
                            3,
                            3,
                            init,
                            block='3',
                            layer='1',
                            l2_reg=l2_reg)
    conv3_2 = segnet_conv2D(conv3_1,
                            256,
                            3,
                            3,
                            init,
                            block='3',
                            layer='2',
                            l2_reg=l2_reg)
    conv3_3 = segnet_conv2D(conv3_2,
                            256,
                            3,
                            3,
                            init,
                            block='3',
                            layer='3',
                            l2_reg=l2_reg)
    pool3 = MaxPooling2D((2, 2), (2, 2), name='pool3')(conv3_3)

    # Block 4
    padded4 = ZeroPadding2D(padding=(1, 1), name='pad4')(pool3)
    conv4_1 = segnet_conv2D(padded4,
                            512,
                            3,
                            3,
                            init,
                            block='4',
                            layer='1',
                            l2_reg=l2_reg)
    conv4_2 = segnet_conv2D(conv4_1,
                            512,
                            3,
                            3,
                            init,
                            block='4',
                            layer='2',
                            l2_reg=l2_reg)
    conv4_3 = segnet_conv2D(conv4_2,
                            512,
                            3,
                            3,
                            init,
                            block='4',
                            layer='3',
                            l2_reg=l2_reg)
    pool4 = MaxPooling2D((2, 2), (2, 2), name='pool4')(conv4_3)

    # Block 5
    padded5 = ZeroPadding2D(padding=(1, 1), name='pad5')(pool4)
    conv5_1 = segnet_conv2D(padded5,
                            512,
                            3,
                            3,
                            init,
                            block='5',
                            layer='1',
                            l2_reg=l2_reg)
    conv5_2 = segnet_conv2D(conv5_1,
                            512,
                            3,
                            3,
                            init,
                            block='5',
                            layer='2',
                            l2_reg=l2_reg)
    conv5_3 = segnet_conv2D(conv5_2,
                            512,
                            3,
                            3,
                            init,
                            block='5',
                            layer='3',
                            l2_reg=l2_reg)
    pool5 = MaxPooling2D((2, 2), (2, 2), name='pool5')(conv5_3)

    # ##### decoding layers

    # Block 6: Unpooling block 5
    unpool5 = DePool2D(pool2d_layer=pool5, size=(2, 2),
                       name='unpool_block5')(pool5)
    conv6_1 = segnet_conv2D(unpool5,
                            512,
                            3,
                            3,
                            init,
                            block='6',
                            layer='1',
                            l2_reg=l2_reg)
    conv6_2 = segnet_conv2D(conv6_1,
                            512,
                            3,
                            3,
                            init,
                            block='6',
                            layer='2',
                            l2_reg=l2_reg)
    conv6_3 = segnet_conv2D(conv6_2,
                            512,
                            3,
                            3,
                            init,
                            block='6',
                            layer='3',
                            l2_reg=l2_reg)

    # Block 7: Unpooling block 4
    unpool4 = DePool2D(pool2d_layer=pool4, size=(2, 2),
                       name='unpool_block4')(conv6_3)
    conv7_1 = segnet_conv2D(unpool4,
                            512,
                            3,
                            3,
                            init,
                            block='7',
                            layer='1',
                            l2_reg=l2_reg)
    conv7_2 = segnet_conv2D(conv7_1,
                            512,
                            3,
                            3,
                            init,
                            block='7',
                            layer='2',
                            l2_reg=l2_reg)
    conv7_3 = segnet_conv2D(conv7_2,
                            512,
                            3,
                            3,
                            init,
                            block='7',
                            layer='3',
                            l2_reg=l2_reg)

    # Block 8: Unpooling block 3
    unpool3 = DePool2D(pool2d_layer=pool3, size=(2, 2),
                       name='unpool_block3')(conv7_3)
    conv8_1 = segnet_conv2D(unpool3,
                            256,
                            3,
                            3,
                            init,
                            block='8',
                            layer='1',
                            l2_reg=l2_reg)
    conv8_2 = segnet_conv2D(conv8_1,
                            256,
                            3,
                            3,
                            init,
                            block='8',
                            layer='2',
                            l2_reg=l2_reg)
    conv8_3 = segnet_conv2D(conv8_2,
                            256,
                            3,
                            3,
                            init,
                            block='8',
                            layer='3',
                            l2_reg=l2_reg)

    # Block 9: Unpooling block 2
    unpool2 = DePool2D(pool2d_layer=pool2, size=(2, 2),
                       name='unpool_block2')(conv8_3)
    conv9_1 = segnet_conv2D(unpool2,
                            128,
                            3,
                            3,
                            init,
                            block='9',
                            layer='1',
                            l2_reg=l2_reg)
    conv9_2 = segnet_conv2D(conv9_1,
                            128,
                            3,
                            3,
                            init,
                            block='9',
                            layer='2',
                            l2_reg=l2_reg)

    # Block 10: Unpooling block 1
    unpool1 = DePool2D(pool2d_layer=pool1, size=(2, 2),
                       name='unpool_block1')(conv9_2)
    conv10_1 = segnet_conv2D(unpool1,
                             64,
                             3,
                             3,
                             init,
                             block='10',
                             layer='1',
                             l2_reg=l2_reg)
    conv10_2 = segnet_conv2D(conv10_1,
                             64,
                             3,
                             3,
                             init,
                             block='10',
                             layer='2',
                             l2_reg=l2_reg)

    l1 = Convolution2D(nclasses, 1, 1, border_mode='valid')(conv10_2)
    score = CropLayer2D(inputs, name='score')(l1)
    # Softmax
    softmax_segnet = NdSoftmax()(score)

    # Complete model
    model = Model(input=inputs, output=softmax_segnet)

    # Load pretrained Model
    if path_weights:
        load_matcovnet(model, n_classes=nclasses)

    # Freeze some layers
    if freeze_layers_from is not None:
        freeze_layers(model, freeze_layers_from)

    return model
示例#18
0
def tiramisu_network(img_shape, n_layers_block, growth_rate,
                     nclasses, weight_decay, compression, dropout, network_name,
                     freeze_layers_from=None, nb_filter=48):
    """
    Creates a Keras model that represents the Tiramisu network specefied according to the number of layers per dense 
    block, the index where the transition from downsampling to upsampling occurs, the growth rate, and other parameters
    related to DenseNet and DNN models in general.
    
    :param img_shape: shape of the input image (e.g. (3, 300, 300) for th backend, (300, 300, 3) for tf)
    :param n_layers_block: list that specifies the number of layers per each dense block, including the dense blocks
    from the downsampling path, the upsampling path and the transition dense block. The length of this list must be
    an even number.
    :param growth_rate: Growth rate (for more details see DenseNet).
    :param nclasses: Number of classes of the segmentation, including background
    :param weight_decay: Amount of L2 norm penalization applied to the weights
    :param compression: Compression factor for DenseNet
    :param dropout: Dropout rate for conv layers.
    :param network_name: Name of the network
    :param freeze_layers_from: The first layers that won't be updated 
    :param nb_filter: Number of kernels in the first convolution
    :return: Keras model and network dictionary with all the feature maps
    :rtype: tuple
    """
    # Placeholder for the feature maps in each dense block, transition down or transition up
    net = dict()

    # Placeholder for skip connections
    skip_connection = list()

    # Number of layers per block must be odd
    assert (len(n_layers_block) - 1) % 2 == 0

    # Transition index
    transition_index = int(np.floor(len(n_layers_block) / 2))

    # Layers per block for the 3 main structures: downsampling path, transition and upsampling path
    down_layers_block = n_layers_block[:transition_index]
    transition_layers_block = n_layers_block[transition_index]
    up_layers_block = n_layers_block[transition_index + 1:]

    assert len(down_layers_block) == len(up_layers_block)

    # Ensure input shape can be handled by the network architecture
    if dim_ordering == 'th':
        input_rows = img_shape[1]
        input_cols = img_shape[2]
    else:
        input_rows = img_shape[0]
        input_cols = img_shape[1]
    num_transitions = len(down_layers_block)
    multiple = 2 ** num_transitions
    if input_rows is not None:
        if input_rows % multiple != 0:
            raise ValueError('The number of rows of the input data must be a multiple of {}'.format(multiple))
    if input_cols is not None:
        if input_cols % multiple != 0:
            raise ValueError('The number of columns of the input data must be a multiple of {}'.format(multiple))

    # Initial convolution
    net['input'] = Input(shape=img_shape)
    x = Convolution2D(nb_filter, 3, 3,
                      init="he_uniform",
                      border_mode="same",
                      name="initial_conv2D",
                      W_regularizer=l2(weight_decay))(net['input'])
    net['init_conv'] = x

    # Dense blocks + Transition down in the downsampling path
    for block_idx, n_layers_block in enumerate(down_layers_block):
        # Dense block
        x, nb_filter = denseblock(x, nb_layers=n_layers_block,
                                  nb_filter=nb_filter, growth_rate=growth_rate,
                                  dropout_rate=dropout,
                                  weight_decay=weight_decay,
                                  stack_input=True,
                                  block_id='down_db{}'.format(block_idx))
        feature_name = 'db_{}'.format(block_idx)
        net[feature_name] = x
        skip_connection.append(x)

        # Compression
        nb_filter = int(compression * nb_filter)

        # Transition Down
        x = transition_down(x, nb_filter,
                            dropout_rate=dropout,
                            weight_decay=weight_decay,
                            td_id='down_td{}'.format(block_idx))
        feature_name = 'td_{}'.format(block_idx)
        net[feature_name] = x

    # Reverse skip connection list
    skip_connection = skip_connection[::-1]

    # The last denseblock does not have a transition down and does not stack the input
    x, nb_filter = denseblock(x, nb_layers=transition_layers_block,
                              nb_filter=nb_filter, growth_rate=growth_rate,
                              dropout_rate=dropout,
                              weight_decay=weight_decay,
                              stack_input=True,
                              block_id='transition')
    feature_name = 'db_{}'.format(transition_index)
    net[feature_name] = x

    # Upsampling path
    keep_filters = growth_rate * transition_layers_block  # Number of filters for the first transposed convolution
    for block_idx, n_layers_block in enumerate(up_layers_block):
        # Skip connection related to this block
        skip = skip_connection[block_idx]
        x_up = transition_up(x, skip, keep_filters,
                             weight_decay=weight_decay,
                             tu_id='up_tu{}'.format(block_idx))
        feature_name = 'tu_{}'.format(block_idx)
        net[feature_name] = x_up

        # Update keep_filters for next upsampling block
        keep_filters = growth_rate * n_layers_block

        # Dense block
        x, _ = denseblock(x_up, n_layers_block,
                          nb_filter=0, growth_rate=growth_rate,
                          dropout_rate=dropout,
                          weight_decay=weight_decay,
                          stack_input=True,
                          block_id='up_db{}'.format(block_idx))
        feature_name = 'up_db_{}'.format(block_idx)
        net[feature_name] = x

    # Stack last transition up and denseblock features and compute the class scores for each pixel using a 1x1 conv
    net['output_features'] = x
    net['pixel_score'] = Convolution2D(nclasses, 1, 1,
                                       init='he_uniform',
                                       border_mode='same',
                                       W_regularizer=l2(weight_decay),
                                       b_regularizer=l2(weight_decay),
                                       name='class_score')(net['output_features'])
    # Softmax
    net['softmax'] = NdSoftmax(name='softmax')(net['pixel_score'])

    # Model
    tiramisu_model = Model(input=[net['input']], output=[net['softmax']], name=network_name)

    # Freeze some layers
    if freeze_layers_from is not None:
        if freeze_layers_from == 'base_model':
            raise ValueError('Freezing the base_model is not supported for network {}'.format(network_name))
        else:
            for i, layer in enumerate(model.layers):
                print(i, layer.name)
            print('   Freezing from layer 0 to ' + str(freeze_layers_from))
            for layer in model.layers[:freeze_layers_from]:
                layer.trainable = False
            for layer in model.layers[freeze_layers_from:]:
                layer.trainable = True

    return tiramisu_model, net
示例#19
0
def build_segnet(img_shape=(None, None, 3),
                 nclasses=8,
                 weight_decay=0.,
                 freeze_layers_from=None,
                 path_weights=None,
                 basic=False):

    # Regularization warning
    if weight_decay > 0.:
        print("Regularizing the weights: " + str(weight_decay))

    # Set axis in which to do batch normalization
    if K.image_dim_ordering() == 'tf':
        bn_axis = 3
    else:
        bn_axis = 1

    # Build network

    # Input layer
    input_tensor = Input(img_shape)

    # Pad image to avoid size problems with pooling-unpooling
    padded = ZeroPadding2D(padding=(100, 100), name='pad100')(input_tensor)

    if not basic:

        # CONTRACTING PATH
        x = conv_block(padded,
                       64,
                       3,
                       weight_decay,
                       bn_axis,
                       block='1',
                       num='1')
        x = conv_block(x, 64, 3, weight_decay, bn_axis, block='1', num='2')
        pool1 = MaxPooling2D(pool_size=(2, 2),
                             strides=(2, 2),
                             name='block1_pool1')(x)

        x = conv_block(pool1,
                       128,
                       3,
                       weight_decay,
                       bn_axis,
                       block='2',
                       num='1')
        x = conv_block(x, 128, 3, weight_decay, bn_axis, block='2', num='2')
        pool2 = MaxPooling2D(pool_size=(2, 2),
                             strides=(2, 2),
                             name='block2_pool1')(x)

        x = conv_block(pool2,
                       256,
                       3,
                       weight_decay,
                       bn_axis,
                       block='3',
                       num='1')
        x = conv_block(x, 256, 3, weight_decay, bn_axis, block='3', num='2')
        x = conv_block(x, 256, 3, weight_decay, bn_axis, block='3', num='3')
        pool3 = MaxPooling2D(pool_size=(2, 2),
                             strides=(2, 2),
                             name='block3_pool1')(x)

        x = conv_block(pool3,
                       512,
                       3,
                       weight_decay,
                       bn_axis,
                       block='4',
                       num='1')
        x = conv_block(x, 512, 3, weight_decay, bn_axis, block='4', num='2')
        x = conv_block(x, 512, 3, weight_decay, bn_axis, block='4', num='3')
        pool4 = MaxPooling2D(pool_size=(2, 2),
                             strides=(2, 2),
                             name='block4_pool1')(x)

        x = conv_block(pool4,
                       512,
                       3,
                       weight_decay,
                       bn_axis,
                       block='5',
                       num='1')
        x = conv_block(x, 512, 3, weight_decay, bn_axis, block='5', num='2')
        x = conv_block(x, 512, 3, weight_decay, bn_axis, block='5', num='3')
        pool5 = MaxPooling2D(pool_size=(2, 2),
                             strides=(2, 2),
                             name='block5_pool1')(x)

        # DECONTRACTING PATH

        x = DePool2D(pool2d_layer=pool5, size=(2, 2),
                     name='block6_unpool1')(pool5)
        x = conv_block(x, 512, 3, weight_decay, bn_axis, block='6', num='1')
        x = conv_block(x, 512, 3, weight_decay, bn_axis, block='6', num='2')
        x = conv_block(x, 512, 3, weight_decay, bn_axis, block='6', num='3')

        x = DePool2D(pool2d_layer=pool4, size=(2, 2), name='block7_unpool1')(x)
        x = conv_block(x, 512, 3, weight_decay, bn_axis, block='7', num='1')
        x = conv_block(x, 512, 3, weight_decay, bn_axis, block='7', num='2')
        x = conv_block(x, 512, 3, weight_decay, bn_axis, block='7', num='3')

        x = DePool2D(pool2d_layer=pool3, size=(2, 2), name='block8_unpool1')(x)
        x = conv_block(x, 256, 3, weight_decay, bn_axis, block='8', num='1')
        x = conv_block(x, 256, 3, weight_decay, bn_axis, block='8', num='2')
        x = conv_block(x, 256, 3, weight_decay, bn_axis, block='8', num='3')

        x = DePool2D(pool2d_layer=pool2, size=(2, 2), name='block9_unpool1')(x)
        x = conv_block(x, 128, 3, weight_decay, bn_axis, block='9', num='1')
        x = conv_block(x, 128, 3, weight_decay, bn_axis, block='9', num='2')

        x = DePool2D(pool2d_layer=pool1, size=(2, 2),
                     name='block10_unpool1')(x)
        x = conv_block(x, 64, 3, weight_decay, bn_axis, block='10', num='1')
        x = conv_block(x,
                       nclasses,
                       3,
                       weight_decay,
                       bn_axis,
                       block='10',
                       num='2')

    elif basic:

        # CONTRACTING PATH
        x = conv_block(padded,
                       64,
                       7,
                       weight_decay,
                       bn_axis,
                       block='1',
                       num='1')
        x = conv_block(x, 64, 7, weight_decay, bn_axis, block='1', num='2')
        pool1 = MaxPooling2D(pool_size=(2, 2),
                             strides=(2, 2),
                             name='block1_pool1')(x)

        x = conv_block(pool1,
                       128,
                       7,
                       weight_decay,
                       bn_axis,
                       block='2',
                       num='1')
        x = conv_block(x, 128, 7, weight_decay, bn_axis, block='2', num='2')
        pool2 = MaxPooling2D(pool_size=(2, 2),
                             strides=(2, 2),
                             name='block2_pool1')(x)

        x = conv_block(pool2,
                       256,
                       7,
                       weight_decay,
                       bn_axis,
                       block='3',
                       num='1')
        x = conv_block(x, 256, 7, weight_decay, bn_axis, block='3', num='2')
        x = conv_block(x, 256, 7, weight_decay, bn_axis, block='3', num='3')
        pool3 = MaxPooling2D(pool_size=(2, 2),
                             strides=(2, 2),
                             name='block3_pool1')(x)

        x = conv_block(pool3,
                       512,
                       7,
                       weight_decay,
                       bn_axis,
                       block='4',
                       num='1')
        x = conv_block(x, 512, 7, weight_decay, bn_axis, block='4', num='2')
        x = conv_block(x, 512, 7, weight_decay, bn_axis, block='4', num='3')
        pool4 = MaxPooling2D(pool_size=(2, 2),
                             strides=(2, 2),
                             name='block4_pool1')(x)

        # DECONTRACTING PATH

        x = DePool2D(pool2d_layer=pool4, size=(2, 2),
                     name='block7_unpool1')(pool4)
        x = conv_block(x,
                       512,
                       7,
                       weight_decay,
                       bn_axis,
                       block='7',
                       num='1',
                       deconv_basic=True)
        x = conv_block(x,
                       512,
                       7,
                       weight_decay,
                       bn_axis,
                       block='7',
                       num='2',
                       deconv_basic=True)
        x = conv_block(x,
                       512,
                       7,
                       weight_decay,
                       bn_axis,
                       block='7',
                       num='3',
                       deconv_basic=True)

        x = DePool2D(pool2d_layer=pool3, size=(2, 2), name='block8_unpool1')(x)
        x = conv_block(x,
                       256,
                       7,
                       weight_decay,
                       bn_axis,
                       block='8',
                       num='1',
                       deconv_basic=True)
        x = conv_block(x,
                       256,
                       7,
                       weight_decay,
                       bn_axis,
                       block='8',
                       num='2',
                       deconv_basic=True)
        x = conv_block(x,
                       256,
                       7,
                       weight_decay,
                       bn_axis,
                       block='8',
                       num='3',
                       deconv_basic=True)

        x = DePool2D(pool2d_layer=pool2, size=(2, 2), name='block9_unpool1')(x)
        x = conv_block(x,
                       128,
                       7,
                       weight_decay,
                       bn_axis,
                       block='9',
                       num='1',
                       deconv_basic=True)
        x = conv_block(x,
                       128,
                       7,
                       weight_decay,
                       bn_axis,
                       block='9',
                       num='2',
                       deconv_basic=True)

        x = DePool2D(pool2d_layer=pool1, size=(2, 2),
                     name='block10_unpool1')(x)
        x = conv_block(x,
                       64,
                       7,
                       weight_decay,
                       bn_axis,
                       block='10',
                       num='1',
                       deconv_basic=True)
        x = conv_block(x,
                       nclasses,
                       7,
                       weight_decay,
                       bn_axis,
                       block='10',
                       num='2',
                       deconv_basic=True)

    # Recover the image's original size
    x = CropLayer2D(input_tensor, name='score')(x)

    # Softmax
    softmax_segnet = NdSoftmax()(x)

    # Complete model
    model = Model(input=input_tensor, output=softmax_segnet)

    #TODO: load weights from caffe
    # Load pretrained Model
    #if path_weights:
    #    load_matcovnet(model, path_weights, n_classes=nclasses)

    #TODO: review freeze layers
    # Freeze some layers
    if freeze_layers_from is not None:
        freeze_layers(model, freeze_layers_from)

    return model
示例#20
0
def DeeplabV2(input_shape, upsampling=8, apply_softmax=True,
              weights='voc2012', input_tensor=None,
              classes=21, weight_decay=0.0005):
    """Instantiate the DeeplabV2 architecture with VGG16 encoder,
    optionally loading weights pre-trained on VOC2012 segmentation.
    Note that pre-trained model is only available for Theano dim ordering.
    The model and the weights should be compatible with both
    TensorFlow and Theano backends.
    # Arguments
        input_shape: shape tuple. It should have exactly 3 inputs channels,
            and the axis ordering should be coherent with what specified in
            your keras.json (e.g. use (3, 512, 512) for 'th' and (512, 512, 3)
            for 'tf').
        upsampling: final front end upsampling (default is 8x).
        apply_softmax: whether to apply softmax or return logits.
        weights: one of `None` (random initialization)
            or `voc2012` (pre-training on VOC2012 segmentation).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        classes: number of classes to classify images into
    # Returns
        A Keras model instance.
    """

    if weights not in {'voc2012', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `voc2012` '
                         '(pre-training on VOC2012 segmentation).')

    if weights == 'voc2012' and classes != 21:
        raise ValueError('If using `weights` as voc2012 `classes` should be 21')

    if input_shape is None:
        raise ValueError('Please provide a valid input_shape to deeplab')

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    # Block 1
    h = ZeroPadding2D(padding=(1, 1))(img_input)
    h = Convolution2D(64, 3, 3, activation='relu', W_regularizer=l2(weight_decay), name='conv1_1')(h)
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = Convolution2D(64, 3, 3, activation='relu', W_regularizer=l2(weight_decay), name='conv1_2')(h)
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(h)

    # Block 2
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = Convolution2D(128, 3, 3, activation='relu', W_regularizer=l2(weight_decay), name='conv2_1')(h)
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = Convolution2D(128, 3, 3, activation='relu', W_regularizer=l2(weight_decay), name='conv2_2')(h)
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(h)

    # Block 3
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = Convolution2D(256, 3, 3, activation='relu', W_regularizer=l2(weight_decay), name='conv3_1')(h)
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = Convolution2D(256, 3, 3, activation='relu', W_regularizer=l2(weight_decay), name='conv3_2')(h)
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = Convolution2D(256, 3, 3, activation='relu', W_regularizer=l2(weight_decay), name='conv3_3')(h)
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(h)

    # Block 4
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = Convolution2D(512, 3, 3, activation='relu', W_regularizer=l2(weight_decay), name='conv4_1')(h)
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = Convolution2D(512, 3, 3, activation='relu', W_regularizer=l2(weight_decay), name='conv4_2')(h)
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = Convolution2D(512, 3, 3, activation='relu', W_regularizer=l2(weight_decay), name='conv4_3')(h)
    h = ZeroPadding2D(padding=(1, 1))(h)
    h = MaxPooling2D(pool_size=(3, 3), strides=(1, 1))(h)

    # Block 5
    h = ZeroPadding2D(padding=(2, 2))(h)
    h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_1')(h)
    h = ZeroPadding2D(padding=(2, 2))(h)
    h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_2')(h)
    h = ZeroPadding2D(padding=(2, 2))(h)
    h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_3')(h)
    h = ZeroPadding2D(padding=(1, 1))(h)
    p5 = MaxPooling2D(pool_size=(3, 3), strides=(1, 1))(h)
    #TODO: possible p5a = AveragePooling kernel_size=3, stride = 1 pad = 1
    # branching for Atrous Spatial Pyramid Pooling
    # hole = 6
    b1 = ZeroPadding2D(padding=(6, 6))(p5)
    b1 = AtrousConvolution2D(1024, 3, 3, atrous_rate=(6, 6), activation='relu', name='fc6_1')(b1)
    b1 = Dropout(0.5)(b1)
    b1 = Convolution2D(1024, 1, 1, activation='relu', name='fc7_1')(b1)
    b1 = Dropout(0.5)(b1)
    b1 = Convolution2D(classes, 1, 1, activation='relu', name='fc8_voc12_1')(b1)

    # hole = 12
    b2 = ZeroPadding2D(padding=(12, 12))(p5)
    b2 = AtrousConvolution2D(1024, 3, 3, atrous_rate=(12, 12), activation='relu', name='fc6_2')(b2)
    b2 = Dropout(0.5)(b2)
    b2 = Convolution2D(1024, 1, 1, activation='relu', name='fc7_2')(b2)
    b2 = Dropout(0.5)(b2)
    b2 = Convolution2D(classes, 1, 1, activation='relu', name='fc8_voc12_2')(b2)

    # hole = 18
    b3 = ZeroPadding2D(padding=(18, 18))(p5)
    b3 = AtrousConvolution2D(1024, 3, 3, atrous_rate=(18, 18), activation='relu', name='fc6_3')(b3)
    b3 = Dropout(0.5)(b3)
    b3 = Convolution2D(1024, 1, 1, activation='relu', name='fc7_3')(b3)
    b3 = Dropout(0.5)(b3)
    b3 = Convolution2D(classes, 1, 1, activation='relu', name='fc8_voc12_3')(b3)

    # hole = 24
    b4 = ZeroPadding2D(padding=(24, 24))(p5)
    b4 = AtrousConvolution2D(1024, 3, 3, atrous_rate=(24, 24), activation='relu', name='fc6_4')(b4)
    b4 = Dropout(0.5)(b4)
    b4 = Convolution2D(1024, 1, 1, activation='relu', name='fc7_4')(b4)
    b4 = Dropout(0.5)(b4)
    b4 = Convolution2D(classes, 1, 1, activation='relu', name='fc8_voc12_4')(b4)

    s = merge([b1, b2, b3, b4], mode='sum')
    #logits = upsample_tf(factor=upsampling, input_img=s)
    #logits = bilinear2D(ratio=upsampling)(s)

    #upsampling=tf.div(s.get_shape(),(tf.TensorShape(input_shape)))
    #logits = UpSampling2D(size=(upsampling, upsampling))(s)

    logits = Deconvolution2D(classes, upsampling, upsampling, init=bilinear_init, subsample=(upsampling, upsampling), input_shape=s._keras_shape)(s)
    if apply_softmax:
        out = NdSoftmax()(logits)
    else:
        out = logits

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input

    #out = CRFLayer(classes)(logits)

    # Create model.
    model = Model(inputs, out, name='deeplabV2')

    # load weights
    if weights == 'voc2012':
        if K.image_dim_ordering() == 'th':
            weights_path = get_file('deeplabV2_weights_th.h5',
                                    TH_WEIGHTS_PATH,
                                    cache_subdir='models')

            model.load_weights(weights_path)

            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image dimension ordering convention '
                              '(`image_dim_ordering="th"`). '
                              'For best performance, set '
                              '`image_dim_ordering="tf"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
                convert_all_kernels_in_model(model)
        else:
            raise NotImplementedError('Pretrained DeeplabV2 model is not available for'
                                      'voc2012 dataset and tensorflow dim ordering')

    return model