Exemple #1
0
def init_model(cfg, device, id_gpu=None):
    model = models.resnet18(pretrained=True)
    model = ModelOut4Resnet18(model)
    cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res18'

    model = Center(backbone=model, cfg=cfg)
    # f_look_model(model, input=(1, 3, *cfg.IMAGE_SIZE))

    if cfg.IS_LOCK_BACKBONE_WEIGHT:
        for name, param in model.net.backbone.named_parameters():
            param.requires_grad_(False)

    model, is_mgpu = model_device_init(model, device, id_gpu, cfg)
    # ------------------------模型完成-------------------------------

    pg = model.parameters()
    optimizer = optim.Adam(pg, cfg.LR0)
    lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [50, 80, 120],
                                                  0.1)
    start_epoch = load_weight(cfg.FILE_FIT_WEIGHT,
                              model,
                              optimizer,
                              lr_scheduler,
                              device,
                              is_mgpu=is_mgpu)

    model.cfg = cfg
    return model, optimizer, lr_scheduler, start_epoch
Exemple #2
0
def init_model(cfg, device, id_gpu=None):
    model = models.resnet18(pretrained=True)
    dims_out = (128, 256, 512)
    model = ModelOuts4Resnet(model, dims_out)
    cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res18'

    if cfg.MODE_TRAIN == 1 or cfg.MODE_TRAIN == 2 or cfg.MODE_TRAIN == 5:
        ''' 
        1. 输出4层 共享输出
        2. 输出4层 使用新FPN 共享
        '''
        cfg.STRIDES = [8, 16, 32, 64]  # 特有参数下采样步距
        cfg.SCALE_THRESHOLDS = [0, 49, 98, 196, 10000000000.0
                                ]  # 用于确保每一个特图预测相应大小的框,且一个GT只在一个层进行匹配
    elif cfg.MODE_TRAIN == 3 or cfg.MODE_TRAIN == 4:
        ''' 输出4层 使用新FPN 共享 '''
        # model = models.resnet50(pretrained=True)
        # dims_out = (512, 1024, 2048)
        # model = ModelOuts4Resnet(model, dims_out)
        # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res50'
        cfg.STRIDES = [8, 16, 32, 64, 128]  # size需要512 128的倍数
        cfg.SCALE_THRESHOLDS = [0, 64, 128, 256, 512, 10000000000.0]
    else:
        raise Exception('cfg.MODE_TRAIN 出错 cfg.MODE_TRAIN=%s' %
                        (cfg.MODE_TRAIN))

    model = Fcos(model, cfg, device)

    if cfg.IS_LOCK_BACKBONE_WEIGHT:
        for name, param in model.backbone.named_parameters():
            param.requires_grad_(False)

    model, is_mgpu = model_device_init(model, device, id_gpu, cfg)

    # ------------------------自定义backbone完成-------------------------------
    pg = model.parameters()
    optimizer = optim.Adam(pg, lr=cfg.LR0)
    # 两次不上升,降低一半
    # lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=3, factor=0.5, verbose=True)
    lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [50, 80, 100],
                                                  0.1)
    start_epoch = load_weight(cfg.FILE_FIT_WEIGHT,
                              model,
                              optimizer,
                              lr_scheduler,
                              device,
                              is_mgpu=is_mgpu)

    model.cfg = cfg
    return model, optimizer, lr_scheduler, start_epoch
Exemple #3
0
def init_model(cfg, device, id_gpu=None):
    # model = models.mobilenet_v2(pretrained=True)
    # model = ModelOut4Mobilenet_v2(model)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_mv3'

    # model = f_get_mv3(cfg.PATH_HOST, device)
    # model = ModelOut4Mobilenet_v3(model)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_mv3'

    model = models.resnet18(pretrained=True)
    model = ModelOut4Resnet18(model)
    cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res18'

    # model = models.resnet50(pretrained=True)
    # model = ModelOut4Resnet50(model)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res50'

    # model = darknet19(pretrained=True)
    # model = ModelOut4DarkNet19(model)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_d19'

    model = YOLOv1(backbone=model, cfg=cfg)
    # f_look_model(model, input=(1, 3, *cfg.IMAGE_SIZE))

    if cfg.IS_LOCK_BACKBONE_WEIGHT:
        for name, param in model.backbone.named_parameters():
            param.requires_grad_(False)

    model, is_mgpu = model_device_init(model, device, id_gpu, cfg)

    # ------------------------自定义backbone完成-------------------------------
    pg = model.parameters()
    optimizer = optim.Adam(pg, cfg.LR0)
    # optimizer = optim.Adam(pg, cfg.LR0, weight_decay=5e-4)
    # optimizer = optim.SGD(pg, lr=cfg.LR0, momentum=0.937, weight_decay=5e-4, nesterov=True)
    # optimizer = optim.SGD(pg, lr=cfg.LR0, momentum=0.9, weight_decay=5e-4)
    # 两次不上升,降低一半
    # lr_scheduler = None
    lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [50, 80, 100], 0.1)
    # lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [10, 20, 30], 0.1)
    # lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.8, patience=1, verbose=True)
    # start_epoch = load_weight(cfg.FILE_FIT_WEIGHT, model, None, lr_scheduler, device, is_mgpu=is_mgpu)
    start_epoch = load_weight(cfg.FILE_FIT_WEIGHT, model, optimizer, lr_scheduler, device, is_mgpu=is_mgpu)

    model.cfg = cfg
    return model, optimizer, lr_scheduler, start_epoch
Exemple #4
0
def init_model(cfg, device, id_gpu=None):
    model = models.resnet18(pretrained=True)
    dims_out = (128, 256, 512)
    model = ModelOuts4Resnet(model, dims_out)
    cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res18'

    # model = models.resnet50(pretrained=True)
    # dims_out = (512, 1024, 2048)
    # model = ModelOuts4Resnet(model, dims_out)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res50'

    cfg.STRIDES = [8, 16, 32, 64]  # 特有参数下采样步距
    assert cfg.IMAGE_SIZE[0] % cfg.STRIDES[
        -1] == 0, 'cfg.IMAGE_SIZE=%s 与 cfg.STRIDES=%s 不能匹配(整除)' % (
            cfg.IMAGE_SIZE, cfg.STRIDES)
    cfg.SCALE_THRESHOLDS = [0, 49, 98, 196,
                            10000000000.0]  # 用于确保每一个特图预测相应大小的框,且一个GT只在一个层进行匹配

    model = KFcos(model, cfg)

    if cfg.IS_LOCK_BACKBONE_WEIGHT:
        for name, param in model.backbone.named_parameters():
            param.requires_grad_(False)

    model, is_mgpu = model_device_init(model, device, id_gpu, cfg)

    # ------------------------自定义backbone完成-------------------------------
    pg = model.parameters()
    # optimizer = optim.Adam(pg, lr=cfg.LR0)
    optimizer = optim.SGD(pg, lr=cfg.LR0, momentum=0.9, weight_decay=5e-6)
    # 两次不上升,降低一半
    lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer,
                                                        patience=10,
                                                        factor=0.1,
                                                        verbose=True)
    # lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [50, 80, 100], 0.1)
    start_epoch = load_weight(cfg.FILE_FIT_WEIGHT,
                              model,
                              optimizer,
                              lr_scheduler,
                              device,
                              is_mgpu=is_mgpu)

    model.cfg = cfg
    return model, optimizer, lr_scheduler, start_epoch
Exemple #5
0
def init_model(cfg, device, id_gpu=None):
    # model = darknet53(pretrained=True)
    # model = ModelOuts4DarkNet53(model)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_dark53'

    # model = darknet19(pretrained=True)
    # model = ModelOuts4DarkNet19(model)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_dark19'

    model = models.resnet18(pretrained=True)
    dims_out = (128, 256, 512)
    model = ModelOuts4Resnet(model, dims_out)
    cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res18'

    # model = CSPDarknet_slim()
    # model.dims_out = (128, 256, 512)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_csspds'

    model = Yolo_v3(backbone=model, cfg=cfg)
    # f_look_model(model, input=(1, 3, *cfg.IMAGE_SIZE))

    if cfg.IS_LOCK_BACKBONE_WEIGHT:
        for name, param in model.net.backbone.named_parameters():
            param.requires_grad_(False)

    model, is_mgpu = model_device_init(model, device, id_gpu, cfg)
    # ------------------------模型完成-------------------------------

    pg = model.parameters()
    optimizer = optim.Adam(pg, cfg.LR0)
    lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [50, 80, 100],
                                                  0.1)
    start_epoch = load_weight(cfg.FILE_FIT_WEIGHT,
                              model,
                              optimizer,
                              lr_scheduler,
                              device,
                              is_mgpu=is_mgpu)

    model.cfg = cfg
    return model, optimizer, lr_scheduler, start_epoch
Exemple #6
0
def init_model(cfg, device, id_gpu=None):
    # model = models.resnet50(pretrained=True)
    # model = ModelOut4Resnet50(model)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res50'

    # model = darknet19(pretrained=True)
    # model = ModelOut4DarkNet19(model)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_d19'

    model = Backbone()
    model = SSD(backbone=model, cfg=cfg, device=device)
    # f_look_model(model, input=(1, 3, *cfg.IMAGE_SIZE))

    if cfg.IS_LOCK_BACKBONE_WEIGHT:
        for name, param in model.backbone.named_parameters():
            param.requires_grad_(False)

    model, is_mgpu = model_device_init(model, device, id_gpu, cfg)

    # ------------------------自定义backbone完成-------------------------------
    pg = model.parameters()
    # optimizer = optim.Adam(pg, cfg.LR0)
    optimizer = optim.SGD(pg, lr=cfg.LR0, momentum=0.9, weight_decay=0.0005)
    # optimizer = optim.Adam(pg, cfg.LR0, weight_decay=5e-4)
    # optimizer = optim.SGD(pg, lr=cfg.LR0, momentum=0.937, weight_decay=5e-4, nesterov=True)
    # optimizer = optim.SGD(pg, lr=cfg.LR0, momentum=0.9, weight_decay=5e-4)

    # 两次不上升,降低一半
    lr_scheduler = None

    # lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [50, 80, 100], 0.1)
    # lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [10, 20, 30], 0.1)
    # lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.8, patience=1, verbose=True)
    # lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [50, 80, 100], 0.1)

    # start_epoch = load_weight(cfg.FILE_FIT_WEIGHT, model, None, lr_scheduler, device, is_mgpu=is_mgpu)

    ''' 定制逻辑 '''

    def ffun(pretrained_dict):
        dd = {}
        s1 = 'net.backbone.'
        s2 = 'net.'
        for k, v in pretrained_dict.items():
            split_key = k.split(".")
            if 'additional_blocks' in split_key:
                dd[s2 + k] = v
            elif 'loc' in split_key:
                dd[s2 + k] = v
            elif 'feature_extractor' in split_key:
                k = '.'.join(split_key[1:])
                dd[s1 + k] = v

        return dd

    if cfg.FILE_NAME_WEIGHT == 'nvidia_ssdpyt_fp32_190826.pt':
        # 定制加载预训练模型
        start_epoch = load_weight(cfg.FILE_FIT_WEIGHT, model, None, None, device, is_mgpu=is_mgpu,
                                  ffun=ffun)
    else:
        start_epoch = load_weight(cfg.FILE_FIT_WEIGHT, model, optimizer, lr_scheduler, device, is_mgpu=is_mgpu,
                                  ffun=None)

    model.cfg = cfg
    return model, optimizer, lr_scheduler, start_epoch
Exemple #7
0
def init_model(cfg, device, id_gpu=None):
    # model = models.densenet121(pretrained=True)
    # ret_name_dict = {'denseblock2': 1, 'denseblock3': 2, 'denseblock4': 3}
    # dims_out = [512, 1024, 1024]
    # model = ModelOuts4Densenet121(model, 'features', ret_name_dict, dims_out)
    # cfg.FEATURE_MAP_STEPS = [8, 16, 32]  # 特图的步距 下采倍数
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + 'densenet121'

    # model = models.resnet50(pretrained=True)
    # dims_out = (512, 1024, 2048)
    # model = ModelOuts4Resnet(model, dims_out)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res50'
    # cfg.FEATURE_MAP_STEPS = [8, 16, 32]  # 特图的步距 下采倍数

    model = models.resnet18(pretrained=True)
    dims_out = (128, 256, 512)
    model = ModelOuts4Resnet(model, dims_out)

    cfg.FEATURE_MAP_STEPS = [8, 16, 32, 64, 128]

    # 每层特图5个anc
    cfg.NUMS_ANC = [1, 1, 1, 1, 1]
    cfg.ANCS_SCALE = [[0.078, 0.07775],
                      [0.174, 0.164],
                      [0.324, 0.336],
                      [0.578, 0.466],
                      [0.698, 0.674]]

    # cfg.NUMS_ANC = [3, 3, 3, 3, 3]
    # cfg.ANCS_SCALE = [[0.052, 0.046],
    #                   [0.088, 0.092],
    #                   [0.152, 0.106],
    #                   [0.184, 0.186],
    #                   [0.224, 0.32],
    #                   [0.38, 0.242],
    #                   [0.338, 0.424],
    #                   [0.572, 0.32],
    #                   [0.34, 0.624],
    #                   [0.484, 0.527],
    #                   [0.7, 0.44],
    #                   [0.642, 0.656],
    #                   [0.836, 0.573],
    #                   [0.61, 0.882],
    #                   [0.92, 0.74231]]
    # cfg.LOSS_WEIGHT = [1, 1, 1, 1, 1]  # conf_pos conf_neg cls loss_txty  loss_twth
    model = Retina2(model, cfg, device)
    # model = Retina3(model, cfg, device)

    # cfg.NUMS_ANC = [3, 3, 3]
    # cfg.FEATURE_MAP_STEPS = [8, 16, 32]
    # model = Retina(model, cfg, device)

    if cfg.IS_LOCK_BACKBONE_WEIGHT:
        for name, param in model.backbone.named_parameters():
            param.requires_grad_(False)

    model, is_mgpu = model_device_init(model, device, id_gpu, cfg)

    # ------------------------自定义backbone完成-------------------------------
    pg = model.parameters()
    optimizer = optim.Adam(pg, lr=cfg.LR0)
    # 两次不上升,降低一半
    # lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=3, factor=0.5, verbose=True)
    lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [50, 70, 100], 0.1)
    start_epoch = load_weight(cfg.FILE_FIT_WEIGHT, model, optimizer, lr_scheduler, device, is_mgpu=is_mgpu)

    model.cfg = cfg
    return model, optimizer, lr_scheduler, start_epoch
Exemple #8
0
def init_model(cfg, device, id_gpu=None):
    if cfg.MODE_TRAIN == 99:
        model = darknet19(pretrained=True)
        model = ModelOuts4DarkNet19(model)
        cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_d19'
    elif cfg.MODE_TRAIN == 98:
        model = darknet_tiny(pretrained=True)
        model = ModelOuts4DarkNet19_Tiny(model)
        cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_d19_t'
    else:
        raise Exception('无法匹配')

    # model = models.resnet18(pretrained=True)
    # dims_out = (128, 256, 512)
    # model = ModelOuts4Resnet(model, dims_out)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res18'

    # model = models.resnet50(pretrained=True)
    # dims_out = (512, 1024, 2048)
    # model = ModelOuts4Resnet(model, dims_out)
    # cfg.SAVE_FILE_NAME = cfg.SAVE_FILE_NAME + '_res50'

    model = Yolo_v2(backbone=model, cfg=cfg)
    # f_look_model(model, input=(1, 3, *cfg.IMAGE_SIZE))

    if cfg.IS_LOCK_BACKBONE_WEIGHT:
        for name, param in model.backbone.named_parameters():
            param.requires_grad_(False)

    model, is_mgpu = model_device_init(model, device, id_gpu, cfg)

    # ------------------------自定义backbone完成-------------------------------
    pg = model.parameters()
    # optimizer = optim.Adam(pg, cfg.LR0)
    # optimizer = optim.Adam(pg, cfg.LR0)
    optimizer = optim.SGD(model.parameters(),
                          lr=cfg.LR0,
                          momentum=0.9,
                          weight_decay=5e-4)
    # 两次不上升,降低一半
    lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [50, 80, 100],
                                                  0.1)

    # lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [150, 200], 0.1)

    def ffun(pretrained_dict):
        '''
        model的 _modules 属性中
        :param pretrained_dict:
        :return:
        '''
        dd = {}
        s1 = 'net.'
        s2 = 'net.backbone.model_hook.'
        for k, v in pretrained_dict.items():
            split_key = k.split(".")
            # dd[s2 + k] = v  # 所有添加net
            if 'convsets_1' in split_key:
                dd[s1 + k] = v
            elif 'route_layer' in split_key:
                dd[s1 + k] = v
            elif 'reorg' in split_key:
                dd[s1 + k] = v
            elif 'convsets_2' in split_key:
                dd[s1 + k] = v
            # elif 'pred' in split_key:  # 这层丢掉 可适应各种数据集
            #     dd[k.replace('pred', 'net.head')] = v  # 直接替换
            elif 'backbone' in split_key:
                k = '.'.join(split_key[1:])  # 截断添加 第一句不要
                dd[s2 + k] = v

        return dd

    if cfg.FILE_NAME_WEIGHT == 'zz/voc/yolov2_d19/yolov2_d19_77.1_78.1.pth':
        # 定制加载预训练模型
        start_epoch = load_weight(cfg.FILE_FIT_WEIGHT,
                                  model,
                                  None,
                                  None,
                                  device,
                                  is_mgpu=is_mgpu,
                                  ffun=ffun)
    else:
        start_epoch = load_weight(cfg.FILE_FIT_WEIGHT,
                                  model,
                                  optimizer,
                                  lr_scheduler,
                                  device,
                                  is_mgpu=is_mgpu,
                                  ffun=None)

    model.cfg = cfg
    return model, optimizer, lr_scheduler, start_epoch