def __init__(self, cfg, weight_dict=None, extra_features=0):
        super(DepthRCNN, self).__init__()

        # Weight losses
        if weight_dict is None:
            self.weight_dict = {
                'loss_objectness': cfg.LOSS_WEIGHTS.loss_objectness,
                'loss_rpn_box_reg': cfg.LOSS_WEIGHTS.loss_rpn_box_reg,
                'loss_classifier': cfg.LOSS_WEIGHTS.loss_classifier,
                'loss_box_reg': cfg.LOSS_WEIGHTS.loss_box_reg,
                'loss_mask': cfg.LOSS_WEIGHTS.loss_mask
            }
        else:
            self.weight_dict = weight_dict

        self.image_backbone = build_backbone(cfg)
        self.freeze_backbone(self.image_backbone,
                             cfg.MODEL.BACKBONE.FREEZE_CONV_BODY_AT)
        self.roi_heads = None
        if cfg.MODEL.BACKBONE.DEPTH:
            self.hha_backbone = build_backbone(cfg)
            self.freeze_backbone(self.hha_backbone,
                                 cfg.MODEL.BACKBONE.FREEZE_CONV_BODY_AT)
            self.rpn = build_rpn(
                cfg, self.image_backbone.out_channels * 2 + extra_features)
            if not cfg.MODEL.RPN_ONLY:
                self.roi_heads = build_roi_heads(
                    cfg, self.image_backbone.out_channels * 2 + extra_features)
        else:
            self.hha_backbone = None
            self.rpn = build_rpn(
                cfg, self.image_backbone.out_channels + extra_features)
            if not cfg.MODEL.RPN_ONLY:
                self.roi_heads = build_roi_heads(
                    cfg, self.image_backbone.out_channels + extra_features)
Exemplo n.º 2
0
    def test_build_shufflenet_backbones(self):
        ''' Make sure backbones run '''

        self.assertGreater(len(registry.BACKBONES), 0)

        name = "ShuffleNetV2"
        print('Testing {}...'.format(name))
        if name in BACKBONE_CFGS:
            cfg = load_config(BACKBONE_CFGS[name])
        else:
            # Use default config if config file is not specified
            cfg = copy.deepcopy(g_cfg)

        backbone = build_backbone(cfg)

        # make sures the backbone has `out_channels`
        self.assertIsNotNone(
            getattr(backbone, 'out_channels', None),
            'Need to provide out_channels for backbone {}'.format(name))

        N, C_in, H, W = 2, 3, 224, 256
        input = torch.rand([N, C_in, H, W], dtype=torch.float32)
        out = backbone(input)
        for cur_out in out:
            self.assertEqual(cur_out.shape[:2],
                             torch.Size([N, backbone.out_channels]))
Exemplo n.º 3
0
    def __init__(self, exp_dict):
        super().__init__()
        cfg_base_path = "./models/configs/"

        self.n_classes = 21
        cfg_path = cfg_base_path + "e2e_mask_rcnn_R_50_FPN_1x.yaml"

        self.cfg = cfg
        self.cfg.merge_from_file(cfg_path)

        self.cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES = self.n_classes

        # ---------------
        # build model
        self.backbone_fpn = backbone.build_backbone(self.cfg)
        self.rpn = rpn.build_rpn(self.cfg, self.backbone_fpn.out_channels)
        self.roi_heads = roi_heads.build_roi_heads(
            self.cfg, self.backbone_fpn.out_channels)

        # ---------------
        # load checkpoint
        checkpoint = _load_file(self.cfg)
        load_state_dict(self, checkpoint.pop("model"))

        #--------
        # Opt stage
        self.cfg.SOLVER.BASE_LR = ((0.0025 * 8) /
                                   (16 / float(exp_dict["batch_size"])))

        optimizer = make_optimizer(self.cfg, self)
        scheduler = make_lr_scheduler(self.cfg, optimizer)

        self.opt = optimizer
        self.scheduler = scheduler
Exemplo n.º 4
0
    def init_maskrcnn_benchmark(self, args):
        if 'datasets' in sys.modules:
            del sys.modules['datasets']
        this_dir = os.path.dirname(__file__)
        lib_path = os.path.join(this_dir, '../../external/maskrcnn-benchmark')
        sys.path.insert(0, lib_path)

        # config
        from maskrcnn_benchmark.utils.env import setup_environment  # noqa F401 isort:skip
        from maskrcnn_benchmark.config import cfg
        self.cfg = cfg
        #cfg.MODEL.BACKBONE.OUT_CHANNELS = 832
        #cfg.MODEL.RESNETS.RES2_OUT_CHANNELS = 208
        cfg.MODEL.RPN.PRE_NMS_TOP_N_TEST = 6000
        cfg.MODEL.RPN.POST_NMS_TOP_N_TEST = 1000
        cfg.MODEL.ROI_HEADS.SCORE_THRESH = 0
        cfg.MODEL.ROI_HEADS.DETECTIONS_PER_IMG = 0

        # bind classes/methods from MaskRCNN-Benchmark
        from maskrcnn_benchmark.modeling.rpn.rpn import build_rpn
        from maskrcnn_benchmark.modeling.roi_heads.roi_heads import build_roi_heads
        from maskrcnn_benchmark.structures.image_list import to_image_list
        from maskrcnn_benchmark.structures.bounding_box import BoxList
        from maskrcnn_benchmark.modeling.backbone import build_backbone

        self.basenet = build_backbone(cfg)
        self.rpn = build_rpn(cfg)
        self.roi_heads = build_roi_heads(cfg)
        self.to_image_list = to_image_list
        self.BoxList = BoxList
Exemplo n.º 5
0
    def __init__(self, cfg):
        super(RelationPredictor, self).__init__(cfg)
        self.cfg = cfg
        self.device = cfg.MODEL.DEVICE
        self.detector_pre_calculated = self.cfg.MODEL.ROI_RELATION_HEAD.DETECTOR_PRE_CALCULATED
        self.detector_force_boxes = self.cfg.MODEL.ROI_BOX_HEAD.FORCE_BOXES
        self.cfg_check()

        feature_dim = self.backbone.out_channels
        if not self.cfg.MODEL.ROI_RELATION_HEAD.SHARE_CONV_BACKBONE:
            self.rel_backbone = build_backbone(cfg)
            feature_dim = self.rel_backbone.out_channels

        self.force_relations = cfg.MODEL.ROI_RELATION_HEAD.FORCE_RELATIONS
        if cfg.MODEL.RELATION_ON and self.cfg.MODEL.ROI_RELATION_HEAD.ALGORITHM in SCENE_MODEL_DICT:
            self.relation_head = build_roi_relation_head(cfg, feature_dim)
        
        if cfg.MODEL.ATTRIBUTE_ON:
            self.attribute_head = build_roi_attribute_head(cfg, feature_dim)

        # self._freeze_components(self.cfg)
        for p in self.backbone.parameters():
            p.requires_grad = False
        for p in self.rpn.parameters():
            p.requires_grad = False
        for p in self.roi_heads.parameters():
            p.requires_grad = False
        
        if not self.cfg.MODEL.ROI_RELATION_HEAD.SHARE_BOX_FEATURE_EXTRACTOR:
            if self.cfg.MODEL.ROI_RELATION_HEAD.SEPERATE_SO_FEATURE_EXTRACTOR:
                self.subj_feature_extractor = make_roi_relation_box_feature_extractor(cfg, feature_dim)
                self.obj_feature_extractor = make_roi_relation_box_feature_extractor(cfg, feature_dim)
            else:
                self.obj_feature_extractor = make_roi_relation_box_feature_extractor(cfg, feature_dim)
Exemplo n.º 6
0
    def __init__(self, cfg):
        super(GeneralizedRCNN, self).__init__()

        self.backbone = build_backbone(cfg)
        self.rpn = build_rpn(cfg, self.backbone.out_channels)
        self.roi_heads = build_roi_heads(cfg, self.backbone.out_channels)

        self.feature_extractor = make_roi_box_feature_extractor(
            cfg, self.backbone.out_channels)
Exemplo n.º 7
0
    def __init__(self, cfg):
        super(PanopticModel, self).__init__()

        self.backbone = build_backbone(cfg)
        self.loss = SegLoss()
        self.training = True
        self.rpn = build_rpn(cfg, self.backbone.out_channels)
        self.roi_heads = build_roi_heads(cfg, self.backbone.out_channels)
        self.panoptic = PanopticRCNN(
            cfg, num_classes=cfg.MODEL.PANOPTIC.NUM_CLASSES)
Exemplo n.º 8
0
def optimal_bias_init(cfg, bias):
    device = torch.device(cfg.MODEL.DEVICE)
    arch = request_arch(cfg)
    if 'retinanet' in arch:
        anchor_generator = make_anchor_generator_retinanet(cfg)
        fg_iou, bg_iou = cfg.MODEL.RETINANET.FG_IOU_THRESHOLD, cfg.MODEL.RETINANET.BG_IOU_THRESHOLD
        num_classes = cfg.MODEL.RETINANET.NUM_CLASSES - 1
        num_anchors = len(cfg.MODEL.RETINANET.ASPECT_RATIOS) \
            * cfg.MODEL.RETINANET.SCALES_PER_OCTAVE
    else:
        assert 'faster_rcnn' in arch
        anchor_generator = make_anchor_generator(cfg)
        fg_iou, bg_iou = cfg.MODEL.RPN.FG_IOU_THRESHOLD, cfg.MODEL.RPN.BG_IOU_THRESHOLD
        num_classes = 1
        num_anchors = anchor_generator.num_anchors_per_location()[0]

    prior = load_prior(cfg, arch)

    if prior is not None:
        nn.init.constant_(bias, -log((1 - prior) / prior))
        return

    data_loader = make_init_data_loader(
        cfg, is_distributed=True, images_per_batch=cfg.SOLVER.IMS_PER_BATCH)

    proposal_matcher = Matcher(
        fg_iou,
        bg_iou,
        allow_low_quality_matches=True,
    )

    backbone = build_backbone(cfg).to(device)
    num_fg, num_all = 0, 0
    num_gpus = get_num_gpus()

    for images, targets, _ in tqdm(data_loader):
        images = images.to(device)
        targets = [target.to(device) for target in targets]
        h, w = images.tensors.shape[-2:]

        if num_all == 0:
            features = backbone(images.tensors)
            n, c = features[0].shape[:2]
            levels = len(features)
            stride = int(h / features[0].shape[2])

        features = [
            torch.zeros(n,
                        c,
                        int(ceil(h / (stride * 2**i))),
                        int(ceil(w / (stride * 2**i))),
                        device=device) for i in range(levels)
        ]
        anchors = anchor_generator(images, features)
        anchors = [
            cat_boxlist(anchors_per_image).to(device)
            for anchors_per_image in anchors
        ]

        for anchor, target in zip(anchors, targets):
            match_quality_matrix = boxlist_iou(target, anchor)
            matched_idxs = proposal_matcher(match_quality_matrix)
            num_fg_per_image, num_bg_per_image = (matched_idxs >= 0).sum(), (
                matched_idxs == Matcher.BELOW_LOW_THRESHOLD).sum()
            num_fg += num_fg_per_image
            num_all += num_fg_per_image + num_bg_per_image
    fg_all_ratio = reduce_div(num_fg.float(), num_all.float()).item()
    prior = fg_all_ratio / num_classes
    nn.init.constant_(bias, -log((1 - prior) / prior))
    if torch.cuda.current_device() == 0:
        save_prior(cfg, prior, arch)
    def __init__(self, cfg):
        super(GeneralizedRCNN, self).__init__()

        self.backbone = build_backbone(cfg)
        self.rpn = build_rpn(cfg, self.backbone.out_channels)