Пример #1
0
def effnet_retinanet(num_classes,
                     backbone='EfficientNetB0',
                     inputs=None,
                     modifier=None,
                     **kwargs):
    """ Constructs a retinanet model using a resnet backbone.

    Args
        num_classes: Number of classes to predict.
        backbone: Which backbone to use (one of ('resnet50', 'resnet101', 'resnet152')).
        inputs: The inputs to the network (defaults to a Tensor of shape (None, None, 3)).
        modifier: A function handler which can modify the backbone before using it in retinanet (this can be used to freeze backbone layers for example).

    Returns
        RetinaNet model with a ResNet backbone.
    """
    # choose default input
    if inputs is None:
        if keras.backend.image_data_format() == 'channels_first':
            inputs = keras.layers.Input(shape=(3, None, None))
        else:
            # inputs = keras.layers.Input(shape=(224, 224, 3))
            inputs = keras.layers.Input(shape=(None, None, 3))

    # get last conv layer from the end of each block [28x28, 14x14, 7x7]
    if backbone == 'EfficientNetB0':
        model = efn.EfficientNetB0(input_tensor=inputs,
                                   include_top=False,
                                   weights=None)
    elif backbone == 'EfficientNetB1':
        model = efn.EfficientNetB1(input_tensor=inputs,
                                   include_top=False,
                                   weights=None)
    elif backbone == 'EfficientNetB2':
        model = efn.EfficientNetB2(input_tensor=inputs,
                                   include_top=False,
                                   weights=None)
    elif backbone == 'EfficientNetB3':
        model = efn.EfficientNetB3(input_tensor=inputs,
                                   include_top=False,
                                   weights=None)
    elif backbone == 'EfficientNetB4':
        model = efn.EfficientNetB4(input_tensor=inputs,
                                   include_top=False,
                                   weights=None)
    elif backbone == 'EfficientNetB5':
        model = efn.EfficientNetB5(input_tensor=inputs,
                                   include_top=False,
                                   weights=None)
    elif backbone == 'EfficientNetB6':
        model = efn.EfficientNetB6(input_tensor=inputs,
                                   include_top=False,
                                   weights=None)
    elif backbone == 'EfficientNetB7':
        model = efn.EfficientNetB7(input_tensor=inputs,
                                   include_top=False,
                                   weights=None)
    else:
        raise ValueError('Backbone (\'{}\') is invalid.'.format(backbone))

    layer_outputs = [
        'block4a_expand_activation', 'block6a_expand_activation',
        'top_activation'
    ]

    layer_outputs = [
        model.get_layer(name=layer_outputs[0]).output,  # 28x28
        model.get_layer(name=layer_outputs[1]).output,  # 14x14
        model.get_layer(name=layer_outputs[2]).output,  # 7x7
    ]
    # create the densenet backbone
    model = keras.models.Model(inputs=inputs,
                               outputs=layer_outputs,
                               name=model.name)

    # invoke modifier if given
    if modifier:
        model = modifier(model)

    # create the full model
    return retinanet.retinanet(inputs=inputs,
                               num_classes=num_classes,
                               backbone_layers=model.outputs,
                               **kwargs)
Пример #2
0
    model = efn.EfficientNetB0(weights=weights)

if b_name == "1":
    model = efn.EfficientNetB1(weights=weights)

if b_name == "2":
    model = efn.EfficientNetB2(weights=weights)

if b_name == "3":
    model = efn.EfficientNetB3(weights=weights)

if b_name == "4":
    model = efn.EfficientNetB4(weights=weights)

if b_name == "5":
    model = efn.EfficientNetB5(weights=weights)

if b_name == "6":
    model = efn.EfficientNetB6(weights=weights)

if b_name == "7":
    model = efn.EfficientNetB7(weights=weights)

image_size = model.input_shape[1]


def read_image(path):
    try:
        return preprocess_input(
            center_crop_and_resize(imread(path)[:, :, :3],
                                   image_size=image_size))