Exemplo n.º 1
0
def tiny_yolo2_body(inputs, num_anchors, num_classes):
    '''Create Tiny YOLO_v2 model CNN body in keras.'''
    x = compose(DarknetConv2D_BN_Leaky(16, (3, 3)),
                MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
                DarknetConv2D_BN_Leaky(32, (3, 3)),
                MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
                DarknetConv2D_BN_Leaky(64, (3, 3)),
                MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
                DarknetConv2D_BN_Leaky(128, (3, 3)),
                MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
                DarknetConv2D_BN_Leaky(256, (3, 3)),
                MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
                DarknetConv2D_BN_Leaky(512, (3, 3)),
                MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'),
                DarknetConv2D_BN_Leaky(1024, (3, 3)))(inputs)

    # TODO: darknet tiny YOLOv2 use different filter number for COCO and VOC
    if num_classes == 80:
        y = compose(
            DarknetConv2D_BN_Leaky(512, (3, 3)),
            DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                          name='predict_conv'))(x)
    else:
        y = compose(
            DarknetConv2D_BN_Leaky(1024, (3, 3)),
            DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                          name='predict_conv'))(x)

    return Model(inputs, y)
Exemplo n.º 2
0
def yolo2lite_efficientnet_body(inputs, num_anchors, num_classes, level=0):
    '''
    Create YOLO_v2 Lite EfficientNet model CNN body in keras.
    # Arguments
        level: EfficientNet level number.
            by default we use basic EfficientNetB0 as backbone
    '''
    efficientnet, feature_map_info = get_efficientnet_backbone_info(inputs, level=level)
    f1_channel_num = feature_map_info['f1_channel_num']

    conv_head1 = compose(
        Depthwise_Separable_Conv2D_BN_Leaky(f1_channel_num, (3, 3)),
        Depthwise_Separable_Conv2D_BN_Leaky(f1_channel_num, (3, 3)))(efficientnet.output)

    f2 = efficientnet.get_layer('block6a_expand_activation').output

    conv_head2 = DarknetConv2D_BN_Leaky(int(64*(f1_channel_num//1024)), (1, 1))(f2)
    # TODO: Allow Keras Lambda to use func arguments for output_shape?
    conv_head2_reshaped = Lambda(
        space_to_depth_x2,
        output_shape=space_to_depth_x2_output_shape,
        name='space_to_depth')(conv_head2)

    x = Concatenate()([conv_head2_reshaped, conv_head1])
    x = Depthwise_Separable_Conv2D_BN_Leaky(f1_channel_num, (3, 3))(x)
    x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1), name='predict_conv')(x)
    return Model(inputs, x)
Exemplo n.º 3
0
def yolo2lite_mobilenetv2_body(inputs, num_anchors, num_classes, alpha=1.0):
    """Create YOLO_V2 Lite MobileNetV2 model CNN body in Keras."""

    mobilenetv2 = MobileNetV2(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha)

    # input: 416 x 416 x 3
    # mobilenetv2.output   : 13 x 13 x 1280
    # block_13_expand_relu(layers[119]) : 26 x 26 x (576*alpha)

    conv_head1 = compose(
        Depthwise_Separable_Conv2D_BN_Leaky(1280, (3, 3)),
        Depthwise_Separable_Conv2D_BN_Leaky(1280, (3, 3)))(mobilenetv2.output)

    # block_13_expand_relu output shape: 26 x 26 x (576*alpha)
    block_13_expand_relu = mobilenetv2.layers[119].output
    conv_head2 = DarknetConv2D_BN_Leaky(int(64*alpha), (1, 1))(block_13_expand_relu)
    # TODO: Allow Keras Lambda to use func arguments for output_shape?
    conv_head2_reshaped = Lambda(
        space_to_depth_x2,
        output_shape=space_to_depth_x2_output_shape,
        name='space_to_depth')(conv_head2)

    x = Concatenate()([conv_head2_reshaped, conv_head1])
    x = Depthwise_Separable_Conv2D_BN_Leaky(1280, (3, 3))(x)
    x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1), name='predict_conv')(x)
    return Model(inputs, x)
Exemplo n.º 4
0
def yolo2_body(inputs, num_anchors, num_classes, weights_path=None):
    """Create YOLO_V2 model CNN body in Keras."""
    darknet19 = Model(inputs, darknet19_body()(inputs))
    if weights_path is not None:
        darknet19.load_weights(weights_path, by_name=True)
        print('Load weights {}.'.format(weights_path))

    # input: 416 x 416 x 3
    # darknet19.output : 13 x 13 x 1024
    # conv13(layers[43]) : 26 x 26 x 512

    conv20 = compose(DarknetConv2D_BN_Leaky(1024, (3, 3)),
                     DarknetConv2D_BN_Leaky(1024, (3, 3)))(darknet19.output)

    # conv13 output shape: 26 x 26 x 512
    conv13 = darknet19.layers[43].output
    conv21 = DarknetConv2D_BN_Leaky(64, (1, 1))(conv13)
    # TODO: Allow Keras Lambda to use func arguments for output_shape?
    conv21_reshaped = Lambda(space_to_depth_x2,
                             output_shape=space_to_depth_x2_output_shape,
                             name='space_to_depth')(conv21)

    x = Concatenate()([conv21_reshaped, conv20])
    x = DarknetConv2D_BN_Leaky(1024, (3, 3))(x)
    x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                      name='predict_conv')(x)
    return Model(inputs, x)
Exemplo n.º 5
0
def yolo2_mobilenetv3small_body(inputs, num_anchors, num_classes, alpha=1.0):
    """Create YOLO_V2 MobileNetV3Small model CNN body in Keras."""

    mobilenetv3small = MobileNetV3Small(input_tensor=inputs,
                                        weights='imagenet',
                                        include_top=False,
                                        alpha=alpha)

    # input: 416 x 416 x 3
    # mobilenetv3small.output(layer 165, final feature map): 13 x 13 x (576*alpha)
    # expanded_conv_10/Add(layer 162, end of block10): 13 x 13 x (96*alpha)

    # activation_22(layer 117, middle in block8) : 26 x 26 x (288*alpha)
    # expanded_conv_7/Add(layer 114, end of block7) : 26 x 26 x (48*alpha)

    conv_head1 = compose(DarknetConv2D_BN_Leaky(int(576 * alpha), (3, 3)),
                         DarknetConv2D_BN_Leaky(int(576 * alpha), (3, 3)))(
                             mobilenetv3small.output)

    # activation_22(layer 117) output shape: 26 x 26 x (288*alpha)
    activation_22 = mobilenetv3small.layers[117].output
    conv_head2 = DarknetConv2D_BN_Leaky(int(64 * alpha), (1, 1))(activation_22)
    # TODO: Allow Keras Lambda to use func arguments for output_shape?
    conv_head2_reshaped = Lambda(space_to_depth_x2,
                                 output_shape=space_to_depth_x2_output_shape,
                                 name='space_to_depth')(conv_head2)

    x = Concatenate()([conv_head2_reshaped, conv_head1])
    x = DarknetConv2D_BN_Leaky(int(576 * alpha), (3, 3))(x)
    x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                      name='predict_conv')(x)
    return Model(inputs, x)
def tiny_yolo2lite_mobilenetv3large_body(inputs,
                                         num_anchors,
                                         num_classes,
                                         alpha=1.0):
    """Create Tiny YOLO_V2 Lite MobileNetV3Large model CNN body in Keras."""
    mobilenetv3large = MobileNetV3Large(input_tensor=inputs,
                                        weights='imagenet',
                                        include_top=False,
                                        alpha=alpha)
    print('backbone layers number: {}'.format(len(mobilenetv3large.layers)))

    # input: 416 x 416 x 3
    # mobilenetv3large.output(layer 194, final feature map): 13 x 13 x (960*alpha)

    # f1: 13 x 13 x (960*alpha)
    f1 = mobilenetv3large.output
    f1_channel_num = int(960 * alpha)

    y = compose(
        Depthwise_Separable_Conv2D_BN_Leaky(f1_channel_num, (3, 3),
                                            block_id_str='pred_1'),
        DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                      name='predict_conv'))(f1)

    return Model(inputs, y)
Exemplo n.º 7
0
def yolo2_mobilenetv3large_body(inputs, num_anchors, num_classes, alpha=1.0):
    """Create YOLO_V2 MobileNetV3Large model CNN body in Keras."""

    mobilenetv3large = MobileNetV3Large(input_tensor=inputs,
                                        weights='imagenet',
                                        include_top=False,
                                        alpha=alpha)

    # input: 416 x 416 x 3
    # mobilenetv3large.output(layer 194, final feature map): 13 x 13 x (960*alpha)
    # expanded_conv_14/Add(layer 191, end of block14): 13 x 13 x (160*alpha)

    # activation_29(layer 146, middle in block12) : 26 x 26 x (672*alpha)
    # expanded_conv_11/Add(layer 143, end of block11) : 26 x 26 x (112*alpha)

    conv_head1 = compose(DarknetConv2D_BN_Leaky(int(960 * alpha), (3, 3)),
                         DarknetConv2D_BN_Leaky(int(960 * alpha), (3, 3)))(
                             mobilenetv3large.output)

    # activation_29(layer 146) output shape: 26 x 26 x (672*alpha)
    activation_29 = mobilenetv3large.layers[146].output
    conv_head2 = DarknetConv2D_BN_Leaky(int(64 * alpha), (1, 1))(activation_29)
    # TODO: Allow Keras Lambda to use func arguments for output_shape?
    conv_head2_reshaped = Lambda(space_to_depth_x2,
                                 output_shape=space_to_depth_x2_output_shape,
                                 name='space_to_depth')(conv_head2)

    x = Concatenate()([conv_head2_reshaped, conv_head1])
    x = DarknetConv2D_BN_Leaky(int(960 * alpha), (3, 3))(x)
    x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                      name='predict_conv')(x)
    return Model(inputs, x)
Exemplo n.º 8
0
def yolo2_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0):
    """Create YOLO_V2 MobileNet model CNN body in Keras."""

    mobilenet = MobileNet(input_tensor=inputs,
                          weights='imagenet',
                          include_top=False,
                          alpha=alpha)

    # input: 416 x 416 x 3
    # mobilenet.output            : 13 x 13 x (1024*alpha)
    # conv_pw_11_relu(layers[73]) : 26 x 26 x (512*alpha)

    conv_head1 = compose(DarknetConv2D_BN_Leaky(int(1024 * alpha), (3, 3)),
                         DarknetConv2D_BN_Leaky(int(1024 * alpha),
                                                (3, 3)))(mobilenet.output)

    # conv_pw_11_relu output shape: 26 x 26 x (512*alpha)
    conv_pw_11_relu = mobilenet.layers[73].output
    conv_head2 = DarknetConv2D_BN_Leaky(int(64 * alpha),
                                        (1, 1))(conv_pw_11_relu)
    # TODO: Allow Keras Lambda to use func arguments for output_shape?
    conv_head2_reshaped = Lambda(space_to_depth_x2,
                                 output_shape=space_to_depth_x2_output_shape,
                                 name='space_to_depth')(conv_head2)

    x = Concatenate()([conv_head2_reshaped, conv_head1])
    x = DarknetConv2D_BN_Leaky(int(1024 * alpha), (3, 3))(x)
    x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                      name='predict_conv')(x)
    return Model(inputs, x)
def darknet19(inputs):
    """Generate Darknet-19 model for Imagenet classification."""
    body = darknet19_body()(inputs)
    x = DarknetConv2D(1000, (1, 1))(body)
    x = GlobalAveragePooling2D()(x)
    logits = Softmax()(x)
    return Model(inputs, logits)
Exemplo n.º 10
0
def yolo2lite_xception_body(inputs, num_anchors, num_classes):
    """Create YOLO_V2 Lite Xception model CNN body in Keras."""
    xception = Xception(input_tensor=inputs,
                        weights='imagenet',
                        include_top=False)

    # input: 416 x 416 x 3
    # xception.output: 13 x 13 x 2048
    # block13_sepconv2_bn(middle in block13, layers[121]): 26 x 26 x 1024
    # add_46(end of block12, layers[115]): 26 x 26 x 728

    conv_head1 = compose(Depthwise_Separable_Conv2D_BN_Leaky(2048, (3, 3)),
                         Depthwise_Separable_Conv2D_BN_Leaky(2048, (3, 3)))(
                             xception.output)

    # block13_sepconv2_bn output shape: 26 x 26 x 1024
    block13_sepconv2_bn = xception.layers[121].output
    conv_head2 = DarknetConv2D_BN_Leaky(128, (1, 1))(block13_sepconv2_bn)
    # TODO: Allow Keras Lambda to use func arguments for output_shape?
    conv_head2_reshaped = Lambda(space_to_depth_x2,
                                 output_shape=space_to_depth_x2_output_shape,
                                 name='space_to_depth')(conv_head2)

    x = Concatenate()([conv_head2_reshaped, conv_head1])
    x = Depthwise_Separable_Conv2D_BN_Leaky(2048, (3, 3))(x)
    x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                      name='predict_conv')(x)
    return Model(inputs, x)
Exemplo n.º 11
0
def tiny_yolo2lite_mobilenetv2_body(inputs, num_anchors, num_classes):
    """Create Tiny YOLO_V2 Lite MobileNetV2 model CNN body in Keras."""
    mobilenetv2 = MobileNetV2(input_tensor=inputs, weights='imagenet', include_top=False, alpha=1.0)

    # input: 416 x 416 x 3
    # mobilenetv2.output : 13 x 13 x 1280
    y = compose(
            Depthwise_Separable_Conv2D_BN_Leaky(1280, (3,3)),
            DarknetConv2D(num_anchors*(num_classes+5), (1,1), name='predict_conv'))(mobilenetv2.output)

    return Model(inputs, y)
Exemplo n.º 12
0
def tiny_yolo2lite_efficientnet_body(inputs, num_anchors, num_classes, level=0):
    '''
    Create Tiny YOLO_v2 Lite EfficientNet model CNN body in keras.
    # Arguments
        level: EfficientNet level number.
            by default we use basic EfficientNetB0 as backbone
    '''
    efficientnet, feature_map_info = get_efficientnet_backbone_info(inputs, level=level)
    f1_channel_num = feature_map_info['f1_channel_num']

    y = compose(
            Depthwise_Separable_Conv2D_BN_Leaky(f1_channel_num, (3,3)),
            DarknetConv2D(num_anchors*(num_classes+5), (1,1), name='predict_conv'))(efficientnet.output)

    return Model(inputs, y)
Exemplo n.º 13
0
def tiny_yolo2_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0):
    """Create Tiny YOLO_V2 MobileNet model CNN body in Keras."""
    mobilenet = MobileNet(input_tensor=inputs,
                          weights='imagenet',
                          include_top=False,
                          alpha=alpha)

    # input: 416 x 416 x 3
    # mobilenet.output : 13 x 13 x (1024*alpha)
    y = compose(
        DarknetConv2D_BN_Leaky(int(1024 * alpha), (3, 3)),
        DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                      name='predict_conv'))(mobilenet.output)

    return Model(inputs, y)
Exemplo n.º 14
0
def tiny_yolo2_mobilenetv3small_body(inputs, num_anchors, num_classes, alpha=1.0):
    """Create Tiny YOLO_V2 MobileNetV3Small model CNN body in Keras."""
    mobilenetv3small = MobileNetV3Small(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha)
    print('backbone layers number: {}'.format(len(mobilenetv3small.layers)))

    # input: 416 x 416 x 3
    # mobilenetv3small.output(layer 165, final feature map): 13 x 13 x (576*alpha)

    # f1: 13 x 13 x (576*alpha)
    f1 = mobilenetv3small.output
    f1_channel_num = int(576*alpha)

    y = compose(
            DarknetConv2D_BN_Leaky(f1_channel_num, (3,3)),
            DarknetConv2D(num_anchors*(num_classes+5), (1,1), name='predict_conv'))(f1)

    return Model(inputs, y)
def tiny_yolo2lite_efficientnet_body(inputs, num_anchors, num_classes, level=0):
    '''
    Create Tiny YOLO_v2 Lite EfficientNet model CNN body in keras.
    # Arguments
        level: EfficientNet level number.
            by default we use basic EfficientNetB0 as backbone
    '''
    efficientnet, feature_map_info = get_efficientnet_backbone_info(inputs, level=level)
    print('backbone layers number: {}'.format(len(efficientnet.layers)))

    f1 = efficientnet.get_layer('top_activation').output
    f1_channel_num = feature_map_info['f1_channel_num']

    y = compose(
            Depthwise_Separable_Conv2D_BN_Leaky(f1_channel_num, (3,3), block_id_str='pred_1'),
            DarknetConv2D(num_anchors*(num_classes+5), (1,1), name='predict_conv'))(f1)

    return Model(inputs, y)
Exemplo n.º 16
0
def tiny_yolo2_mobilenetv3large_body(inputs,
                                     num_anchors,
                                     num_classes,
                                     alpha=1.0):
    """Create Tiny YOLO_V2 MobileNetV3Large model CNN body in Keras."""
    mobilenetv3large = MobileNetV3Large(input_tensor=inputs,
                                        weights='imagenet',
                                        include_top=False,
                                        alpha=alpha)

    # input: 416 x 416 x 3
    # mobilenetv3large.output(layer 194, final feature map): 13 x 13 x (960*alpha)
    y = compose(
        DarknetConv2D_BN_Leaky(int(960 * alpha), (3, 3)),
        DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                      name='predict_conv'))(mobilenetv3large.output)

    return Model(inputs, y)
Exemplo n.º 17
0
def tiny_yolo2lite_mobilenetv3small_body(inputs,
                                         num_anchors,
                                         num_classes,
                                         alpha=1.0):
    """Create Tiny YOLO_V2 Lite MobileNetV3Small model CNN body in Keras."""
    mobilenetv3small = MobileNetV3Small(input_tensor=inputs,
                                        weights='imagenet',
                                        include_top=False,
                                        alpha=alpha)

    # input: 416 x 416 x 3
    # mobilenetv3small.output(layer 165, final feature map): 13 x 13 x (576*alpha)
    y = compose(
        Depthwise_Separable_Conv2D_BN_Leaky(int(576 * alpha), (3, 3),
                                            block_id_str='11'),
        DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                      name='predict_conv'))(mobilenetv3small.output)

    return Model(inputs, y)
Exemplo n.º 18
0
def tiny_yolo2_mobilenetv2_body(inputs, num_anchors, num_classes):
    """Create Tiny YOLO_V2 MobileNetV2 model CNN body in Keras."""
    mobilenetv2 = MobileNetV2(input_tensor=inputs,
                              weights='imagenet',
                              include_top=False,
                              alpha=1.0)
    print('backbone layers number: {}'.format(len(mobilenetv2.layers)))

    # input: 416 x 416 x 3
    # mobilenetv2.output : 13 x 13 x 1280

    # f1: 13 x 13 x 1280
    f1 = mobilenetv2.output
    f1_channel_num = 1280

    y = compose(
        DarknetConv2D_BN_Leaky(f1_channel_num, (3, 3)),
        DarknetConv2D(num_anchors * (num_classes + 5), (1, 1),
                      name='predict_conv'))(f1)

    return Model(inputs, y)