def deconv3d(layer_input, skip_input, filters, axis=-1, se_res_block=True, se_ratio=16):
     u1 = ZeroPadding3D(((0, 1), (0, 1), (0, 1)))(layer_input)
     u1 = Conv3DTranspose(filters, (2, 2, 2), strides=(2, 2, 2), use_bias=False, padding='same')(u1)
     u1 = InstanceNormalization(axis=axis)(u1)
     u1 = LeakyReLU(alpha=0.3)(u1)
     u1 = CropToConcat3D()([u1, skip_input])
     u2 = Conv3D(filters, (3, 3, 3), use_bias=False, padding='same')(u1)
     u2 = InstanceNormalization(axis=axis)(u2)
     u2 = LeakyReLU(alpha=0.3)(u2)
     u2 = Conv3D(filters, (3, 3, 3), use_bias=False, padding='same')(u2)
     u2 = InstanceNormalization(axis=axis)(u2)
     if se_res_block == True:
         se = GlobalAveragePooling3D()(u2)
         se = Dense(filters // se_ratio, activation='relu')(se)
         se = Dense(filters, activation='sigmoid')(se)
         se = Reshape([1, 1, 1, filters])(se)
         u2 = Multiply()([u2, se])
         shortcut = Conv3D(filters, (3, 3, 3), use_bias=False, padding='same')(u1)
         shortcut = InstanceNormalization(axis=axis)(shortcut)
         u2 = layers.add([u2, shortcut])
     u2 = LeakyReLU(alpha=0.3)(u2)
     return u2
Example #2
0
def channel_att(input_feature, ratio=8):
    print(K.image_data_format())
    channel_axis = 1 if K.image_data_format() == "channels_first" else -1
    channel = input_feature._keras_shape[channel_axis]

    shared_layer_one = Dense(channel // ratio,
                             activation='relu',
                             kernel_initializer='he_normal',
                             use_bias=True,
                             bias_initializer='zeros')
    shared_layer_two = Dense(channel,
                             kernel_initializer='he_normal',
                             use_bias=True,
                             bias_initializer='zeros')

    avg_pool = GlobalAveragePooling3D()(input_feature)
    avg_pool = Reshape((1, 1, 1, channel))(avg_pool)
    assert avg_pool._keras_shape[1:] == (1, 1, 1, channel)
    avg_pool = shared_layer_one(avg_pool)
    assert avg_pool._keras_shape[1:] == (1, 1, 1, channel // ratio)
    avg_pool = shared_layer_two(avg_pool)
    assert avg_pool._keras_shape[1:] == (1, 1, 1, channel)

    max_pool = GlobalMaxPooling3D()(input_feature)
    max_pool = Reshape((1, 1, 1, channel))(max_pool)
    assert max_pool._keras_shape[1:] == (1, 1, 1, channel)
    max_pool = shared_layer_one(max_pool)
    assert max_pool._keras_shape[1:] == (1, 1, 1, channel // ratio)
    max_pool = shared_layer_two(max_pool)
    assert max_pool._keras_shape[1:] == (1, 1, 1, channel)

    cbam_feature = Add()([avg_pool, max_pool])
    cbam_feature = Activation('sigmoid')(cbam_feature)

    if K.image_data_format() == "channels_first":
        cbam_feature = Permute((3, 1, 2))(cbam_feature)

    return multiply([input_feature, cbam_feature])
Example #3
0
    def deconv3d(layer_input, skip_input, filters, axis=-1, se_res_block=True, se_ratio=16, atten_gate=False):
        if atten_gate == True:
            gating = Conv3D(filters, (1, 1, 1), use_bias=False, padding='same')(layer_input)
            gating = InstanceNormalization(axis=axis)(gating)
            attention = Conv3D(filters, (2, 2, 2), strides=(2, 2, 2), use_bias=False, padding='valid')(skip_input)
            attention = InstanceNormalization(axis=axis)(attention)
            attention = add([gating, attention])
            attention = Conv3D(1, (1, 1, 1), use_bias=False, padding='same', activation='sigmoid')(attention)
            # attention = Lambda(resize_by_axis, arguments={'dim_1':skip_input.get_shape().as_list()[1],'dim_2':skip_input.get_shape().as_list()[2],'ax':3})(attention) # error when None dimension is feeded.
            # attention = Lambda(resize_by_axis, arguments={'dim_1':skip_input.get_shape().as_list()[1],'dim_2':skip_input.get_shape().as_list()[3],'ax':2})(attention)
            attention = ZeroPadding3D(((0, 1), (0, 1), (0, 1)))(attention)
            attention = UpSampling3D((2, 2, 2))(attention)
            attention = CropToConcat3D(mode='crop')([attention, skip_input])
            attention = Lambda(lambda x: K.tile(x, [1, 1, 1, 1, filters]))(attention)
            skip_input = multiply([skip_input, attention])

        u1 = ZeroPadding3D(((0, 1), (0, 1), (0, 1)))(layer_input)
        u1 = Conv3DTranspose(filters, (2, 2, 2), strides=(2, 2, 2), use_bias=False, padding='same')(u1)
        u1 = InstanceNormalization(axis=axis)(u1)
        u1 = LeakyReLU(alpha=0.3)(u1)
        u1 = CropToConcat3D()([u1, skip_input])
        u2 = Conv3D(filters, (3, 3, 3), use_bias=False, padding='same')(u1)
        u2 = InstanceNormalization(axis=axis)(u2)
        u2 = LeakyReLU(alpha=0.3)(u2)
        u2 = Conv3D(filters, (3, 3, 3), use_bias=False, padding='same')(u2)
        u2 = InstanceNormalization(axis=axis)(u2)
        if se_res_block == True:
            se = GlobalAveragePooling3D()(u2)
            se = Dense(filters // se_ratio, activation='relu')(se)
            se = Dense(filters, activation='sigmoid')(se)
            se = Reshape([1, 1, 1, filters])(se)
            u2 = Multiply()([u2, se])
            shortcut = Conv3D(filters, (3, 3, 3), use_bias=False, padding='same')(u1)
            shortcut = InstanceNormalization(axis=axis)(shortcut)
            u2 = add([u2, shortcut])
        u2 = LeakyReLU(alpha=0.3)(u2)
        return u2
def Slow_body(x, lateral, layers, block):
    slow_inplanes = 64 + 64 // 8 * 2
    x = Conv_BN_ReLU(64, kernel_size=(1, 7, 7), strides=(1, 2, 2))(x)
    x = MaxPool3D(pool_size=(1, 3, 3), strides=(1, 2, 2), padding='same')(x)
    x = Concatenate()([x, lateral[0]])
    x, slow_inplanes = make_layer_slow(x,
                                       block,
                                       64,
                                       layers[0],
                                       head_conv=1,
                                       slow_inplanes=slow_inplanes)
    x = Concatenate()([x, lateral[1]])
    x, slow_inplanes = make_layer_slow(x,
                                       block,
                                       128,
                                       layers[1],
                                       stride=2,
                                       head_conv=1,
                                       slow_inplanes=slow_inplanes)
    x = Concatenate()([x, lateral[2]])
    x, slow_inplanes = make_layer_slow(x,
                                       block,
                                       256,
                                       layers[2],
                                       stride=2,
                                       head_conv=1,
                                       slow_inplanes=slow_inplanes)
    x = Concatenate()([x, lateral[3]])
    x, slow_inplanes = make_layer_slow(x,
                                       block,
                                       512,
                                       layers[3],
                                       stride=2,
                                       head_conv=1,
                                       slow_inplanes=slow_inplanes)
    x = GlobalAveragePooling3D()(x)
    return x
Example #5
0
def NonLocal_SeNet_Block(x_input, out_dims, mode=None, compression=2, reduction_ratio=4):
    residual_abs = Lambda(abs_backend, name="abs_non" + str(out_dims))(x_input)

    x = Conv3D(out_dims, (1, 1, 1), padding='same', use_bias=False,
               kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(x_input)

    # NonLocal
    x_non_local = non_local_block(x, mode='embedded', compression=2)

    # SeNet
    abs_mean = GlobalAveragePooling3D()(x_non_local)

    # scales = Dense(units=out_dims // reduction_ratio, activation=None, kernel_initializer='he_normal',
    #                kernel_regularizer=l2(1e-4))(abs_mean)
    # scales = Activation('relu')(scales)
    # scales = Dense(units=out_dims)(scales)
    # scales = Activation('sigmoid')(scales)
    # scales = Reshape((1, 1, 1, out_dims))(scales)

    scales = Reshape((1, 1, 1, out_dims))(abs_mean)
    scales = Conv3D(filters=out_dims // reduction_ratio, kernel_size=1,
                    use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(scales)
    scales = Activation('relu')(scales)
    scales = Conv3D(filters=out_dims, kernel_size=1,
                    use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(scales)
    scales = Activation('sigmoid')(scales)

    thres = multiply([x, scales])

    # Soft thresholding
    sub = keras.layers.subtract([residual_abs, thres])
    zeros = keras.layers.subtract([sub, sub])
    n_sub = keras.layers.maximum([sub, zeros])
    residual = keras.layers.multiply([Lambda(sign_backend, name="sign_non" + str(out_dims))(x_input), n_sub])

    return residual
Example #6
0
    def squeeze_and_excite_3d(model, ratio=16):
        initial = model
        number_of_filters = K.int_shape(initial)[1]
        if K.image_data_format() == "channels_last":
            number_of_filters = K.int_shape(initial)[-1]

        block_shape = (1, 1, 1, number_of_filters)

        block = GlobalAveragePooling3D()(initial)
        block = Reshape(target_shape=block_shape)(block)
        block = Dense(units=number_of_filters//ratio,
                      activation='relu',
                      kernel_initializer='he_normal',
                      use_bias=False)(block)
        block = Dense(units=number_of_filters,
                      activation='sigmoid',
                      kernel_initializer='he_normal',
                      use_bias=False)(block)

        if K.image_data_format() == "channels_first":
            block = Permute((4, 1, 2, 3))(block)

        x = Multiply()([initial, block])
        return(x)
Example #7
0
def squeeze_excite_block(input_tensor, ratio=16):
    """ Create a channel-wise squeeze-excite block

    Args:
        input_tensor: input Keras tensor
        ratio: number of output filters

    Returns: a Keras tensor

    References
    -   [Squeeze and Excitation Networks](https://arxiv.org/abs/1709.01507)
    """
    init = input_tensor
    channel_axis = 1 if K.image_data_format() == "channels_first" else -1
    filters = _tensor_shape(init)[channel_axis]
    se_shape = (1, 1, 1, filters)

    se = GlobalAveragePooling3D()(init)
    se = Reshape(se_shape)(se)
    se = Dense(filters // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False)(se)
    se = Dense(filters, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se)

    x = multiply([init, se])
    return x
Example #8
0
def testnet_model(input_shape=(4, 128, 128, 128),
                  optimizer=Adam,
                  initial_learning_rate=5e-4,
                  activation_name="sigmoid",
                  **kwargs):

    inputs_1 = Input(input_shape)
    #inputs_2 = Input(input_shape)

    #pool_1 = testnet_backbone(inputs_1, n_base_filters)
    #pool_2 = testnet_backbone(inputs_2, n_base_filters)
    #sf_add = concatenate([pool_1, pool_2], axis=1)

    sf_return = siam3dunet_backbone(inputs_1, mask_name='mask1', **kwargs)
    sf_add = GlobalAveragePooling3D()(sf_return)

    out_pred_score = Dense(1, activation=None)(sf_add)
    out_pred_score = Lambda(print_output,
                            arguments={'msg': ' output'})(out_pred_score)
    out_pred_score = Activation(activation_name)(out_pred_score)
    out_pred_score = Lambda(print_output,
                            arguments={'msg': ' output sigmoid'},
                            name='score')(out_pred_score)

    print(initial_learning_rate)
    print(activation_name)

    model = Model(inputs=inputs_1, outputs=out_pred_score)
    #model = Model(inputs=[inputs_1, inputs_2], outputs=out_pred_score)
    model.compile(optimizer=SGD(lr=initial_learning_rate, momentum=0.9),
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
    #model.compile(optimizer=optimizer(lr=initial_learning_rate), loss={'score':'binary_crossentropy'}, metrics=['accuracy'])

    #model.metrics_tensors += model.outputs
    return model
def unet_model_3d(first_input_shape, second_input_shape, nb_classes,
                  feature_size):

    channel_first_first_input = Input(first_input_shape)
    first_input = Permute([2, 3, 4, 1])(channel_first_first_input)

    first_conv_permute = Permute([4, 2, 3, 1])(first_input)
    first_gpooling_0 = GlobalAveragePooling3D()(first_conv_permute)
    first_gpooling_dense_0 = Dense(units=32,
                                   activation='linear')(first_gpooling_0)
    first_gpooling_dense_1_1 = Dense(
        units=29, activation='sigmoid')(first_gpooling_dense_0)
    first_gpooling_fused_2 = Lambda(fuse)(
        [first_conv_permute, first_gpooling_dense_1_1])

    first_conv_layer0 = Conv3D(8, (5, 5, 5),
                               padding='same',
                               activation='linear')(first_input)

    first_conv_permute = Permute([4, 2, 3, 1])(first_conv_layer0)
    first_gpooling_0 = GlobalAveragePooling3D()(first_conv_permute)
    first_gpooling_dense_0 = Dense(units=32,
                                   activation='linear')(first_gpooling_0)
    first_gpooling_dense_1_0 = Dense(
        units=29, activation='sigmoid')(first_gpooling_dense_0)
    first_gpooling_fused_0 = Lambda(fuse)(
        [first_conv_permute, first_gpooling_dense_1_0])

    first_conv_layer1 = Conv3D(8, (3, 3, 3),
                               padding='same',
                               activation='linear')(first_conv_layer0)

    first_conv_permute = Permute([4, 2, 3, 1])(first_conv_layer1)
    first_gpooling_0 = GlobalAveragePooling3D()(first_conv_permute)
    first_gpooling_dense_0 = Dense(units=32,
                                   activation='linear')(first_gpooling_0)
    first_gpooling_dense_1_1 = Dense(
        units=29, activation='sigmoid')(first_gpooling_dense_0)
    first_gpooling_fused_1 = Lambda(fuse)(
        [first_conv_permute, first_gpooling_dense_1_1])

    first_gpooling_add_0 = Add()([
        first_gpooling_fused_0, first_gpooling_fused_1, first_gpooling_fused_2
    ])

    first_conv_layer2 = Conv2D(16, (3, 3), padding='same',
                               activation='linear')(first_gpooling_add_0)
    first_pooling_layer1 = MaxPooling2D(pool_size=(2, 2))(first_conv_layer2)

    first_conv_layer3 = Conv2D(16, (3, 3), padding='same',
                               activation='linear')(first_pooling_layer1)
    first_pooling_layer2 = MaxPooling2D(pool_size=(2, 2))(first_conv_layer3)

    first_conv_layer4 = Conv2D(16, (3, 3), padding='same',
                               activation='linear')(first_pooling_layer2)
    first_pooling_layer3 = MaxPooling2D(pool_size=(2, 2),
                                        padding='same')(first_conv_layer4)

    first_flatten_layer1 = Flatten()(first_pooling_layer3)
    first_dense_layer1 = Dense(units=feature_size,
                               activation='relu')(first_flatten_layer1)
    first_dense_layer2 = Dense(units=feature_size,
                               activation='relu')(first_dense_layer1)

    base_model = Xception(weights='imagenet',
                          include_top=False,
                          input_shape=[128, 128, 3])
    second_input = base_model.input
    block_14_sp_act_output = base_model.output
    gpooling = GlobalAveragePooling2D()(block_14_sp_act_output)

    concat_layer = concatenate([first_dense_layer2, gpooling], axis=1)

    input_target = Input(shape=(1, ))
    centers = Embedding(nb_classes, feature_size * 3)(input_target)
    l2_loss = Lambda(center_loss, name='l2_loss')([concat_layer, centers])

    concat_result = Dense(units=nb_classes,
                          activation='softmax',
                          name='softmax')(concat_layer)
    concat_model = Model(
        inputs=[channel_first_first_input, second_input, input_target],
        outputs=[concat_result, l2_loss])
    concat_test_model = Model(inputs=[channel_first_first_input, second_input],
                              outputs=concat_result)

    return concat_model, concat_test_model
Example #10
0
    def build(self, plot=False):
        inputs = Input(shape=self.shape)

        x = self._conv_block(inputs, 16, (3, 3, 3), strides=(2, 2, 2), nl='HS')

        x = self._bottleneck(x,
                             16, (3, 3, 3),
                             e=16,
                             s=1,
                             squeeze=False,
                             nl='RE')
        x = self._bottleneck(x,
                             24, (3, 3, 3),
                             e=64,
                             s=2,
                             squeeze=False,
                             nl='RE')
        x = self._bottleneck(x,
                             24, (3, 3, 3),
                             e=72,
                             s=1,
                             squeeze=False,
                             nl='RE   ')
        x = self._bottleneck(x,
                             40, (5, 5, 5),
                             e=72,
                             s=2,
                             squeeze=True,
                             nl='RE')
        x = self._bottleneck(x,
                             40, (5, 5, 5),
                             e=120,
                             s=1,
                             squeeze=True,
                             nl='RE')
        x = self._bottleneck(x,
                             40, (5, 5, 5),
                             e=120,
                             s=1,
                             squeeze=True,
                             nl='RE')
        x = self._bottleneck(x,
                             80, (3, 3, 3),
                             e=240,
                             s=2,
                             squeeze=False,
                             nl='HS')
        x = self._bottleneck(x,
                             80, (3, 3, 3),
                             e=200,
                             s=1,
                             squeeze=False,
                             nl='HS')
        x = self._bottleneck(x,
                             80, (3, 3, 3),
                             e=184,
                             s=1,
                             squeeze=False,
                             nl='HS')
        x = self._bottleneck(x,
                             80, (3, 3, 3),
                             e=184,
                             s=1,
                             squeeze=False,
                             nl='HS')
        x = self._bottleneck(x,
                             112, (3, 3, 3),
                             e=480,
                             s=1,
                             squeeze=True,
                             nl='HS')
        x = self._bottleneck(x,
                             112, (3, 3, 3),
                             e=672,
                             s=1,
                             squeeze=True,
                             nl='HS')
        x = self._bottleneck(x,
                             160, (5, 5, 5),
                             e=672,
                             s=2,
                             squeeze=True,
                             nl='HS')
        x = self._bottleneck(x,
                             160, (5, 5, 5),
                             e=960,
                             s=1,
                             squeeze=True,
                             nl='HS')
        x = self._bottleneck(x,
                             160, (5, 5, 5),
                             e=960,
                             s=1,
                             squeeze=True,
                             nl='HS')

        x = self._conv_block(x, 960, (1, 1, 1), strides=(1, 1, 1), nl='HS')
        x = GlobalAveragePooling3D()(x)
        x = Reshape((1, 1, 1, 960))(x)

        x = Conv3D(1280, (1, 1, 1), padding='same')(x)
        x = self._return_activation(x, 'HS')

        if self.include_top:
            if self.n_class == 1:
                x = Conv3D(self.n_class, (1, 1, 1),
                           padding='same',
                           activation='sigmoid')(x)
                x = Reshape((self.n_class, ))(x)
            else:
                x = Conv3D(self.n_class, (1, 1, 1),
                           padding='same',
                           activation='softmax')(x)
                x = Reshape((self.n_class, ))(x)

        model = Model(inputs, x)

        if plot:
            plot_model(model,
                       to_file='images/MobileNetv3_large.png',
                       show_shapes=True)

        return model
def dual_path_net(initial_conv_filters,
                  filter_increment,
                  depth,
                  cardinality,
                  width,
                  pooling='max-avg',
                  bias_flag=False):
    '''
    Args:
        initial_conv_filters: number of features for the initial convolution  初始化输出的张量通道数
        include_top: Flag to include the last dense layer
        initial_conv_filters: number of features for the initial convolution
        filter_increment: number of filters incremented per block, defined as a list.
            DPN-92  = [16, 32, 24, 128]
            DON-98  = [16, 32, 32, 128]
            DPN-131 = [16, 32, 32, 128]
            DPN-107 = [20, 64, 64, 128]
        depth: number or layers in the each block, defined as a list.
            DPN-92  = [3, 4, 20, 3]
            DPN-98  = [3, 6, 20, 3]
            DPN-131 = [4, 8, 28, 3]
            DPN-107 = [4, 8, 20, 3]
        width: width multiplier for network 分组卷积每组的卷积核数量,所以也就是直接指定每组包括多少卷积核和组数即可,不过确实有点绕,因为这样做则要求dpn block函数中的参数pointwise_filters_a和grouped_conv_filters_b相同
        pooling: Optional pooling mode for feature extraction
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
            - `max-avg` means that both global average and global max
                pooling will be applied to the output of the last
                convolution layer
    Returns: a Keras Model
    '''
    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1  #
    N = list(depth)  #
    base_filters = 256  #

    # input set
    img_input = Input(shape=(280, 280, 16, 1))  #
    # block 1 (initial conv block)
    x = _initial_conv_block_inception(img_input,
                                      initial_conv_filters,
                                      bias_flag=bias_flag)  #
    print('BLOCK 1 init shape :', x.shape)  #
    # block 2 (projection block)
    filter_inc = filter_increment[0]  #
    # filter_increment: number of filters incremented per block, defined as a list.
    # DPN-92  = [16, 32, 24, 128]
    filters = int(cardinality * width)  #

    x = _dual_path_block(x,
                         pointwise_filters_a=filters,
                         grouped_conv_filters_b=filters,
                         pointwise_filters_c=base_filters,
                         filter_increment=filter_inc,
                         cardinality=cardinality,
                         block_type='projection',
                         bias_flag=bias_flag)  #

    for i in range(N[0] - 1):
        x = _dual_path_block(x,
                             pointwise_filters_a=filters,
                             grouped_conv_filters_b=filters,
                             pointwise_filters_c=base_filters,
                             filter_increment=filter_inc,
                             cardinality=cardinality,
                             block_type='normal',
                             bias_flag=bias_flag)  #

    print("BLOCK 1 out shape : res_path:", x[0].shape, " vs.  dense_path",
          x[1].shape)  #
    # remaining blocks
    for k in range(1, len(N)):

        filter_inc = filter_increment[k]  #
        filters *= 2  # 进入到下一个大的Block(注意不是dpn block),filters(等于分组卷积的通道数)也要翻倍
        base_filters *= 2  # 这个参数相当于把densepath的通道数改变一下,有点过度模块的意思,因此这个参数要不断增加,因为原始dense net的过度模块也是随着网络深入而逐渐卷积核变多的

        x = _dual_path_block(x,
                             pointwise_filters_a=filters,
                             grouped_conv_filters_b=filters,
                             pointwise_filters_c=base_filters,
                             filter_increment=filter_inc,
                             cardinality=cardinality,
                             block_type='downsample',
                             bias_flag=bias_flag)  #
        print("BLOCK", (k + 1), "d_sample shape : res_path:", x[0].shape,
              " vs.  dense_path", x[1].shape)  #
        for i in range(N[k] - 1):
            x = _dual_path_block(x,
                                 pointwise_filters_a=filters,
                                 grouped_conv_filters_b=filters,
                                 pointwise_filters_c=base_filters,
                                 filter_increment=filter_inc,
                                 cardinality=cardinality,
                                 block_type='normal',
                                 bias_flag=bias_flag)  #

        print("BLOCK", (k + 1), "out shape : res_path:", x[0].shape,
              " vs.  dense_path", x[1].shape)  #

    x = concatenate(x, axis=channel_axis)  #
    print("CONCAT out shape : ", x.shape)

    if pooling == 'avg':
        x = GlobalAveragePooling3D(data_format='channels_last')(x)
    elif pooling == 'max':
        x = GlobalMaxPooling3D(data_format='channels_last')(x)
    elif pooling == 'max-avg':
        a = GlobalMaxPooling3D(data_format='channels_last')(x)
        b = GlobalAveragePooling3D(data_format='channels_last')(x)
        x = add([a, b])
        x = Lambda(lambda z: 0.5 * z)(x)

    print("GApooling shape:", x.shape)
    out_drop = Dropout(rate=0.3)(x)
    out = Dense(1, name='fc1', use_bias=bias_flag)(out_drop)
    print("out shape:", out.shape)
    output = Activation(activation='sigmoid')(out)

    model = Model(input=img_input, output=output)

    return model
Example #12
0
def resnext(classes=2,use_bias_flag=False):
    inputs = Input(shape=(280, 280, 16, 1), name='input1')
    # 256*256*128
    print("input shape:", inputs.shape)  # (?, 140, 140, 16, 64)
    out = Conv3D(64, 7, strides=(2, 2, 1), padding='same', kernel_initializer='he_normal', use_bias=False, name='conv1')(inputs)
    print("conv0 shape:", out.shape)#(?, 140, 140, 16, 64)
    out = BatchNormalization(axis = -1, epsilon=1e-6, name='bn1')(out)
    out = Activation('relu')(out)
    out = MaxPooling3D((3, 3, 3), strides=(2, 2, 1), padding='same')(out)
    print("pooling1 shape:", out.shape)#(?, 70, 70, 16, 64)

    out = conv_block(out, [64, 64, 256], name='L1_block1')  # 一定是[n,n,Xn]这个形式(因为有分组卷积),X可取任意值,且输出通道数为Xn,另外n最好是32的整数(因为分组卷积是分32组的,当然这个可以自己改)
    print("conv1 shape:", out.shape)
    out = identity_block(out, [64, 64, 256], name='L1_block2')  # 一定是[n,n,a]的形式,a一定要等于上一个conv_block或identity_block的输出通道,identity_block的输入输出通道相同。

    out = identity_block(out, [64, 64, 256], name='L1_block3')

    out = conv_block(out, [128, 128, 512], name='L2_block1')
    print("conv2 shape:", out.shape)
    out = identity_block(out, [128, 128, 512], name='L2_block2')

    out = identity_block(out, [128, 128, 512], name='L2_block3')

    out = identity_block(out, [128, 128, 512], name='L2_block4')

    out = conv_block(out, [256, 256, 1024], name='L3_block1')
    print("conv3 shape:", out.shape)
    out = identity_block(out, [256, 256, 1024], name='L3_block2')
    out = identity_block(out, [256, 256, 1024], name='L3_block3')
    out = identity_block(out, [256, 256, 1024], name='L3_block4')
    out = identity_block(out, [256, 256, 1024], name='L3_block5')
    out = identity_block(out, [256, 256, 1024], name='L3_block6')

    out = conv_block(out, [512, 512, 2048], name='L4_block1')
    print("conv4 shape:", out.shape)
    out = identity_block(out, [512, 512, 2048], name='L4_block2')
    out = identity_block(out, [512, 512, 2048], name='L4_block3')

    out = GlobalAveragePooling3D(data_format = 'channels_last')(out)
    print("Gpooling shape:", out.shape)
    out_drop = Dropout(rate=0.3)(out)



    if classes == 1:
        output = Dense(classes, activation='sigmoid', use_bias=use_bias_flag, name='fc1')(out_drop)
        print("predictions1 shape:", output.shape, 'activition:sigmoid')
    else:
        output = Dense(classes, activation='softmax', use_bias=use_bias_flag, name='fc1')(out_drop)
        print("predictions2 shape:", output.shape, 'activition:softmax')



    #out = Dense(classes, name = 'fc1')(out_drop)
    #print("out shape:", out.shape)
    #out = Dense(1, name = 'fc1')(out)
    #output = Activation(activation = 'sigmoid')(out)

    model = Model(input = inputs, output = output)
    #mean_squared_logarithmic_error or binary_crossentropy
    #model.compile(optimizer=SGD(lr = 1e-6, momentum = 0.9), loss = EuiLoss, metrics= [y_t, y_pre, Acc] )
    return model
Example #13
0
def InceptionTemporal(n_neurons=256,
                      seq_len=3,
                      classes=101,
                      weights='imagenet',
                      dropout=0.5,
                      fine=True,
                      retrain=False,
                      pre_file='',
                      old_epochs=0,
                      cross_index=1):

    if K.image_data_format() == 'channels_first':
        bn_axis = 1
    else:
        bn_axis = 3
    # inception = InceptionV3(input_shape=(299,299,3), pooling='avg', include_top=False, weights=weights)
    inceptionv3a = Inception_temp_v3a(input_shape=(299, 299, 20),
                                      weights=weights)
    inceptionv3b = Inception_v3b(input_shape=inceptionv3a.output_shape,
                                 input_tensor=inceptionv3a.output,
                                 weights=weights)
    inceptionv3c = Inception_v3c(input_shape=inceptionv3b.output_shape,
                                 input_tensor=inceptionv3b.output,
                                 weights=weights)

    input = Input(shape=(seq_len, 299, 299, 20))
    model = TimeDistributed(inceptionv3a)(input)
    loss1 = TimeDistributed(
        AveragePooling2D((5, 5), strides=(3, 3), padding='same'))(model)
    loss1 = TimeDistributed(
        Conv2D(128, (1, 1), strides=(1, 1), padding='same',
               use_bias=False))(loss1)
    loss1 = TimeDistributed(BatchNormalization(axis=bn_axis,
                                               scale=False))(loss1)
    loss1 = TimeDistributed(Activation('relu'))(loss1)
    loss1 = GlobalAveragePooling3D()(loss1)
    loss1 = Dense(classes, activation='softmax')(loss1)

    model = TimeDistributed(inceptionv3b)(model)
    loss2 = TimeDistributed(
        AveragePooling2D((5, 5), strides=(3, 3), padding='same'))(model)
    loss2 = TimeDistributed(
        Conv2D(128, (1, 1), strides=(1, 1), padding='same',
               use_bias=False))(loss2)
    loss2 = TimeDistributed(BatchNormalization(axis=bn_axis,
                                               scale=False))(loss2)
    loss2 = TimeDistributed(Activation('relu'))(loss2)
    loss2 = GlobalAveragePooling3D()(loss2)
    loss2 = Dense(classes, activation='softmax')(loss2)

    model = TimeDistributed(inceptionv3c)(model)
    model = LSTM(n_neurons, return_sequences=True)(model)
    model = Flatten()(model)
    model = Dense(256, activation='relu')(model)
    model = Dropout(dropout)(model)
    model = Dense(classes, activation='softmax')(model)

    result_model = Model(inputs=input,
                         outputs=[loss1, loss2, model],
                         name="InceptionSpatial")

    if retrain:
        print(
            glob.glob('weights/' + pre_file +
                      '-{:02d}*.hdf5'.format(old_epochs))[-1])
        result_model.load_weights(
            glob.glob('weights/' + pre_file +
                      '-{:02d}*.hdf5'.format(old_epochs))[-1])

    return result_model
Example #14
0
def model_architecture():

	input_imgs = Input(shape=(217, 178, 60, 1))

	conv2 = Conv3D(filters=2, kernel_size=(3, 3, 3), kernel_initializer='he_uniform', padding='same') (input_imgs)

	maxPool1 = MaxPool3D(pool_size=(2, 2, 2), strides=(2,2,2)) (conv2)
	
	bn4 = BatchNormalization() (maxPool1)
	act4 = Activation('relu') (bn4)
	conv4 = Conv3D(filters=4, kernel_size=(3, 3, 3), kernel_initializer='he_uniform', padding='same') (act4)

	maxPool2 = MaxPool3D(pool_size=(2, 2, 2), strides=(2,2,2)) (conv4)

	bn8 = BatchNormalization() (maxPool2)
	act8 = Activation('relu') (bn8)
	conv8 = Conv3D(filters=8, kernel_size=(3, 3, 3), kernel_initializer='he_uniform', padding='same') (act8)

	bn16 = BatchNormalization() (conv8)
	act16 = Activation('relu') (bn16)
	conv16 = Conv3D(filters=16, kernel_size=(3, 3, 3), kernel_initializer='he_uniform', padding='same') (act16)

	conv_block16 = id_block(conv16, 16)
	conv_block16 = id_block(conv_block16, 16)
	conv_block16 = id_block(conv_block16, 16)
	conv_block16 = id_block(conv_block16, 16)
	conv_block16 = id_block(conv_block16, 16)
	conv_block16 = id_block(conv_block16, 16)
	conv_block16 = id_block(conv_block16, 16)
	conv_block16 = id_block(conv_block16, 16)

	maxPool3 = MaxPool3D(pool_size=(2, 2, 2), strides=(2,2,2)) (conv_block16)

	bn32 = BatchNormalization() (maxPool3)
	act32 = Activation('relu') (bn32)
	conv32 = Conv3D(filters=32, kernel_size=(3, 3, 3), kernel_initializer='he_uniform', padding='same') (act32)

	bn64 = BatchNormalization() (conv32)
	act64 = Activation('relu') (bn64)
	conv64 = Conv3D(filters=64, kernel_size=(3, 3, 3), kernel_initializer='he_uniform', padding='same') (act64)	

	maxPool4 = MaxPool3D(pool_size=(2, 2, 2), strides=(2,2,2)) (conv64)

	bn128 = BatchNormalization() (maxPool4)
	act128 = Activation('relu') (bn128)
	conv128 = Conv3D(filters=128, kernel_size=(3, 3, 3), kernel_initializer='he_uniform', padding='same') (act128)

	bn256 = BatchNormalization() (conv128)
	act256 = Activation('relu') (bn256)
	conv256 = Conv3D(filters=256, kernel_size=(3, 3, 3), kernel_initializer='he_uniform', padding='same') (act256)

	bn512 = BatchNormalization() (conv256)
	act512 = Activation('relu') (bn512)
	conv512 = Conv3D(filters=512, kernel_size=(3, 3, 3), kernel_initializer='he_uniform', padding='same') (act512)

	conv_block512 = id_block(conv512, 512)
	conv_block512 = id_block(conv_block512, 512)
	conv_block512 = id_block(conv_block512, 512)
	conv_block512 = id_block(conv_block512, 512)
	conv_block512 = id_block(conv_block512, 512)
	conv_block512 = id_block(conv_block512, 512)
	conv_block512 = id_block(conv_block512, 512)
	conv_block512 = id_block(conv_block512, 512)
	conv_block512 = id_block(conv_block512, 512)
	conv_block512 = id_block(conv_block512, 512)
	conv_block512 = id_block(conv_block512, 512)

	bn1024 = BatchNormalization() (conv_block512)
	act1024 = Activation('relu') (bn1024)
	conv1024 = Conv3D(filters=1024, kernel_size=(3, 3, 3), kernel_initializer='he_uniform', padding='same') (act1024)

	conv_block1024 = id_block(conv1024, 1024)
	conv_block1024 = id_block(conv_block1024, 1024)
	conv_block1024 = id_block(conv_block1024, 1024)
	conv_block1024 = id_block(conv_block1024, 1024)
	conv_block1024 = id_block(conv_block1024, 1024)
	conv_block1024 = id_block(conv_block1024, 1024)

	bn2048 = BatchNormalization() (conv_block1024)
	act2048 = Activation('relu') (bn2048)
	conv2048 = Conv3D(filters=2048, kernel_size=(3, 3, 3), kernel_initializer='he_uniform', padding='same') (act2048)

	conv_block2048 = id_block(conv2048, 2048)
	conv_block2048 = id_block(conv_block2048, 2048)
	conv_block2048 = id_block(conv_block2048, 2048)
	conv_block2048 = id_block(conv_block2048, 2048)
	conv_block2048 = id_block(conv_block2048, 2048)
	conv_block2048 = id_block(conv_block2048, 2048)

	conv_block2048 = BatchNormalization() (conv_block2048)
	conv_block2048 = Activation('relu') (conv_block2048)

	globalAP = GlobalAveragePooling3D() (conv_block2048)
	dense1 = Dense(units=256, activation='relu', kernel_initializer='he_uniform') (globalAP)
	dp = Dropout(0.4) (dense1)
	dense2 = Dense(units=1, activation='linear') (dp)

	model = Model(inputs=input_imgs, outputs=dense2)
	return model
Example #15
0
def _create_se_resnet(num_outputs,
                      img_input,
                      include_top,
                      initial_conv_filters,
                      filters,
                      depth,
                      width,
                      bottleneck,
                      weight_decay,
                      pooling,
                      activation="softmax",
                      padding="same"):
    """Creates a SE ResNet model with specified parameters
    Args:
        initial_conv_filters: number of features for the initial convolution
        include_top: Flag to include the last fc layer
        filters: number of filters per block, defined as a list.
            filters = [64, 128, 256, 512
        depth: number or layers in the each block, defined as a list.
            ResNet-50  = [3, 4, 6, 3]
            ResNet-101 = [3, 6, 23, 3]
            ResNet-152 = [3, 8, 36, 3]
        width: width multiplier for network (for Wide ResNet)
        bottleneck: adds a bottleneck conv to reduce computation
        weight_decay: weight_decay (l2 norm)
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 3D tensor.
            - `max` means that global max pooling will
                be applied.
    Returns: a Keras Model
    """
    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
    N = list(depth)

    # block 1 (initial conv block)
    x = Conv3D(initial_conv_filters, (7, 7, 7),
               padding=padding,
               use_bias=False,
               strides=(2, 2, 2),
               kernel_initializer='he_normal',
               kernel_regularizer=l2(weight_decay))(img_input)

    x = MaxPooling3D((3, 3, 3), strides=(2, 2, 2), padding=padding)(x)

    # block 2 (projection block)
    for i in range(N[0]):
        if bottleneck:
            x = _resnet_bottleneck_block(x, filters[0], width)
        else:
            x = _resnet_block(x, filters[0], width)

    # block 3 - N
    for k in range(1, len(N)):
        if bottleneck:
            x = _resnet_bottleneck_block(x,
                                         filters[k],
                                         width,
                                         strides=(2, 2, 2))
        else:
            x = _resnet_block(x, filters[k], width, strides=(2, 2, 2))

        for i in range(N[k] - 1):
            if bottleneck:
                x = _resnet_bottleneck_block(x, filters[k], width)
            else:
                x = _resnet_block(x, filters[k], width)

    x = BatchNormalization(axis=channel_axis)(x)
    x = Activation('relu')(x)

    if include_top:
        x = GlobalAveragePooling3D()(x)
        x = Dense(num_outputs,
                  use_bias=False,
                  kernel_regularizer=l2(weight_decay),
                  activation=activation)(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling3D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling3D()(x)

    return x
def create_resnet_model_3d(input_image_size,
                           number_of_classification_labels=1000,
                           layers=(1, 2, 3, 4),
                           residual_block_schedule=(3, 4, 6, 3),
                           lowest_resolution=64,
                           cardinality=1,
                           mode='classification'
                          ):
    """
    3-D implementation of the ResNet deep learning architecture.

    Creates a keras model of the ResNet deep learning architecture for image
    classification.  The paper is available here:

            https://arxiv.org/abs/1512.03385

    This particular implementation was influenced by the following python
    implementation:

            https://gist.github.com/mjdietzx/0cb95922aac14d446a6530f87b3a04ce

    Arguments
    ---------
    input_image_size : tuple of length 4
        Used for specifying the input tensor shape.  The shape (or dimension) of
        that tensor is the image dimensions followed by the number of channels
        (e.g., red, green, and blue).

    number_of_classification_labels : integer
        Number of classification labels.

    layers : tuple
        A tuple determining the number of 'filters' defined at for each layer.

    residual_block_schedule : tuple
        A tuple defining the how many residual blocks repeats for each layer.

    lowest_resolution : integer
        Number of filters at the initial layer.

    cardinality : integer
        perform ResNet (cardinality = 1) or ResNeX (cardinality does not 1 but,
        instead, powers of 2---try '32').

    mode : string
        'classification' or 'regression'.  Default = 'classification'.

    Returns
    -------
    Keras model
        A 3-D Keras model defining the network.

    Example
    -------
    >>> model = create_resnet_model_3d((128, 128, 128, 1))
    >>> model.summary()
    """

    def add_common_layers(model):
        model = BatchNormalization()(model)
        model = LeakyReLU()(model)
        return(model)

    def grouped_convolution_layer_3d(model, number_of_filters, strides):
        # Per standard ResNet, this is just a 3-D convolution
        if cardinality == 1:
            grouped_model = Conv3D(filters=number_of_filters,
                                   kernel_size=(3, 3, 3),
                                   strides=strides,
                                   padding='same')(model)
            return(grouped_model)

        if number_of_filters % cardinality != 0:
            raise ValueError('number_of_filters `%` cardinality != 0')

        number_of_group_filters = int(number_of_filters / cardinality)

        convolution_layers = []
        for j in range(cardinality):
            local_layer = Lambda(lambda z: z[:, :, :,
              j * number_of_group_filters:j * number_of_group_filters + number_of_group_filters])(model)
            convolution_layers.append(Conv3D(filters=number_of_group_filters,
                                             kernel_size=(3, 3, 3),
                                             strides=strides,
                                             padding='same')(local_layer))

        grouped_model = Concatenate()(convolution_layers)
        return(grouped_model)

    def residual_block_3d(model, number_of_filters_in, number_of_filters_out, strides=(1, 1, 1), project_shortcut=False):
        shortcut = model

        model = Conv3D(filters=number_of_filters_in,
                       kernel_size=(1, 1, 1),
                       strides=(1, 1, 1),
                       padding='same')(model)
        model = add_common_layers(model)

        # ResNeXt (identical to ResNet when `cardinality` == 1)
        model = grouped_convolution_layer_3d(model,
                                             number_of_filters=number_of_filters_in,
                                             strides=strides)
        model = add_common_layers(model)

        model = Conv3D(filters=number_of_filters_out,
                       kernel_size=(1, 1, 1),
                       strides=(1, 1, 1),
                       padding='same')(model)
        model = BatchNormalization()(model)

        if project_shortcut == True or strides != (1,1):
            shortcut = Conv3D(filters=number_of_filters_out,
                              kernel_size=(1, 1, 1),
                              strides=strides,
                              padding='same')(shortcut)
            shortcut = BatchNormalization()(shortcut)

        model = Add()([shortcut, model])
        model = LeakyReLU()(model)
        return(model)


    inputs = Input(shape = input_image_size)

    n_filters = lowest_resolution

    outputs = Conv3D(filters=n_filters,
                     kernel_size=(7, 7, 7),
                     strides=(2, 2, 2),
                     padding='same')(inputs)
    outputs = add_common_layers(outputs)
    outputs = MaxPooling3D(pool_size=(3, 3, 3),
                           strides=(2, 2, 2),
                           padding='same')(outputs)

    for i in range(len(layers)):
        n_filters_in = lowest_resolution * 2**layers[i]
        n_filters_out = 2 * n_filters_in

        for j in range(residual_block_schedule[i]):
            project_shortcut = False
            if i == 0 and j == 0:
                project_shortcut = True

            if i > 0 and j == 0:
                strides = (2, 2, 2)
            else:
                strides = (1, 1, 1)

            outputs = residual_block_3d(outputs,
                                        number_of_filters_in=n_filters_in,
                                        number_of_filters_out=n_filters_out,
                                        strides=strides,
                                        project_shortcut=project_shortcut)


    outputs = GlobalAveragePooling3D()(outputs)

    layer_activation = ''
    if mode == 'classification':
        if number_of_classification_labels == 2:
            layer_activation = 'sigmoid'
        else:
            layer_activation = 'softmax'
    elif mode == 'regression':
        layerActivation = 'linear'
    else:
        raise ValueError('mode must be either `classification` or `regression`.')

    outputs = Dense(units=number_of_classification_labels,
                    activation=layer_activation)(outputs)

    resnet_model = Model(inputs=inputs, outputs=outputs)

    return(resnet_model)
Example #17
0
model.add(Conv3D(256,kernel_size=(3,3,3),strides=(1,1,1)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling3D(pool_size=(2,2,2)))
model.add(Residual(256,(3,3,3)))
model.add(Conv3D(512,kernel_size=(3,3,3),strides=(1,1,1)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling3D(pool_size=(2,2,2)))
model.add(Residual(512,(3,3,3)))
model.add(Conv3D(1024,kernel_size=(3,3,3),strides=(1,1,1)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling3D(pool_size=(2,2,2)))
model.add(Residual(1024,(3,3,3)))
model.add(GlobalAveragePooling3D())
#x=Dropout(0.3)(x)
model.add(BatchNormalization())
model.add(Dense(512))
#x=Dropout(0.3)(x)
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(256))
#x=Dropout(0.3)(x)
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(10, activation='softmax'))

# model = Model(inputs=input_tensor, outputs=output)

# In[14]:
    def build_model(self, input_shape, n_classes):
        # input layer
        input_layer = Input(input_shape)
        channel_axis = -1  # channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1

        # Block 1
        x = Conv3D(8, (3, 3, 3), use_bias=False,
                   name='block1_conv1')(input_layer)
        x = BatchNormalization(axis=channel_axis, name='block1_conv1_bn')(x)
        x = Activation('relu', name='block1_conv1_act')(x)
        x = Conv3D(8, (3, 3, 2), use_bias=False, name='block1_conv2')(x)
        x = BatchNormalization(axis=channel_axis, name='block1_conv2_bn')(x)
        x = Activation('relu', name='block1_conv2_act')(x)

        residual = Conv3D(16, (1, 1, 1),
                          strides=(2, 2, 1),
                          padding='same',
                          use_bias=False)(x)
        residual = BatchNormalization(axis=channel_axis)(residual)

        # Block 2
        x = Conv3D(16, (3, 3, 1),
                   padding='same',
                   use_bias=False,
                   name='block2_conv1')(x)
        x = BatchNormalization(axis=channel_axis, name='block2_conv1_bn')(x)

        x = MaxPooling3D((3, 3, 1),
                         strides=(2, 2, 1),
                         padding='same',
                         name='block2_pool')(x)
        x = add([x, residual])

        residual = Conv3D(32, (1, 1, 1),
                          strides=(2, 2, 1),
                          padding='same',
                          use_bias=False)(x)
        residual = BatchNormalization(axis=channel_axis)(residual)

        # Block 3
        x = Activation('relu', name='block3_conv1_act')(x)
        x = Conv3D(32, (3, 3, 1),
                   padding='same',
                   use_bias=False,
                   name='block3_conv1')(x)
        x = BatchNormalization(axis=channel_axis, name='block3_conv1_bn')(x)

        x = MaxPooling3D((3, 3, 1),
                         strides=(2, 2, 1),
                         padding='same',
                         name='block3_pool')(x)
        x = add([x, residual])

        # Block 4
        x = Conv3D(64, (3, 3, 1),
                   padding='same',
                   use_bias=False,
                   name='block4_conv1')(x)
        x = BatchNormalization(axis=channel_axis, name='block4_conv1_bn')(x)
        x = Activation('relu', name='block4_conv1_act')(x)

        # Classification block
        x = GlobalAveragePooling3D(name='avg_pool')(x)
        output_layer = Dense(n_classes,
                             activation='softmax',
                             name='predictions')(x)

        # ## create an MLP architecture with dense layers : 4096 -> 512 -> 10
        # ## add dropouts to avoid overfitting / perform regularization
        # dense_layer1 = Dense(units=2048, activation='relu')(x)
        # dense_layer1 = Dropout(0.4)(dense_layer1)
        # dense_layer2 = Dense(units=512, activation='relu')(dense_layer1)
        # dense_layer2 = Dropout(0.4)(dense_layer2)
        # output_layer = Dense(units=n_classes, activation='softmax')(dense_layer2)

        # define the model with input layer and output layer
        model = Model(inputs=input_layer, outputs=output_layer)
        model.summary()

        plot_model(model,
                   to_file=self.output_directory + '/model_graph.png',
                   show_shapes=True,
                   show_layer_names=True)

        model.compile(loss=categorical_crossentropy,
                      optimizer=Adadelta(lr=0.1),
                      metrics=['acc'])

        # model save
        file_path = self.output_directory + '/best_model.hdf5'
        model_checkpoint = keras.callbacks.ModelCheckpoint(filepath=file_path,
                                                           monitor='loss',
                                                           save_best_only=True)

        # Tensorboard log
        log_dir = self.output_directory + '/tf_logs'
        chk_n_mkdir(log_dir)
        tb_cb = TrainValTensorBoard(log_dir=log_dir)

        self.callbacks = [model_checkpoint, tb_cb]
        return model
Example #19
0
def multiinput_resnext(classes=2):
    # 4 input1:a =======================================================================================================
    inputs_1 = Input(shape=(280, 280, 16, 1), name='path1_input1')
    # 256*256*128
    print("path1_input shape:", inputs_1.shape)  # (?, 140, 140, 16, 64)
    out1 = Conv3D(64, 7, strides=(2, 2, 1), padding = 'same', kernel_initializer='he_normal', use_bias = False, name = 'path1_conv1')(inputs_1)
    print("path1_conv0 shape:", out1.shape)#(?, 140, 140, 16, 64)
    out1 = BatchNormalization(axis = -1, epsilon = 1e-6, name = 'path1_bn1')(out1)
    out1 = Activation('relu')(out1)
    out1 = MaxPooling3D((3, 3, 3), strides=(2, 2, 1), padding = 'same')(out1)
    print("path1_pooling1 shape:", out1.shape)#(?, 70, 70, 16, 64)

    out1 = conv_block(out1, [64, 64, 256], name = 'path1_L1_block1')
    print("path1_conv1 shape:", out1.shape)
    out1 = identity_block(out1, [64, 64, 256], name = 'path1_L1_block2')
    out1 = identity_block(out1, [64, 64, 256], name = 'path1_L1_block3')

    # 4 input2:v =======================================================================================================
    inputs_2 = Input(shape=(280, 280, 16, 1), name='path2_input2')
    # 256*256*128
    print("path2_input shape:", inputs_2.shape)  # (?, 140, 140, 16, 64)
    out2 = Conv3D(64, 7, strides=(2, 2, 1), padding = 'same', kernel_initializer='he_normal', use_bias = False, name = 'path2_conv1')(inputs_2)
    print("path2_conv0 shape:", out1.shape)#(?, 140, 140, 16, 64)
    out2 = BatchNormalization(axis = -1, epsilon = 1e-6, name = 'path2_bn1')(out2)
    out2 = Activation('relu')(out2)
    out2 = MaxPooling3D((3, 3, 3), strides=(2, 2, 1), padding = 'same')(out2)
    print("path2_pooling1 shape:", out1.shape)#(?, 70, 70, 16, 64)

    out2 = conv_block(out2, [64, 64, 256], name = 'path2_L1_block1')
    print("path2_conv1 shape:", out2.shape)
    out2 = identity_block(out2, [64, 64, 256], name = 'path2_L1_block2')
    out2 = identity_block(out2, [64, 64, 256], name = 'path2_L1_block3')


    #main path:concatenate 'out1' and 'out2' into 'out' ================================================================
    out = concatenate([out1, out2], axis=-1)
    print("concatenate shape:", out.shape)



    out = conv_block(out, [128, 128, 512], name = 'L2_block1')
    print("conv2 shape:", out.shape)
    out = identity_block(out, [128, 128, 512], name = 'L2_block2')
    out = identity_block(out, [128, 128, 512], name = 'L2_block3')
    out = identity_block(out, [128, 128, 512], name = 'L2_block4')


    out = conv_block(out, [256, 256, 1024], name = 'L3_block1')
    print("conv3 shape:", out.shape)
    out = identity_block(out, [256, 256, 1024], name = 'L3_block2')
    out = identity_block(out, [256, 256, 1024], name = 'L3_block3')
    out = identity_block(out, [256, 256, 1024], name = 'L3_block4')
    out = identity_block(out, [256, 256, 1024], name = 'L3_block5')
    out = identity_block(out, [256, 256, 1024], name = 'L3_block6')

    out = conv_block(out, [512, 512, 2048], name = 'L4_block1')
    print("conv4 shape:", out.shape)
    out = identity_block(out, [512, 512, 2048], name = 'L4_block2')
    out = identity_block(out, [512, 512, 2048], name = 'L4_block3')

    out = GlobalAveragePooling3D(data_format = 'channels_last')(out)
    print("Gpooling shape:", out.shape)
    out_drop = Dropout(rate=0.3)(out)
    out = Dense(classes, name = 'fc1')(out_drop)
    print("out shape:", out.shape)
    output = Activation(activation = 'sigmoid')(out)

    model = Model(input = [inputs_1, inputs_2], output = output)
    #mean_squared_logarithmic_error or binary_crossentropy
    model.compile(optimizer=SGD(lr = 1e-6, momentum = 0.9), loss = EuiLoss, metrics= [y_t, y_pre, Acc] )

    print('im multi_input_ClassNet')
    return model
Example #20
0
def se_create_dense_net(nb_layers,
                        growth_rate=12,
                        nb_filter=64,
                        bottleneck=True,
                        reduction=0.1,
                        dropout_rate=None,
                        subsample_initial_block=True):

    inputs = Input(shape=(280, 280, 16, 1))
    print("0 :inputs shape:", inputs.shape)

    # 设定每个denseblock中convblock的数量:nb_layers = [3,3,3]

    concat_axis = -1  # 设定concat的轴(即叠加的轴)
    bn_axis = -1  # 设定BN的轴(即叠加的轴)
    nb_dense_block = nb_layers.__len__(
    )  # nb_dense_block :denseblock的数量,需要和nb_layers对应
    final_nb_layer = nb_layers[-1]
    compression = 1.0 - reduction  # denseblock的通道衰减率,即实际输出通道数=原输出通道数x通道衰减率

    # Initial convolution =======================================================================================
    if subsample_initial_block:
        initial_kernel = (7, 7, 7)
        initial_strides = (2, 2, 1)
    else:
        initial_kernel = (3, 3, 3)
        initial_strides = (1, 1, 1)

    x = Conv3D(nb_filter,
               initial_kernel,
               kernel_initializer='he_normal',
               padding='same',
               strides=initial_strides,
               use_bias=False)(inputs)

    if subsample_initial_block:
        x = BatchNormalization(axis=bn_axis, epsilon=1.1e-5)(x)
        x = Activation('relu')(x)
        x = MaxPooling3D((3, 3, 3), strides=(2, 2, 1), padding='same')(x)

    print("0 :Initial conv shape:", x.shape)
    # Initial convolution finished ================================================================================

    # Add dense blocks start  ==================================================================================
    for block_idx in range(nb_dense_block - 1):
        x, nb_filter = __dense_block(x,
                                     nb_layers[block_idx],
                                     nb_filter,
                                     growth_rate,
                                     concat_axis=concat_axis,
                                     bn_axis=bn_axis,
                                     bottleneck=bottleneck,
                                     dropout_rate=dropout_rate,
                                     grow_nb_filters=True)
        print(block_idx + 1, ":dense_block shape:", x.shape)

        x = __transition_block(x,
                               nb_filter,
                               compression=compression,
                               concat_axis=concat_axis,
                               bias_allow=False)
        print(block_idx + 1, ":transition_block shape:", x.shape)

        x = squeeze_excite_block3d(x)
        print(block_idx + 1, ":se_block_out shape:", x.shape)

        nb_filter = int(nb_filter * compression)
    # Add dense blocks finish ==================================================================================

    # The last dense_block does not have a transition_block
    x, nb_filter = __dense_block(x,
                                 final_nb_layer,
                                 nb_filter,
                                 growth_rate,
                                 concat_axis=concat_axis,
                                 bn_axis=bn_axis,
                                 bottleneck=bottleneck,
                                 dropout_rate=dropout_rate,
                                 grow_nb_filters=True)
    print(nb_dense_block, ":dense_block shape:", x.shape)

    x = BatchNormalization(axis=bn_axis, epsilon=1.1e-5)(x)
    x = Activation('relu')(x)

    out = GlobalAveragePooling3D(data_format='channels_last')(x)
    print("GApooling shape:", out.shape)
    out_drop = Dropout(rate=0.3)(out)
    out = Dense(1, name='fc1')(out_drop)
    print("out shape:", out.shape)
    output = Activation(activation='sigmoid')(out)

    model = Model(input=inputs, output=output)
    #mean_squared_logarithmic_error or binary_crossentropy
    model.compile(optimizer=SGD(lr=1e-6, momentum=0.9),
                  loss=EuiLoss,
                  metrics=[y_t, y_pre, Acc])

    return model
Example #21
0
def preds3d_baseline(width):

    learning_rate = 5e-5
    optimizer = SGD(lr=learning_rate, momentum=0.9, decay=1e-3, nesterov=True)
    #optimizer = Adam(lr=learning_rate)

    inputs = Input(shape=(1, 136, 168, 168))
    conv1 = Convolution3D(width,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(inputs)
    conv1 = BatchNormalization(axis=1)(conv1)
    conv1 = Convolution3D(width * 2,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv1)
    conv1 = BatchNormalization(axis=1)(conv1)
    pool1 = MaxPooling3D(pool_size=(2, 2, 2), border_mode='same')(conv1)

    conv2 = Convolution3D(width * 2,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(pool1)
    conv2 = BatchNormalization(axis=1)(conv2)
    conv2 = Convolution3D(width * 4,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv2)
    conv2 = BatchNormalization(axis=1)(conv2)
    pool2 = MaxPooling3D(pool_size=(2, 2, 2), border_mode='same')(conv2)

    conv3 = Convolution3D(width * 4,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(pool2)
    conv3 = BatchNormalization(axis=1)(conv3)
    conv3 = Convolution3D(width * 8,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv3)
    conv3 = BatchNormalization(axis=1)(conv3)
    pool3 = MaxPooling3D(pool_size=(2, 2, 2), border_mode='same')(conv3)

    output = GlobalAveragePooling3D()(pool3)
    output = Dense(2, activation='softmax', name='predictions')(output)
    model3d = Model(inputs, output)
    model3d.compile(loss='categorical_crossentropy',
                    optimizer=optimizer,
                    metrics=['accuracy'])
    return model3d
Example #22
0
def ynet_model_3d(input_shape,
                  pool_size=(2, 2, 2),
                  n_labels=1,
                  deconvolution=False,
                  depth=4,
                  n_base_filters=32,
                  batch_normalization=False,
                  activation_name="sigmoid",
                  unet_weights=None,
                  encoder_weights=None,
                  cls_classes=44):
    """
    Builds the 3D YNet Keras model.f
    :param metrics: List metrics to be calculated during model training (default is dice coefficient).
    :param include_label_wise_dice_coefficients: If True and n_labels is greater than 1, model will report the dice
    coefficient for each label as metric.
    :param n_base_filters: The number of filters that the first layer in the convolution network will have. Following
    layers will contain a multiple of this number. Lowering this number will likely reduce the amount of memory required
    to train the model.
    :param depth: indicates the depth of the U-shape for the model. The greater the depth, the more max pooling
    layers will be added to the model. Lowering the depth may reduce the amount of memory required for training.
    :param input_shape: Shape of the input data (n_chanels, x_size, y_size, z_size). The x, y, and z sizes must be
    divisible by the pool size to the power of the depth of the YNet, that is pool_size^depth.
    :param pool_size: Pool size for the max pooling operations.
    :param n_labels: Number of binary labels that the model is learning.
    :param initial_learning_rate: Initial learning rate for the model. This will be decayed during training.
    :param deconvolution: If set to True, will use transpose convolution(deconvolution) instead of up-sampling. This
    increases the amount memory required during training.
    :return: Untrained 3D YNet Model
    """
    inputs = Input(input_shape)
    current_layer = inputs
    levels = list()
    num_layer = 0

    # add levels with max pooling
    for layer_depth in range(depth):
        layer1 = create_convolution_block(
            input_layer=current_layer,
            n_filters=n_base_filters * (2**layer_depth),
            batch_normalization=batch_normalization,
            layer_depth=num_layer)
        num_layer += 1
        layer2 = create_convolution_block(
            input_layer=layer1,
            n_filters=n_base_filters * (2**layer_depth) * 2,
            batch_normalization=batch_normalization,
            layer_depth=num_layer)
        num_layer += 1
        if layer_depth < depth - 1:
            current_layer = MaxPooling3D(pool_size=pool_size)(layer2)
            levels.append([layer1, layer2, current_layer])
        else:
            current_layer = layer2
            levels.append([layer1, layer2])

    if encoder_weights is not None:
        x = GlobalAveragePooling3D()(current_layer)
        o = Dense(cls_classes, activation="softmax")(x)
        encoder_model = Model(inputs=inputs, outputs=o)
        encoder_model.load_weights(encoder_weights)
        encoder_model.summary()
        current_layer = encoder_model.get_layer('depth_7_relu').output

    for layer_depth in range(depth - 2, -1, -1):

        up_convolution = get_up_convolution(
            pool_size=pool_size,
            deconvolution=deconvolution,
            n_filters=current_layer._keras_shape[1])(current_layer)
        concat = concatenate([up_convolution, levels[layer_depth][1]], axis=1)
        current_layer = create_convolution_block(
            n_filters=levels[layer_depth][1]._keras_shape[1],
            layer_depth=num_layer,
            input_layer=concat,
            batch_normalization=batch_normalization)
        num_layer += 1
        current_layer = create_convolution_block(
            n_filters=levels[layer_depth][1]._keras_shape[1],
            layer_depth=num_layer,
            input_layer=current_layer,
            batch_normalization=batch_normalization)
        num_layer += 1

    final_convolution = Conv3D(n_labels, (1, 1, 1),
                               name="final_conv")(current_layer)
    act = Activation(activation_name, name="reconst_output")(final_convolution)
    unet_model = Model(inputs=inputs, outputs=act)

    if unet_weights is not None:
        unet_model.load_weights(unet_weights)

    cls_input = unet_model.get_layer('depth_7_relu').output
    layer1 = create_convolution_block(input_layer=cls_input,
                                      n_filters=512,
                                      batch_normalization=batch_normalization,
                                      layer_depth=num_layer)

    cls_conv2 = GlobalAveragePooling3D()(layer1)
    cls_fc = Dense(1024, activation="relu", name='cls_fc')(cls_conv2)
    cls_output = Dense(cls_classes, activation="softmax",
                       name='cls_output')(cls_fc)
    model = Model(inputs, [unet_model.output, cls_output])
    return model
def densenet(input_shape=(32,32,32,1), dense_blocks=4, dense_layers=-1, growth_rate=12, nb_classes=10, dropout_rate=0.1,
             bottleneck=False, compression=1.0, weight_decay=1e-4, depth=40):
    """
    Creating a DenseNet
    
    Arguments:
        input_shape  : shape of the input images. E.g. (28,28,1) for MNIST    
        dense_blocks : amount of dense blocks that will be created (default: 3)    
        dense_layers : number of layers in each dense block. You can also use a list for numbers of layers [2,4,3]
                       or define only 2 to add 2 layers at all dense blocks. -1 means that dense_layers will be calculated
                       by the given depth (default: -1)
        growth_rate  : number of filters to add per dense block (default: 12)
        nb_classes   : number of classes
        dropout_rate : defines the dropout rate that is accomplished after each conv layer (except the first one).
                       In the paper the authors recommend a dropout of 0.2 (default: None)
        bottleneck   : (True / False) if true it will be added in convolution block (default: False)
        compression  : reduce the number of feature-maps at transition layer. In the paper the authors recomment a compression
                       of 0.5 (default: 1.0 - will have no compression effect)
        weight_decay : weight decay of L2 regularization on weights (default: 1e-4)
        depth        : number or layers (default: 40)
        
    Returns:
        Model        : A Keras model instance
    """
    
    if nb_classes==None:
        raise Exception('Please define number of classes (e.g. num_classes=10). This is required for final softmax.')
    
    if compression <=0.0 or compression > 1.0:
        raise Exception('Compression have to be a value between 0.0 and 1.0. If you set compression to 1.0 it will be turn off.')
    
    if type(dense_layers) is list:
        if len(dense_layers) != dense_blocks:
            raise AssertionError('Number of dense blocks have to be same length to specified layers')
    elif dense_layers == -1:
        if bottleneck:
            dense_layers = (depth - (dense_blocks + 1))/dense_blocks // 2
        else:
            dense_layers = (depth - (dense_blocks + 1))//dense_blocks
        dense_layers = [int(dense_layers) for _ in range(dense_blocks)]
    else:
        dense_layers = [int(dense_layers) for _ in range(dense_blocks)]
        
    img_input = Input(shape=input_shape)
    nb_channels = growth_rate * 2
    dense_layers = [6, 12, 24, 16]
    
    print('Creating DenseNet')
    print('#############################################')
    print('Dense blocks: %s' % dense_blocks)
    print('Layers per dense block: %s' % dense_layers)
    print('#############################################')
    
    # Initial convolution layer
    x = Conv3D(nb_channels, (3,3,3), padding='same',strides=(1,1,1),
                      use_bias=False, kernel_regularizer=l2(weight_decay))(img_input)
    
    # Building dense blocks
    for block in range(dense_blocks):
        
        # Add dense block
        x, nb_channels = dense_block(x, dense_layers[block], nb_channels, growth_rate, dropout_rate, bottleneck, weight_decay)
        
        if block < dense_blocks - 1:  # if it's not the last dense block
            # Add transition_block
            x = transition_layer(x, nb_channels, dropout_rate, compression, weight_decay)
            nb_channels = int(nb_channels * compression)
    
    x = BatchNormalization(gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(x)
    x = Activation('relu')(x)
    x = GlobalAveragePooling3D()(x)
    x = Dropout(0.5)(x)
    
    x = Dense(nb_classes, activation='softmax', kernel_regularizer=l2(weight_decay), bias_regularizer=l2(weight_decay))(x)
    
    model_name = None
    if growth_rate >= 36:
        model_name = 'widedense'
    else:
        model_name = 'dense'
        
    if bottleneck:
        model_name = model_name + 'b'
        
    if compression < 1.0:
        model_name = model_name + 'c'
        
    return Model(img_input, x, name=model_name), model_name
Example #24
0
def Dense3DNet(blocks,
               growth_rate=12,
               reduction=0.5,
               input_tensor=None,
               input_shape=(256, 256, 192, 1),
               pooling=None):
    """Instantiates the DenseNet architecture.
    Optionally loads weights pre-trained
    on ImageNet. Note that when using TensorFlow,
    for best performance you should set
    `image_data_format='channels_last'` in your Keras config
    at ~/.keras/keras.json.
    The model and the weights are compatible with
    TensorFlow, Theano, and CNTK. The data format
    convention used by the model is the one
    specified in your Keras config file.
    # Arguments
        blocks: numbers of building blocks for the four dense layers.
        include_top: whether to include the fully-connected
            layer at the top of the network.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels.
        pooling: optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
    # Returns
        A Keras model instance.
    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    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

    bn_axis = 4 if K.image_data_format() == 'channels_last' else 1

    x = ZeroPadding3D(padding=3)(img_input)
    x = Conv3D(2 * growth_rate,
               7,
               strides=2,
               use_bias=False,
               name='conv1/conv')(x)
    x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x)
    x = Activation('relu', name='conv1/relu')(x)
    x = ZeroPadding3D(padding=1)(x)
    x = MaxPooling3D(3, strides=2, name='pool1')(x)

    x = dense_block(x, blocks[0], growth_rate, name='conv2')
    x = transition_block(x, reduction, name='pool2')
    x = dense_block(x, blocks[1], growth_rate, name='conv3')
    x = transition_block(x, reduction, name='pool3')
    x = dense_block(x, blocks[2], growth_rate, name='conv4')
    x = transition_block(x, reduction, name='pool4')
    x = dense_block(x, blocks[3], growth_rate, name='conv5')

    x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x)

    if pooling == 'avg':
        x = GlobalAveragePooling3D(name='avg_pool')(x)
    elif pooling == 'max':
        x = GlobalMaxPooling3D(name='max_pool')(x)

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

    model = Model(inputs=inputs, outputs=x)
    return model
Example #25
0
    def build(self, plot=False):
        inputs = Input(shape=self.shape)

        x = self._conv_block(inputs, 16, (3, 3, 3), strides=(2, 2, 2), nl='HS')

        x = self._bottleneck(x,
                             16, (3, 3, 3),
                             e=16,
                             s=2,
                             squeeze=True,
                             nl='RE')
        x = self._bottleneck(x,
                             24, (3, 3, 3),
                             e=72,
                             s=2,
                             squeeze=False,
                             nl='RE')
        x = self._bottleneck(x,
                             24, (3, 3, 3),
                             e=88,
                             s=1,
                             squeeze=False,
                             nl='RE')
        x = self._bottleneck(x,
                             40, (5, 5, 5),
                             e=96,
                             s=2,
                             squeeze=True,
                             nl='HS')
        x = self._bottleneck(x,
                             40, (5, 5, 5),
                             e=240,
                             s=1,
                             squeeze=True,
                             nl='HS')
        x = self._bottleneck(x,
                             40, (5, 5, 5),
                             e=240,
                             s=1,
                             squeeze=True,
                             nl='HS')
        x = self._bottleneck(x,
                             48, (5, 5, 5),
                             e=120,
                             s=1,
                             squeeze=True,
                             nl='HS')
        x = self._bottleneck(x,
                             48, (5, 5, 5),
                             e=144,
                             s=1,
                             squeeze=True,
                             nl='HS')
        x = self._bottleneck(x,
                             96, (5, 5, 5),
                             e=288,
                             s=2,
                             squeeze=True,
                             nl='HS')
        x = self._bottleneck(x,
                             96, (5, 5, 5),
                             e=576,
                             s=1,
                             squeeze=True,
                             nl='HS')
        x = self._bottleneck(x,
                             96, (5, 5, 5),
                             e=576,
                             s=1,
                             squeeze=True,
                             nl='HS')

        x = self._conv_block(x, 576, (1, 1, 1), strides=(1, 1, 1), nl='HS')
        x = GlobalAveragePooling3D()(x)
        x = Reshape((1, 1, 1, 576))(x)

        x = Conv3D(1280, (1, 1, 1), padding='same')(x)
        x = self._return_activation(x, 'HS')

        if self.include_top:
            if self.n_class == 1:
                x = Conv3D(self.n_class, (1, 1, 1),
                           padding='same',
                           activation='sigmoid')(x)
                x = Reshape((self.n_class, ))(x)
            else:
                x = Conv3D(self.n_class, (1, 1, 1),
                           padding='same',
                           activation='softmax')(x)
                x = Reshape((self.n_class, ))(x)

        model = Model(inputs, x)
        return model
Example #26
0
def modelConstructor():
    model = Sequential()

    # 1st conv. layer and pooling
    model.add(
        Conv3D(64,
               kernel_size=(3, 3, 3),
               activation='relu',
               strides=1,
               input_shape=(16, 240, 320, 3),
               padding="same"))
    model.add(MaxPooling3D(strides=2, pool_size=(2, 2, 1)))

    # 2nd conv. layer and pooling
    model.add(
        Conv3D(128,
               kernel_size=(3, 3, 3),
               strides=1,
               activation='relu',
               padding="same"))
    model.add(MaxPooling3D(strides=1, pool_size=(2, 2, 2)))

    # 3rd conv. layer a,b and pooling
    model.add(
        Conv3D(256,
               kernel_size=(3, 3, 3),
               strides=1,
               activation='relu',
               padding="same"))
    model.add(
        Conv3D(256,
               kernel_size=(3, 3, 3),
               strides=1,
               activation='relu',
               padding="same"))
    model.add(MaxPooling3D(strides=1, pool_size=(2, 2, 2)))

    # 4th conv. layer a,b and pooling
    model.add(
        Conv3D(512,
               kernel_size=(3, 3, 3),
               strides=1,
               activation='relu',
               padding="same"))
    model.add(
        Conv3D(512,
               kernel_size=(3, 3, 3),
               strides=1,
               activation='relu',
               padding="same"))
    model.add(MaxPooling3D(strides=1, pool_size=(2, 2, 2)))

    # 5th conv. layer a,b and pooling
    model.add(
        Conv3D(512,
               kernel_size=(3, 3, 3),
               strides=1,
               activation='relu',
               padding="same"))
    model.add(
        Conv3D(512,
               kernel_size=(3, 3, 3),
               strides=1,
               activation='relu',
               padding="same"))
    model.add(MaxPooling3D(strides=1, pool_size=(2, 2, 2)))

    # adding a conv. layer: filter = 3 by 3, stride = 1, pad =1, channel= 1024
    model.add(
        Conv3D(1023,
               kernel_size=(3, 3, 3),
               strides=1,
               activation='relu',
               padding="same"))

    # global average pooling
    model.add(GlobalAveragePooling3D())

    model.add(Dense(2, activation='softmax'))
    model.summary()

    optimizerAdam = keras.optimizers.Adam(lr=0.001,
                                          beta_1=0.9,
                                          beta_2=0.999,
                                          epsilon=None,
                                          decay=0.0,
                                          amsgrad=False)

    model.compile(optimizer=optimizerAdam,
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

    return model
Example #27
0
def create_squeezenet3d_model(patch_size=(40, 40, 40), drop_rate=0.2):
    """
    Fully Convolutional Network, from:
    http://cs231n.stanford.edu/reports/2017/pdfs/23.pdf
    """
    if not isinstance(patch_size, tuple):
        patch_size = (patch_size, patch_size, patch_size)

    in_x = Input(shape=(*patch_size, 1), name='input')
    x = Conv3D(64, 3, activation='relu', name='conv1')(in_x)
    x = MaxPooling3D(3, 2, name='maxpool1')(x)

    # fire module 2
    x = Conv3D(16, 1, padding='valid', activation='relu',
               name='fire2_squeeze')(x)
    x1 = Conv3D(64,
                1,
                padding='valid',
                activation='relu',
                name='fire2_expand1')(x)
    x2 = Conv3D(64, 3, padding='same', activation='relu',
                name='fire2_expand2')(x)
    x = Concatenate(name='concatenate_25')([x1, x2])

    # fire module 3
    x = Conv3D(16, 1, padding='valid', activation='relu',
               name='fire3_squeeze')(x)
    x1 = Conv3D(64,
                1,
                padding='valid',
                activation='relu',
                name='fire3_expand1')(x)
    x2 = Conv3D(64, 3, padding='same', activation='relu',
                name='fire3_expand2')(x)
    x = Concatenate(name='concatenate_26')([x1, x2])
    x = MaxPooling3D(3, 2, name='maxpool3')(x)

    # fire module 4
    x = Conv3D(32, 1, padding='valid', activation='relu',
               name='fire4_squeeze')(x)
    x1 = Conv3D(128,
                1,
                padding='valid',
                activation='relu',
                name='fire4_expand1')(x)
    x2 = Conv3D(128,
                3,
                padding='same',
                activation='relu',
                name='fire4_expand2')(x)
    x = Concatenate(name='concatenate_27')([x1, x2])

    # fire module 5
    x = Conv3D(32, 1, padding='valid', activation='relu',
               name='fire5_squeeze')(x)
    x1 = Conv3D(128,
                1,
                padding='valid',
                activation='relu',
                name='fire5_expand1')(x)
    x2 = Conv3D(128,
                3,
                padding='same',
                activation='relu',
                name='fire5_expand2')(x)
    x = Concatenate(name='concatenate_28')([x1, x2])
    x = MaxPooling3D(3, 2, name='pool5')(x)

    # fire module 6
    #     x = Conv3D(48, 1, padding='valid', activation='relu', name='fire6_squeeze')(x)
    #     x1 = Conv3D(192, 1, padding='valid', activation='relu', name='fire6_expand1')(x)
    #     x2 = Conv3D(192, 3, padding='same', activation='relu', name='fire6_expand2')(x)
    #     x = Concatenate(name='concatenate_29')([x1, x2])

    #     # fire module 7
    #     x = Conv3D(48, 1, padding='valid', activation='relu', name='fire7_squeeze')(x)
    #     x1 = Conv3D(192, 1, padding='valid', activation='relu', name='fire7_expand1')(x)
    #     x2 = Conv3D(192, 3, padding='same', activation='relu', name='fire7_expand2')(x)
    #     x = Concatenate(name='concatenate_30')([x1, x2])

    #     # fire module 8
    #     x = Conv3D(64, 1, padding='valid', activation='relu', name='fire8_squeeze')(x)
    #     x1 = Conv3D(256, 1, padding='valid', activation='relu', name='fire8_expand1')(x)
    #     x2 = Conv3D(256, 3, padding='same', activation='relu', name='fire8_expand2')(x)
    #     x = Concatenate(name='concatenate_31')([x1, x2])
    #     x = MaxPooling3D(3, 2, name='maxpool8')(x)

    #     # fire module 9
    #     x = Conv3D(64, 1, padding='valid', activation='relu', name='fire9_squeeze')(x)
    #     x1 = Conv3D(256, 1, padding='valid', activation='relu', name='fire9_expand1')(x)
    #     x2 = Conv3D(256, 3, padding='same', activation='relu', name='fire9_expand2')(x)
    #     x = Concatenate(axis=1, name='concatenate_32')([x1, x2])

    x = Dropout(drop_rate, name='fire9_dropout')(x)

    x = Conv3D(2, 1, activation='relu', name='conv10')(x)
    x = GlobalAveragePooling3D(name='avgpool10')(x)
    out_x = Activation(activation='softmax')(x)

    model = Model(in_x, out_x)
    return model
Example #28
0
def vgg16_w_3d_gb(classes=2, dropout_rate=0.3, use_bias_flag=False):
    inputs = Input(shape=(280, 280, 16, 1), name='input')
    # Block 1
    x = Conv3D(64, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block1_conv1',
               use_bias=use_bias_flag)(inputs)
    x = Conv3D(64, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block1_conv2',
               use_bias=use_bias_flag)(x)
    x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), name='block1_pool')(x)
    print("block1 shape:", x.shape)

    # Block 2
    x = Conv3D(128, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block2_conv1',
               use_bias=use_bias_flag)(x)
    x = Conv3D(128, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block2_conv2',
               use_bias=use_bias_flag)(x)
    x = MaxPooling3D((2, 2, 2), strides=(2, 2, 1), name='block2_pool')(x)
    print("block2 shape:", x.shape)
    # Block 3
    x = Conv3D(256, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block3_conv1',
               use_bias=use_bias_flag)(x)
    x = Conv3D(256, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block3_conv2',
               use_bias=use_bias_flag)(x)
    x = Conv3D(256, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block3_conv3',
               use_bias=use_bias_flag)(x)
    x = MaxPooling3D((2, 2, 2), strides=(2, 2, 1), name='block3_pool')(x)
    print("block3 shape:", x.shape)
    # Block 4
    x = Conv3D(512, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block4_conv1',
               use_bias=use_bias_flag)(x)
    x = Conv3D(512, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block4_conv2',
               use_bias=use_bias_flag)(x)
    x = Conv3D(512, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block4_conv3',
               use_bias=use_bias_flag)(x)
    x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), name='block4_pool')(x)
    print("block4 shape:", x.shape)
    # Block 5
    x = Conv3D(512, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block5_conv1',
               use_bias=use_bias_flag)(x)
    x = Conv3D(512, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block5_conv2',
               use_bias=use_bias_flag)(x)
    x = Conv3D(512, (3, 3, 3),
               activation='relu',
               padding='same',
               name='block5_conv3',
               use_bias=use_bias_flag)(x)
    x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), name='block5_pool')(x)
    print("block5 shape:", x.shape)

    x = GlobalAveragePooling3D(data_format='channels_last')(x)
    print("Gpooling shape:", x.shape)
    x = Dropout(rate=dropout_rate)(x)

    if classes == 1:
        output = Dense(classes,
                       activation='sigmoid',
                       use_bias=use_bias_flag,
                       name='predictions')(x)
        print("predictions1 shape:", output.shape, 'activition:sigmoid')
    else:
        output = Dense(classes,
                       activation='softmax',
                       use_bias=use_bias_flag,
                       name='predictions')(x)
        print("predictions2 shape:", output.shape, 'activition:softmax')

    model = Model(inputs=inputs, outputs=output, name='vgg16')

    return model
def Residual_DenseNet(nb_classes,
                      input_shape,
                      weight_decay=0.005,
                      dropout_rate=0.2,
                      extract_feat=False):
    internal_layers = 3

    model_input = Input(shape=input_shape)

    # 112x112x8
    # stage 1 Initial convolution
    x = Conv3D(64, (3, 3, 3),
               kernel_initializer="he_normal",
               padding="same",
               use_bias=False,
               kernel_regularizer=l2(weight_decay))(model_input)
    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(x)
    x = Activation('relu')(x)
    x = MaxPool3D((2, 2, 1), strides=(2, 2, 1), padding='same')(x)
    # 56x56x8
    # stage 2 convolution
    x = Conv3D(96, (3, 3, 3),
               kernel_initializer="he_normal",
               padding="same",
               use_bias=False,
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(x)
    y = Activation('relu')(x)
    y = MaxPool3D((2, 2, 2), strides=(2, 2, 2), padding='same')(y)
    # 28x28x4

    x1 = Conv3D(96, (1, 1, 4),
                kernel_initializer='he_normal',
                padding="valid",
                strides=(4, 4, 4),
                use_bias=False,
                kernel_regularizer=l2(weight_decay))(y)

    # stage 3
    x = denseblock(y,
                   32,
                   internal_layers=internal_layers,
                   dropout_rate=dropout_rate)

    y = add([x, y])
    y = MaxPool3D((2, 2, 2), strides=(2, 2, 2), padding='same')(y)
    # 14x14x2

    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(y)
    x = Activation('relu')(x)

    x2 = Conv3D(96, (1, 1, 2),
                kernel_initializer='he_normal',
                padding="valid",
                strides=(2, 2, 2),
                use_bias=False,
                kernel_regularizer=l2(weight_decay))(x)

    # stage 4
    x = denseblock(x,
                   32,
                   internal_layers=internal_layers,
                   dropout_rate=dropout_rate)
    y = add([x, y])

    x3 = MaxPool3D((2, 2, 2), strides=(2, 2, 2), padding='same')(y)
    # 7x7x1
    # stage 5
    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(x3)
    x = Activation('relu')(x)

    x = denseblock(x,
                   32,
                   internal_layers=internal_layers,
                   dropout_rate=dropout_rate)
    y = add([x, x3])

    # concat y, x3, x2, x1
    x = concatenate([y, x3, x2, x1], axis=-1)
    # 7x7x1
    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(x)
    x = Activation('relu')(x)

    x = Conv3D(512, (1, 1, 1),
               kernel_initializer='he_normal',
               padding="same",
               strides=(1, 1, 1),
               use_bias=False,
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(x)
    x = Activation('relu')(x)

    x = GlobalAveragePooling3D()(x)
    if not extract_feat:
        x = Dense(nb_classes,
                  activation='softmax',
                  kernel_regularizer=l2(weight_decay),
                  bias_regularizer=l2(weight_decay))(x)

    DRN = Model(input=[model_input], output=[x], name="DRN")

    return DRN
Example #30
0
def dense_resnet_3d(nb_classes,
                    input_shape,
                    weight_decay=0.005,
                    dropout_rate=0.2):

    model_input = Input(shape=input_shape)

    # 112x112x8
    # stage 1 Initial convolution
    x = Conv3D(64, (3, 3, 3),
               kernel_initializer="he_normal",
               padding="same",
               use_bias=False,
               kernel_regularizer=l2(weight_decay))(model_input)
    x = MaxPool3D((2, 2, 1), strides=(2, 2, 1), padding='same')(x)
    # 56x56x8

    y = BatchNormalization(axis=-1, epsilon=1.1e-5)(x)
    y = Activation('relu')(y)
    y = Conv3D(128, (1, 1, 1),
               kernel_initializer="he_normal",
               padding="same",
               use_bias=False,
               kernel_regularizer=l2(weight_decay))(y)

    # stage 2
    x = dense_block(x, 32, internal_layers=4, dropout_rate=dropout_rate)
    x = add([x, y])
    y = MaxPool3D((2, 2, 2), strides=(2, 2, 2), padding='same')(x)
    # 28x28x4

    # stage 3
    x = dense_block(y, 32, internal_layers=4, dropout_rate=dropout_rate)
    x = add([x, y])
    x = MaxPool3D((2, 2, 2), strides=(2, 2, 2), padding='same')(x)
    # 14x14x2

    y = BatchNormalization(axis=-1, epsilon=1.1e-5)(x)
    y = Activation('relu')(y)
    y = Conv3D(256, (1, 1, 1),
               kernel_initializer="he_normal",
               padding="same",
               use_bias=False,
               kernel_regularizer=l2(weight_decay))(y)
    x1 = conv_factory(x, 128, (1, 1, 2), (2, 2, 2), padding='valid')

    # stage 4
    x = dense_block(x, 64, internal_layers=4, dropout_rate=dropout_rate)
    x = add([x, y])
    y = MaxPool3D((2, 2, 2), strides=(2, 2, 2), padding='same')(x)
    # 7x7x1

    x2 = conv_factory(y, 128, (1, 1, 1), (1, 1, 1), padding='same')

    # stage 5
    x = dense_block(y, 64, internal_layers=4, dropout_rate=dropout_rate)
    x = add([x, y])
    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(x)
    x = Activation('relu')(x)

    x = concatenate([x, x2, x1], axis=-1)
    x = Conv3D(512, (1, 1, 1),
               kernel_initializer='he_normal',
               padding="same",
               strides=(1, 1, 1),
               use_bias=False,
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(axis=-1, epsilon=1.1e-5)(x)
    x = Activation('relu')(x)

    x = GlobalAveragePooling3D()(x)
    x = Dense(nb_classes,
              activation='softmax',
              kernel_regularizer=l2(weight_decay),
              bias_regularizer=l2(weight_decay))(x)

    model = Model(inputs=model_input, outputs=x)

    return model