Ejemplo n.º 1
0
def get_featuremap_model(config, num_classes):
    inputs = Input(shape=(None, None, 3))
    base_layers = ResNet50(inputs)

    featuremap_model = Model(inputs, base_layers)

    return featuremap_model
Ejemplo n.º 2
0
def get_model(config, num_classes):
    # 设定输入
    inputs = Input(shape=(None, None, 3))
    # 设定roi的个数以及他们的位置坐标,None一般为32,可以在config.py中的num.rois中设置
    roi_input = Input(shape=(None, 4))
    # 获取特征提取层的输出
    base_layers = ResNet50(inputs)

    # 一般采用9个anchor,可以在config.py中设置
    num_anchors = len(config.anchor_box_scales) * len(config.anchor_box_ratios)
    # 获取RPN
    rpn = get_rpn(base_layers, num_anchors)
    # 建立rpn模型
    model_rpn = Model(inputs, rpn[:2])

    # 获取分类模型的输出
    classifier = get_classifier(base_layers,
                                roi_input,
                                config.num_rois,
                                nb_classes=num_classes,
                                trainable=True)
    # 建立分类模型
    model_classifier = Model([inputs, roi_input], classifier)

    # 同时包含RPN和分类网络的model,用来保存和读取权重信息
    model_all = Model([inputs, roi_input], rpn[:2] + classifier)

    # 返回模型
    return model_rpn, model_classifier, model_all
Ejemplo n.º 3
0
def get_predict_model(config, num_classes):
    inputs = Input(shape=(None, None, 3))
    roi_input = Input(shape=(None, 4))
    feature_map_input = Input(shape=(None, None, 1024))
    #----------------------------------------------------#
    #   假设输入为600,600,3
    #   获得一个38,38,1024的共享特征层base_layers
    #----------------------------------------------------#
    base_layers = ResNet50(inputs)
    #----------------------------------------------------#
    #   每个特征点9个先验框
    #----------------------------------------------------#
    num_anchors = len(config.anchor_box_scales) * len(config.anchor_box_ratios)

    #----------------------------------------------------#
    #   将共享特征层传入建议框网络
    #   该网络结果会对先验框进行调整获得建议框
    #----------------------------------------------------#
    rpn = get_rpn(base_layers, num_anchors)
    model_rpn = Model(inputs, rpn + [base_layers])

    #----------------------------------------------------#
    #   将共享特征层和建议框传入classifier网络
    #   该网络结果会对建议框进行调整获得预测框
    #----------------------------------------------------#
    classifier = get_classifier(feature_map_input, roi_input, num_classes,
                                config.pooling_regions)
    model_classifier_only = Model([feature_map_input, roi_input], classifier)
    return model_rpn, model_classifier_only
Ejemplo n.º 4
0
def get_predict_model(config, num_classes):

    # 设定输入
    inputs = Input(shape=(None, None, 3))
    # 设定roi的个数以及他们的位置坐标,None一般为32,可以在config.py中的num.rois中设置
    roi_input = Input(shape=(None, 4))
    # 输入特征层规格
    feature_map_input = Input(shape=(None, None, 1024))

    # 获取特征提取层的输出
    base_layers = ResNet50(inputs)
    # 生成多少个anchor box
    num_anchors = len(config.anchor_box_scales) * len(config.anchor_box_ratios)
    # 获取RPN
    rpn = get_rpn(base_layers, num_anchors)
    # 建立rpn模型
    model_rpn = Model(inputs, rpn)

    # 获取分类模型的输出
    classifier = get_classifier(feature_map_input,
                                roi_input,
                                config.num_rois,
                                nb_classes=num_classes,
                                trainable=True)
    # 分类模型
    model_classifier_only = Model([feature_map_input, roi_input], classifier)

    # 返回模型
    return model_rpn, model_classifier_only
Ejemplo n.º 5
0
def resnet_retinanet(num_classes, inputs=None, num_anchors=None, submodels=None, name='retinanet'):
    if inputs==None:
        inputs = keras.layers.Input(shape=(600, 600, 3))
    else:
        inputs = inputs
    resnet = ResNet50(inputs)
    
    if num_anchors is None:
        num_anchors = AnchorParameters.default.num_anchors()

    C3, C4, C5 = resnet.outputs[1:]

    P5           = keras.layers.Conv2D(256, kernel_size=1, strides=1, padding='same', name='C5_reduced')(C5)
    # 38x38x256
    P5_upsampled = layers.UpsampleLike(name='P5_upsampled')([P5, C4])
    P5           = keras.layers.Conv2D(256, kernel_size=3, strides=1, padding='same', name='P5')(P5)
    
    # 38x38x256
    P4           = keras.layers.Conv2D(256, kernel_size=1, strides=1, padding='same', name='C4_reduced')(C4)
    P4           = keras.layers.Add(name='P4_merged')([P5_upsampled, P4])

    P4_upsampled = layers.UpsampleLike(name='P4_upsampled')([P4, C3])
    P4           = keras.layers.Conv2D(256, kernel_size=3, strides=1, padding='same', name='P4')(P4)

    # 75x75x256
    P3 = keras.layers.Conv2D(256, kernel_size=1, strides=1, padding='same', name='C3_reduced')(C3)
    P3 = keras.layers.Add(name='P3_merged')([P4_upsampled, P3])
    P3 = keras.layers.Conv2D(256, kernel_size=3, strides=1, padding='same', name='P3')(P3)

    P6 = keras.layers.Conv2D(256, kernel_size=3, strides=2, padding='same', name='P6')(C5)

    P7 = keras.layers.Activation('relu', name='C6_relu')(P6)
    P7 = keras.layers.Conv2D(256, kernel_size=3, strides=2, padding='same', name='P7')(P7)

    features =  [P3, P4, P5, P6, P7]

    regression_model = make_last_layer_loc(num_classes,num_anchors)
    classification_model = make_last_layer_cls(num_classes,num_anchors)

    regressions = []
    classifications = []
    for feature in features:
        regression = regression_model(feature)
        classification = classification_model(feature)
        
        regressions.append(regression)
        classifications.append(classification)

    regressions = keras.layers.Concatenate(axis=1, name="regression")(regressions)
    classifications = keras.layers.Concatenate(axis=1, name="classification")(classifications)
    pyramids = [regressions,classifications]

    model = keras.models.Model(inputs=inputs, outputs=pyramids, name=name)

    return model
Ejemplo n.º 6
0
def centernet(input_shape,
              num_classes,
              backbone='resnet50',
              max_objects=100,
              mode="train",
              num_stacks=2):
    assert backbone in ['resnet50', 'hourglass']
    image_input = Input(shape=input_shape)

    if backbone == 'resnet50':
        #-----------------------------------#
        #   对输入图片进行特征提取
        #   512, 512, 3 -> 16, 16, 2048
        #-----------------------------------#
        C5 = ResNet50(image_input)
        #--------------------------------------------------------------------------------------------------------#
        #   对获取到的特征进行上采样,进行分类预测和回归预测
        #   16, 16, 2048 -> 32, 32, 256 -> 64, 64, 128 -> 128, 128, 64 -> 128, 128, 64 -> 128, 128, num_classes
        #                                                              -> 128, 128, 64 -> 128, 128, 2
        #                                                              -> 128, 128, 64 -> 128, 128, 2
        #--------------------------------------------------------------------------------------------------------#
        y1, y2, y3 = centernet_head(C5, num_classes)

        if mode == "train":
            model = Model(inputs=image_input, outputs=[y1, y2, y3])
            return model
        elif mode == "predict":
            detections = Lambda(lambda x: decode(*x, max_objects=max_objects))(
                [y1, y2, y3])
            prediction_model = Model(inputs=image_input, outputs=detections)
            return prediction_model
        elif mode == "heatmap":
            prediction_model = Model(inputs=image_input, outputs=y1)
            return prediction_model

    else:
        outs = HourglassNetwork(image_input, num_stacks, num_classes)

        if mode == "train":
            temp_outs = []
            for out in outs:
                temp_outs += out
            model = Model(inputs=image_input, outputs=out)
            return model
        elif mode == "predict":
            y1, y2, y3 = outs[-1]
            detections = Lambda(lambda x: decode(*x, max_objects=max_objects))(
                [y1, y2, y3])
            prediction_model = Model(inputs=image_input, outputs=[detections])
            return prediction_model
        elif mode == "heatmap":
            y1, y2, y3 = outs[-1]
            prediction_model = Model(inputs=image_input, outputs=y1)
            return prediction_model
Ejemplo n.º 7
0
    def __init__(self, args):
        self.args = args

        kwargs = {"num_classes": self.args.num_class}

        if args.net == "resnet18":
            from nets.resnet import ResNet18
            model = ResNet18(pretrained=(args.load == 'imagenet'), **kwargs)
        elif args.net == "resnet50":
            from nets.resnet import ResNet50
            model = ResNet50(pretrained=(args.load == 'imagenet'), **kwargs)
        elif args.net == "wideresnet282":
            from nets.wideresnet import WRN28_2
            model = WRN28_2(**kwargs)
        else:
            raise NotImplementedError

        print("Number of parameters",
              sum(p.numel() for p in model.parameters() if p.requires_grad))

        self.model = nn.DataParallel(model).cuda()

        self.optimizer = torch.optim.SGD(self.model.parameters(),
                                         lr=self.args.lr,
                                         momentum=0.9,
                                         nesterov=True,
                                         weight_decay=5e-4)
        self.criterion = nn.CrossEntropyLoss(ignore_index=-1)
        self.criterion_nored = nn.CrossEntropyLoss(reduction="none")

        self.kwargs = {"num_workers": 12, "pin_memory": False}
        self.train_loader, self.val_loader = make_data_loader(
            args, **self.kwargs)

        self.best = 0
        self.best_epoch = 0
        self.acc = []
        self.train_acc = []
        self.med_clean = []
        self.med_noisy = []
        self.perc_clean = []
        self.perc_noisy = []

        self.reductor_plot = umap.UMAP(n_components=2)

        self.toPIL = torchvision.transforms.ToPILImage()

        self.unorm = UnNormalize(mean=(0.485, 0.456, 0.406),
                                 std=(0.229, 0.224, 0.225))
Ejemplo n.º 8
0
def get_model(config, num_classes):
    inputs = Input(shape=(None, None, 3))
    roi_input = Input(shape=(None, 4))
    base_layers = ResNet50(inputs)

    num_anchors = len(config.anchor_box_scales) * len(config.anchor_box_ratios)
    rpn = get_rpn(base_layers, num_anchors)
    model_rpn = Model(inputs, rpn[:2])

    classifier = get_classifier(base_layers,
                                roi_input,
                                config.num_rois,
                                nb_classes=num_classes,
                                trainable=True)
    model_classifier = Model([inputs, roi_input], classifier)

    model_all = Model([inputs, roi_input], rpn[:2] + classifier)
    return model_rpn, model_classifier, model_all
Ejemplo n.º 9
0
def get_predict_model(config, num_classes):
    inputs = Input(shape=(None, None, 3))
    roi_input = Input(shape=(None, 4))
    feature_map_input = Input(shape=(None, None, 1024))

    base_layers = ResNet50(inputs)
    num_anchors = len(config.anchor_box_scales) * len(config.anchor_box_ratios)
    rpn = get_rpn(base_layers, num_anchors)
    model_rpn = Model(inputs, rpn)

    classifier = get_classifier(feature_map_input,
                                roi_input,
                                config.num_rois,
                                nb_classes=num_classes,
                                trainable=True)
    model_classifier_only = Model([feature_map_input, roi_input], classifier)

    return model_rpn, model_classifier_only
Ejemplo n.º 10
0
def get_predict_model(num_classes, backbone, num_anchors=9):
    inputs = Input(shape=(None, None, 3))
    roi_input = Input(shape=(None, 4))

    if backbone == 'vgg':
        feature_map_input = Input(shape=(None, None, 512))
        #----------------------------------------------------#
        #   假设输入为600,600,3
        #   获得一个37,37,512的共享特征层base_layers
        #----------------------------------------------------#
        base_layers = VGG16(inputs)
        #----------------------------------------------------#
        #   将共享特征层传入建议框网络
        #   该网络结果会对先验框进行调整获得建议框
        #----------------------------------------------------#
        rpn = get_rpn(base_layers, num_anchors)
        #----------------------------------------------------#
        #   将共享特征层和建议框传入classifier网络
        #   该网络结果会对建议框进行调整获得预测框
        #----------------------------------------------------#
        classifier = get_vgg_classifier(feature_map_input, roi_input, 7,
                                        num_classes)
    else:
        feature_map_input = Input(shape=(None, None, 1024))
        #----------------------------------------------------#
        #   假设输入为600,600,3
        #   获得一个38,38,1024的共享特征层base_layers
        #----------------------------------------------------#
        base_layers = ResNet50(inputs)
        #----------------------------------------------------#
        #   将共享特征层传入建议框网络
        #   该网络结果会对先验框进行调整获得建议框
        #----------------------------------------------------#
        rpn = get_rpn(base_layers, num_anchors)
        #----------------------------------------------------#
        #   将共享特征层和建议框传入classifier网络
        #   该网络结果会对建议框进行调整获得预测框
        #----------------------------------------------------#
        classifier = get_resnet50_classifier(feature_map_input, roi_input, 14,
                                             num_classes)

    model_rpn = Model(inputs, rpn + [base_layers])
    model_classifier_only = Model([feature_map_input, roi_input], classifier)
    return model_rpn, model_classifier_only
Ejemplo n.º 11
0
def get_model(num_classes,
              backbone,
              num_anchors=9,
              input_shape=[None, None, 3]):
    inputs = Input(shape=input_shape)
    roi_input = Input(shape=(None, 4))

    if backbone == 'vgg':
        #----------------------------------------------------#
        #   假设输入为600,600,3
        #   获得一个37,37,512的共享特征层base_layers
        #----------------------------------------------------#
        base_layers = VGG16(inputs)
        #----------------------------------------------------#
        #   将共享特征层传入建议框网络
        #   该网络结果会对先验框进行调整获得建议框
        #----------------------------------------------------#
        rpn = get_rpn(base_layers, num_anchors)
        #----------------------------------------------------#
        #   将共享特征层和建议框传入classifier网络
        #   该网络结果会对建议框进行调整获得预测框
        #----------------------------------------------------#
        classifier = get_vgg_classifier(base_layers, roi_input, 7, num_classes)
    else:
        #----------------------------------------------------#
        #   假设输入为600,600,3
        #   获得一个38,38,1024的共享特征层base_layers
        #----------------------------------------------------#
        base_layers = ResNet50(inputs)
        #----------------------------------------------------#
        #   将共享特征层传入建议框网络
        #   该网络结果会对先验框进行调整获得建议框
        #----------------------------------------------------#
        rpn = get_rpn(base_layers, num_anchors)
        #----------------------------------------------------#
        #   将共享特征层和建议框传入classifier网络
        #   该网络结果会对建议框进行调整获得预测框
        #----------------------------------------------------#
        classifier = get_resnet50_classifier(base_layers, roi_input, 14,
                                             num_classes)

    model_rpn = Model(inputs, rpn)
    model_all = Model([inputs, roi_input], rpn + classifier)
    return model_rpn, model_all
Ejemplo n.º 12
0
def get_model(config, num_classes):
    inputs = Input(shape=(None, None, 3))
    roi_input = Input(shape=(None, 4))
    base_layers = ResNet50(inputs)  # base_layer是ResNet提取的特征图

    num_anchors = len(config.anchor_box_scales) * len(
        config.anchor_box_ratios)  #3*3
    rpn = get_rpn(base_layers, num_anchors)  #RPN的输出是粗分类和粗定位
    model_rpn = Model(inputs, rpn[:2])
    #classifier是网络的最后输出 包括精分类和精定位 RoiPooling
    classifier = get_classifier(base_layers,
                                roi_input,
                                config.num_rois,
                                nb_classes=num_classes,
                                trainable=True)
    model_classifier = Model([inputs, roi_input], classifier)

    model_all = Model([inputs, roi_input], rpn[:2] + classifier)
    return model_rpn, model_classifier, model_all
Ejemplo n.º 13
0
def get_model(config, num_classes):
    ''' RPN网络输出 置信度+ 框回归 '''
    inputs = Input(shape=(None, None, 3))
    roi_input = Input(shape=(None, 4))
    base_layers = ResNet50(inputs)  # 共享特征层FeatureMap
    # 1 rpn网络
    num_anchors = len(config.anchor_box_scales) * len(
        config.anchor_box_ratios)  # 9=3*3
    rpn = get_rpn(base_layers,
                  num_anchors)  # rpn网络输出[x_class, x_regr, base_layers]
    model_rpn = Model(inputs, rpn[:2])  # ???
    # 2 fast rcnn网络
    classifier = get_classifier(base_layers,
                                roi_input,
                                config.num_rois,
                                nb_classes=num_classes,
                                trainable=True)
    model_classifier = Model([inputs, roi_input], classifier)
    # 3 综合
    model_all = Model([inputs, roi_input], rpn[:2] + classifier)
    return model_rpn, model_classifier, model_all
Ejemplo n.º 14
0
def get_model(config, num_classes):
    '''
    创建训练网络模型
    Parameters
    ----------
    config
    num_classes

    Returns
    -------

    '''
    # 输入主干提取的图片
    inputs = Input(shape=(None, None, 3))
    # roi-pooling层的输入,从rpn层获得的最后建议框,None:一张图片中建议框数量
    roi_input = Input(shape=(None, 4))

    #   假设输入为600,600,3
    #   获得一个38,38,1024的共享特征层base_layers
    base_layers = ResNet50(inputs)

    #   每个特征点9个先验框,先验框边长数量*先验框宽高比例数
    num_anchors = len(config.anchor_box_scales) * len(config.anchor_box_ratios)

    #   将共享特征层传入建议框网络
    #   该网络结果会对先验框进行调整获得建议框
    rpn = get_rpn(base_layers, num_anchors)
    # 这是包含在下面moel_all中的子网络,不必单独训练model_rpn,因为下面model_all训练时会同步更新
    # model_rpn的权重。model_rpn的作用是每次训练和预测时生成用于截取特征图的建议框。训练时每个batch_size
    # 都会更新权重,向好的方向调整,获得的建议框也会越来越精确。额~,我怎么突然想起了GAN网络...
    model_rpn = Model(inputs, rpn)

    #   将共享特征层和建议框传入classifier网络
    #   该网络结果会对建议框进行调整获得预测框
    classifier = get_classifier(base_layers, roi_input, num_classes,
                                config.pooling_regions)
    # 构建包含rpn和分类的两个网络,一起训练,使得两个网络的损失函数整体最小
    model_all = Model([inputs, roi_input], rpn + classifier)

    return model_rpn, model_all
Ejemplo n.º 15
0
def RetinaFace(cfg, backbone="mobilenet"):
    inputs = Input(shape=(None, None, 3))

    if backbone == "mobilenet":
        C3, C4, C5 = MobileNet(inputs)
    elif backbone == "resnet50":
        C3, C4, C5 = ResNet50(inputs)
    else:
        raise ValueError('Unsupported backbone - `{}`, Use mobilenet, resnet50.'.format(backbone))

    leaky = 0
    if (cfg['out_channel'] <= 64):
        leaky = 0.1
    P3 = Conv2D_BN_Leaky(cfg['out_channel'], kernel_size=1, strides=1, padding='same', name='C3_reduced', leaky=leaky)(C3)
    P4 = Conv2D_BN_Leaky(cfg['out_channel'], kernel_size=1, strides=1, padding='same', name='C4_reduced', leaky=leaky)(C4)
    P5 = Conv2D_BN_Leaky(cfg['out_channel'], kernel_size=1, strides=1, padding='same', name='C5_reduced', leaky=leaky)(C5)

    P5_upsampled = UpsampleLike(name='P5_upsampled')([P5, P4])
    P4 = Add(name='P4_merged')([P5_upsampled, P4])
    P4 = Conv2D_BN_Leaky(cfg['out_channel'], kernel_size=3, strides=1, padding='same', name='Conv_P4_merged', leaky=leaky)(P4)

    P4_upsampled = UpsampleLike(name='P4_upsampled')([P4, P3])
    P3 = Add(name='P3_merged')([P4_upsampled, P3])
    P3 = Conv2D_BN_Leaky(cfg['out_channel'], kernel_size=3, strides=1, padding='same', name='Conv_P3_merged', leaky=leaky)(P3)
    
    SSH1 = SSH(P3, cfg['out_channel'], leaky=leaky)
    SSH2 = SSH(P4, cfg['out_channel'], leaky=leaky)
    SSH3 = SSH(P5, cfg['out_channel'], leaky=leaky)

    SSH_all = [SSH1,SSH2,SSH3]

    bbox_regressions = Concatenate(axis=1,name="bbox_reg")([BboxHead(feature) for feature in SSH_all])
    classifications = Concatenate(axis=1,name="cls")([ClassHead(feature) for feature in SSH_all])
    ldm_regressions = Concatenate(axis=1,name="ldm_reg")([LandmarkHead(feature) for feature in SSH_all])

    output = [bbox_regressions, classifications, ldm_regressions]

    model = Model(inputs=inputs, outputs=output)
    return model
Ejemplo n.º 16
0
def get_predict_model(config, num_classes):
    '''
    训练时两步一起训练,使得loss全部最小,预测时分开,加快预测速度
    Parameters
    ----------
    config
    num_classes

    Returns
    -------

    '''
    # 输入主干提取的图片
    inputs = Input(shape=(None, None, 3))
    # roi-pooling层的输入,从rpn层获得的最后建议框,None:一张图片中建议框数量
    roi_input = Input(shape=(None, 4))
    # 主干网络输出的特征层,预测时作为分类网络的输入
    feature_map_input = Input(shape=(None, None, 1024))

    # 假设输入为600,600,3, 获得一个38,38,1024的共享特征层base_layers
    base_layers = ResNet50(inputs)

    #   每个特征点9个先验框
    num_anchors = len(config.anchor_box_scales) * len(config.anchor_box_ratios)

    #   将共享特征层传入建议框网络
    #   该网络结果会对先验框进行调整获得建议框
    rpn = get_rpn(base_layers, num_anchors)
    model_rpn = Model(inputs, rpn + [base_layers])

    #   将共享特征层和建议框传入classifier网络
    #   该网络结果会对建议框进行调整获得预测框
    classifier = get_classifier(feature_map_input, roi_input, num_classes,
                                config.pooling_regions)
    # 此处仅构建分类模型,与训练时的模型不同
    model_classifier_only = Model([feature_map_input, roi_input], classifier)

    return model_rpn, model_classifier_only
Ejemplo n.º 17
0
def centernet(input_shape,
              num_classes,
              backbone='resnet50',
              max_objects=100,
              mode="train",
              num_stacks=2):
    assert backbone in ['resnet50', 'hourglass']
    output_size = input_shape[0] // 4
    image_input = Input(shape=input_shape)
    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, ))

    if backbone == 'resnet50':
        #-----------------------------------#
        #   对输入图片进行特征提取
        #   512, 512, 3 -> 16, 16, 2048
        #-----------------------------------#
        C5 = ResNet50(image_input)
        #--------------------------------------------------------------------------------------------------------#
        #   对获取到的特征进行上采样,进行分类预测和回归预测
        #   16, 16, 2048 -> 32, 32, 256 -> 64, 64, 128 -> 128, 128, 64 -> 128, 128, 64 -> 128, 128, num_classes
        #                                                              -> 128, 128, 64 -> 128, 128, 2
        #                                                              -> 128, 128, 64 -> 128, 128, 2
        #--------------------------------------------------------------------------------------------------------#
        y1, y2, y3 = centernet_head(C5, num_classes)

        if mode == "train":
            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_])
            return model
        elif mode == "predict":
            detections = Lambda(lambda x: decode(*x, max_objects=max_objects))(
                [y1, y2, y3])
            prediction_model = Model(inputs=image_input, outputs=detections)
            return prediction_model
        elif mode == "heatmap":
            prediction_model = Model(inputs=image_input, outputs=y1)
            return prediction_model

    else:
        outs = HourglassNetwork(image_input, num_stacks, num_classes)

        if mode == "train":
            loss_all = []
            for out in outs:
                y1, y2, y3 = out
                loss_ = Lambda(loss)([
                    y1, y2, y3, hm_input, wh_input, reg_input, reg_mask_input,
                    index_input
                ])
                loss_all.append(loss_)
            loss_all = Lambda(tf.reduce_mean, name='centernet_loss')(loss_all)

            model = Model(inputs=[
                image_input, hm_input, wh_input, reg_input, reg_mask_input,
                index_input
            ],
                          outputs=loss_all)
            return model
        elif mode == "predict":
            y1, y2, y3 = outs[-1]
            detections = Lambda(lambda x: decode(*x, max_objects=max_objects))(
                [y1, y2, y3])
            prediction_model = Model(inputs=image_input, outputs=[detections])
            return prediction_model
        elif mode == "heatmap":
            y1, y2, y3 = outs[-1]
            prediction_model = Model(inputs=image_input, outputs=y1)
            return prediction_model
Ejemplo n.º 18
0
def RetinaFace(cfg, backbone="mobilenet"):
    inputs = Input(shape=(None, None, 3))
    #-------------------------------------------#
    #   获得三个shape的有效特征层
    #   分别是C3  80, 80, 64
    #         C4  40, 40, 128
    #         C5  20, 20, 256
    #-------------------------------------------#
    if backbone == "mobilenet":
        C3, C4, C5 = MobileNet(inputs)
    elif backbone == "resnet50":
        C3, C4, C5 = ResNet50(inputs)
    else:
        raise ValueError(
            'Unsupported backbone - `{}`, Use mobilenet, resnet50.'.format(
                backbone))

    leaky = 0
    if (cfg['out_channel'] <= 64):
        leaky = 0.1
    #-------------------------------------------#
    #   获得三个shape的有效特征层
    #   分别是P3  80, 80, 64
    #         P4  40, 40, 64
    #         P5  20, 20, 64
    #-------------------------------------------#
    P3 = Conv2D_BN_Leaky(cfg['out_channel'],
                         kernel_size=1,
                         strides=1,
                         padding='same',
                         kernel_initializer=random_normal(stddev=0.02),
                         name='C3_reduced',
                         leaky=leaky)(C3)
    P4 = Conv2D_BN_Leaky(cfg['out_channel'],
                         kernel_size=1,
                         strides=1,
                         padding='same',
                         kernel_initializer=random_normal(stddev=0.02),
                         name='C4_reduced',
                         leaky=leaky)(C4)
    P5 = Conv2D_BN_Leaky(cfg['out_channel'],
                         kernel_size=1,
                         strides=1,
                         padding='same',
                         kernel_initializer=random_normal(stddev=0.02),
                         name='C5_reduced',
                         leaky=leaky)(C5)

    #-------------------------------------------#
    #   P5上采样和P4特征融合
    #   P4  40, 40, 64
    #-------------------------------------------#
    P5_upsampled = UpsampleLike(name='P5_upsampled')([P5, P4])
    P4 = Add(name='P4_merged')([P5_upsampled, P4])
    P4 = Conv2D_BN_Leaky(cfg['out_channel'],
                         kernel_size=3,
                         strides=1,
                         padding='same',
                         kernel_initializer=random_normal(stddev=0.02),
                         name='Conv_P4_merged',
                         leaky=leaky)(P4)

    #-------------------------------------------#
    #   P4上采样和P3特征融合
    #   P3  80, 80, 64
    #-------------------------------------------#
    P4_upsampled = UpsampleLike(name='P4_upsampled')([P4, P3])
    P3 = Add(name='P3_merged')([P4_upsampled, P3])
    P3 = Conv2D_BN_Leaky(cfg['out_channel'],
                         kernel_size=3,
                         strides=1,
                         padding='same',
                         kernel_initializer=random_normal(stddev=0.02),
                         name='Conv_P3_merged',
                         leaky=leaky)(P3)

    SSH1 = SSH(P3, cfg['out_channel'], leaky=leaky)
    SSH2 = SSH(P4, cfg['out_channel'], leaky=leaky)
    SSH3 = SSH(P5, cfg['out_channel'], leaky=leaky)

    SSH_all = [SSH1, SSH2, SSH3]

    #-------------------------------------------#
    #   将所有结果进行堆叠
    #-------------------------------------------#
    bbox_regressions = Concatenate(
        axis=1, name="bbox_reg")([BboxHead(feature) for feature in SSH_all])
    classifications = Concatenate(
        axis=1, name="cls")([ClassHead(feature) for feature in SSH_all])
    ldm_regressions = Concatenate(
        axis=1, name="ldm_reg")([LandmarkHead(feature) for feature in SSH_all])

    output = [bbox_regressions, classifications, ldm_regressions]

    model = Model(inputs=inputs, outputs=output)
    return model
Ejemplo n.º 19
0
def centernet(input_shape,
              num_classes,
              backbone='resnet50',
              max_objects=100,
              mode="train",
              num_stacks=2):
    assert backbone in ['resnet50', 'hourglass']
    output_size = input_shape[0] // 4
    image_input = Input(shape=input_shape)
    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, ))

    if backbone == 'resnet50':
        #-------------------------------#
        #   编码器
        #-------------------------------#
        C5 = ResNet50(image_input)

        y1, y2, y3 = centernet_head(C5, num_classes)

        if mode == "train":
            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_])
            return model
        else:
            detections = Lambda(lambda x: decode(
                *x, max_objects=max_objects, num_classes=num_classes))(
                    [y1, y2, y3])
            prediction_model = Model(inputs=image_input, outputs=detections)
            return prediction_model

    else:
        outs = HourglassNetwork(image_input, num_stacks, num_classes)

        if mode == "train":
            loss_all = []
            for out in outs:
                y1, y2, y3 = out
                loss_ = Lambda(loss)([
                    y1, y2, y3, hm_input, wh_input, reg_input, reg_mask_input,
                    index_input
                ])
                loss_all.append(loss_)
            loss_all = Lambda(tf.reduce_mean, name='centernet_loss')(loss_all)

            model = Model(inputs=[
                image_input, hm_input, wh_input, reg_input, reg_mask_input,
                index_input
            ],
                          outputs=loss_all)
            return model
        else:
            y1, y2, y3 = outs[-1]
            detections = Lambda(lambda x: decode(
                *x, max_objects=max_objects, num_classes=num_classes))(
                    [y1, y2, y3])
            prediction_model = Model(inputs=image_input, outputs=[detections])
            return prediction_model
Ejemplo n.º 20
0
            num_params += np.prod(data[key][name].shape)
    return num_params

def check_shape(net):
    for var in tf.global_variables():
        key, name = net.find_key_name(var)
        var_shape = tuple(var.get_shape().as_list())
        np_shape = data[key][name].shape
        if var_shape != np_shape:
            print('incorrect var shape')
 
if __name__ == '__main__':
    from os.path import join, abspath, dirname
    pretrain_root = abspath(join(dirname(__file__), '../../model/pretrained_model/ori_resnet/'))

    resnet50 = ResNet50()
    resnet101 = ResNet101()
    resnet152 = ResNet152()

    X = tf.placeholder(tf.float32, shape=(None, 224, 224, 3))
    y = resnet50(X)
    data = np.load(join(pretrain_root, 'resnet50.npy'), encoding='bytes').item()
    print('our resnet50 has parameters: %d'%count_total_variables())
    print('pretrained resnet50 parameters: %d'%count_total_npvars(data))
    check_shape(resnet50)
 
    tf.reset_default_graph()
    X = tf.placeholder(tf.float32, shape=(None, 224, 224, 3))
    y = resnet101(X)
    data = np.load(join(pretrain_root, 'resnet101.npy'), encoding='bytes').item()
    print('our resnet101 has parameters: %d'%count_total_variables())
Ejemplo n.º 21
0
from utils import load_image
from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from tensorflow.keras import optimizers

import tensorflow as tf

if __name__ == "__main__":
    gpus = tf.config.list_physical_devices(device_type="GPU")
    for gpu in gpus:
        tf.config.experimental.set_memory_growth(device=gpu, enable=True)

    train_dir = "datasets/train"
    val_dir = "datasets/val"
    batch_size = 2048
    image_size = 224
    model = ResNet50(input_shape=(image_size, image_size, 3), classes=2)
    # model.load_weights("ResNet50.h5")
    model.summary()
    opt = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
    model.compile(optimizer=opt,
                  loss=tf.keras.losses.sparse_categorical_crossentropy,
                  metrics=[tf.keras.metrics.sparse_categorical_accuracy])


    plot_model(model, to_file='models/ResNet50.png',show_shapes=True, show_layer_names=True)

    logging = TensorBoard(log_dir="logs")
    checkpoint = ModelCheckpoint("models" + "/ResNet_{epoch:04d}-{val_loss:.4f}.h5", monitor="val_loss", mode="min",
                                 verbose=1, save_weights_only=True, save_best_only=True, period=3)

    early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=15, verbose=1)
Ejemplo n.º 22
0
def resnet_retinanet(num_classes, input_shape=(600, 600, 3), name='retinanet'):
    inputs = keras.layers.Input(shape=input_shape)
    resnet = ResNet50(inputs)
    #-----------------------------------------#
    #   取出三个有效特征层,分别是C3、C4、C5
    #   C3     75,75,512
    #   C4     38,38,1024
    #   C5     19,19,2048
    #-----------------------------------------#
    C3, C4, C5 = resnet.outputs[1:]

    # 75,75,512 -> 75,75,256
    P3 = keras.layers.Conv2D(256,
                             kernel_size=1,
                             strides=1,
                             padding='same',
                             name='C3_reduced')(C3)
    # 38,38,1024 -> 38,38,256
    P4 = keras.layers.Conv2D(256,
                             kernel_size=1,
                             strides=1,
                             padding='same',
                             name='C4_reduced')(C4)
    # 19,19,2048 -> 19,19,256
    P5 = keras.layers.Conv2D(256,
                             kernel_size=1,
                             strides=1,
                             padding='same',
                             name='C5_reduced')(C5)

    # 19,19,256 -> 38,38,256
    P5_upsampled = layers.UpsampleLike(name='P5_upsampled')([P5, P4])
    # 38,38,256 + 38,38,256 -> 38,38,256
    P4 = keras.layers.Add(name='P4_merged')([P5_upsampled, P4])
    # 38,38,256 -> 75,75,256
    P4_upsampled = layers.UpsampleLike(name='P4_upsampled')([P4, P3])
    # 75,75,256 + 75,75,256 -> 75,75,256
    P3 = keras.layers.Add(name='P3_merged')([P4_upsampled, P3])

    # 75,75,256 -> 75,75,256
    P3 = keras.layers.Conv2D(256,
                             kernel_size=3,
                             strides=1,
                             padding='same',
                             name='P3')(P3)
    # 38,38,256 -> 38,38,256
    P4 = keras.layers.Conv2D(256,
                             kernel_size=3,
                             strides=1,
                             padding='same',
                             name='P4')(P4)
    # 19,19,256 -> 19,19,256
    P5 = keras.layers.Conv2D(256,
                             kernel_size=3,
                             strides=1,
                             padding='same',
                             name='P5')(P5)

    # 19,19,2048 -> 10,10,256
    P6 = keras.layers.Conv2D(256,
                             kernel_size=3,
                             strides=2,
                             padding='same',
                             name='P6')(C5)
    P7 = keras.layers.Activation('relu', name='C6_relu')(P6)
    # 10,10,256 -> 5,5,256
    P7 = keras.layers.Conv2D(256,
                             kernel_size=3,
                             strides=2,
                             padding='same',
                             name='P7')(P7)

    features = [P3, P4, P5, P6, P7]

    num_anchors = AnchorParameters.default.num_anchors()
    regression_model = make_last_layer_loc(num_classes, num_anchors)
    classification_model = make_last_layer_cls(num_classes, num_anchors)

    regressions = []
    classifications = []

    #----------------------------------------------------------#
    #   将获取到的P3, P4, P5, P6, P7传入到
    #   Retinahead里面进行预测,获得回归预测结果和分类预测结果
    #   将所有特征层的预测结果进行堆叠
    #----------------------------------------------------------#
    for feature in features:
        regression = regression_model(feature)
        classification = classification_model(feature)

        regressions.append(regression)
        classifications.append(classification)

    regressions = keras.layers.Concatenate(axis=1,
                                           name="regression")(regressions)
    classifications = keras.layers.Concatenate(
        axis=1, name="classification")(classifications)

    model = keras.models.Model(inputs, [regressions, classifications],
                               name=name)

    return model