Esempio n. 1
0
def conv_block(input_tensor,
               kernel_size,
               filters,
               stage,
               block,
               strides=(2, 2)):
    '''conv_block is the block that has a conv layer at shortcut
    # Arguments
        input_tensor: input tensor
        kernel_size: defualt 3, the kernel size of middle conv layer at main path
        filters: list of integers, the nb_filters of 3 conv layer at main path
        stage: integer, current stage label, used for generating layer names
        block: 'a','b'..., current block label, used for generating layer names
    Note that from stage 3, the first conv layer at main path is with subsample=(2,2)
    And the shortcut should have subsample=(2,2) as well
    '''
    eps = 1.1e-5
    nb_filter1, nb_filter2, nb_filter3 = filters
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    scale_name_base = 'scale' + str(stage) + block + '_branch'

    x = Convolution2D(nb_filter1, (1, 1),
                      strides=strides,
                      name=conv_name_base + '2a',
                      use_bias=False)(input_tensor)
    x = BatchNormalization(epsilon=eps, axis=3, name=bn_name_base + '2a')(x)
    x = Scale(axis=3, name=scale_name_base + '2a')(x)
    x = Activation('relu', name=conv_name_base + '2a_relu')(x)

    x = ZeroPadding2D((1, 1), name=conv_name_base + '2b_zeropadding')(x)
    x = Convolution2D(nb_filter2, (kernel_size, kernel_size),
                      name=conv_name_base + '2b',
                      use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=3, name=bn_name_base + '2b')(x)
    x = Scale(axis=3, name=scale_name_base + '2b')(x)
    x = Activation('relu', name=conv_name_base + '2b_relu')(x)

    x = Convolution2D(nb_filter3, (1, 1),
                      name=conv_name_base + '2c',
                      use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=3, name=bn_name_base + '2c')(x)
    x = Scale(axis=3, name=scale_name_base + '2c')(x)

    shortcut = Convolution2D(nb_filter3, (1, 1),
                             strides=strides,
                             name=conv_name_base + '1',
                             use_bias=False)(input_tensor)
    shortcut = BatchNormalization(epsilon=eps, axis=3,
                                  name=bn_name_base + '1')(shortcut)
    shortcut = Scale(axis=3, name=scale_name_base + '1')(shortcut)

    x = add([x, shortcut], name='res' + str(stage) + block)
    x = Activation('relu', name='res' + str(stage) + block + '_relu')(x)
    return x
Esempio n. 2
0
def conv_block(x,
               stage,
               branch,
               nb_filter,
               dropout_rate=None,
               weight_decay=1e-4):
    '''Apply BatchNorm, Relu, bottleneck 1x1 Conv2D, 3x3 Conv2D, and option dropout
        # Arguments
            x: input tensor 
            stage: index for dense block
            branch: layer index within each dense block
            nb_filter: number of filters
            dropout_rate: dropout rate
            weight_decay: weight decay factor
    '''
    eps = 1.1e-5
    conv_name_base = 'conv' + str(stage) + '_' + str(branch)
    relu_name_base = 'relu' + str(stage) + '_' + str(branch)

    ## 1x1x1 Convolution (Bottleneck layer)
    ## Note: I'm not really sure what this 4 is. This isn't the number of layers (see nb_layers above)
    inter_channel = nb_filter * 4
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name=conv_name_base + '_x1_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_x1_scale')(x)
    x = Activation('relu', name=relu_name_base + '_x1')(x)
    x = Convolution3D(inter_channel,
                      1,
                      1,
                      1,
                      name=conv_name_base + '_x1',
                      bias=False)(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    ## 3x3x3 Convolution
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name=conv_name_base + '_x2_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_x2_scale')(x)
    x = Activation('relu', name=relu_name_base + '_x2')(x)
    x = ZeroPadding3D((1, 1, 1), name=conv_name_base + '_x2_zeropadding')(x)
    x = Convolution3D(nb_filter,
                      3,
                      3,
                      3,
                      name=conv_name_base + '_x2',
                      bias=False)(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    return x
def conv_block(x, stage, branch, nb_filter, dropout_rate=None):
    """Apply BatchNorm, Relu, bottleneck 1x1 Conv2D, 3x3 Conv2D, and option dropout
        # Arguments
            x: input tensor
            stage: index for dense block
            branch: layer index within each dense block
            nb_filter: number of filters
            dropout_rate: dropout rate
            weight_decay: weight decay factor
    """
    eps = 1.1e-5
    conv_name_base = 'convolution_' + str(stage) + '_' + str(branch)
    relu_name_base = 'ReLU_' + str(stage) + '_' + str(branch)

    # 1x1 Convolution (Bottleneck layer)
    inter_channel = nb_filter * 4
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           momentum=1,
                           name=conv_name_base + '_x1_batch_normalization',
                           trainable=False)(x, training=False)
    x = Scale(axis=concat_axis,
              name=conv_name_base + '_x1_scale',
              trainable=False)(x)
    x = Activation('relu', name=relu_name_base + '_x1')(x)
    x = Conv2D(inter_channel, (1, 1),
               name=conv_name_base + '_x1',
               use_bias=False,
               trainable=False)(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    # 3x3 Convolution
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           momentum=1,
                           name=conv_name_base + '_x2_batch_normalization',
                           trainable=False)(x, training=False)
    x = Scale(axis=concat_axis,
              name=conv_name_base + '_x2_scale',
              trainable=False)(x)
    x = Activation('relu', name=relu_name_base + '_x2')(x)
    x = ZeroPadding2D((1, 1), name=conv_name_base + '_x2_zero_padding')(x)
    x = Conv2D(nb_filter, (3, 3),
               name=conv_name_base + '_x2',
               use_bias=False,
               trainable=False)(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    return x
Esempio n. 4
0
def conv_block(x,
               stage,
               branch,
               nb_filter,
               dropout_rate=None,
               weight_decay=1e-4):
    '''Apply BatchNorm, Relu, bottleneck 1x1 Conv2D, 3x3 Conv2D, and option dropout
        # Arguments
            x: input tensor 
            stage: index for dense block
            branch: layer index within each dense block
            nb_filter: number of filters
            dropout_rate: dropout rate
            weight_decay: weight decay factor
    '''
    eps = 1.1e-5
    conv_name_base = 'conv' + str(stage) + '_' + str(branch)
    relu_name_base = 'relu' + str(stage) + '_' + str(branch)

    # 1x1 Convolution (Bottleneck layer)
    inter_channel = nb_filter * 4
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name=conv_name_base + '_x1_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_x1_scale')(x)
    x = Conv2D(inter_channel, (1, 1),
               activation='relu',
               padding='same',
               name=conv_name_base + '_x1',
               use_bias=False)(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    # 3x3 Convolution
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name=conv_name_base + '_x2_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_x2_scale')(x)
    x = Conv2D(nb_filter, (3, 3),
               activation='relu',
               padding='same',
               name=conv_name_base + '_x2',
               use_bias=False)(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    return x
Esempio n. 5
0
def transition_block(x,
                     stage,
                     nb_filter,
                     compression=1.0,
                     dropout_rate=None,
                     weight_decay=1E-4):
    eps = 1.1e-5
    conv_name_base = 'conv' + str(stage) + '_blk'
    relu_name_base = 'relu' + str(stage) + '_blk'
    pool_name_base = 'pool' + str(stage)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name=conv_name_base + '_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_scale')(x)
    x = Activation('relu', name=relu_name_base)(x)
    x = Conv2D(int(nb_filter * compression),
               1,
               1,
               name=conv_name_base,
               bias=False)(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    x = AveragePooling2D((2, 2), strides=(2, 2), name=pool_name_base)(x)

    return x
def transition_block(x, stage, nb_filter, compression=1.0, dropout_rate=None, weight_decay=1E-4):
    ''' Apply BatchNorm, 1x1 Convolution, averagePooling, optional compression, dropout 
        # Arguments
            x: input tensor
            stage: index for dense block
            nb_filter: number of filters
            compression: calculated as 1 - reduction. Reduces the number of feature maps in the transition block.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
    '''

    eps = 1.1e-5
    conv_name_base = 'conv' + str(stage) + '_blk'
    relu_name_base = 'relu' + str(stage) + '_blk'
    pool_name_base = 'pool' + str(stage) 

    x = BatchNormalization(epsilon=eps, axis=concat_axis, name=conv_name_base+'_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base+'_scale')(x)
    x = Activation('relu', name=relu_name_base)(x)
    x = Conv2D(int(nb_filter * compression), (1, 1), name=conv_name_base, use_bias=False)(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    x = AveragePooling2D((2, 2), strides=(2, 2), name=pool_name_base)(x)

    return x
Esempio n. 7
0
def identity_block(input_tensor, kernel_size, filters, stage, block):
    '''The identity_block is the block that has no conv layer at shortcut
    # Arguments
        input_tensor: input tensor
        kernel_size: default 3, the kernel size of middle conv layer at main path
        filters: list of integers, the nb_filters of 3 conv layer at main path
        stage: integer, current stage label, used for generating layer names
        block: 'a','b'..., current block label, used for generating layer names
    '''
    eps = 1.1e-5
    nb_filter1, nb_filter2, nb_filter3 = filters
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    scale_name_base = 'scale' + str(stage) + block + '_branch'

    x = Convolution2D(nb_filter1, 1, 1, name=conv_name_base + '2a',
                      bias=False)(input_tensor)
    x = BatchNormalization(epsilon=eps, axis=bn_axis,
                           name=bn_name_base + '2a')(x)
    x = Scale(axis=bn_axis, name=scale_name_base + '2a')(x)
    x = Activation('relu', name=conv_name_base + '2a_relu')(x)

    x = ZeroPadding2D((1, 1), name=conv_name_base + '2b_zeropadding')(x)
    x = Convolution2D(nb_filter2,
                      kernel_size,
                      kernel_size,
                      name=conv_name_base + '2b',
                      bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=bn_axis,
                           name=bn_name_base + '2b')(x)
    x = Scale(axis=bn_axis, name=scale_name_base + '2b')(x)
    x = Activation('relu', name=conv_name_base + '2b_relu')(x)

    x = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '2c',
                      bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=bn_axis,
                           name=bn_name_base + '2c')(x)
    x = Scale(axis=bn_axis, name=scale_name_base + '2c')(x)

    x = merge([x, input_tensor], mode='sum', name='res' + str(stage) + block)
    x = Activation('relu', name='res' + str(stage) + block + '_relu')(x)
    return x
Esempio n. 8
0
def get_resnet(img_rows=224, img_cols=224, separately=False):
    """
    Resnet 152 Model for Keras

    Model Schema and layer naming follow that of the original Caffe implementation
    https://github.com/KaimingHe/deep-residual-networks

    ImageNet Pretrained Weights
    Theano: https://drive.google.com/file/d/0Byy2AcGyEVxfZHhUT3lWVWxRN28/view?usp=sharing
    TensorFlow: https://drive.google.com/file/d/0Byy2AcGyEVxfeXExMzNNOHpEODg/view?usp=sharing

    Parameters:
      img_rows, img_cols - resolution of inputs
      channel - 1 for grayscale, 3 for color
    """
    eps = 1.1e-5
    # Handle Dimension Ordering for different backends
    img_input = Input(shape=(img_rows, img_cols, 3), name='data')

    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution2D(64, (7, 7), strides=(2, 2), name='conv1',
                      use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=3, name='bn_conv1')(x)
    x = Scale(axis=3, name='scale_conv1')(x)
    x = Activation('relu', name='conv1_relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    for i in range(1, 8):
        x = identity_block(x, 3, [128, 128, 512], stage=3, block='b' + str(i))

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    for i in range(1, 36):
        x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b' + str(i))

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    x_fc = AveragePooling2D((7, 7), name='avg_pool')(x)
    x_fc = Flatten()(x_fc)
    x_fc = Dense(1000, activation='softmax', name='fc1000')(x_fc)

    model = Model(img_input, x_fc)

    weights_path = 'utils/weights/resnet152_weights_tf.h5'
    assert (os.path.exists(weights_path))
    model.load_weights(weights_path, by_name=True)

    # Truncate and replace softmax layer for transfer learning
    # Cannot use model.layers.pop() since model is not of Sequential() type
    # The method below works since pre-trained weights are stored in layers but not in the model
    x_newfc = AveragePooling2D((7, 7), name='avg_pool')(x)
    x_newfc = Flatten()(x_newfc)
    x_newfc = Dense(256, name='fc8')(x_newfc)
    model = Model(img_input, x_newfc)

    return model
Esempio n. 9
0
def DenseNet(nb_dense_block=3,
             growth_rate=24,
             nb_filter=64,
             reduction=0.0,
             dropout_rate=0.0,
             weight_decay=1e-4,
             classes=1000,
             weights_path=None):
    '''Instantiate the DenseNet 121 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    '''
    ## nb_dense_block = 3 from paper
    ## k=24 from paper
    ## I'm not sure what nb_filter should be
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    ## Change the size of images here. Note, I think the 3 represents RGB layers? So a 3d spatio-temopral densenet should be (width, height, num_frames)
    ## Paper has images scaled to 100Hx100W, 16 frames
    # Handle Dimension Ordering for different backends
    global concat_axis
    concat_axis = 3
    img_input = Input(shape=(100, 100, 3, 16), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    ## The paper says x4 for each layer, 3 total
    nb_layers = [4, 4, 4]

    ## Note: subsample = strides
    ## Convolution-3D: 7x7x7 conv, stride=2
    x = ZeroPadding3D((3, 3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution3D(nb_filter,
                      7,
                      7,
                      7,
                      subsample=(2, 2, 2),
                      name='conv1',
                      bias=False)(x)

    ## Pooling-3D: 3x3x3 avg pool, stride=2
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding3D((1, 1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling3D((3, 3, 3), strides=(2, 2, 2), name='pool1')(x)

    ## Add dense blocks 1, 2
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        ## Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    ## Add last dense block: 3 (since we don't have another transition after)
    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    ## Classification Layer
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)

    ## For our paper, we want a 7x7x4 avg pool instead of a global average pool, assumed to be strides = 1 b/c not specified
    ## x = GlobalAveragePooling2D(name='pool'+str(final_stage))(x)
    x = AveragePooling3D((7, 7, 4), strides=(1, 1, 1), name=pool_name_base)(x)

    ## Fully connected (dense) 2D softmax
    ## Note: the original 2d densenet paper does a 1000D softmax, but I think this should be 2d since the pooling is no longer global.
    x = Dense(classes, name='fc6')(x)
    x = Activation('softmax', name='prob')(x)

    model = Model(img_input, x, name='densenet')

    if weights_path is not None:
        model.load_weights(weights_path)

    return model
def DenseUNet(img_input,
              nb_dense_block=4,
              growth_rate=48,
              nb_filter=96,
              reduction=0.0,
              dropout_rate=0.0):
    """Instantiate the DenseNet 161 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    """
    eps = 1.1e-5
    compression = 1.0 - reduction
    # From architecture for ImageNet (Table 1 in the paper)
    nb_layers = [6, 12, 36, 24]  # For DenseNet-161
    box = []
    stage = 0

    x = ZeroPadding2D((3, 3), name='convolution_1_zero_padding')(img_input)
    x = Conv2D(nb_filter, (7, 7),
               strides=(2, 2),
               name='convolution_1',
               use_bias=False,
               trainable=False)(x)
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           momentum=1,
                           name='convolution_1_batch_normalization',
                           trainable=False)(x, training=False)
    x = Scale(axis=concat_axis, name='convolution_1_scale', trainable=False)(x)
    x = Activation('relu', name='convolution_1_ReLU')(x)
    box.append(x)
    x = ZeroPadding2D((1, 1), name='pooling_1_zero_padding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pooling_1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate)
        box.append(x)
        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           momentum=1,
                           name='convolution_' + str(final_stage) +
                           '_blk_batch_normalization',
                           trainable=False)(x, training=False)
    x = Scale(axis=concat_axis,
              name='convolution_' + str(final_stage) + '_blk_scale',
              trainable=False)(x)
    x = Activation('relu',
                   name='convolution_ReLU_' + str(final_stage) + '_blk')(x)
    box.append(x)

    up0 = UpSampling2D(size=(2, 2))(x)
    conv_up0 = Conv2D(768, (3, 3),
                      padding="same",
                      name="upsampling_0",
                      trainable=False)(up0)
    bn_up0 = BatchNormalization(name="upsampling_0_batch_normalization",
                                momentum=1,
                                trainable=False)(conv_up0, training=False)
    ac_up0 = Activation('relu', name='upsampling_0_ReLU')(bn_up0)

    up1 = UpSampling2D(size=(2, 2))(ac_up0)
    conv_up1 = Conv2D(384, (3, 3),
                      padding="same",
                      name="upsampling_1",
                      trainable=False)(up1)
    bn_up1 = BatchNormalization(name="upsampling_1_batch_normalization",
                                momentum=1,
                                trainable=False)(conv_up1, training=False)
    ac_up1 = Activation('relu', name='upsampling_1_ReLU')(bn_up1)

    up2 = UpSampling2D(size=(2, 2))(ac_up1)
    conv_up2 = Conv2D(96, (3, 3),
                      padding="same",
                      name="upsampling_2",
                      trainable=False)(up2)
    bn_up2 = BatchNormalization(name="upsampling_2_batch_normalization",
                                momentum=1,
                                trainable=False)(conv_up2, training=False)
    ac_up2 = Activation('relu', name='upsampling_2_ReLU')(bn_up2)

    up3 = UpSampling2D(size=(2, 2))(ac_up2)
    conv_up3 = Conv2D(96, (3, 3),
                      padding="same",
                      name="upsampling_3",
                      trainable=False)(up3)
    bn_up3 = BatchNormalization(name="upsampling_3_batch_normalization",
                                momentum=1,
                                trainable=False)(conv_up3, training=False)
    ac_up3 = Activation('relu', name='upsampling_3_ReLU')(bn_up3)

    up4 = UpSampling2D(size=(2, 2))(ac_up3)
    conv_up4 = Conv2D(64, (3, 3),
                      padding="same",
                      name="upsampling_4",
                      trainable=False)(up4)
    bn_up4 = BatchNormalization(name="upsampling_4_batch_normalization",
                                momentum=1,
                                trainable=False)(conv_up4, training=False)
    ac_up4 = Activation('relu', name='upsampling_4_ReLU')(bn_up4)

    x = Conv2D(3, (1, 1),
               padding="same",
               name='2D-DenseUNet_classifier',
               trainable=False)(ac_up4)

    return ac_up4, x
Esempio n. 11
0
def densenet(img_rows,
             img_cols,
             lr=0.01,
             color_type=1,
             nb_dense_block=4,
             growth_rate=32,
             nb_filter=64,
             reduction=0.5,
             dropout_rate=0.0,
             weight_decay=1e-4,
             num_classes=None,
             opt='sgd',
             bn_type='bn'):
    '''
    DenseNet 121 Model for Keras

    Model Schema is based on 
    https://github.com/flyyufelix/DenseNet-Keras

    ImageNet Pretrained Weights 
    Theano: https://drive.google.com/open?id=0Byy2AcGyEVxfMlRYb3YzV210VzQ
    TensorFlow: https://drive.google.com/open?id=0Byy2AcGyEVxfSTA4SHJVOHNuTXc

    # Arguments
        nb_dense_block: number of dense blocks to add to end
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters
        reduction: reduction factor of transition blocks.
        dropout_rate: dropout rate
        weight_decay: weight decay factor
        classes: optional number of classes to classify images
        weights_path: path to pre-trained weights
    # Returns
        A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(shape=(img_rows, img_cols, color_type), name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(img_rows, img_cols), name='data')

    #Batch normalization layer definition
    global BatchNorm
    if bn_type == 'bn':
        BatchNorm = BatchNormalization
    elif bn_type == 'brn':
        BatchNorm = BatchRenormalization

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    nb_layers = [6, 12, 24, 16]  # For DenseNet-121

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Conv2D(nb_filter, (7, 7), strides=(2, 2), name='conv1',
               use_bias=False)(x)
    x = BatchNorm(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNorm(epsilon=eps,
                  axis=concat_axis,
                  name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)

    x_fc = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)
    x_fc = Dense(1, name='fc6')(x_fc)
    x_fc = Activation('sigmoid', name='prob')(x_fc)

    model = Model(img_input, x_fc, name='densenet')

    # Learning rate is changed to 0.001
    if opt == 'sgd':
        optim = SGD(lr=lr, decay=0.01, momentum=0.9, nesterov=True)
    elif opt == 'adam':
        optim = Adam(lr=lr, decay=0.01)
    model.compile(optimizer=optim,
                  loss='binary_crossentropy',
                  metrics=['binary_accuracy'])

    return model
Esempio n. 12
0
def DenseUNet(nb_dense_block=4,
              growth_rate=48,
              nb_filter=96,
              reduction=0.0,
              dropout_rate=0.0,
              weight_decay=1e-4,
              classes=1000,
              weights_path=None):
    '''Instantiate the DenseNet 161 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(batch_shape=(batch_size, img_deps, img_rows, 3),
                          name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(3, 224, 224), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 96
    nb_layers = [6, 12, 36, 24]  # For DenseNet-161
    box = []
    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Conv2D(nb_filter, (7, 7), strides=(2, 2), name='conv1',
               use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    box.append(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)
        box.append(x)
        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)
    box.append(x)

    up0 = UpSampling2D(size=(2, 2))(x)
    line0 = Conv2D(2208, (1, 1),
                   padding="same",
                   kernel_initializer="normal",
                   name="line0")(box[3])
    up0_sum = add([line0, up0])
    conv_up0 = Conv2D(768, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="conv_up0")(up0_sum)
    bn_up0 = BatchNormalization(name="bn_up0")(conv_up0)
    ac_up0 = Activation('relu', name='ac_up0')(bn_up0)

    up1 = UpSampling2D(size=(2, 2))(ac_up0)
    up1_sum = add([box[2], up1])
    conv_up1 = Conv2D(384, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="conv_up1")(up1_sum)
    bn_up1 = BatchNormalization(name="bn_up1")(conv_up1)
    ac_up1 = Activation('relu', name='ac_up1')(bn_up1)

    up2 = UpSampling2D(size=(2, 2))(ac_up1)
    up2_sum = add([box[1], up2])
    conv_up2 = Conv2D(96, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="conv_up2")(up2_sum)
    bn_up2 = BatchNormalization(name="bn_up2")(conv_up2)
    ac_up2 = Activation('relu', name='ac_up2')(bn_up2)

    up3 = UpSampling2D(size=(2, 2))(ac_up2)
    up3_sum = add([box[0], up3])
    conv_up3 = Conv2D(96, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="conv_up3")(up3_sum)
    bn_up3 = BatchNormalization(name="bn_up3")(conv_up3)
    ac_up3 = Activation('relu', name='ac_up3')(bn_up3)

    up4 = UpSampling2D(size=(2, 2))(ac_up3)
    conv_up4 = Conv2D(64, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="conv_up4")(up4)
    conv_up4 = Dropout(rate=0.3)(conv_up4)
    bn_up4 = BatchNormalization(name="bn_up4")(conv_up4)
    ac_up4 = Activation('relu', name='ac_up4')(bn_up4)

    x = Conv2D(3, (1, 1),
               padding="same",
               kernel_initializer="normal",
               name="dense167classifer")(ac_up4)

    model = Model(img_input, x, name='denseu161')

    if weights_path is not None:
        model.load_weights(weights_path)

    return model
def DenseNet3D(img_input,
               nb_dense_block=4,
               growth_rate=32,
               nb_filter=96,
               reduction=0.0,
               dropout_rate=0.0):
    """Instantiate the DenseNet 161 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    """
    eps = 1.1e-5
    compression = 1.0 - reduction
    # From architecture for ImageNet (Table 1 in the paper)
    nb_layers = [3, 4, 12, 8]
    box = []
    stage = 0

    x = ZeroPadding3D((3, 3, 3),
                      name='3D_convolution_1_zero_padding')(img_input)
    x = Conv3D(nb_filter, (7, 7, 7),
               strides=(2, 2, 2),
               name='3D_convolution_1',
               use_bias=False)(x)
    x = BatchNormalization(epsilon=eps,
                           axis=4,
                           name='3D_convolution_1_batch_normalization')(x)
    x = Scale(axis=4, name='3D_convolution_1_scale')(x)
    x = Activation('relu', name='3D_convolution_1_ReLU')(x)

    box.append(x)

    x = ZeroPadding3D((1, 1, 1), name='3D_pooling_1_zero_padding')(x)
    x = MaxPooling3D((3, 3, 3), strides=(2, 2, 2), name='3D_pooling_1')(x)

    for block_id in range(nb_dense_block - 1):
        stage = block_id + 2
        x, nb_filter = dense_block3d(x,
                                     stage,
                                     nb_layers[block_id],
                                     nb_filter,
                                     growth_rate,
                                     dropout_rate=dropout_rate)

        box.append(x)

        x = transition_block3d(x,
                               stage,
                               nb_filter,
                               compression=compression,
                               dropout_rate=dropout_rate)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block3d(x,
                                 final_stage,
                                 nb_layers[-1],
                                 nb_filter,
                                 growth_rate,
                                 dropout_rate=dropout_rate)

    x = BatchNormalization(epsilon=eps,
                           axis=4,
                           name='3D_convolution_' + str(final_stage) +
                           '_blk_batch_normalization')(x)
    x = Scale(axis=4,
              name='3D_convolution_' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu',
                   name='3D_convolution_ReLU' + str(final_stage) + '_blk')(x)

    box.append(x)

    up0 = UpSampling3D(size=(2, 2, 1))(x)
    conv_up0 = Conv3D(504, (3, 3, 3), padding="same",
                      name="3D_upsampling_0")(up0)
    bn_up0 = BatchNormalization(
        name="3D_upsampling_0_batch_normalization")(conv_up0)
    ac_up0 = Activation('relu', name='3D_upsampling_0_ReLU')(bn_up0)

    up1 = UpSampling3D(size=(2, 2, 1))(ac_up0)
    conv_up1 = Conv3D(224, (3, 3, 3), padding="same",
                      name="3D_upsampling_1")(up1)
    bn_up1 = BatchNormalization(
        name="3D_upsampling_1_batch_normalization")(conv_up1)
    ac_up1 = Activation('relu', name='3D_upsampling_1_ReLU')(bn_up1)

    up2 = UpSampling3D(size=(2, 2, 1))(ac_up1)
    conv_up2 = Conv3D(192, (3, 3, 3), padding="same",
                      name="3D_upsampling_2")(up2)
    bn_up2 = BatchNormalization(
        name="3D_upsampling_2_batch_normalization")(conv_up2)
    ac_up2 = Activation('relu', name='3D_upsampling_2_ReLU')(bn_up2)

    up3 = UpSampling3D(size=(2, 2, 2))(ac_up2)
    conv_up3 = Conv3D(96, (3, 3, 3), padding="same",
                      name="3D_upsampling_3")(up3)
    bn_up3 = BatchNormalization(
        name="3D_upsampling_3_batch_normalization")(conv_up3)
    ac_up3 = Activation('relu', name='3D_upsampling_3_ReLU')(bn_up3)

    up4 = UpSampling3D(size=(2, 2, 2))(ac_up3)
    conv_up4 = Conv3D(64, (3, 3, 3), padding="same",
                      name="3D_upsampling_4")(up4)
    bn_up4 = BatchNormalization(
        name="3D_upsampling_4_batch_normalization")(conv_up4)
    ac_up4 = Activation('relu', name='3D_upsampling_4_ReLU')(bn_up4)

    x = Conv3D(3, (1, 1, 1), padding="same", name='3D_classifier')(ac_up4)

    return ac_up4, x
Esempio n. 14
0
def conv_block(x, stage, branch, nb_filter, dropout_rate=None, weight_decay=0):
    '''Apply BatchNorm, Relu, bottleneck 1x1 Conv2D, 3x3 Conv2D, and option dropout
        # Arguments
            x: input tensor 
            stage: index for dense block
            branch: layer index within each dense block
            nb_filter: number of filters
            dropout_rate: dropout rate
            weight_decay: weight decay factor
    '''
    eps = 1.1e-5
    conv_name_base = 'conv' + str(stage) + '_' + str(branch)
    relu_name_base = 'relu' + str(stage) + '_' + str(branch)

    # 1x1 Convolution (Bottleneck layer)
    inter_channel = nb_filter * 3
    #x = BatchNormalization(epsilon=eps, axis=concat_axis, name=conv_name_base+'_x1_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_x1_scale')(x)
    #x = Activation('relu', name=relu_name_base+'_x1')(x)
    x1 = Conv2D(inter_channel, (1, 1),
                name=conv_name_base + '_x1',
                kernel_initializer='he_normal',
                activation='relu')(x)

    x2_1 = Conv2D(inter_channel / 2, (1, 1),
                  name=conv_name_base + '_x2_1',
                  kernel_initializer='he_normal',
                  activation='relu')(x)
    x2_2 = Conv2D(inter_channel, (3, 3),
                  name=conv_name_base + '_x2_2',
                  kernel_initializer='he_normal',
                  activation='relu',
                  padding='same')(x2_1)

    x3_1 = Conv2D(inter_channel / 2, (1, 1),
                  name=conv_name_base + '_x3_1',
                  kernel_initializer='he_normal',
                  activation='relu')(x)
    x3_2 = Conv2D(inter_channel, (3, 3),
                  name=conv_name_base + '_x3_2',
                  kernel_initializer='he_normal',
                  activation='relu',
                  padding='same')(x3_1)
    x3_3 = Conv2D(inter_channel, (3, 3),
                  name=conv_name_base + '_x3_3',
                  kernel_initializer='he_normal',
                  activation='relu',
                  padding='same')(x3_2)

    x4_1 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x)
    x4_2 = Conv2D(inter_channel, (1, 1),
                  name=conv_name_base + '_x4_2',
                  kernel_initializer='he_normal',
                  activation='relu')(x4_1)

    x5 = add([x1, x2_2, x3_3, x4_2])
    if dropout_rate:
        x5 = Dropout(dropout_rate)(x5)

    # 3x3 Convolution
    #x = BatchNormalization(epsilon=eps, axis=concat_axis, name=conv_name_base+'_x2_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_x2_scale')(x5)
    #x = Activation('relu', name=relu_name_base+'_x2')(x)
    #x = ZeroPadding2D((1, 1), name=conv_name_base+'_x2_zeropadding')(x)
    x = Conv2D(nb_filter, (1, 1),
               name=conv_name_base + '_x2',
               kernel_initializer='he_normal',
               activation='relu')(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    return x
Esempio n. 15
0
    def DenseNet(input_shape,
                 classes=100,
                 weights="trained_model/inception.hdf5"):
        eps = 1.1e-5
        nb_dense_block = 4
        growth_rate = 32
        dropout_rate = 0.0
        weight_decay = 1e-4
        # compute compression factor
        reduction = 0.5
        compression = 1.0 - reduction
        global concat_axis
        concat_axis = 3
        img_input = Input(shape=input_shape, name='data')

        # From architecture for ImageNet (Table 1 in the paper)
        nb_filter = 64
        nb_layers = [6, 12, 24, 16]  # For DenseNet-121

        # Initial convolution
        x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
        x = Convolution2D(nb_filter,
                          7,
                          7,
                          subsample=(2, 2),
                          name='conv1',
                          bias=False)(x)
        x = BatchNormalization(epsilon=eps, axis=concat_axis,
                               name='conv1_bn')(x)
        x = Scale(axis=concat_axis, name='conv1_scale')(x)
        x = Activation('relu', name='relu1')(x)
        x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
        x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

        # Add dense blocks
        for block_idx in range(nb_dense_block - 1):
            stage = block_idx + 2
            x, nb_filter = dense_block(x,
                                       stage,
                                       nb_layers[block_idx],
                                       nb_filter,
                                       growth_rate,
                                       dropout_rate=dropout_rate,
                                       weight_decay=weight_decay)

            # Add transition_block
            x = transition_block(x,
                                 stage,
                                 nb_filter,
                                 compression=compression,
                                 dropout_rate=dropout_rate,
                                 weight_decay=weight_decay)
            nb_filter = int(nb_filter * compression)

        final_stage = stage + 1
        x, nb_filter = dense_block(x,
                                   final_stage,
                                   nb_layers[-1],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        x = BatchNormalization(epsilon=eps,
                               axis=concat_axis,
                               name='conv' + str(final_stage) + '_blk_bn')(x)
        x = Scale(axis=concat_axis,
                  name='conv' + str(final_stage) + '_blk_scale')(x)
        x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)
        x = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)

        x = Dense(classes, name='fc6')(x)
        x = Activation('softmax', name='prob')(x)

        model = Model(img_input, x, name='densenet')

        if os.path.isfile(weights):
            model.load_weights(weights)
            print("Model loaded")
        else:
            print("No model is found")

        return model
def DenseNet(nb_dense_block=3,
             growth_rate=20,
             nb_filter=32,
             reduction=0.0,
             dropout_rate=0.0,
             weight_decay=0,
             classes=1000,
             weights_path=None):
    '''Instantiate the DenseNet 121 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    '''
    with tf.device('/gpu:0'):
        eps = 1.1e-5

        # compute compression factor
        compression = 1.0 - reduction

        # Handle Dimension Ordering for different backends
        global concat_axis
        if K.image_dim_ordering() == 'tf':
            concat_axis = 3
            img_input = Input(shape=(512, 512, 3), name='data')
        else:
            concat_axis = 1
            img_input = Input(shape=(3, 224, 224), name='data')

        # From architecture for ImageNet (Table 1 in the paper)
        nb_filter = 32  ##make this 16
        nb_layers = [3, 6, 12, 8]  #[6,12,24,16] # For DenseNet-121

        # Initial convolution
        x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
        x = Conv2D(nb_filter, (7, 7),
                   name='conv1',
                   kernel_initializer='he_normal',
                   padding='valid')(
                       x)  ### Put strides of 2*2 here and padding = valid
        x = BatchNormalization(axis=concat_axis, name='conv1_bn')(x)
        x = Scale(axis=concat_axis, name='conv1_scale')(x)
        x = Activation('relu', name='relu1')(x)
        x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
        x = MaxPooling2D(
            (3, 3), strides=(4, 4),
            name='pool1')(x)  ### Put strides of 2*2 and pooling  3*3
        #x = SeparableConv2D(nb_filter,(4, 4), strides = (4,4),name='pool1',kernel_initializer='he_normal',activation = 'relu')(x)

        # Add dense blocks
        for block_idx in range(nb_dense_block - 1):
            stage = block_idx + 2
            x, nb_filter = dense_block(x,
                                       stage,
                                       nb_layers[block_idx],
                                       nb_filter,
                                       growth_rate,
                                       dropout_rate=dropout_rate,
                                       weight_decay=weight_decay)

            # Add transition_block
            x = transition_block(x,
                                 stage,
                                 nb_filter,
                                 compression=compression,
                                 dropout_rate=dropout_rate,
                                 weight_decay=weight_decay)
            nb_filter = int(nb_filter * compression)

        final_stage = stage + 1
        x, nb_filter = dense_block(x,
                                   final_stage,
                                   nb_layers[-1],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        #x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv'+str(final_stage)+'_blk_bn')(x)
        x = Scale(axis=concat_axis,
                  name='conv' + str(final_stage) + '_blk_scale')(x)
        '''x1 = Conv2D(32,(3,3),dilation_rate = 6, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv1', activation = 'relu',data_format='channels_last')(x)
	    x2 = Conv2D(32,(3,3),dilation_rate = 12, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv2', activation = 'relu',data_format='channels_last')(x)
	    x3 = Conv2D(32,(3,3),dilation_rate = 18, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv3', activation = 'relu',data_format='channels_last')(x)
	    x4 = Conv2D(32,(3,3),dilation_rate = 24, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv4', activation = 'relu',data_format='channels_last')(x)
	    
	    sum_b = add([x1,x2,x3,x4],name = 'sum_b')'''

        #x = Activation('relu', name='relu'+str(final_stage)+'_blk')(x)
        #x = Conv2D(128,(1,1),padding = 'valid',kernel_initializer='he_normal')(x)
        x = Conv2D(1, (1, 1), padding='valid',
                   kernel_initializer='he_normal')(x)

        model = Model(img_input, x, name='densenet')
        '''xp = model.get_layer(name = 'conv5_blk_scale').output
 	    x1 = Conv2D(16,(3,3),dilation_rate = 6, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv1', activation = 'relu',data_format='channels_last')(xp)
	    x2 = Conv2D(16,(3,3),dilation_rate = 12, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv2', activation = 'relu',data_format='channels_last')(xp)
	    x3 = Conv2D(16,(3,3),dilation_rate = 18, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv3', activation = 'relu',data_format='channels_last')(xp)
            x4 = Conv2D(16,(3,3),dilation_rate = 24, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv4', activation = 'relu',data_format='channels_last')(xp)    
	    sum_b = add([x1,x2,x3,x4],name = 'sum_b')
	    #sum_b_activation = Activation('relu', name='relu_sum')(sum_b)
	    out = Conv2D(1,(1,1),padding = 'valid',kernel_initializer='he_normal',activation = 'relu')(sum_b)
	    model_2 = Model(inputs = model.input, outputs = out) '''
        if weights_path is not None:
            model.load_weights(weights_path)

        return model
Esempio n. 17
0
def get_densenet(img_rows,
                 img_cols,
                 nb_dense_block=4,
                 growth_rate=32,
                 nb_filter=64,
                 reduction=0.5,
                 dropout_rate=0.0,
                 weight_decay=1e-4):
    '''
    DenseNet 169 Model for Keras

    Model Schema is based on
    https://github.com/flyyufelix/DenseNet-Keras

    ImageNet Pretrained Weights
    Theano: https://drive.google.com/open?id=0Byy2AcGyEVxfN0d3T1F1MXg0NlU
    TensorFlow: https://drive.google.com/open?id=0Byy2AcGyEVxfSEc5UC1ROUFJdmM

    # Arguments
        nb_dense_block: number of dense blocks to add to end
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters
        reduction: reduction factor of transition blocks.
        dropout_rate: dropout rate
        weight_decay: weight decay factor
        classes: optional number of classes to classify images
        weights_path: path to pre-trained weights
    # Returns
        A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    img_input = Input(shape=(224, 224, 3), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    nb_layers = [6, 12, 32, 32]  # For DenseNet-169

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution2D(nb_filter, (7, 7),
                      strides=(2, 2),
                      name='conv1',
                      use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=3, name='conv1_bn')(x)
    x = Scale(axis=3, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=3,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=3, name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)

    x_fc = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)
    x_fc = Dense(1000, name='fc6')(x_fc)
    x_fc = Activation('softmax', name='prob')(x_fc)

    model = Model(img_input, x_fc, name='densenet')

    # Use pre-trained weights for Tensorflow backend
    weights_path = 'utils/weights/densenet169_weights_tf.h5'

    model.load_weights(weights_path, by_name=True)

    # Truncate and replace softmax layer for transfer learning
    # Cannot use model.layers.pop() since model is not of Sequential() type
    # The method below works since pre-trained weights are stored in layers but not in the model
    x_newfc = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)

    x_newfc = Dense(256, name='fc7')(x_newfc)
    model = Model(img_input, x_newfc)

    return model
Esempio n. 18
0
def resnet152_model(img_rows, img_cols, color_type=1, num_classes=None):
    """
    Resnet 152 Model for Keras
    Model Schema and layer naming follow that of the original Caffe implementation
    https://github.com/KaimingHe/deep-residual-networks
    ImageNet Pretrained Weights
    Theano: https://drive.google.com/file/d/0Byy2AcGyEVxfZHhUT3lWVWxRN28/view?usp=sharing
    TensorFlow: https://drive.google.com/file/d/0Byy2AcGyEVxfeXExMzNNOHpEODg/view?usp=sharing
    Parameters:
      img_rows, img_cols - resolution of inputs
      channel - 1 for grayscale, 3 for color
      num_classes - number of class labels for our classification task
    """
    eps = 1.1e-5

    # Handle Dimension Ordering for different backends
    global bn_axis
    if K.image_dim_ordering() == 'tf':
        bn_axis = 3
        img_input = Input(shape=(img_rows, img_cols, color_type), name='data')
    else:
        bn_axis = 1
        img_input = Input(shape=(color_type, img_rows, img_cols), name='data')

    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution2D(64, 7, 7, subsample=(2, 2), name='conv1', bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=bn_axis, name='bn_conv1')(x)
    x = Scale(axis=bn_axis, name='scale_conv1')(x)
    x = Activation('relu', name='conv1_relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    for i in range(1, 8):
        x = identity_block(x, 3, [128, 128, 512], stage=3, block='b' + str(i))

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    for i in range(1, 36):
        x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b' + str(i))

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    x_fc = AveragePooling2D((7, 7), name='avg_pool')(x)
    x_fc = Flatten()(x_fc)
    x_fc = Dense(1000, activation='softmax', name='fc1000')(x_fc)

    model = Model(img_input, x_fc)

    if K.image_dim_ordering() == 'th':
        # Use pre-trained weights for Theano backend
        weights_path = WEIGHTS_PATH + 'resnet152_weights_th.h5'
    else:
        # Use pre-trained weights for Tensorflow backend
        #weights_path = WEIGHTS_PATH + 'resnet152_weights_tf.h5'

        #model.load_weights(weights_path, by_name=True)

    return model


def get_resnet152(classes_number, dim_ordering='tf', input_size=None):
    # One Dense - 0.09372
    if input_size == None:
        base_model = resnet152_model(224, 224, 3, classes_number)
    else:
        base_model = resnet152_model(
            input_size[0], input_size[1], input_size[2], )
    x = base_model.layers[-2].output
    del base_model.layers[-1:]
    x = Dense(4096, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(classes_number, activation='sigmoid', name='predictions')(x)
    model = Model(input=base_model.input, output=x)
    print(model.summary())
    return model
Esempio n. 19
0
def resnet101_base(freeze_blocks=[1, 2, 3],
                   weight_regularizer=None,
                   bias_regularizer=None):
    """
    Creates a model of the ResNet-101 base layers used for both the RPN and detector.
    :param freeze_blocks: list of block numbers to make untrainable, e.g. [1,2,3] to not train the first 3 blocks.
    :param weight_regularizer: keras.regularizers.Regularizer object for weight regularization on all layers, None if no
    regularization.
    :param bias_regularizer: keras.regularizers.Regularizer object for bias regularization on all layers, None if no
        regularization.
    :return: Keras model for the base network.
    """
    img_input = Input(shape=(None, None, 3))
    bn_axis = 3
    train1 = 1 not in freeze_blocks
    x = Conv2D(64, (7, 7),
               strides=(2, 2),
               padding='same',
               name='conv1',
               trainable=train1,
               use_bias=False,
               kernel_regularizer=weight_regularizer,
               bias_regularizer=bias_regularizer)(img_input)
    x = BatchNormalization(axis=bn_axis, name='bn_conv1',
                           trainable=False)(x, training=False)
    x = Scale(axis=bn_axis, name='scale_conv1', trainable=False)(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

    train2 = 2 not in freeze_blocks
    x = conv_block(x,
                   3, [64, 64, 256],
                   stage=2,
                   block='a',
                   strides=(1, 1),
                   trainable=train2,
                   weight_regularizer=weight_regularizer,
                   bias_regularizer=bias_regularizer,
                   use_conv_bias=False,
                   separate_scale=True)
    x = identity_block(x,
                       3, [64, 64, 256],
                       stage=2,
                       block='b',
                       trainable=train2,
                       weight_regularizer=weight_regularizer,
                       bias_regularizer=bias_regularizer,
                       use_conv_bias=False,
                       separate_scale=True)
    x = identity_block(x,
                       3, [64, 64, 256],
                       stage=2,
                       block='c',
                       trainable=train2,
                       weight_regularizer=weight_regularizer,
                       bias_regularizer=bias_regularizer,
                       use_conv_bias=False,
                       separate_scale=True)

    train3 = 3 not in freeze_blocks
    x = conv_block(x,
                   3, [128, 128, 512],
                   stage=3,
                   block='a',
                   trainable=train3,
                   weight_regularizer=weight_regularizer,
                   bias_regularizer=bias_regularizer,
                   use_conv_bias=False,
                   separate_scale=True)
    for i in range(1, 4):
        x = identity_block(x,
                           3, [128, 128, 512],
                           stage=3,
                           block='b' + str(i),
                           trainable=train3,
                           weight_regularizer=weight_regularizer,
                           bias_regularizer=bias_regularizer,
                           use_conv_bias=False,
                           separate_scale=True)

    train4 = 4 not in freeze_blocks
    x = conv_block(x,
                   3, [256, 256, 1024],
                   stage=4,
                   block='a',
                   trainable=train4,
                   weight_regularizer=weight_regularizer,
                   bias_regularizer=bias_regularizer,
                   use_conv_bias=False,
                   separate_scale=True)
    for i in range(1, 23):
        x = identity_block(x,
                           3, [256, 256, 1024],
                           stage=4,
                           block='b' + str(i),
                           trainable=train4,
                           weight_regularizer=weight_regularizer,
                           bias_regularizer=bias_regularizer,
                           use_conv_bias=False,
                           separate_scale=True)

    base_model = Model(img_input, x, name='resnet101')

    return base_model
Esempio n. 20
0
def td_conv_block(input_tensor,
                  kernel_size,
                  filters,
                  stage,
                  block,
                  td_input_shape,
                  strides=(2, 2),
                  use_conv_bias=True,
                  weight_regularizer=None,
                  bias_regularizer=None,
                  bn_training=False,
                  separate_scale=False):
    """A time distributed block that has a conv layer at shortcut.

    # Arguments
        input_tensor: input tensor
        kernel_size: default 3, the kernel size of middle conv layer at main path
        filters: list of integers, the filterss of 3 conv layer at main path
        stage: integer, current stage label, used for generating layer names
        block: 'a','b'..., current block label, used for generating layer names

    # Additional arguments
        use_conv_bias: boolean for whether or not convolutional layers should have a bias.
        weight_regularizer: keras.regularizers.Regularizer object for weight regularization on all layers, None if no
        regularization.
        bias_regularizer: keras.regularizers.Regularizer object for bias regularization on all layers, None if no
        regularization.
        bn_training: boolean for whether or not BatchNormalization layers should be trained. Should always be false as
        the model doesn't train correctly with batch normalization.
        separate_scale: boolean for whether or not the BatchNormalization layers should be followed by a separate Scale
        layer.

    # Returns
        Output tensor for the block.

    Note that from stage 3, the first conv layer at main path is with strides=(2,2)
    And the shortcut should have strides=(2,2) as well
    """
    filters1, filters2, filters3 = filters
    bn_axis = 3
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    scale_name_base = 'scale' + str(stage) + block + '_branch'
    eps = 1e-5

    x = TimeDistributed(Conv2D(filters1, (1, 1),
                               strides=strides,
                               use_bias=use_conv_bias,
                               kernel_regularizer=weight_regularizer,
                               bias_regularizer=bias_regularizer),
                        name=conv_name_base + '2a',
                        input_shape=td_input_shape)(input_tensor)
    x = TimeDistributed(BatchNormalization(epsilon=eps,
                                           axis=bn_axis,
                                           trainable=bn_training),
                        name=bn_name_base + '2a')(x, training=bn_training)
    if separate_scale:
        x = TimeDistributed(Scale(axis=bn_axis, trainable=bn_training),
                            name=scale_name_base + '2a')(x,
                                                         training=bn_training)
    x = Activation('relu')(x)

    x = TimeDistributed(Conv2D(filters2,
                               kernel_size,
                               padding='same',
                               use_bias=use_conv_bias,
                               kernel_regularizer=weight_regularizer,
                               bias_regularizer=bias_regularizer),
                        name=conv_name_base + '2b')(x)
    x = TimeDistributed(BatchNormalization(epsilon=eps,
                                           axis=bn_axis,
                                           trainable=bn_training),
                        name=bn_name_base + '2b')(x, training=bn_training)
    if separate_scale:
        x = TimeDistributed(Scale(axis=bn_axis, trainable=bn_training),
                            name=scale_name_base + '2b')(x,
                                                         training=bn_training)
    x = Activation('relu')(x)

    x = TimeDistributed(Conv2D(filters3, (1, 1),
                               use_bias=use_conv_bias,
                               kernel_regularizer=weight_regularizer,
                               bias_regularizer=bias_regularizer),
                        name=conv_name_base + '2c')(x)
    x = TimeDistributed(BatchNormalization(epsilon=eps,
                                           axis=bn_axis,
                                           trainable=bn_training),
                        name=bn_name_base + '2c')(x, training=bn_training)
    if separate_scale:
        x = TimeDistributed(Scale(axis=bn_axis, trainable=bn_training),
                            name=scale_name_base + '2c')(x,
                                                         training=bn_training)

    shortcut = TimeDistributed(Conv2D(filters3, (1, 1),
                                      strides=strides,
                                      use_bias=use_conv_bias,
                                      kernel_regularizer=weight_regularizer,
                                      bias_regularizer=bias_regularizer),
                               name=conv_name_base + '1')(input_tensor)
    shortcut = TimeDistributed(BatchNormalization(epsilon=eps,
                                                  axis=bn_axis,
                                                  trainable=bn_training),
                               name=bn_name_base + '1')(shortcut,
                                                        training=bn_training)
    if separate_scale:
        shortcut = TimeDistributed(Scale(axis=bn_axis, trainable=bn_training),
                                   name=scale_name_base + '1')(
                                       shortcut, training=bn_training)

    x = layers.add([x, shortcut])
    x = TimeDistributed(Activation('relu'))(x)
    return x
Esempio n. 21
0
def identity_block(input_tensor,
                   kernel_size,
                   filters,
                   stage,
                   block,
                   trainable=True,
                   use_conv_bias=True,
                   weight_regularizer=None,
                   bias_regularizer=None,
                   bn_training=False,
                   separate_scale=False):
    """The identity block is the block that has no conv layer at shortcut.

    # Arguments
        input_tensor: input tensor
        kernel_size: default 3, the kernel size of middle conv layer at main path
        filters: list of integers, the filters of 3 conv layer at main path
        stage: integer, current stage label, used for generating layer names
        block: 'a','b'..., current block label, used for generating layer names

    # Additional arguments
        trainable: boolean for whether to make this block's layers trainable.
        use_conv_bias: boolean for whether or not convolutional layers should have a bias.
        weight_regularizer: keras.regularizers.Regularizer object for weight regularization on all layers, None if no
        regularization.
        bias_regularizer: keras.regularizers.Regularizer object for bias regularization on all layers, None if no
        regularization.
        bn_training: boolean for whether or not BatchNormalization layers should be trained. Should always be false as
        the model doesn't train correctly with batch normalization.
        separate_scale: boolean for whether or not the BatchNormalization layers should be followed by a separate Scale
        layer.

    # Returns
        Output tensor for the block.
    """
    filters1, filters2, filters3 = filters
    if K.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    scale_name_base = 'scale' + str(stage) + block + '_branch'
    eps = 1e-5

    x = Conv2D(filters1, (1, 1),
               name=conv_name_base + '2a',
               trainable=trainable,
               use_bias=use_conv_bias,
               kernel_regularizer=weight_regularizer,
               bias_regularizer=bias_regularizer)(input_tensor)
    x = BatchNormalization(epsilon=eps,
                           axis=bn_axis,
                           name=bn_name_base + '2a',
                           trainable=bn_training)(x, training=bn_training)
    if separate_scale:
        x = Scale(axis=bn_axis,
                  name=scale_name_base + '2a',
                  trainable=bn_training)(x)
    x = Activation('relu')(x)

    x = Conv2D(filters2, (kernel_size, kernel_size),
               padding='same',
               name=conv_name_base + '2b',
               trainable=trainable,
               use_bias=use_conv_bias,
               kernel_regularizer=weight_regularizer,
               bias_regularizer=bias_regularizer)(x)
    x = BatchNormalization(epsilon=eps,
                           axis=bn_axis,
                           name=bn_name_base + '2b',
                           trainable=bn_training)(x, training=bn_training)
    if separate_scale:
        x = Scale(axis=bn_axis,
                  name=scale_name_base + '2b',
                  trainable=bn_training)(x)
    x = Activation('relu')(x)

    x = Conv2D(filters3, (1, 1),
               name=conv_name_base + '2c',
               trainable=trainable,
               use_bias=use_conv_bias,
               kernel_regularizer=weight_regularizer,
               bias_regularizer=bias_regularizer)(x)
    x = BatchNormalization(epsilon=eps,
                           axis=bn_axis,
                           name=bn_name_base + '2c',
                           trainable=bn_training)(x, training=bn_training)
    if separate_scale:
        x = Scale(axis=bn_axis,
                  name=scale_name_base + '2c',
                  trainable=bn_training)(x)

    x = layers.add([x, input_tensor])
    x = Activation('relu')(x)
    return x
Esempio n. 22
0
def DenseNet(nb_dense_block=4,
             growth_rate=32,
             nb_filter=64,
             reduction=0.0,
             dropout_rate=0.0,
             weight_decay=1e-4,
             classes=None,
             weights_path=None,
             NumNonTrainable=None):
    '''Instantiate the DenseNet architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(shape=(224, 224, 3), name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(3, 224, 224), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    nb_layers = [6, 12, 32, 32]  # For DenseNet-169

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution2D(nb_filter, (7, 7),
                      strides=(2, 2),
                      name='conv1',
                      use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)
    x = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)

    x = Dense(1000, name='fc6')(x)
    x = Activation('softmax', name='prob')(x)

    model = Model(img_input, x, name='densenet')

    if weights_path is not None:
        model.load_weights(weights_path)

    # Truncate and replace softmax layer for transfer learning
    model.layers.pop()
    model.layers[-2].outbound_nodes = []
    x = model.layers[-2].output
    x = Dense(classes, name='fc6')(x)
    x = Activation('softmax')(x)

    model = Model(img_input, x, name='densenet')
    f = open(
        "/home/shayan/Codes/DenseNet-Keras-master/results/Model_Layers.txt",
        "w")
    f.write('Model Layers:')
    f.write('\n')
    for ix in range(len(model.layers)):
        f.write(str(ix) + " " + str(model.layers[ix]))
        f.write('\n')
    f.close()

    # Set the first 10 layers to non-trainable (weights will not be updated)
    for layer in model.layers[:NumNonTrainable]:
        layer.trainable = False

    return model
Esempio n. 23
0
def densenet121_model(img_rows,
                      img_cols,
                      color_type=1,
                      nb_dense_block=4,
                      growth_rate=32,
                      nb_filter=64,
                      reduction=0.5,
                      dropout_rate=0.0,
                      weight_decay=1e-4,
                      num_classes=None):
    '''
    DenseNet 121 Model for Keras

    Model Schema is based on
    https://github.com/flyyufelix/DenseNet-Keras

    ImageNet Pretrained Weights
    Theano: https://drive.google.com/open?id=0Byy2AcGyEVxfMlRYb3YzV210VzQ
    TensorFlow: https://drive.google.com/open?id=0Byy2AcGyEVxfSTA4SHJVOHNuTXc

    # Arguments
        nb_dense_block: number of dense blocks to add to end
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters
        reduction: reduction factor of transition blocks.
        dropout_rate: dropout rate
        weight_decay: weight decay factor
        classes: optional number of classes to classify images
        weights_path: path to pre-trained weights
    # Returns
        A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(shape=(img_rows, img_cols, color_type), name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(color_type, img_rows, img_cols), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    nb_layers = [6, 12, 24, 16]  # For DenseNet-121

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Conv2D(nb_filter, (7, 7), strides=(2, 2), name='conv1',
               use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)

    x_fc = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)
    x_fc = Dense(1000, name='fc6')(x_fc)
    x_fc = Activation('softmax', name='prob')(x_fc)

    model = Model(img_input, x_fc, name='densenet')

    if K.image_dim_ordering() == 'th':
        # Use pre-trained weights for Theano backend
        weights_path = '../imagenet_models/densenet121_weights_th.h5'
    else:
        # Use pre-trained weights for Tensorflow backend
        weights_path = '../imagenet_models/densenet121_weights_tf.h5'

    # model.load_weights(weights_path, by_name=True)

    # Truncate and replace softmax layer for transfer learning
    # Cannot use model.layers.pop() since model is not of Sequential() type
    # The method below works since pre-trained weights are stored in layers but not in the model
    x_newfc = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)
    x_newfc = Dense(num_classes, name='fc6')(x_newfc)
    x_newfc = Activation('softmax', name='prob')(x_newfc)

    model = Model(img_input, x_newfc)

    # Learning rate is changed to 0.001
    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    return model
def DenseUNet(nb_dense_block=4,
              growth_rate=48,
              reduction=0.0,
              dropout_rate=0.0,
              weight_decay=1e-4,
              weights_path=None):
    """Instantiate the DenseNet 161 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    """
    eps = 1.1e-5

    compression = 1.0 - reduction

    global concat_axis

    # Handle Dimension Ordering (input dimension) for different backends
    # If Keras is on Tensorflow backends
    if k.image_data_format() == 'channels_last':
        concat_axis = 3
        img_input = Input(batch_shape=(batch_size, img_depth, img_row, 3),
                          name='data')
    else:
        # Keras is on Theano backends
        concat_axis = 1
        # Then the channel dimension is in the first position instead
        img_input = Input(shape=(3, 224, 224), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 96
    nb_layers = [6, 12, 36, 24]  # For DenseNet-161
    box = []

    x = ZeroPadding2D((3, 3), name='convolution_1_zero_padding')(img_input)
    x = Conv2D(nb_filter, (7, 7),
               strides=(2, 2),
               name='convolution_1',
               use_bias=False)(x)
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='convolution_1_batch_normalization')(x)
    x = Scale(axis=concat_axis, name='convolution_1_scale')(x)
    x = Activation('relu', name='convolution_1_ReLU')(x)

    box.append(x)

    x = ZeroPadding2D((1, 1), name='pooling_1_zero_padding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pooling_1')(x)

    for block_id in range(nb_dense_block - 1):
        stage = block_id + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_id],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        box.append(x)

        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        # Compression is added to further reduce the number of feature maps
        nb_filter = int(nb_filter * compression)

    final_stage = nb_dense_block
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='convolution' + str(final_stage) +
                           '_blk_batch_normalization')(x)
    x = Scale(axis=concat_axis,
              name='convolution' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='ReLU' + str(final_stage) + '_blk')(x)

    box.append(x)

    up0 = UpSampling2D(size=(2, 2))(x)
    line0 = Conv2D(2208, (1, 1),
                   padding="same",
                   kernel_initializer="normal",
                   name="line_0")(box[3])
    up0_sum = add([line0, up0])
    conv_up0 = Conv2D(768, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="upsampling_0")(up0_sum)
    bn_up0 = BatchNormalization(
        name="batch_normalization_upsampling_0")(conv_up0)
    ac_up0 = Activation('relu', name='ReLU_upsampling_0')(bn_up0)

    up1 = UpSampling2D(size=(2, 2))(ac_up0)
    up1_sum = add([box[2], up1])
    conv_up1 = Conv2D(384, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="upsampling_1")(up1_sum)
    bn_up1 = BatchNormalization(
        name="batch_normalization_upsampling_1")(conv_up1)
    ac_up1 = Activation('relu', name='ReLU_upsampling_1')(bn_up1)

    up2 = UpSampling2D(size=(2, 2))(ac_up1)
    up2_sum = add([box[1], up2])
    conv_up2 = Conv2D(96, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="upsampling_2")(up2_sum)
    bn_up2 = BatchNormalization(
        name="batch_normalization_upsampling_2")(conv_up2)
    ac_up2 = Activation('relu', name='ReLU_upsampling_2')(bn_up2)

    up3 = UpSampling2D(size=(2, 2))(ac_up2)
    up3_sum = add([box[0], up3])
    conv_up3 = Conv2D(96, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="upsampling_3")(up3_sum)
    bn_up3 = BatchNormalization(
        name="batch_normalization_upsampling_3")(conv_up3)
    ac_up3 = Activation('relu', name='ReLU_upsampling_3')(bn_up3)

    up4 = UpSampling2D(size=(2, 2))(ac_up3)
    conv_up4 = Conv2D(64, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="upsamping_4")(up4)
    conv_up4 = Dropout(rate=0.3)(conv_up4)
    bn_up4 = BatchNormalization(
        name="batch_normalization_upsampling_4")(conv_up4)
    ac_up4 = Activation('relu', name='ReLU_upsampling_4')(bn_up4)

    # Convolution 2
    x = Conv2D(3, (1, 1),
               padding="same",
               kernel_initializer="normal",
               name="dense167_classifier")(ac_up4)

    model = Model(img_input, x, name='denseunet_161')

    if weights_path is not None:
        model.load_weights(weights_path)

    return model
Esempio n. 25
0
def __DenseNet121(nb_dense_block=4, growth_rate=32, nb_filter=64, reduction=0.5, dropout_rate=0.0,
        weight_decay=1e-4, load_weights=True, include_top=False, input_tensor=None,
        pooling='avg', input_shape=(224, 224, 3), classes=1000):
    '''Instantiate the DenseNet 121 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    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
        # endif
    # endif

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_data_format() == 'channels_last':
        concat_axis = 3
    else:
        concat_axis = 1
    # endif

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    nb_layers = [6,12,24,16] # For DenseNet-121

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution2D(nb_filter, kernel_size=(7, 7), strides=(2, 2), name='conv1', use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx+2
        x, nb_filter = dense_block(x, stage, nb_layers[block_idx], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x, stage, nb_filter, compression=compression, dropout_rate=dropout_rate, weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)
    # endfor

    final_stage = stage + 1
    x, nb_filter = dense_block(x, final_stage, nb_layers[-1], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv'+str(final_stage)+'_blk_bn')(x)
    x = Scale(axis=concat_axis, name='conv'+str(final_stage)+'_blk_scale')(x)
    x = Activation('relu', name='relu'+str(final_stage)+'_blk')(x)
    if include_top:
        x = GlobalAveragePooling2D(name='pool'+str(final_stage))(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D(name='pool'+str(final_stage))(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D(name='pool'+str(final_stage))(x)
        # endif
    # endif
    fc_ll = x

    x = Dense(classes, name='fc6')(x)
    x = Activation('softmax', name='prob')(x)

    model = Model(img_input, x, name='densenet121')
    if load_weights:
        weights_path = get_file('densenet121_weights_tf.h5', DENSENET_121_WEIGHTS_PATH, cache_subdir='models')
        model.load_weights(weights_path)
    # endif
 
    if include_top == False:
        model = Model(img_input, fc_ll)
    # endif
        
    return model
Esempio n. 26
0
def proposed_model():

    # Parameters set on our proposed model
    nb_dense_block = 4
    growth_rate = 32
    nb_filter = 64
    reduction = 0.0
    dropout_rate = 0.0
    weight_decay = 1e-4
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(shape=(96, 96, 3), name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(3, 96, 96), name='data')

    nb_filter = 64
    nb_layers = [6, 12, 32, 32]

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Conv2D(nb_filter, 7, 7, subsample=(2, 2), name='conv1', bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)

    model = Model(img_input, x, name='our_model')

    return model
Esempio n. 27
0
def DenseNet(nb_dense_block=4,
             growth_rate=32,
             nb_filter=64,
             reduction=0.0,
             dropout_rate=0.0,
             weight_decay=1e-4,
             classes=1000,
             weights_path=None):
    '''Instantiate the DenseNet 121 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(shape=(224, 224, 3), name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(3, 224, 224), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    nb_layers = [6, 12, 24, 16]  # For DenseNet-121

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Conv2D(nb_filter, (7, 7), strides=(2, 2), name='conv1',
               use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)
    x = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)

    x = Dense(classes, name='fc6')(x)
    x = Activation('softmax', name='prob')(x)

    model = Model(img_input, x, name='densenet')

    if weights_path is not None:
        model.load_weights(weights_path)

    return model
Esempio n. 28
0
def conv_block(x,
               t,
               stage,
               branch,
               nb_filter,
               dropout_rate=None,
               weight_decay=1e-4):
    '''Apply BatchNorm, Relu, bottleneck 1x1 Conv2D, 3x3 Conv2D, and option dropout
        # Arguments
            x: input tensor 
            stage: index for dense block
            branch: layer index within each dense block
            nb_filter: number of filters
            dropout_rate: dropout rate
            weight_decay: weight decay factor
    '''
    eps = 1.1e-5
    conv_name_base = 'conv' + str(stage) + '_' + str(branch)
    relu_name_base = 'relu' + str(stage) + '_' + str(branch)
    '''
    def bottleneck_block(x, expand=64, squeeze=16):
        m = Conv2D(expand, (1,1))(x)
        m = BatchNormalization()(m)
        m = Activation('relu6')(m)
        m = DepthwiseConv2D((3,3))(m)
        m = BatchNormalization()(m)
        m = Activation('relu6')(m)
        m = Conv2D(squeeze, (1,1))(m)
        m = BatchNormalization()(m)
        return Add()([m, x])
    '''

    # 1x1 Convolution (Bottleneck layer)
    tchannel = K.int_shape(x)[concat_axis] * t
    #inter_channel = nb_filter * 4
    x = Convolution2D(tchannel, 1, 1, name=conv_name_base + '_x1',
                      bias=False)(x)
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name=conv_name_base + '_x1_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_x1_scale')(x)
    x = ReLU(6., name=relu_name_base + '_x1')(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    # 3x3 Convolution
    x = ZeroPadding2D((1, 1), name=conv_name_base + '_x2_zeropadding')(x)
    x = Convolution2D(nb_filter, 3, 3, name=conv_name_base + '_x2',
                      bias=False)(x)
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name=conv_name_base + '_x2_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_x2_scale')(x)
    x = ReLU(6., name=relu_name_base + '_x2')(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    # 1x1 Convolution (Bottleneck layer)
    inter_channel = nb_filter * 4
    x = Convolution2D(inter_channel,
                      1,
                      1,
                      name=conv_name_base + '_x3',
                      bias=False)(x)
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name=conv_name_base + '_x3_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_x3_scale')(x)
    #x = Activation('relu6', name=relu_name_base+'_x3')(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    return x