示例#1
0
def encoder_model_200(input_shape=(160, 192, 160, 1), bottleneck=200):

    enc = Sequential()

    enc.add(Conv3D(64, 5, padding='same', strides=2, input_shape=input_shape))
    enc.add(LeakyReLU(alpha=.2))
    enc.add(Conv3D(128, 5, padding='same', strides=2))
    enc.add(LeakyReLU(alpha=.2))
    enc.add(Conv3D(128, 5, padding='same', strides=2))
    enc.add(LeakyReLU(alpha=.2))
    enc.add(Conv3D(256, 5, padding='same', strides=2))
    enc.add(LeakyReLU(alpha=.2))
    enc.add(Conv3D(256, 5, padding='same', strides=2))
    enc.add(LeakyReLU(alpha=.2))

    enc.add(Flatten())
    enc.add(Dense(bottleneck, activation='sigmoid'))

    enc.add(Dense(256 * 5 * 6 * 5))
    enc.add(Reshape((5, 6, 5, 256)))

    enc.add(
        Deconvolution3D(filters=256,
                        kernel_size=4,
                        padding='same',
                        strides=2,
                        activation='relu'))
    enc.add(
        Deconvolution3D(filters=256,
                        kernel_size=4,
                        padding='same',
                        strides=2,
                        activation='relu'))
    enc.add(
        Deconvolution3D(filters=128,
                        kernel_size=4,
                        padding='same',
                        strides=2,
                        activation='relu'))
    enc.add(
        Deconvolution3D(filters=64,
                        kernel_size=4,
                        padding='same',
                        strides=2,
                        activation='relu'))
    enc.add(
        Deconvolution3D(filters=1,
                        kernel_size=4,
                        padding='same',
                        strides=2,
                        activation='relu'))
    enc.summary()
    return enc
def get_up_convolution(n_filters, pool_size, kernel_size=(2, 2, 2), strides=(2, 2, 2),
                       deconvolution=False):
    if deconvolution:
        return Deconvolution3D(filters=n_filters, kernel_size=kernel_size,
                               strides=strides, data_format="channels_last")
    else:
        return UpSampling3D(size=pool_size, data_format="channels_last")
示例#3
0
def get_up_convolution(n_filters, pool_size, kernel_size=(2, 2, 2), strides=(2, 2, 2),
                       deconvolution=False):
    if deconvolution:
        return Deconvolution3D(filters=n_filters, kernel_size=kernel_size,
                               strides=strides)
    else:
        return UpSampling3D(size=pool_size)
示例#4
0
def CAE3D_deconv(img_width, img_height, win_length):
    """
    int win_length: Length of window of frames

    Replace Upsampling with Deconv
    """

    input_shape = (win_length, img_width, img_height, 1)

    input_window = Input(shape=input_shape)

    temp_pool = 2
    temp_depth = 5

    x = Conv3D(16, (temp_depth, 3, 3), activation='relu',
               padding='same')(input_window)
    x = MaxPooling3D((2, 2, 2), padding='same')(x)
    #x = Conv3D(8, (temp_depth, 3, 3), activation='relu', padding='same')(x)
    #x = MaxPooling3D((temp_pool, 2, 2), padding='same')(x)
    x = Dropout(0.25)(x)
    x = Conv3D(8, (temp_depth, 3, 3), activation='relu', padding='same')(x)
    encoded = MaxPooling3D((temp_pool, 2, 2), padding='same')(x)

    x = Deconvolution3D(8, (temp_depth, 3, 3),
                        strides=(2, 2, 2),
                        activation='relu',
                        padding='same')(encoded)
    x = Deconvolution3D(16, (temp_depth, 3, 3),
                        strides=(2, 2, 2),
                        activation='relu',
                        padding='same')(x)

    decoded = Conv3D(1, (temp_depth, 3, 3), activation='tanh',
                     padding='same')(x)

    autoencoder = Model(input_window, decoded)
    autoencoder.compile(optimizer='adadelta', loss='mean_squared_error')

    model_type = 'conv'
    model_name = 'CAE3D_Deconv-pooling-win_{}'.format(win_length)
    model = autoencoder

    return model, model_name, model_type
def unet_inference(model_weight_file,
                   loss_func=dice_coefficient_loss_gen,
                   eval_metric=dice_coefficient,
                   base_filter=8,
                   patch_size=(1, 128, 128, 128),
                   depth=3,
                   final_activation='sigmoid'):
    base_filter = base_filter
    inputs = Input(patch_size)

    conv_kernal = (3, 3, 3)
    padding = 'same'
    strides = (1, 1, 1)
    pool_size = (2, 2, 2)
    upsample_kernal = (2, 2, 2)

    #Downsampling block
    layers_downsample = []
    current_layer = inputs
    current_filter_size = base_filter * 2
    for layer in range(depth + 1):
        layer_conv = conv_block(current_filter_size, current_layer,
                                conv_kernal, padding, strides)

        if (layer < depth):
            current_layer = MaxPooling3D(pool_size=pool_size)(layer_conv)
            layers_downsample.append([layer_conv, current_layer])
            current_filter_size = current_filter_size * 2
        else:
            current_layer = layer_conv
            layers_downsample.append([layer_conv])

    #Upsampling block
    for layer in range(depth - 1, -1, -1):
        deconv = Deconvolution3D(filters=current_filter_size,
                                 kernel_size=upsample_kernal,
                                 strides=upsample_kernal)(current_layer)
        concat = concatenate([deconv, layers_downsample[layer][0]], axis=1)

        current_filter_size = current_filter_size // 2
        current_layer = conv_block(current_filter_size, concat, conv_kernal,
                                   padding, strides)

    final_layer = Conv3D(5, (1, 1, 1))(current_layer)
    act = Activation(final_activation)(final_layer)

    model = Model(inputs=inputs, outputs=act)
    model.load_weights(model_weight_file)
    model.compile(optimizer=Adam(learning_rate=0.00001),
                  loss=loss_func,
                  metrics=[eval_metric])

    #print(model.summary())
    return model
def create_up_convolution(input_layer, n_filters, strided_conv_size, kernel_size=(2, 2, 2), strides=(2, 2, 2),
                       deconvolution=False):
    '''
    Construct upsampling block.
    :param deconvolution: Boolean - If True use deconvolution (transposed3D convolution), if False use Upsampling (default is False)
    '''
    if deconvolution:
        return Deconvolution3D(filters=n_filters, kernel_size=kernel_size,
                               strides=strides, kernel_initializer='he_normal', name='DeConv'+str(n_filters))(input_layer)
    else:
        return UpSampling3D(size=strided_conv_size)(input_layer)
示例#7
0
    def upward_layer(cls, input0, input1, n_convolutions, n_output_channels, kernel_size=(2, 2, 2), strides=(2, 2, 2)):
        merged = concatenate([input0, input1], axis=4)
        inl = merged
        for _ in range(n_convolutions - 1):
            inl = PReLU()(
                Conv3D(n_output_channels * 4, (5, 5, 5), padding='same')(inl)
            )
        conv = Conv3D(n_output_channels * 4, (5, 5, 5), padding='same')(inl)
        add_u = add([conv, merged])

        upsample = Deconvolution3D(filters=n_output_channels, kernel_size=kernel_size, strides=strides)(add_u)
        return PReLU()(upsample)
示例#8
0
def get_upconv(depth, nb_filters, pool_size, image_shape, kernel_size=(2, 2, 2), strides=(2, 2, 2),
               deconvolution=False):
    if deconvolution:
        return Deconvolution3D(filters=nb_filters, kernel_size=kernel_size,
                               output_shape=compute_level_output_shape(filters=nb_filters, depth=depth,
                                                                       pool_size=pool_size, image_shape=image_shape),
                               strides=strides, input_shape=compute_level_output_shape(filters=nb_filters,
                                                                                       depth=depth+1,
                                                                                       pool_size=pool_size,
                                                                                       image_shape=image_shape))
    else:
        return UpSampling3D(size=pool_size)
示例#9
0
def brain_enc_model(input_shape=(160, 192, 160, 1)):

    enc = Sequential()
    enc.add(
        Conv3D(8, (3, 3, 3),
               padding='same',
               activation='relu',
               input_shape=input_shape))
    enc.add(
        MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='same'))

    enc.add(Conv3D(8, (3, 3, 3), padding='same', activation='relu'))
    enc.add(
        MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='same'))

    enc.add(Conv3D(8, (3, 3, 3), padding='same', activation='relu'))
    enc.add(
        MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='same'))

    enc.add(
        Deconvolution3D(filters=8,
                        kernel_size=4,
                        padding='same',
                        strides=2,
                        activation='relu'))
    enc.add(
        Deconvolution3D(filters=8,
                        kernel_size=4,
                        padding='same',
                        strides=2,
                        activation='relu'))
    enc.add(
        Deconvolution3D(filters=8,
                        kernel_size=4,
                        padding='same',
                        strides=2,
                        activation='relu'))

    enc.summary()
    return enc
示例#10
0
    def decode_layer(self, kernel_num, kernel_size, input, code_layer):
        deconv = Deconvolution3D(kernel_num, kernel_size, strides=(1, 2, 2), activation='relu', padding='same',
                                 kernel_initializer='he_normal')(input)
        # deconv = Conv3D(kernel_num, kernel_size, activation='relu', padding='same', kernel_initializer='he_normal')(
        #          UpSampling3D(size=(1, 2, 2))(input))

        merge = Concatenate(axis=4)([deconv, code_layer])
        conv = Conv3D(kernel_num, kernel_size, activation='relu', padding='same',
                      kernel_initializer='he_normal')(merge)
        conv = Dropout(self.drop)(conv)

        res = layers.add([deconv, conv])
        return res
示例#11
0
 def up_convolution_block(self,
                          name,
                          input_layer,
                          n_filters,
                          pool_size,
                          kernel_size=(2, 2, 2),
                          strides=(2, 2, 2),
                          deconvolution=False):
     if deconvolution:
         return Deconvolution3D(filters=n_filters,
                                kernel_size=kernel_size,
                                strides=strides,
                                kernel_initializer='he_normal',
                                name=name + '_deconv')(input_layer)
     return UpSampling3D(size=pool_size,
                         name=name + '_Upsampling')(input_layer)
示例#12
0
def get_up_convolution(inputs,
                       filter_num,
                       deconv=False,
                       weight_decay=0.0,
                       batch_norm=False):
    if deconv:
        deconv = Deconvolution3D(filters=filter_num,
                                 kernel_size=(2, 2, 2),
                                 strides=(2, 2, 2),
                                 kernel_initializer='he_normal',
                                 kernel_regularizer=l2(weight_decay))(inputs)
        if batch_norm:
            deconv = BatchNormalization(axis=-1)(deconv)
        return deconv
    else:
        return UpSampling3D(size=(2, 2, 2))(inputs)
示例#13
0
    def vnet_struct(self, input_layer):
        # Layer 1
        conv_1 = Conv3D(16, (5, 5, 5), padding='same')(input_layer)
        repeat_1 = concatenate([input_layer] * 16)
        add_1 = add([conv_1, repeat_1])
        # prelu_1_1 = PReLU()(add_1)
        downsample_1 = Conv3D(filters=32, kernel_size=(1, 4, 4), strides=(1, 4, 4))(add_1)
        prelu_1_2 = PReLU()(downsample_1)

        # Layer 2,3,4
        out2, left2 = self.downward_layer(input_layer=prelu_1_2, n_convolutions=2, n_output_channels=64)
        out3, left3 = self.downward_layer(out2, 3, 128)
        out4, left4 = self.downward_layer(out3, 3, 256)

        # Layer 5
        conv_5_1 = Conv3D(256, (5, 5, 5), padding='same')(out4)
        prelu_5_1 = PReLU()(conv_5_1)
        conv_5_2 = Conv3D(256, (5, 5, 5), padding='same')(prelu_5_1)
        prelu_5_2 = PReLU()(conv_5_2)
        conv_5_3 = Conv3D(256, (5, 5, 5), padding='same')(prelu_5_2)
        add_5 = add([conv_5_3, out4])
        prelu_5_1 = PReLU()(add_5)

        downsample_5 = Deconvolution3D(filters=128, kernel_size=(2, 2, 2), strides=(2, 2, 2))(prelu_5_1)
        prelu_5_2 = PReLU()(downsample_5)

        # Layer 6,7,8
        out6 = self.upward_layer(input0=prelu_5_2, input1=left4, n_convolutions=3, n_output_channels=64)
        out7 = self.upward_layer(out6, left3, 3, 32)
        out8 = self.upward_layer(out7, left2, 2, 16, kernel_size=(1, 4, 4), strides=(1, 4, 4))

        # Layer 9
        merged_9 = concatenate([out8, add_1], axis=4)

        conv_9_1 = Conv3D(32, (5, 5, 5), padding='same')(merged_9)
        add_9 = add([conv_9_1, merged_9])

        conv_9_2 = Conv3D(4, (1, 1, 1), padding='same')(add_9)

        softmax = Softmax()(conv_9_2)

        return softmax
示例#14
0
def get_up_convolution(inputs,
                       n_filters,
                       pool_size,
                       kernel_size=(2, 2, 2),
                       strides=(2, 2, 2),
                       deconvolution=False,
                       activation=None,
                       batch_normalization=False,
                       instance_normalization=False,
                       training=False):
    if deconvolution:
        layer = Deconvolution3D(filters=n_filters,
                                kernel_size=kernel_size,
                                strides=strides)(inputs)
        if batch_normalization:
            layer = BatchNormalization(axis=-1)(layer, training=training)
        elif instance_normalization:
            layer = InstanceNormalization(axis=-1)(layer, training=training)
        if activation is None:
            return Activation('relu')(layer)
        else:
            return get_activation(activation, layer)
    else:
        return UpSampling3D(size=pool_size)(inputs)
示例#15
0
    def build_model_alt(self,
                        num_layers,
                        n_base_filters,
                        deconvolution,
                        use_bn=False):
        """
        Create a 3D Unet model with a variable number of layers and initial number of filters
        :param num_layers: number of layers (i.e. number of skip connections + 1)
        :param n_base_filters: number of filters to use in the first conv layer
        :param deconvolution: True for Deconvolution3D, False for UpSampling3D
        :param use_bn: True to use BatchNormalisation, False otherwise
        :return: Keras model
        """
        POOL_SIZE = (2, 2, 2)
        POOL_STRIDE = (2, 2, 2)
        CONV_KERNEL = (3, 3, 3)
        CONV_STRIDE = (1, 1, 1)
        DECONV_KERNEL = (2, 2, 2)
        DECONV_STRIDE = (2, 2, 2)
        UPSAMPLE_SIZE = (2, 2, 2)
        FEATURE_AXIS = -1

        self._title = "UNet3D_{}layer_{}flt_deconv{}".format(
            num_layers, n_base_filters, int(deconvolution))
        self._title += "_BN" if use_bn else ""

        inputs = self._input
        current_layer = inputs
        layers = list()

        # Contracting path
        for layer_ix in range(num_layers):
            # Two conv layers, note the difference in the number of filters
            contr_conv1 = Conv3D(filters=n_base_filters * (2**layer_ix),
                                 kernel_size=CONV_KERNEL,
                                 strides=CONV_STRIDE,
                                 padding="same",
                                 activation="relu",
                                 kernel_initializer="he_normal")(current_layer)
            if use_bn:
                contr_conv1 = BatchNormalization(
                    axis=FEATURE_AXIS)(contr_conv1)

            contr_conv2 = Conv3D(filters=n_base_filters * (2**layer_ix) * 2,
                                 kernel_size=CONV_KERNEL,
                                 strides=CONV_STRIDE,
                                 padding="same",
                                 activation="relu",
                                 kernel_initializer="he_normal")(contr_conv1)
            if use_bn:
                contr_conv2 = BatchNormalization(
                    axis=FEATURE_AXIS)(contr_conv2)

            # Do not include maxpooling in the final bottom layer
            if layer_ix < num_layers - 1:
                current_layer = MaxPooling3D(pool_size=POOL_SIZE,
                                             strides=POOL_STRIDE,
                                             padding="same")(contr_conv2)
                layers.append([contr_conv1, contr_conv2, current_layer])
            else:
                current_layer = contr_conv2
                layers.append([contr_conv1, contr_conv2])

        # Expanding path
        for layer_ix in range(num_layers - 2, -1, -1):
            if deconvolution:
                exp_deconv = Deconvolution3D(
                    filters=current_layer._keras_shape[-1],
                    kernel_size=DECONV_KERNEL,
                    strides=DECONV_STRIDE)(current_layer)
            else:
                exp_deconv = UpSampling3D(size=UPSAMPLE_SIZE)(current_layer)

            concat_layer = Concatenate(axis=FEATURE_AXIS)(
                [exp_deconv, layers[layer_ix][1]])
            current_layer = Conv3D(
                filters=layers[layer_ix][1]._keras_shape[FEATURE_AXIS],
                kernel_size=CONV_KERNEL,
                strides=CONV_STRIDE,
                padding="same",
                activation="relu",
                kernel_initializer="he_normal")(concat_layer)
            if use_bn:
                current_layer = BatchNormalization(
                    axis=FEATURE_AXIS)(current_layer)

            current_layer = Conv3D(
                filters=layers[layer_ix][1]._keras_shape[FEATURE_AXIS],
                kernel_size=CONV_KERNEL,
                strides=CONV_STRIDE,
                padding="same",
                activation="relu",
                kernel_initializer="he_normal")(current_layer)
            if use_bn:
                current_layer = BatchNormalization(
                    axis=FEATURE_AXIS)(current_layer)

        act = Conv3D(self._num_classes, (1, 1, 1),
                     activation="softmax",
                     padding="same",
                     kernel_initializer="he_normal")(current_layer)

        self._model = Model(inputs=[inputs], outputs=[act])

        return self._model
             activation='relu')(in_layer)
cn2 = Conv3D(8, kernel_size=(3, 3, 3), padding='same',
             activation='linear')(cn1)
bn2 = Activation('relu')(cn2)

dn1 = MaxPooling3D((2, 2, 2))(bn2)
cn3 = Conv3D(16, kernel_size=(3, 3, 3), padding='same',
             activation='linear')(dn1)
bn3 = Activation('relu')(cn3)
dn2 = MaxPooling3D((1, 2, 2))(bn3)
cn4 = Conv3D(32, kernel_size=(3, 3, 3), padding='same',
             activation='linear')(dn2)
bn4 = Activation('relu')(cn4)

up1 = Deconvolution3D(16,
                      kernel_size=(3, 3, 3),
                      strides=(1, 2, 2),
                      padding='same')(bn4)

cat1 = concatenate([up1, bn3])

up2 = Deconvolution3D(8,
                      kernel_size=(3, 3, 3),
                      strides=(2, 2, 2),
                      padding='same')(cat1)

pre_out = concatenate([up2, bn2])

pre_out = Conv3D(1,
                 kernel_size=(1, 1, 1),
                 padding='same',
                 activation='sigmoid')(pre_out)
示例#17
0
def C3D_AE_no_pool(img_width,
                   img_height,
                   win_length,
                   regularizer_list=[],
                   channels=1):
    """
        3DCAE model
    """

    input_shape = (win_length, img_width, img_height, channels)

    input_window = Input(shape=input_shape)

    temp_pool = 2
    temp_depth = 5
    x = Conv3D(16, (5, 3, 3), activation='relu', padding='same')(input_window)
    if 'BN' in regularizer_list:
        x = BatchNormalization()(x)

    x = Conv3D(8, (5, 3, 3),
               activation='relu',
               strides=(1, 2, 2),
               padding='same')(x)
    if 'Dropout' in regularizer_list:
        x = Dropout(0.25)(x)
    if 'BN' in regularizer_list:
        x = BatchNormalization()(x)

    x = Conv3D(8, (5, 3, 3),
               activation='relu',
               strides=(2, 2, 2),
               padding='same')(x)
    if 'BN' in regularizer_list:
        x = BatchNormalization()(x)

    encoded = Conv3D(8, (5, 3, 3),
                     activation='relu',
                     strides=(2, 2, 2),
                     padding='same')(x)

    x = Deconvolution3D(8, (temp_depth, 3, 3),
                        strides=(2, 2, 2),
                        activation='relu',
                        padding='same')(encoded)
    if 'BN' in regularizer_list:
        x = BatchNormalization()(x)
    #Double all the dimensions if win_length is even other wise double the H and W, change T to 2*T-1
    if win_length % 2 == 0:
        x = Deconvolution3D(8, (temp_depth, 3, 3),
                            strides=(2, 2, 2),
                            activation='relu',
                            padding='same')(x)
        if 'BN' in regularizer_list:
            x = BatchNormalization()(x)
    else:
        #Deconvolution formula for valid padding
        #out=stride*input-stride+kernel_size
        input_temporal_size = int((win_length + 1) / 2)
        #         print(input_temporal_size)
        x = Deconvolution3D(8, (input_temporal_size, 2, 2),
                            strides=(1, 2, 2),
                            activation='relu',
                            padding='valid')(x)

    # if 'BN' in regularizer_list:
    #     x= BatchNormalization()(x)

    x = Deconvolution3D(16, (temp_depth, 3, 3),
                        strides=(1, 2, 2),
                        activation='relu',
                        padding='same')(x)
    if 'BN' in regularizer_list:
        x = BatchNormalization()(x)

    decoded = Conv3D(channels, (5, 3, 3),
                     activation='tanh',
                     padding='same',
                     name='decoded')(x)

    autoencoder = Model(input_window, decoded, name="R")
    autoencoder.compile(optimizer='adadelta', loss='mean_squared_error')

    model_type = '3Dconv'
    model_name = 'C3DAE-no_pool'

    for reg in regularizer_list:
        model_name += '-' + reg
    model = autoencoder

    return model, model_name, model_type
示例#18
0
def diff_ROI_C3D_AE_no_pool(img_width,
                            img_height,
                            win_length,
                            regularizer_list=[],
                            channels=1,
                            lambda_S=1,
                            lambda_T=1,
                            d_type=None):
    """
        diff-ROI-3DCAE model
    """
    def multiply(x):
        image, mask = x  #could be K.stack([mask]*3, axis=-1) too
        return mask * image

    input_shape = (win_length, img_width, img_height, channels)
    input_diff_shape = (win_length - 1, img_width, img_height, channels)

    input_window = Input(shape=input_shape)
    input_mask = Input(shape=input_shape)
    input_diff_mask = Input(shape=input_diff_shape)

    temp_pool = 2
    temp_depth = 5
    x = Conv3D(16, (5, 3, 3), activation='relu', padding='same')(input_window)
    if 'BN' in regularizer_list:
        x = BatchNormalization()(x)

    x = Conv3D(8, (5, 3, 3),
               activation='relu',
               strides=(1, 2, 2),
               padding='same')(x)
    if 'Dropout' in regularizer_list:
        x = Dropout(0.25)(x)
    if 'BN' in regularizer_list:
        x = BatchNormalization()(x)

    x = Conv3D(8, (5, 3, 3),
               activation='relu',
               strides=(2, 2, 2),
               padding='same')(x)
    if 'BN' in regularizer_list:
        x = BatchNormalization()(x)

    encoded = Conv3D(8, (5, 3, 3),
                     activation='relu',
                     strides=(2, 2, 2),
                     padding='same')(x)

    x = Deconvolution3D(8, (temp_depth, 3, 3),
                        strides=(2, 2, 2),
                        activation='relu',
                        padding='same')(encoded)
    if 'BN' in regularizer_list:
        x = BatchNormalization()(x)

    #Double all the dimensions if win_length is even other wise double the H and W, change T to 2*T-1
    if win_length % 2 == 0:
        x = Deconvolution3D(8, (temp_depth, 3, 3),
                            strides=(2, 2, 2),
                            activation='relu',
                            padding='same')(x)
    else:
        #Deconvolution formula for valid padding
        #out=stride*input-stride+kernel_size
        input_temporal_size = int((win_length + 1) / 2)
        #         print(input_temporal_size)
        x = Deconvolution3D(8, (input_temporal_size, 2, 2),
                            strides=(1, 2, 2),
                            activation='relu',
                            padding='valid')(x)
    if 'BN' in regularizer_list:
        x = BatchNormalization()(x)

    x = Deconvolution3D(16, (temp_depth, 3, 3),
                        strides=(1, 2, 2),
                        activation='relu',
                        padding='same')(x)
    if 'BN' in regularizer_list:
        x = BatchNormalization()(x)

    layer_name = 'decoded'
    if d_type != None:
        layer_name = d_type + '_' + layer_name
    decoded = Conv3D(channels, (5, 3, 3),
                     activation='tanh',
                     padding='same',
                     name=layer_name)(x)
    #Model_name
    model_name = "R"
    if d_type != None:
        model_name = d_type + '_AE'
    autoencoder = Model(inputs=[input_window, input_mask, input_diff_mask],
                        outputs=decoded,
                        name="R")

    autoencoder.compile(optimizer='adadelta', loss=ROI_diff_mse_joint_loss(input_mask,input_diff_mask,lambda_S,lambda_T), \
             metrics=[ROI_mean_squared_error_loss(input_mask),ROI_diff_temporal_loss(input_mask,input_diff_mask)])
    #     autoencoder.compile(optimizer='adadelta', loss='mean_squared_error')

    model_type = '3Dconv'
    model_name = 'diff_ROI_C3DAE_no_pool'

    for reg in regularizer_list:
        model_name += '-' + reg
    model = autoencoder

    return model, model_name, model_type
示例#19
0
    def get_vnet(self):
        inputs = Input((self.img_depth, self.img_rows, self.img_cols, self.img_channel))

        # 卷积层1
        conv1 = self.encode_layer(32, [1, 3, 3], inputs)
        # 下采样1
        down1 = self.down_operation(64, [1, 3, 3], conv1)

        # 卷积层2
        conv2 = self.encode_layer(64, [1, 3, 3], down1)
        # 下采样2
        down2 = self.down_operation(128, [1, 3, 3], conv2)

        # 卷积层3
        conv3 = self.encode_layer(128, [1, 3, 3], down2)
        # 下采样3
        down3 = self.down_operation(256, [1, 3, 3], conv3)

        # 卷积层4
        conv4 = self.encode_layer(256, [1, 3, 3], down3)
        # 下采样4
        down4 = self.down_operation(512, [1, 3, 3], conv4)

        # 卷积层5
        conv5 = self.encode_layer(512, [1, 3, 3], down4)
        conv5 = Conv3D(512, [3, 1, 1], activation='relu', padding='valid',
                       kernel_initializer='he_normal')(conv5)
#######################################################################################################################
        # 反卷积6
        deconv6 = Deconvolution3D(256, [1, 3, 3], strides=(1, 2, 2), activation='relu', padding='same',
                                  kernel_initializer='he_normal')(conv5)
        conv4 = Conv3D(256, [3, 1, 1], activation='relu', padding='valid',
                       kernel_initializer='he_normal')(conv4)
        merge6 = Concatenate(axis=4)([deconv6, conv4])
        conv6 = Conv3D(256, [1, 3, 3], activation='relu', padding='same',
                       kernel_initializer='he_normal')(merge6)
        conv6 = Dropout(self.drop)(conv6)
        res6 = layers.add([deconv6, conv6])
#######################################################################################################################

#######################################################################################################################
        # 反卷积7
        deconv7 = Deconvolution3D(128, [1, 3, 3], strides=(1, 2, 2), activation='relu', padding='same',
                                  kernel_initializer='he_normal')(res6)
        conv3 = Conv3D(128, [3, 1, 1], activation='relu', padding='valid',
                       kernel_initializer='he_normal')(conv3)
        # conv3 = Conv3D(128, [3, 1, 1], activation='relu', padding='valid',
        #                kernel_initializer='he_normal')(conv3)
        merge7 = Concatenate(axis=4)([deconv7, conv3])
        conv7 = Conv3D(128, [1, 3, 3], activation='relu', padding='same',
                       kernel_initializer='he_normal')(merge7)
        conv7 = Dropout(self.drop)(conv7)
        res7 = layers.add([deconv7, conv7])
#######################################################################################################################

#######################################################################################################################
        # 反卷积8
        deconv8 = Deconvolution3D(64, [1, 3, 3], strides=(1, 2, 2), activation='relu', padding='same',
                                  kernel_initializer='he_normal')(res7)
        conv2 = Conv3D(64, [3, 1, 1], activation='relu', padding='valid',
                       kernel_initializer='he_normal')(conv2)
        # conv2 = Conv3D(64, [3, 1, 1], activation='relu', padding='valid',
        #                kernel_initializer='he_normal')(conv2)
        # conv2 = Conv3D(64, [3, 1, 1], activation='relu', padding='valid',
        #                kernel_initializer='he_normal')(conv2)
        merge8 = Concatenate(axis=4)([deconv8, conv2])
        conv8 = Conv3D(64, [1, 3, 3], activation='relu', padding='same',
                       kernel_initializer='he_normal')(merge8)
        conv8 = Dropout(self.drop)(conv8)
        res8 = layers.add([deconv8, conv8])
#######################################################################################################################

#######################################################################################################################
        # 反卷积9
        deconv9 = Deconvolution3D(32, [1, 3, 3], strides=(1, 2, 2), activation='relu', padding='same',
                                  kernel_initializer='he_normal')(res8)
        conv1 = Conv3D(32, [3, 1, 1], activation='relu', padding='valid',
                       kernel_initializer='he_normal')(conv1)
        # conv1 = Conv3D(32, [3, 1, 1], activation='relu', padding='valid',
        #                kernel_initializer='he_normal')(conv1)
        # conv1 = Conv3D(32, [3, 1, 1], activation='relu', padding='valid',
        #                kernel_initializer='he_normal')(conv1)
        # conv1 = Conv3D(32, [3, 1, 1], activation='relu', padding='valid',
        #                kernel_initializer='he_normal')(conv1)
        merge9 = Concatenate(axis=4)([deconv9, conv1])
        conv9 = Conv3D(32, [1, 3, 3], activation='relu', padding='same',
                       kernel_initializer='he_normal')(merge9)
        conv9 = Dropout(self.drop)(conv9)
        res9 = layers.add([deconv9, conv9])
#######################################################################################################################

        conv10 = Conv3D(1, [1, 1, 1], activation='sigmoid')(res9)

        model = Model(inputs=inputs, outputs=conv10)

        # 在这里可以自定义损失函数loss和准确率函数accuracy
        # model.compile(optimizer=Adam(lr=1e-4), loss='binary_crossentropy', metrics=['accuracy'])
        
        losses = WeightedBinaryCrossEntropy()
        model.compile(optimizer=Adam(lr=1e-4), loss=losses.weighted_binary_crossentropy, metrics=['accuracy',dice_coef])
#        model.compile(optimizer=Adam(lr=1e-4), loss=dice_coef_loss, metrics=['accuracy',dice_coef])        
#        model.compile(optimizer=Adam(lr=1e-4), loss='binary_crossentropy', metrics=['accuracy',dice_coef])
        print('model compile')
        return model
示例#20
0
def CNN_3D_AE(input_shape=(160, 192, 160, 1), bottleneck=200):
    filters = [4, 8, 16, 32, 64]
    model = Sequential()

    model.add(
        Conv3D(filters=filters[0],
               kernel_size=(3, 3, 3),
               padding='same',
               input_shape=input_shape))
    model.add(Activation('relu'))
    model.add(Conv3D(filters=filters[0], kernel_size=(3, 3, 3),
                     padding='same'))
    model.add(BatchNormalization(axis=-1))
    model.add(Activation('relu'))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=2, padding='valid'))

    model.add(Conv3D(filters=filters[1], kernel_size=(3, 3, 3),
                     padding='same'))
    model.add(Activation('relu'))
    model.add(Conv3D(filters=filters[1], kernel_size=(3, 3, 3),
                     padding='same'))
    model.add(BatchNormalization(axis=-1))
    model.add(Activation('relu'))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=2, padding='valid'))

    model.add(Conv3D(filters=filters[2], kernel_size=(3, 3, 3),
                     padding='same'))
    model.add(Activation('relu'))
    model.add(Conv3D(filters=filters[2], kernel_size=(3, 3, 3),
                     padding='same'))
    model.add(BatchNormalization(axis=-1))
    model.add(Activation('relu'))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=2, padding='valid'))

    model.add(Conv3D(filters=filters[3], kernel_size=(3, 3, 3),
                     padding='same'))
    model.add(Activation('relu'))
    model.add(Conv3D(filters=filters[3], kernel_size=(3, 3, 3),
                     padding='same'))
    model.add(BatchNormalization(axis=-1))
    model.add(Activation('relu'))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=2, padding='valid'))

    model.add(Conv3D(filters=filters[4], kernel_size=(3, 3, 3),
                     padding='same'))
    model.add(Activation('relu'))
    model.add(Conv3D(filters=filters[4], kernel_size=(3, 3, 3),
                     padding='same'))
    model.add(BatchNormalization(axis=-1))
    model.add(Activation('relu'))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=2, padding='valid'))

    model.add(Flatten())
    model.add(Dense(bottleneck, activation='sigmoid'))
    model.add(Dense(64 * 5 * 6 * 5))
    model.add(Reshape((5, 6, 5, 64)))

    model.add(
        Deconvolution3D(filters=64,
                        kernel_size=3,
                        padding='same',
                        strides=2,
                        activation='relu'))
    model.add(
        Deconvolution3D(filters=32,
                        kernel_size=3,
                        padding='same',
                        strides=2,
                        activation='relu'))
    model.add(
        Deconvolution3D(filters=16,
                        kernel_size=3,
                        padding='same',
                        strides=2,
                        activation='relu'))
    model.add(
        Deconvolution3D(filters=8,
                        kernel_size=3,
                        padding='same',
                        strides=2,
                        activation='relu'))
    model.add(
        Deconvolution3D(filters=1,
                        kernel_size=3,
                        padding='same',
                        strides=2,
                        activation='relu'))

    model.summary()
    return model