Example #1
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 = Activation('relu', name=relu_name_base+'_x1')(x)
    x = Conv2D(inter_channel, (1, 1), 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 = Activation('relu', name=relu_name_base+'_x2')(x)
    x = ZeroPadding2D((1, 1), name=conv_name_base+'_x2_zeropadding')(x)
    x = Conv2D(nb_filter, (3, 3), name=conv_name_base+'_x2', use_bias=False)(x)

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

    return x
Example #2
0
def transition_block3d(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 = '3dconv' + str(stage) + '_blk'
    relu_name_base = '3drelu' + str(stage) + '_blk'
    pool_name_base = '3dpool' + str(stage)

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

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

    x = AveragePooling3D((2, 2, 1), strides=(2, 2, 1), name=pool_name_base)(x)

    return x
def transition_block(x, stage, nb_filter, compression=1.0, dropout_rate=None):
    """ 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 = 'convolution_' + str(stage) + '_blk'
    relu_name_base = 'ReLU_' + str(stage) + '_blk'
    pool_name_base = 'pooling_' + str(stage)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           momentum=1,
                           name=conv_name_base + '_batch_normalization',
                           trainable=False)(x, training=False)
    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,
               trainable=True)(x)

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

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

    return x
def conv_block3d(x, stage, branch, nb_filter, dropout_rate=None):
    """Apply BatchNorm, Relu, bottleneck 1x1 Conv3D, 3x3 Conv3D, 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 = '3D_convolution_' + str(stage) + '_' + str(branch)
    relu_name_base = '3D_ReLU_' + str(stage) + '_' + str(branch)

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

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

    x = BatchNormalization(epsilon=eps,
                           axis=4,
                           name=conv_name_base + '_x2_batch_normalization',
                           momentum=1.0,
                           trainable=False)(x, training=False)
    x = Scale(axis=4, 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_zero_padding')(x)
    x = Conv3D(nb_filter, (3, 3, 3),
               name=conv_name_base + '_x2',
               use_bias=False)(x)

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

    return x
Example #5
0
def DenseUNet(img_input,
              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
    concat_axis = 3

    # 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,
               trainable=False)(x)
    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           momentum=1,
                           name='conv1_bn',
                           trainable=False)(x, training=False)
    x = Scale(axis=concat_axis, name='conv1_scale', trainable=False)(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,
                           momentum=1,
                           name='conv' + str(final_stage) + '_blk_bn',
                           trainable=False)(x, training=False)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale',
              trainable=False)(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)
    box.append(x)

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

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

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

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

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

    x = Conv2D(3, (1, 1),
               padding="same",
               name='dense167classifer',
               trainable=False)(ac_up4)

    return ac_up4, x
Example #6
0
def DenseNet3D(img_input,
               nb_dense_block=4,
               growth_rate=32,
               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

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 96
    nb_layers = [3, 4, 12, 8]  # For DenseNet-161
    box = []
    # Initial convolution
    x = ZeroPadding3D((3, 3, 3), name='3dconv1_zeropadding')(img_input)
    x = Conv3D(nb_filter, (7, 7, 7),
               strides=(2, 2, 2),
               name='3dconv1',
               use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=4, name='3dconv1_bn')(x)
    x = Scale(axis=4, name='3dconv1_scale')(x)
    x = Activation('relu', name='3drelu1')(x)
    box.append(x)
    x = ZeroPadding3D((1, 1, 1), name='3dpool1_zeropadding')(x)
    x = MaxPooling3D((3, 3, 3), strides=(2, 2, 2), name='3dpool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block3d(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_block3d(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_block3d(x,
                                 final_stage,
                                 nb_layers[-1],
                                 nb_filter,
                                 growth_rate,
                                 dropout_rate=dropout_rate,
                                 weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=4,
                           name='3dconv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=4, name='3dconv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='3drelu' + str(final_stage) + '_blk')(x)
    box.append(x)
    # print (box)
    up0 = UpSampling3D(size=(2, 2, 1))(x)
    # line0 = Conv3D(504, (1, 1, 1), padding="same", name="3dline0")(box[3])
    # up0_sum = add([line0, up0])
    conv_up0 = Conv3D(504, (3, 3, 3), padding="same", name="3dconv_up0")(up0)
    bn_up0 = BatchNormalization(name="3dbn_up0")(conv_up0)
    ac_up0 = Activation('relu', name='3dac_up0')(bn_up0)

    up1 = UpSampling3D(size=(2, 2, 1))(ac_up0)
    # up1_sum = add([box[2], up1])
    conv_up1 = Conv3D(224, (3, 3, 3), padding="same", name="3dconv_up1")(up1)
    bn_up1 = BatchNormalization(name="3dbn_up1")(conv_up1)
    ac_up1 = Activation('relu', name='3dac_up1')(bn_up1)

    up2 = UpSampling3D(size=(2, 2, 1))(ac_up1)
    # up2_sum = add([box[1], up2])
    conv_up2 = Conv3D(192, (3, 3, 3), padding="same", name="3dconv_up2")(up2)
    bn_up2 = BatchNormalization(name="3dbn_up2")(conv_up2)
    ac_up2 = Activation('relu', name='3dac_up2')(bn_up2)

    up3 = UpSampling3D(size=(2, 2, 2))(ac_up2)
    # up3_sum = add([box[0], up3])
    conv_up3 = Conv3D(96, (3, 3, 3), padding="same", name="3dconv_up3")(up3)
    bn_up3 = BatchNormalization(name="3dbn_up3")(conv_up3)
    ac_up3 = Activation('relu', name='3dac_up3')(bn_up3)

    up4 = UpSampling3D(size=(2, 2, 2))(ac_up3)
    conv_up4 = Conv3D(64, (3, 3, 3), padding="same", name="3dconv_up4")(up4)
    bn_up4 = BatchNormalization(name="3dbn_up4")(conv_up4)
    ac_up4 = Activation('relu', name='3dac_up4')(bn_up4)

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

    return ac_up4, x
Example #7
0
def DenseUNet(nb_dense_block=4,
              growth_rate=48,
              nb_filter=96,
              reduction=0.0,
              dropout_rate=0.0,
              weight_decay=1e-4,
              weights_path=None,
              args=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=(args.b, args.input_size,
                                       args.input_size, 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')

    return model
Example #8
0
def DenseNet3D(img_input, nb_dense_block=4, growth_rate=32, 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

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 96
    nb_layers = [3, 4, 12, 8]  # For DenseNet-161
    box = []
    # Initial convolution
    print("Input: " + str(img_input.shape))
    x = ZeroPadding3D((3, 3, 3), name='3dconv1_zeropadding')(img_input)
    x = Conv3D(nb_filter, (7, 7, 7), strides=(2, 2, 2), name='3dconv1', use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=4, name='3dconv1_bn')(x)
    x = Scale(axis=4, name='3dconv1_scale')(x)
    x = Activation('relu', name='3drelu1')(x)
    box.append(x)
    print("Conv1: " + str(x.shape))

    x = ZeroPadding3D((1, 1, 1), name='3dpool1_zeropadding')(x)
    x = MaxPooling3D((3, 3, 3), strides=(2, 2, 2), name='3dpool1')(x)
    print("Pooling: " + str(x.shape))

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block3d(x, stage, nb_layers[block_idx], nb_filter, growth_rate, dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)
        print("Dense Block " + str(block_idx + 1) + ": " + str(x.shape))
        box.append(x)
        # Add transition_block
        x = transition_block3d(x, stage, nb_filter, compression=compression, dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)
        print("Transition " + str(block_idx + 1) + ": " + str(x.shape))

    final_stage = stage + 1

    # The `latent space block`, i.e., the final dense block before upsampling
    latent_space, nb_filter = dense_block3d(x, final_stage, nb_layers[-1], nb_filter, growth_rate, dropout_rate=dropout_rate,
                                 weight_decay=weight_decay)

    print("Dense Block 4: " + str(latent_space.shape))
    x = BatchNormalization(epsilon=eps, axis=4, name='3dconv' + str(final_stage) + '_blk_bn')(latent_space)
    x = Scale(axis=4, name='3dconv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='3drelu' + str(final_stage) + '_blk')(x)

    # First upsampling layer
    up0 = UpSampling3D(size=(2, 2, 1))(x)
    conv_up0 = Conv3D(504, (3, 3, 3), padding="same", name="3dconv_up0")(up0)
    bn_up0 = BatchNormalization(name="3dbn_up0")(conv_up0)
    ac_up0 = Activation('relu', name='3dac_up0')(bn_up0)
    print("Upsampling 1: " + str(ac_up0.shape))

    up1 = UpSampling3D(size=(2, 2, 1))(ac_up0)
    conv_up1 = Conv3D(224, (3, 3, 3), padding="same", name="3dconv_up1")(up1)
    bn_up1 = BatchNormalization(name="3dbn_up1")(conv_up1)
    ac_up1 = Activation('relu', name='3dac_up1')(bn_up1)
    print("Upsampling 2: " + str(ac_up1.shape))

    up2 = UpSampling3D(size=(2, 2, 1))(ac_up1)
    conv_up2 = Conv3D(192, (3, 3, 3), padding="same", name="3dconv_up2")(up2)
    bn_up2 = BatchNormalization(name="3dbn_up2")(conv_up2)
    ac_up2 = Activation('relu', name='3dac_up2')(bn_up2)
    print("Upsampling 3: " + str(ac_up2.shape))

    up3 = UpSampling3D(size=(2, 2, 2))(ac_up2)
    conv_up3 = Conv3D(96, (3, 3, 3), padding="same", name="3dconv_up3")(up3)
    bn_up3 = BatchNormalization(name="3dbn_up3")(conv_up3)
    ac_up3 = Activation('relu', name='3dac_up3')(bn_up3)
    print("Upsampling 4: " + str(ac_up3.shape))

    up4 = UpSampling3D(size=(2, 2, 2))(ac_up3)
    conv_up4 = Conv3D(64, (3, 3, 3), padding="same", name="3dconv_up4")(up4)
    bn_up4 = BatchNormalization(name="3dbn_up4")(conv_up4)
    ac_up4 = Activation('relu', name='3dac_up4')(bn_up4)
    print("Upsampling 5: " + str(ac_up4.shape))

    x = Conv3D(3, (1, 1, 1), padding="same", name='3dclassifer')(ac_up4)
    print("Conv2: " + str(x.shape))

    return ac_up4, x, latent_space
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)
        # Add transition_block
        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
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]
    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=True)(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')(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)

        box.append(x)

        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')(x)
    x = Activation('relu', name='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=True)(up0)
    bn_up0 = BatchNormalization(name="batch_normalization_upsampling_0",
                                momentum=1,
                                trainable=False)(conv_up0, training=False)
    ac_up0 = Activation('relu', name='ReLU_upsampling_0')(bn_up0)

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

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

    up3 = UpSampling2D(size=(2, 2))(ac_up2)
    conv_up3 = Conv2D(96, (3, 3),
                      padding="same",
                      name="upsampling_3",
                      trainable=True)(up3)
    bn_up3 = BatchNormalization(name="batch_normalization_upsampling_3",
                                momentum=1,
                                trainable=False)(conv_up3, training=False)
    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",
                      name="upsampling_4",
                      trainable=True)(up4)
    bn_up4 = BatchNormalization(name="batch_normalization_upsampling_4",
                                momentum=1,
                                trainable=False)(conv_up4, training=False)
    ac_up4 = Activation('relu', name='ReLU_upsampling_4')(bn_up4)

    x = Conv2D(3, (1, 1),
               padding="same",
               name='2D-DenseUNet-167_classifier',
               trainable=True)(ac_up4)

    return ac_up4, x