コード例 #1
0
def eval_anchor_cfg(anchor_scales,
                    anchor_ratios,
                    img_dim=(512,512)):

    my_cfg=copy.deepcopy(cfg.model['bbox_head'])
    if not (len(anchor_ratios)==len(anchor_scales)==0):
        my_cfg['anchor_scales']=anchor_scales
        my_cfg['anchor_ratios']=anchor_ratios
    anchor_head=build_head(my_cfg)

    anchs, flags =anchor_head.get_anchors(featmap_sizes,[met])

    all_anchors=torch.cat(anchs[0])
    all_flags=torch.cat(flags[0])

    f_anchors=all_anchors[all_flags,:]
    a_mins=f_anchors.min(1)[0]
    f_anchors=f_anchors[a_mins>=0,:]

    x_max=f_anchors[:,2]
    f_anchors=f_anchors[x_max<=img_dim[0],:]
    y_max=f_anchors[:,3]
    f_anchors=f_anchors[y_max<=img_dim[1],:]



    f_anchors=f_anchors.cpu().numpy()

    s=score(boxes,f_anchors)

    #heights=boxes[:,3]-boxes[:,1]
    #widths=boxes[:,2]-boxes[:,0]
    #plt.scatter(widths,heights,s=0.5,c=s>0.8,cmap='coolwarm')
    #plt.savefig("out.png")
    return -s.sum()
コード例 #2
0
 def init_track_head(self, track_roi_extractor, track_head):
     """Initialize ``track_head``"""
     if track_roi_extractor is not None:
         self.track_roi_extractor = build_roi_extractor(track_roi_extractor)
         self.track_share_extractor = False
     else:
         self.track_share_extractor = True
         self.track_roi_extractor = self.bbox_roi_extractor
     self.track_head = build_head(track_head)
コード例 #3
0
ファイル: two_stage.py プロジェクト: anorthman/custom
    def __init__(self, cfg, train_cfg, test_cfg):
        super(BaseDetector, self).__init__()

        if 'neck' in cfg:
            self.neck = build_neck(cfg['neck'])

        if 'rpn_head' in cfg:
            self.rpn_head = build_head(cfg['rpn_head'])

        if 'bbox_head' in cfg:
            self.bbox_roi_extractor = build_roi_extractor(
                cfg['bbox_roi_extractor'])
            self.bbox_head = build_head(cfg['bbox_head'])

        if 'mask_head' in cfg:
            self.mask_roi_extractor = build_roi_extractor(
                cfg['mask_roi_extractor'])
            self.mask_head = build_head(cfg['mask_head'])

        self.train_cfg = train_cfg
        self.test_cfg = test_cfg
コード例 #4
0
 def __init__(self,
              backbone,
              neck=None,
              bbox_head=None,
              train_cfg=None,
              test_cfg=None,
              init_cfg=None,
              pretrained=None):
     super(SingleStage3DDetector, self).__init__(init_cfg)
     self.backbone = build_backbone(backbone)
     if neck is not None:
         self.neck = build_neck(neck)
     bbox_head.update(train_cfg=train_cfg)
     bbox_head.update(test_cfg=test_cfg)
     self.bbox_head = build_head(bbox_head)
     self.train_cfg = train_cfg
     self.test_cfg = test_cfg
コード例 #5
0
def main():
    args = parse_args()

    config = mmcv.Config.fromfile(os.path.join(root, args.config))
    data = torch.randn(1, 3, 800, 800)

    with torch.no_grad():
        backbone = build_backbone(config.model.backbone)
        neck = build_neck(config.model.neck)
        rpn_head = build_head(config.model.rpn_head)

    backbone.eval()
    neck.eval()
    rpn_head.eval()

    #torch.jit.save(rpn_head,'./rpn_head.pt')
    exit()
コード例 #6
0
def ml():
            idxs = [idx for idx in range(popsize) if idx != j]
            a, b, c = pop[np.random.choice(idxs, 3, replace = False)]

            mutant = a + mut * (b - c)
            #mutant = np.clip(a + mut * (b - c), 0, 1)
            cross_points = np.random.rand(dimensions) < crossp
            if not np.any(cross_points):
                cross_points[np.random.randint(0, dimensions)] = True
            trial = np.where(cross_points, mutant, pop[j])


            trial_denorm = min_b + trial * diff

            assert len(trial_denorm)==5
            scales=trial_denorm[:3].tolist()
            ratios=trial_denorm[3:].tolist()
            ratios.extend([1,1/ratios[0],1/ratios[1]])
            my_cfg=copy.deepcopy(cfg.model['bbox_head'])

            if not (len(ratios)==len(scales)==0):
                my_cfg['anchor_scales']=scales
                my_cfg['anchor_ratios']=ratios
            anchor_head=build_head(my_cfg)

            anchs, flags =anchor_head.get_anchors(featmap_sizes,[met])

            all_anchors=torch.cat(anchs[0])
            all_flags=torch.cat(flags[0])

            f_anchors=all_anchors[all_flags,:]
            a_mins=f_anchors.min(1)[0]
            f_anchors=f_anchors[a_mins>=0,:]

            x_max=f_anchors[:,2]
            f_anchors=f_anchors[x_max<=img_dim[0],:]
            y_max=f_anchors[:,3]
            f_anchors=f_anchors[y_max<=img_dim[1],:]

            f = fobj(trial_denorm)
            if f < fitness[j]:
                fitness[j] = f
                pop[j] = trial
                if f < fitness[best_idx]:
                    best_idx = j
                    best = trial_denorm
コード例 #7
0
    def __init__(self,
                 detector=None,
                 track_head=None,
                 tracker=None,
                 freeze_detector=False,
                 *args,
                 **kwargs):
        super().__init__(*args, **kwargs)
        if detector is not None:
            self.detector = build_detector(detector)

        if track_head is not None:
            self.track_head = build_head(track_head)

        if tracker is not None:
            self.tracker = build_tracker(tracker)

        self.freeze_detector = freeze_detector
        if self.freeze_detector:
            self.freeze_module('detector')
コード例 #8
0
 def __init__(self,
              backbone,
              neck,
              neck_3d,
              bbox_head,
              n_voxels,
              anchor_generator,
              train_cfg=None,
              test_cfg=None,
              pretrained=None,
              init_cfg=None):
     super().__init__(init_cfg=init_cfg)
     self.backbone = build_backbone(backbone)
     self.neck = build_neck(neck)
     self.neck_3d = build_neck(neck_3d)
     bbox_head.update(train_cfg=train_cfg)
     bbox_head.update(test_cfg=test_cfg)
     self.bbox_head = build_head(bbox_head)
     self.n_voxels = n_voxels
     self.anchor_generator = build_anchor_generator(anchor_generator)
     self.train_cfg = train_cfg
     self.test_cfg = test_cfg
コード例 #9
0
def test_guided_anchor():
    from mmdet.models import build_head
    if torch.cuda.is_available():
        device = 'cuda'
    else:
        device = 'cpu'
    # head configs modified from
    # configs/guided_anchoring/ga_retinanet_r50_fpn_1x_coco.py
    bbox_head = dict(type='GARetinaHead',
                     num_classes=8,
                     in_channels=4,
                     stacked_convs=1,
                     feat_channels=4,
                     approx_anchor_generator=dict(type='AnchorGenerator',
                                                  octave_base_scale=4,
                                                  scales_per_octave=3,
                                                  ratios=[0.5, 1.0, 2.0],
                                                  strides=[8, 16, 32, 64,
                                                           128]),
                     square_anchor_generator=dict(type='AnchorGenerator',
                                                  ratios=[1.0],
                                                  scales=[4],
                                                  strides=[8, 16, 32, 64,
                                                           128]))

    ga_retina_head = build_head(bbox_head)
    assert ga_retina_head.approx_anchor_generator is not None

    # use the featmap sizes in NASFPN setting to test ga_retina_head
    featmap_sizes = [(100, 152), (50, 76), (25, 38), (13, 19), (7, 10)]
    # check base anchors
    expected_approxs = [
        torch.Tensor([[-22.6274, -11.3137, 22.6274, 11.3137],
                      [-28.5088, -14.2544, 28.5088, 14.2544],
                      [-35.9188, -17.9594, 35.9188, 17.9594],
                      [-16.0000, -16.0000, 16.0000, 16.0000],
                      [-20.1587, -20.1587, 20.1587, 20.1587],
                      [-25.3984, -25.3984, 25.3984, 25.3984],
                      [-11.3137, -22.6274, 11.3137, 22.6274],
                      [-14.2544, -28.5088, 14.2544, 28.5088],
                      [-17.9594, -35.9188, 17.9594, 35.9188]]),
        torch.Tensor([[-45.2548, -22.6274, 45.2548, 22.6274],
                      [-57.0175, -28.5088, 57.0175, 28.5088],
                      [-71.8376, -35.9188, 71.8376, 35.9188],
                      [-32.0000, -32.0000, 32.0000, 32.0000],
                      [-40.3175, -40.3175, 40.3175, 40.3175],
                      [-50.7968, -50.7968, 50.7968, 50.7968],
                      [-22.6274, -45.2548, 22.6274, 45.2548],
                      [-28.5088, -57.0175, 28.5088, 57.0175],
                      [-35.9188, -71.8376, 35.9188, 71.8376]]),
        torch.Tensor([[-90.5097, -45.2548, 90.5097, 45.2548],
                      [-114.0350, -57.0175, 114.0350, 57.0175],
                      [-143.6751, -71.8376, 143.6751, 71.8376],
                      [-64.0000, -64.0000, 64.0000, 64.0000],
                      [-80.6349, -80.6349, 80.6349, 80.6349],
                      [-101.5937, -101.5937, 101.5937, 101.5937],
                      [-45.2548, -90.5097, 45.2548, 90.5097],
                      [-57.0175, -114.0350, 57.0175, 114.0350],
                      [-71.8376, -143.6751, 71.8376, 143.6751]]),
        torch.Tensor([[-181.0193, -90.5097, 181.0193, 90.5097],
                      [-228.0701, -114.0350, 228.0701, 114.0350],
                      [-287.3503, -143.6751, 287.3503, 143.6751],
                      [-128.0000, -128.0000, 128.0000, 128.0000],
                      [-161.2699, -161.2699, 161.2699, 161.2699],
                      [-203.1873, -203.1873, 203.1873, 203.1873],
                      [-90.5097, -181.0193, 90.5097, 181.0193],
                      [-114.0350, -228.0701, 114.0350, 228.0701],
                      [-143.6751, -287.3503, 143.6751, 287.3503]]),
        torch.Tensor([[-362.0387, -181.0193, 362.0387, 181.0193],
                      [-456.1401, -228.0701, 456.1401, 228.0701],
                      [-574.7006, -287.3503, 574.7006, 287.3503],
                      [-256.0000, -256.0000, 256.0000, 256.0000],
                      [-322.5398, -322.5398, 322.5398, 322.5398],
                      [-406.3747, -406.3747, 406.3747, 406.3747],
                      [-181.0193, -362.0387, 181.0193, 362.0387],
                      [-228.0701, -456.1401, 228.0701, 456.1401],
                      [-287.3503, -574.7006, 287.3503, 574.7006]])
    ]
    approxs = ga_retina_head.approx_anchor_generator.base_anchors
    for i, base_anchor in enumerate(approxs):
        assert base_anchor.allclose(expected_approxs[i])

    # check valid flags
    expected_valid_pixels = [136800, 34200, 8550, 2223, 630]
    multi_level_valid_flags = ga_retina_head.approx_anchor_generator \
        .valid_flags(featmap_sizes, (800, 1216), device)
    for i, single_level_valid_flag in enumerate(multi_level_valid_flags):
        assert single_level_valid_flag.sum() == expected_valid_pixels[i]

    # check number of base anchors for each level
    assert ga_retina_head.approx_anchor_generator.num_base_anchors == [
        9, 9, 9, 9, 9
    ]

    # check approx generation
    squares = ga_retina_head.square_anchor_generator.grid_anchors(
        featmap_sizes, device)
    assert len(squares) == 5

    expected_squares = [
        torch.Tensor([[-16., -16., 16., 16.]]),
        torch.Tensor([[-32., -32., 32., 32]]),
        torch.Tensor([[-64., -64., 64., 64.]]),
        torch.Tensor([[-128., -128., 128., 128.]]),
        torch.Tensor([[-256., -256., 256., 256.]])
    ]
    squares = ga_retina_head.square_anchor_generator.base_anchors
    for i, base_anchor in enumerate(squares):
        assert base_anchor.allclose(expected_squares[i])

    # square_anchor_generator does not check valid flags
    # check number of base anchors for each level
    assert (ga_retina_head.square_anchor_generator.num_base_anchors == [
        1, 1, 1, 1, 1
    ])

    # check square generation
    anchors = ga_retina_head.square_anchor_generator.grid_anchors(
        featmap_sizes, device)
    assert len(anchors) == 5
コード例 #10
0
def test_retina_anchor():
    from mmdet.models import build_head
    if torch.cuda.is_available():
        device = 'cuda'
    else:
        device = 'cpu'

    # head configs modified from
    # configs/nas_fpn/retinanet_r50_fpn_crop640_50e.py
    bbox_head = dict(type='RetinaSepBNHead',
                     num_classes=4,
                     num_ins=5,
                     in_channels=4,
                     stacked_convs=1,
                     feat_channels=4,
                     anchor_generator=dict(type='AnchorGenerator',
                                           octave_base_scale=4,
                                           scales_per_octave=3,
                                           ratios=[0.5, 1.0, 2.0],
                                           strides=[8, 16, 32, 64, 128]),
                     bbox_coder=dict(type='DeltaXYWHBBoxCoder',
                                     target_means=[.0, .0, .0, .0],
                                     target_stds=[1.0, 1.0, 1.0, 1.0]))

    retina_head = build_head(bbox_head)
    assert retina_head.anchor_generator is not None

    # use the featmap sizes in NASFPN setting to test retina head
    featmap_sizes = [(80, 80), (40, 40), (20, 20), (10, 10), (5, 5)]
    # check base anchors
    expected_base_anchors = [
        torch.Tensor([[-22.6274, -11.3137, 22.6274, 11.3137],
                      [-28.5088, -14.2544, 28.5088, 14.2544],
                      [-35.9188, -17.9594, 35.9188, 17.9594],
                      [-16.0000, -16.0000, 16.0000, 16.0000],
                      [-20.1587, -20.1587, 20.1587, 20.1587],
                      [-25.3984, -25.3984, 25.3984, 25.3984],
                      [-11.3137, -22.6274, 11.3137, 22.6274],
                      [-14.2544, -28.5088, 14.2544, 28.5088],
                      [-17.9594, -35.9188, 17.9594, 35.9188]]),
        torch.Tensor([[-45.2548, -22.6274, 45.2548, 22.6274],
                      [-57.0175, -28.5088, 57.0175, 28.5088],
                      [-71.8376, -35.9188, 71.8376, 35.9188],
                      [-32.0000, -32.0000, 32.0000, 32.0000],
                      [-40.3175, -40.3175, 40.3175, 40.3175],
                      [-50.7968, -50.7968, 50.7968, 50.7968],
                      [-22.6274, -45.2548, 22.6274, 45.2548],
                      [-28.5088, -57.0175, 28.5088, 57.0175],
                      [-35.9188, -71.8376, 35.9188, 71.8376]]),
        torch.Tensor([[-90.5097, -45.2548, 90.5097, 45.2548],
                      [-114.0350, -57.0175, 114.0350, 57.0175],
                      [-143.6751, -71.8376, 143.6751, 71.8376],
                      [-64.0000, -64.0000, 64.0000, 64.0000],
                      [-80.6349, -80.6349, 80.6349, 80.6349],
                      [-101.5937, -101.5937, 101.5937, 101.5937],
                      [-45.2548, -90.5097, 45.2548, 90.5097],
                      [-57.0175, -114.0350, 57.0175, 114.0350],
                      [-71.8376, -143.6751, 71.8376, 143.6751]]),
        torch.Tensor([[-181.0193, -90.5097, 181.0193, 90.5097],
                      [-228.0701, -114.0350, 228.0701, 114.0350],
                      [-287.3503, -143.6751, 287.3503, 143.6751],
                      [-128.0000, -128.0000, 128.0000, 128.0000],
                      [-161.2699, -161.2699, 161.2699, 161.2699],
                      [-203.1873, -203.1873, 203.1873, 203.1873],
                      [-90.5097, -181.0193, 90.5097, 181.0193],
                      [-114.0350, -228.0701, 114.0350, 228.0701],
                      [-143.6751, -287.3503, 143.6751, 287.3503]]),
        torch.Tensor([[-362.0387, -181.0193, 362.0387, 181.0193],
                      [-456.1401, -228.0701, 456.1401, 228.0701],
                      [-574.7006, -287.3503, 574.7006, 287.3503],
                      [-256.0000, -256.0000, 256.0000, 256.0000],
                      [-322.5398, -322.5398, 322.5398, 322.5398],
                      [-406.3747, -406.3747, 406.3747, 406.3747],
                      [-181.0193, -362.0387, 181.0193, 362.0387],
                      [-228.0701, -456.1401, 228.0701, 456.1401],
                      [-287.3503, -574.7006, 287.3503, 574.7006]])
    ]
    base_anchors = retina_head.anchor_generator.base_anchors
    for i, base_anchor in enumerate(base_anchors):
        assert base_anchor.allclose(expected_base_anchors[i])

    # check valid flags
    expected_valid_pixels = [57600, 14400, 3600, 900, 225]
    multi_level_valid_flags = retina_head.anchor_generator.valid_flags(
        featmap_sizes, (640, 640), device)
    for i, single_level_valid_flag in enumerate(multi_level_valid_flags):
        assert single_level_valid_flag.sum() == expected_valid_pixels[i]

    # check number of base anchors for each level
    assert retina_head.anchor_generator.num_base_anchors == [9, 9, 9, 9, 9]

    # check anchor generation
    anchors = retina_head.anchor_generator.grid_anchors(featmap_sizes, device)
    assert len(anchors) == 5
コード例 #11
0
 def init_embed_head(self, roi_extractor, embed_head):
     """Initialize ``embed_head``"""
     self.roi_extractor = build_roi_extractor(roi_extractor)
     self.embed_head = build_head(embed_head)