Beispiel #1
0
    def __init__(self,
                 num_classes,
                 backbone_resnet='resnet50',
                 input_size=512,
                 max_objects=100,
                 score_threshold=0.1,
                 nms=True,
                 flip_test=False,
                 mode="train"):

        self.nms = nms
        self.flip_test = flip_test
        self.score_threshold = score_threshold
        self.max_objects = max_objects
        self.output_size = input_size // 4
        self.image_input = Input(shape=(None, None, 3))
        self.heatmap_input = Input(shape=(self.output_size, self.output_size,
                                          num_classes))
        self.width_height_input = Input(shape=(max_objects, 2))
        self.offset_input = Input(shape=(max_objects, 2))
        self.offset_mask_input = Input(shape=(max_objects, ))
        self.index_input = Input(shape=(max_objects, ))
        self.num_classes = num_classes
        self.backbone = resnet_models.ResNet50(
            self.image_input, include_top=False
        ) if backbone_resnet == 'resnet50' else resnet_models.ResNet101(
            image_input, include_top=False)
Beispiel #2
0
def test_resnet():
    inputs = keras.layers.Input(shape=(None, None, 3))

    model = resnet_models.ResNet50(inputs, include_top=False, freeze_bn=True)

    image = np.random.rand(600, 600, 3)
    image = np.expand_dims(image, 0)

    print(model.predict(image))
    Model(inputs, outputs=model.outputs[-1])
    # print('[{0}] {1} x {2} = '.format(i, height, width), model.predict(image).shape)
    print()
def resnet_retinanet(num_classes, backbone='resnet50', inputs=None):

    # choose default input
    if inputs is None:
        inputs = keras.layers.Input(shape=(224, 224, 3))

    # create the resnet backbone
    if backbone == 'resnet50':
        resnet = Resmodel.ResNet50(inputs, include_top=False, freeze_bn=True)
    elif backbone == 'resnet101':
        resnet = Resmodel.ResNet101(inputs, include_top=False, freeze_bn=True)
    elif backbone == 'resnet152':
        resnet = Resmodel.ResNet152(inputs, include_top=False, freeze_bn=True)

    # create the full model
    model = retinanet(inputs=inputs, num_classes=num_classes, backbone=resnet)
    model.summary()
    plot_model(model, to_file='retinanet_model.png', show_shapes=True)

    return model
Beispiel #4
0
def centernet(num_classes,
              backbone='resnet18',
              input_size=512,
              max_objects=100,
              score_threshold=0.1,
              nms=True,
              flip_test=False,
              freeze_bn=False):
    assert backbone in [
        'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152'
    ]
    output_size = input_size // 4
    image_input = Input(shape=(None, None, 3))
    hm_input = Input(shape=(output_size, output_size, num_classes))
    wh_input = Input(shape=(max_objects, 2))
    reg_input = Input(shape=(max_objects, 2))
    reg_mask_input = Input(shape=(max_objects, ))
    index_input = Input(shape=(max_objects, ))

    print('\nBACKBONE IS {}\n'.format(backbone))
    if backbone == 'resnet18':
        resnet = resnet_models.ResNet18(image_input,
                                        include_top=False,
                                        freeze_bn=freeze_bn)
    elif backbone == 'resnet34':
        resnet = resnet_models.ResNet34(image_input,
                                        include_top=False,
                                        freeze_bn=freeze_bn)
    elif backbone == 'resnet50':
        resnet = resnet_models.ResNet50(image_input,
                                        include_top=False,
                                        freeze_bn=freeze_bn)
        # resnet = ResNet50(input_tensor=image_input, include_top=False)
    elif backbone == 'resnet101':
        resnet = resnet_models.ResNet101(image_input,
                                         include_top=False,
                                         freeze_bn=freeze_bn)
    else:
        resnet = resnet_models.ResNet152(image_input,
                                         include_top=False,
                                         freeze_bn=freeze_bn)

    # (b, 16, 16, 2048)
    C5 = resnet.outputs[-1]
    # C5 = resnet.get_layer('activation_49').output

    x = Dropout(rate=0.5)(C5)
    # x = Conv2D(64, 1, padding='same', use_bias=True, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(
    # x)
    # decoder
    num_filters = 256
    for i in range(3):
        num_filters = num_filters // pow(2, i)
        # x = Conv2D(num_filters, 3, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(
        #     x)
        # x = BatchNormalization()(x)
        # x = ReLU()(x)
        x = Conv2DTranspose(num_filters, (4, 4),
                            strides=2,
                            use_bias=False,
                            padding='same',
                            kernel_initializer='he_normal',
                            kernel_regularizer=l2(5e-4))(x)
        x = BatchNormalization()(x)
        x = ReLU()(x)

    # hm header
    y1 = Conv2D(64,
                3,
                padding='same',
                use_bias=True,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4))(x)
    y1 = BatchNormalization()(y1)
    y1 = ReLU()(y1)
    y1 = Conv2D(num_classes,
                1,
                use_bias=True,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4),
                activation='sigmoid')(y1)

    # wh header
    y2 = Conv2D(64,
                3,
                padding='same',
                use_bias=True,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4))(x)
    y2 = BatchNormalization()(y2)
    y2 = ReLU()(y2)
    y2 = Conv2D(2,
                1,
                use_bias=True,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4))(y2)

    # reg header
    y3 = Conv2D(64,
                3,
                padding='same',
                use_bias=True,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4))(x)
    y3 = BatchNormalization()(y3)
    y3 = ReLU()(y3)
    y3 = Conv2D(2,
                1,
                use_bias=True,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4))(y3)

    loss_ = Lambda(loss, name='centernet_loss')([
        y1, y2, y3, hm_input, wh_input, reg_input, reg_mask_input, index_input
    ])
    model = Model(inputs=[
        image_input, hm_input, wh_input, reg_input, reg_mask_input, index_input
    ],
                  outputs=[loss_])

    # detections = decode(y1, y2, y3)
    detections = Lambda(lambda x: decode(*x,
                                         max_objects=max_objects,
                                         score_threshold=score_threshold,
                                         nms=nms,
                                         flip_test=flip_test,
                                         num_classes=num_classes))(
                                             [y1, y2, y3])
    prediction_model = Model(inputs=image_input, outputs=detections)
    debug_model = Model(inputs=image_input, outputs=[y1, y2, y3])
    return model, prediction_model, debug_model
def centernet(num_classes,
              backbone='resnet50',
              input_size=512,
              max_objects=100,
              score_threshold=0.1,
              nms=True,
              flip_test=False,
              freeze_bn=True):
    assert backbone in [
        'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152'
    ]
    output_size = input_size // 4
    image_input = Input(shape=(None, None, 3))

    if backbone == 'resnet18':
        resnet = resnet_models.ResNet18(image_input,
                                        include_top=False,
                                        freeze_bn=freeze_bn)
    elif backbone == 'resnet34':
        resnet = resnet_models.ResNet34(image_input,
                                        include_top=False,
                                        freeze_bn=freeze_bn)
    elif backbone == 'resnet50':
        resnet = resnet_models.ResNet50(image_input,
                                        include_top=False,
                                        freeze_bn=freeze_bn)
    elif backbone == 'resnet101':
        resnet = resnet_models.ResNet101(image_input,
                                         include_top=False,
                                         freeze_bn=freeze_bn)
    else:
        resnet = resnet_models.ResNet152(image_input,
                                         include_top=False,
                                         freeze_bn=freeze_bn)

    # (b, 16, 16, 2048)
    C5 = resnet.outputs[-1]

    x = Dropout(rate=0.5)(C5)
    # decoder
    num_filters = 256
    for i in range(3):
        num_filters = num_filters // pow(2, i)
        x = Conv2DTranspose(num_filters, (4, 4),
                            strides=2,
                            use_bias=False,
                            padding='same',
                            kernel_initializer='he_normal',
                            kernel_regularizer=l2(5e-4))(x)
        x = BatchNormalization()(x)
        x = ReLU()(x)

    # hm header
    y1 = Conv2D(64,
                3,
                padding='same',
                use_bias=False,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4))(x)
    y1 = BatchNormalization()(y1)
    y1 = ReLU()(y1)
    y1 = Conv2D(num_classes,
                1,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4),
                activation='sigmoid')(y1)

    # wh header
    y2 = Conv2D(64,
                3,
                padding='same',
                use_bias=False,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4))(x)
    y2 = BatchNormalization()(y2)
    y2 = ReLU()(y2)
    y2 = Conv2D(2,
                1,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4))(y2)

    # reg header
    y3 = Conv2D(64,
                3,
                padding='same',
                use_bias=False,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4))(x)
    y3 = BatchNormalization()(y3)
    y3 = ReLU()(y3)
    y3 = Conv2D(2,
                1,
                kernel_initializer='he_normal',
                kernel_regularizer=l2(5e-4))(y3)

    detections = Lambda(lambda x: decode(*x,
                                         max_objects=max_objects,
                                         score_threshold=score_threshold,
                                         nms=nms,
                                         flip_test=flip_test,
                                         num_classes=num_classes))(
                                             [y1, y2, y3])
    prediction_model = Model(inputs=image_input, outputs=detections)
    return prediction_model
    debug_model = Model(inputs=image_input, outputs=[y1, y2, y3])
    return model, prediction_model, debug_model


if __name__ == '__main__':
    import numpy as np

    # model = ResNet50(include_top=False)
    # count = 0
    # for i in range(len(model.layers)):
    #     if isinstance(model.layers[i], ZeroPadding2D):
    #         count += 1
    #     print(i, model.layers[i], model.layers[i].name)
    # print(count)

    model_2 = resnet_models.ResNet50(inputs=Input(shape=(None, None, 3)),
                                     include_top=False)
    count_2 = 0
    for i in range(len(model_2.layers)):
        if isinstance(model_2.layers[i], ZeroPadding2D):
            count_2 += 1
        print(i, model_2.layers[i], model_2.layers[i].name)
    print(count_2)
    # model, *_ = centernet(num_classes=20)
    # for i in range(len(model.layers)):
    #     print(i, model.layers[i], model.layers[i].name)

    #
    # hm = np.load('/home/adam/workspace/github/xuannianz/CenterNet/hm.npy')
    # hm = np.transpose(hm, (0, 2, 3, 1))
    # wh = np.load('/home/adam/workspace/github/xuannianz/CenterNet/wh.npy')
    # wh = np.transpose(wh, (0, 2, 3, 1))