示例#1
0
def default_final_detection_model(pyramid_feature_size=256,
                                  final_detection_feature_size=256,
                                  roi_size=(14, 14),
                                  name='final_detection_submodel'):
    """Creates a final detection model for 3D RetinaMask models.

    Args:
        pyramid_feature_size (int): Number of features for the input to the
            final detection model.
        final_detection_feature_size (int): Number of filters used in the 2D
            convolution layers.
        roi_size (tuple): Size of the region of interest, serves as the
            x and y dimensions of the input to the final detection model.
        name (str): Name of the model.

    Returns:
        tensorflow.keras.Model: a FinalDetection submodel for 3D RetinaMask.
    """
    options = {
        'kernel_size': 3,
        'strides': 1,
        'padding': 'same',
        'kernel_initializer': normal(mean=0.0, stddev=0.01, seed=None),
        'bias_initializer': 'zeros',
        'activation': 'relu'
    }

    if K.image_data_format() == 'channels_first':
        input_shape = (None, pyramid_feature_size, roi_size[0], roi_size[1])
    else:
        input_shape = (None, roi_size[0], roi_size[1], pyramid_feature_size)
    inputs = Input(shape=input_shape)
    outputs = inputs

    for i in range(2):
        outputs = TimeDistributed(
            Conv2D(filters=final_detection_feature_size, **options),
            name='final_detection_submodel_conv1_block{}'.format(i))(outputs)
        outputs = TimeDistributed(
            Conv2D(filters=final_detection_feature_size, **options),
            name='final_detection_submodel_conv2_block{}'.format(i))(outputs)
        outputs = TimeDistributed(
            MaxPool2D(),
            name='final_detection_submodel_pool1_block{}'.format(i))(outputs)

    outputs = TimeDistributed(
        Conv2D(filters=final_detection_feature_size,
               kernel_size=3,
               padding='valid',
               kernel_initializer=normal(mean=0.0, stddev=0.01, seed=None),
               bias_initializer='zeros',
               activation='relu'))(outputs)

    outputs = TimeDistributed(
        Conv2D(filters=1, kernel_size=1, activation='sigmoid'))(outputs)

    outputs = Lambda(lambda x: tf.squeeze(x, axis=[2, 3]))(outputs)

    return Model(inputs=inputs, outputs=outputs, name=name)
示例#2
0
def default_final_detection_model(pyramid_feature_size=256,
                                  final_detection_feature_size=256,
                                  roi_size=(14, 14),
                                  name='final_detection_submodel'):
    options = {
        'kernel_size': 3,
        'strides': 1,
        'padding': 'same',
        'kernel_initializer': normal(mean=0.0, stddev=0.01, seed=None),
        'bias_initializer': 'zeros',
        'activation': 'relu'
    }

    inputs = Input(shape=(None, roi_size[0], roi_size[1],
                          pyramid_feature_size))
    outputs = inputs

    for i in range(2):
        outputs = TimeDistributed(
            Conv2D(filters=final_detection_feature_size, **options),
            name='final_detection_submodel_conv1_block{}'.format(i))(outputs)
        outputs = TimeDistributed(
            Conv2D(filters=final_detection_feature_size, **options),
            name='final_detection_submodel_conv2_block{}'.format(i))(outputs)
        outputs = TimeDistributed(
            MaxPool2D(),
            name='final_detection_submodel_pool1_block{}'.format(i))(outputs)

    outputs = TimeDistributed(
        Conv2D(filters=final_detection_feature_size,
               kernel_size=3,
               padding='valid',
               kernel_initializer=normal(mean=0.0, stddev=0.01, seed=None),
               bias_initializer='zeros',
               activation='relu'))(outputs)

    outputs = TimeDistributed(
        Conv2D(filters=1, kernel_size=1, activation='sigmoid'))(outputs)

    outputs = Lambda(lambda x: tf.squeeze(x, axis=[2, 3]))(outputs)

    return Model(inputs=inputs, outputs=outputs, name=name)
示例#3
0
def denoise_model(image):
    """Model that denoises the noisy image."""

    initializer = normal(mean=0, stddev=0.01, seed=13)

    x = Conv2D(64, (3, 3), padding='same',
               kernel_initializer=initializer)(image)

    bn1 = BatchNormalization()(x)

    act1 = Activation(activation='selu')(bn1)

    x = Conv2D(64, (3, 3), padding='same',
               kernel_initializer=initializer)(act1)

    bn1 = BatchNormalization()(x)

    act1 = Activation(activation='selu')(bn1)

    encoded = Conv2D(32, (3, 3),
                     padding='same',
                     kernel_initializer=initializer)(act1)

    bn1 = BatchNormalization()(encoded)

    act1 = Activation(activation='selu')(bn1)

    x = Conv2D(32, (3, 3), padding='same',
               kernel_initializer=initializer)(act1)

    bn1 = BatchNormalization()(x)

    act1 = Activation(activation='selu')(bn1)

    x = Conv2D(64, (3, 3), padding='same',
               kernel_initializer=initializer)(act1)

    bn1 = BatchNormalization()(x)

    act1 = Activation(activation='selu')(bn1)

    x = Conv2D(64, (3, 3), padding='same',
               kernel_initializer=initializer)(act1)

    bn1 = BatchNormalization()(x)

    act1 = Activation(activation='selu')(bn1)

    decoded = Conv2D(1, (3, 3), padding='same',
                     kernel_initializer=initializer)(act1)

    decoded = Subtract()([image, decoded])

    return decoded
示例#4
0
def default_mask_model_3D(num_classes,
                          pyramid_feature_size=256,
                          mask_feature_size=256,
                          roi_size=(14, 14, 14),
                          mask_size=(28, 28, 28),
                          name='mask_submodel',
                          mask_dtype=K.floatx(),
                          retinanet_dtype=K.floatx()):
    """Creates the default mask submodel.

    Args:
        num_classes: Number of classes to predict a score for at each feature level.
        pyramid_feature_size: The number of filters to expect from the
            feature pyramid levels.
        mask_feature_size: The number of filters to expect from the masks.
        roi_size: The number of filters to use in the Roi Layers.
        mask_size: The size of the masks.
        mask_dtype: Dtype to use for mask tensors.
        retinanet_dtype: Dtype retinanet models expect.
        name: The name of the submodel.

    Returns:
        A keras.models.Model that predicts classes for each anchor.
    """
    options = {
        'kernel_size': 3,
        'strides': 1,
        'padding': 'same',
        'kernel_initializer': normal(mean=0.0, stddev=0.01, seed=None),
        'bias_initializer': 'zeros',
        'activation': 'relu',
    }

    inputs = Input(shape=(None, roi_size[0], roi_size[1], roi_size[2],
                          pyramid_feature_size))
    outputs = inputs

    # casting to the desidered data type, which may be different than
    # the one used for the underlying keras-retinanet model
    if mask_dtype != retinanet_dtype:
        outputs = TimeDistributed(Cast(dtype=mask_dtype),
                                  name='cast_masks')(outputs)

    for i in range(4):
        outputs = TimeDistributed(Conv3D(filters=mask_feature_size, **options),
                                  name='roi_mask_{}'.format(i))(outputs)

    # perform upsampling + conv instead of deconv as in the paper
    # https://distill.pub/2016/deconv-checkerboard/

    outputs = TimeDistributed(Upsample3D(mask_size),
                              name='roi_mask_upsample')(outputs)

    outputs = TimeDistributed(Conv3D(filters=mask_feature_size, **options),
                              name='roi_mask_features')(outputs)

    outputs = TimeDistributed(Conv3D(filters=num_classes,
                                     kernel_size=1,
                                     activation='sigmoid'),
                              name='roi_mask')(outputs)

    # casting back to the underlying keras-retinanet model data type
    if mask_dtype != retinanet_dtype:
        outputs = TimeDistributed(Cast(dtype=retinanet_dtype),
                                  name='recast_masks')(outputs)

    return Model(inputs=inputs, outputs=outputs, name=name)
示例#5
0
def weights_init(shape, name=None, dim_ordering=None):
    return normal(shape, scale=0.01, name=name)