Exemplo n.º 1
0
def default_classification_model(
    num_classes,
    num_anchors,
    pyramid_feature_size=256,
    prior_probability=0.01,
    classification_feature_size=256,
    name='classification_submodel'
):
    """ Creates the default classification submodel.

    Args
        num_classes                 : Number of classes to predict a score for at each feature level.
        num_anchors                 : Number of anchors to predict classification scores for at each feature level.
        pyramid_feature_size        : The number of filters to expect from the feature pyramid levels.
        classification_feature_size : The number of filters to use in the layers in the classification submodel.
        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',
    }

    if keras.backend.image_data_format() == 'channels_first':
        inputs  = keras.layers.Input(shape=(pyramid_feature_size, None, None))
    else:
        inputs  = keras.layers.Input(shape=(None, None, pyramid_feature_size))
    outputs = inputs
    for i in range(4):
        outputs = keras.layers.Conv2D(
            filters=classification_feature_size,
            activation='relu',
            name='pyramid_classification_{}'.format(i),
            kernel_initializer=keras.initializers.normal(mean=0.0, stddev=0.01, seed=None),
            bias_initializer='zeros',
            **options
        )(outputs)

    outputs = keras.layers.Conv2D(
        filters=num_classes * num_anchors,
        kernel_initializer=keras.initializers.normal(mean=0.0, stddev=0.01, seed=None),
        bias_initializer=initializers.PriorProbability(probability=prior_probability),
        name='pyramid_classification',
        **options
    )(outputs)

    # reshape output and apply sigmoid
    if keras.backend.image_data_format() == 'channels_first':
        outputs = keras.layers.Permute((2, 3, 1), name='pyramid_classification_permute')(outputs)
    outputs = keras.layers.Reshape((-1, num_classes), name='pyramid_classification_reshape')(outputs)
    outputs = keras.layers.Activation('sigmoid', name='pyramid_classification_sigmoid')(outputs)

    return keras.models.Model(inputs=inputs, outputs=outputs, name=name)
Exemplo n.º 2
0
def default_classification_model(num_classes,
                                 shared_model,
                                 pyramid_feature_size=256,
                                 prior_probability=0.01,
                                 name='classification_submodel'):
    """
    Creates the default classification submodel.

    Args
        num_classes: Number of classes to predict a score for at each feature level.
        shared_model:
        pyramid_feature_size: The number of filters to expect from the feature pyramid levels.
        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',
    }

    inputs = keras.layers.Input(shape=(None, None, pyramid_feature_size))

    outputs = shared_model(inputs)

    outputs = keras.layers.Conv2D(
        filters=num_classes,
        kernel_initializer=keras.initializers.normal(mean=0.0,
                                                     stddev=0.01,
                                                     seed=None),
        bias_initializer=initializers.PriorProbability(
            probability=prior_probability),
        name='pyramid_classification',
        **options)(outputs)

    # reshape output and apply sigmoid
    outputs = keras.layers.Reshape(
        (-1, num_classes), name='pyramid_classification_reshape')(outputs)
    outputs = keras.layers.Activation(
        'sigmoid', name='pyramid_classification_sigmoid')(outputs)

    return keras.models.Model(inputs=inputs, outputs=outputs, name=name)
def default_classification_model(num_classes,
                                 pyramid_feature_size=256,
                                 prior_probability=0.01,
                                 classification_feature_size=256,
                                 name='classification_submodel'):
    options = {
        'kernel_size': 3,
        'strides': 1,
        'padding': 'same',
    }

    inputs = keras.layers.Input(shape=(None, None, pyramid_feature_size))
    outputs = inputs
    for i in range(4):
        outputs = keras.layers.Conv2D(
            filters=classification_feature_size,
            activation='relu',
            name='pyramid_classification_{}'.format(i),
            kernel_initializer=keras.initializers.normal(mean=0.0,
                                                         stddev=0.01,
                                                         seed=None),
            bias_initializer='zeros',
            **options)(outputs)

    outputs = keras.layers.Conv2D(
        filters=num_classes,
        kernel_initializer=keras.initializers.zeros(),
        bias_initializer=initializers.PriorProbability(
            probability=prior_probability),
        name='pyramid_classification',
        **options)(outputs)

    # reshape output and apply sigmoid
    outputs = keras.layers.Reshape(
        (-1, num_classes), name='pyramid_classification_reshape')(outputs)
    outputs = keras.layers.Activation(
        'sigmoid', name='pyramid_classification_sigmoid')(outputs)

    return keras.models.Model(inputs=inputs, outputs=outputs, name=name)
Exemplo n.º 4
0
def default_classification_model(num_classes,
                                 num_anchors,
                                 pyramid_feature_size=256,
                                 name='classification_submodel',
                                 width=256,
                                 depth=1,
                                 separable_conv=False,
                                 kernel_size=3):
    options = {
        'kernel_size': kernel_size,
        'strides': 1,
        'padding': 'same',
    }

    if keras.backend.image_data_format() == 'channels_first':
        inputs = keras.layers.Input(shape=(pyramid_feature_size, None, None))
    else:
        inputs = keras.layers.Input(shape=(None, None, pyramid_feature_size))

    if separable_conv:
        kernel_initializer = {
            'depthwise_initializer': keras.initializers.VarianceScaling(),
            'pointwise_initializer': keras.initializers.VarianceScaling(),
        }
        options.update(kernel_initializer)
        convs = [
            keras.layers.SeparableConv2D(filters=width,
                                         bias_initializer='zeros',
                                         name=f'{name}/class-{i}',
                                         **options) for i in range(depth)
        ]

        head = keras.layers.SeparableConv2D(
            filters=num_classes * num_anchors,
            bias_initializer=initializers.PriorProbability(probability=0.01),
            name=f'{name}/class-predict',
            **options)
    else:
        kernel_initializer = {
            'kernel_initializer':
            keras.initializers.RandomNormal(mean=0.0, stddev=0.01, seed=None)
        }
        options.update(kernel_initializer)
        convs = [
            keras.layers.Conv2D(filters=width,
                                bias_initializer='zeros',
                                name=f'{name}/class-{i}',
                                **options) for i in range(depth)
        ]
        head = keras.layers.Conv2D(
            filters=num_classes * num_anchors,
            bias_initializer=initializers.PriorProbability(probability=0.01),
            name='class-predict',
            **options)
    bns = [[
        keras.layers.BatchNormalization(momentum=MOMENTUM,
                                        epsilon=EPSILON,
                                        name=f'{name}/class-{i}-bn-{j}')
        for j in range(3, 8)
    ] for i in range(depth)]

    relu = keras.layers.Activation('sigmoid',
                                   name='pyramid_classification_sigmoid')
    reshape = keras.layers.Reshape((-1, num_classes))
    activation = keras.layers.Activation('sigmoid')
    level = 0

    outputs = inputs
    for i in range(depth):
        outputs = convs[i](outputs)
        outputs = bns[i][level](outputs)
        outputs = relu(outputs)
        level += 1
    outputs = head(outputs)
    outputs = reshape(outputs)
    outputs = activation(outputs)
    return keras.models.Model(inputs=inputs, outputs=outputs, name=name)