Beispiel #1
0
 def __init__(self, proposal_matcher, discretization_size, use_mil_loss,
              use_aff, use_box_mask):
     """
     Arguments:
         proposal_matcher (Matcher)
         discretization_size (int)
     """
     self.proposal_matcher = proposal_matcher
     self.discretization_size = discretization_size
     center_weight = torch.zeros((3, 3))
     center_weight[1][1] = 1.
     aff_weights = []
     for i in range(3):
         for j in range(3):
             if i == 1 and j == 1:
                 continue
             weight = torch.zeros((3, 3))
             weight[i][j] = 1.
             aff_weights.append(center_weight - weight)
     aff_weights = [w.view(1, 1, 3, 3).to("cuda") for w in aff_weights]
     self.aff_weights = torch.cat(aff_weights, 0)
     self.box_coder = BoxCoder(weights=(10., 10., 5., 5.))
     self.use_mil_loss = use_mil_loss
     self.use_aff = use_aff
     if use_box_mask:
         assert not use_mil_loss
     self.use_box_mask = use_box_mask
Beispiel #2
0
    def __init__(self, cfg, in_channels):
        super(GARPNModule, self).__init__()

        self.cfg = cfg.clone()

        anchor_generator = make_ga_anchor_generator(cfg)

        rpn_head = registry.RPN_HEADS[cfg.MODEL.RPN.RPN_HEAD]
        head = rpn_head(cfg, in_channels, 1)

        #rpn_box_coder = BoxCoder(weights=(1.0, 1.0, 1.0, 1.0))
        rpn_box_coder = BoxCoder(weights=cfg.MODEL.RPN.GA.TARGET_WEIGHTS)
        anchor_box_coder = BoxCoder(weights=cfg.MODEL.RPN.GA.ANCHOR_WEIGHTS)

        box_selector_train = make_rpn_postprocessor(cfg,
                                                    rpn_box_coder,
                                                    is_train=True)
        box_selector_test = make_rpn_postprocessor(cfg,
                                                   rpn_box_coder,
                                                   is_train=False)

        loss_evaluator = make_ga_rpn_loss_evaluator(cfg, rpn_box_coder,
                                                    anchor_box_coder)

        self.anchor_generator = anchor_generator
        self.head = head
        self.box_selector_train = box_selector_train
        self.box_selector_test = box_selector_test
        self.loss_evaluator = loss_evaluator
Beispiel #3
0
    def __init__(self, cfg):
        self._proposals = None

        bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS
        self.box_coder = BoxCoder(weights=bbox_reg_weights)
        self.proposal_matcher = Matcher(
            cfg.MODEL.VG.FG_IOU_THRESHOLD,
            cfg.MODEL.VG.BG_IOU_THRESHOLD,
            allow_low_quality_matches=False,
        )
Beispiel #4
0
 def __init__(self, cfg, in_channels):
     super(ROIBoxHead, self).__init__()
     self.feature_extractor = make_roi_box_feature_extractor(
         cfg, in_channels)
     self.predictor = make_roi_box_predictor(
         cfg, self.feature_extractor.out_channels)
     self.post_processor = make_roi_box_post_processor(cfg)
     self.loss_evaluator = make_roi_box_loss_evaluator(cfg)
     self.box_coder = BoxCoder(weights=(10., 10., 5., 5.))
     self.cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG
Beispiel #5
0
    def __init__(
        self,
        cfg,
        is_teacher=False,
    ):
        super(DuplicationRemovalNetwork, self).__init__()
        self.cfg = cfg.clone()
        # if reg_iou = True, then this network is used to regress
        # the iou to the GT. if not True, this predict
        # true-object/duplicate
        self.reg_iou = self.cfg.MODEL.RELATION_NMS.REG_IOU
        self.first_n = cfg.MODEL.RELATION_NMS.FIRST_N
        self.NMS_thread = cfg.MODEL.RELATION_NMS.THREAD
        self.nms_rank_fc = nn.Linear(
            cfg.MODEL.RELATION_NMS.ROI_FEAT_DIM,
            cfg.MODEL.RELATION_NMS.APPEARANCE_FEAT_DIM,
            bias=True)
        self.roi_feat_embedding_fc = nn.Linear(
            cfg.MODEL.RELATION_NMS.ROI_FEAT_DIM,
            cfg.MODEL.RELATION_NMS.APPEARANCE_FEAT_DIM,
            bias=True)
        self.target_thresh = cfg.MODEL.RELATION_NMS.THREAD
        self.geo_feature_dim = cfg.MODEL.RELATION_NMS.GEO_FEAT_DIM

        if cfg.MODEL.RELATION_NMS.USE_IOU:
            self.geo_feature_dim = int(self.geo_feature_dim / 4 * 5)
        self.relation_module = RelationModule(
            cfg.MODEL.RELATION_NMS.APPEARANCE_FEAT_DIM,
            geo_feature_dim=self.geo_feature_dim,
            fc_dim=(self.geo_feature_dim, 16),
            group=cfg.MODEL.RELATION_NMS.GROUP,
            dim=cfg.MODEL.RELATION_NMS.HID_DIM,
            topk=cfg.MODEL.RELATION_NMS.TOPK,
            iou_method=cfg.MODEL.RELATION_NMS.IOU_METHOD)

        self.nms_fg_weight = torch.tensor([1., cfg.MODEL.RELATION_NMS.WEIGHT])
        self.mt_fg_weight = torch.tensor([1., 10.])
        self.alpha = cfg.MODEL.RELATION_NMS.ALPHA
        self.gamma = cfg.MODEL.RELATION_NMS.GAMMA
        self.boxcoder = BoxCoder(weights=(10., 10., 5., 5.))
        self.class_agnostic = cfg.MODEL.RELATION_NMS.CLASS_AGNOSTIC
        self.fg_class = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES - 1
        self.classifier = nn.Linear(128, len(self.target_thresh), bias=True)
        self.relu1 = nn.ReLU(inplace=True)
        self.fg_thread = cfg.MODEL.RELATION_NMS.FG_THREAD
        self.detections_per_img = cfg.MODEL.ROI_HEADS.DETECTIONS_PER_IMG
        self.nms = cfg.MODEL.RELATION_NMS.POS_NMS
        self.nms_loss_type = cfg.MT.NMS_LOSS_TYPE
        self.mode = None
 def __init__(self,
              score_thresh=0.05,
              nms=0.5,
              post_nms_per_cls_topn=300,
              nms_filter_duplicates=True,
              detections_per_img=100,
              box_coder=None,
              cls_agnostic_bbox_reg=False,
              bbox_aug_enabled=False,
              save_proposals=False):
     """
     Arguments:
         score_thresh (float)
         nms (float)
         detections_per_img (int)
         box_coder (BoxCoder)
     """
     super(PostProcessor, self).__init__()
     self.score_thresh = score_thresh
     self.nms = nms
     self.post_nms_per_cls_topn = post_nms_per_cls_topn
     self.nms_filter_duplicates = nms_filter_duplicates
     self.detections_per_img = detections_per_img
     if box_coder is None:
         box_coder = BoxCoder(weights=(10., 10., 5., 5.))
     self.box_coder = box_coder
     self.cls_agnostic_bbox_reg = cls_agnostic_bbox_reg
     self.bbox_aug_enabled = bbox_aug_enabled
     self.save_proposals = save_proposals
Beispiel #7
0
def make_roi_box_loss_evaluator(cfg):
    matcher = Matcher(
        cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD,
        cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD,
        allow_low_quality_matches=False,
    )

    bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS
    box_coder = BoxCoder(weights=bbox_reg_weights)

    fg_bg_sampler = BalancedPositiveNegativeSampler(
        cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION
    )

    cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG
    attribute_on = cfg.MODEL.ATTRIBUTE_ON

    loss_evaluator = FastRCNNLossComputation(
        matcher, 
        fg_bg_sampler, 
        box_coder, 
        cls_agnostic_bbox_reg,
        attribute_on
    )

    return loss_evaluator
Beispiel #8
0
def make_roi_box_loss_evaluator(cfg, stage=None):
    if stage is None or stage == 1:
        matcher = Matcher(
            cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD,
            cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD,
            allow_low_quality_matches=False,
        )
        bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS
    elif stage == 1:
        matcher = Matcher(
            cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD,
            cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD,
            allow_low_quality_matches=True,
        )
        bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS
    elif stage == 2:
        matcher = Matcher(
            cfg.MODEL.ROI_HEADS.STAGE2.FG_IOU_THRESHOLD,
            cfg.MODEL.ROI_HEADS.STAGE2.BG_IOU_THRESHOLD,
            allow_low_quality_matches=True,
        )
        bbox_reg_weights = cfg.MODEL.ROI_HEADS.STAGE2.BBOX_REG_WEIGHTS
    elif stage == 3:
        matcher = Matcher(
            cfg.MODEL.ROI_HEADS.STAGE3.FG_IOU_THRESHOLD,
            cfg.MODEL.ROI_HEADS.STAGE3.BG_IOU_THRESHOLD,
            allow_low_quality_matches=True,
        )
        bbox_reg_weights = cfg.MODEL.ROI_HEADS.STAGE3.BBOX_REG_WEIGHTS


    box_coder = BoxCoder(weights=bbox_reg_weights)

    sampling = cfg.MODEL.SAMPLING
    if sampling == "random": 
        fg_bg_sampler = BalancedPositiveNegativeSampler(
            cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION
        )
    elif sampling == "balanced":
        fg_bg_sampler = IOUBalancedPositiveNegativeSampler(
            cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION
        )

    cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG
    bbox_loss_type = cfg.MODEL.BBOX_LOSS_TYPE
    label_smoothing = cfg.MODEL.LABEL_SMOOTHING
    num_classes = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES

    loss_evaluator = FastRCNNLossComputation(
        matcher, 
        fg_bg_sampler, 
        box_coder, 
        cls_agnostic_bbox_reg,
        bbox_loss_type,
        sampling,
        label_smoothing,
        num_classes
    )

    return loss_evaluator
def make_roi_box3d_post_processor(cfg):

    bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS
    box_coder = BoxCoder(weights=bbox_reg_weights)

    depth_post_processor = Box3dPostProcessor(cfg, box_coder)
    return depth_post_processor
    def __init__(
        self,
        pre_nms_top_n,
        post_nms_top_n,
        nms_thresh,
        min_size,
        box_coder=None,
        fpn_post_nms_top_n=None,
    ):
        """
        Arguments:
            pre_nms_top_n (int)
            post_nms_top_n (int)
            nms_thresh (float)
            min_size (int)
            box_coder (BoxCoder)
            fpn_post_nms_top_n (int)
        """
        super(RPNPostProcessor, self).__init__()
        self.pre_nms_top_n = pre_nms_top_n
        self.post_nms_top_n = post_nms_top_n
        self.nms_thresh = nms_thresh
        self.min_size = min_size

        if box_coder is None:
            box_coder = BoxCoder(weights=(1.0, 1.0, 1.0, 1.0))
        self.box_coder = box_coder

        if fpn_post_nms_top_n is None:
            fpn_post_nms_top_n = post_nms_top_n
        self.fpn_post_nms_top_n = fpn_post_nms_top_n
Beispiel #11
0
    def __init__(
        self,
        pre_nms_thresh,
        pre_nms_top_n,
        nms_thresh,
        fpn_post_nms_top_n,
        min_size,
        num_classes,
        box_coder=None,
    ):
        """
        Arguments:
            pre_nms_thresh (float)
            pre_nms_top_n (int)
            nms_thresh (float)
            fpn_post_nms_top_n (int)
            min_size (int)
            num_classes (int)
            box_coder (BoxCoder)
        """
        super(RetinaNetPostProcessor, self).__init__(pre_nms_thresh, 0,
                                                     nms_thresh, min_size)
        self.pre_nms_thresh = pre_nms_thresh
        self.pre_nms_top_n = pre_nms_top_n
        self.nms_thresh = nms_thresh
        self.fpn_post_nms_top_n = fpn_post_nms_top_n
        self.min_size = min_size
        self.num_classes = num_classes

        if box_coder is None:
            # Q: How are the weights chosen?
            # A: cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS.
            box_coder = BoxCoder(weights=(10., 10., 5., 5.))
        self.box_coder = box_coder
Beispiel #12
0
 def __init__(
     self,
     pre_nms_thresh,
     pre_nms_top_n,
     nms_thresh,
     fpn_post_nms_top_n,
     min_size,
     num_classes,
     box_coder=None,
     imbalanced_decider=None,
 ):
     """
     Arguments:
         pre_nms_thresh (float)
         pre_nms_top_n (int)
         nms_thresh (float)
         fpn_post_nms_top_n (int)
         min_size (int)
         num_classes (int)
         box_coder (BoxCoder)
     """
     super(RetinaNetPostProcessor, self).__init__(pre_nms_thresh, 0,
                                                  nms_thresh, min_size)
     self.pre_nms_thresh = pre_nms_thresh
     self.pre_nms_top_n = pre_nms_top_n
     self.nms_thresh = nms_thresh
     self.fpn_post_nms_top_n = fpn_post_nms_top_n
     self.min_size = min_size
     self.num_classes = num_classes
     if box_coder is None:
         box_coder = BoxCoder(weights=(10., 10., 5., 5.))
     self.box_coder = box_coder
     self.imbalanced_decider = imbalanced_decider
Beispiel #13
0
def make_roi_box_loss_evaluator(cfg):
    matcher = Matcher(
        cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD,
        cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD,
        allow_low_quality_matches=False,
    )

    bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS
    box_coder = BoxCoder(weights=bbox_reg_weights)

    fg_bg_sampler = BalancedPositiveNegativeSampler(
        cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION
    )

    cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG

    # ######################## add by hui ########################################################
    ohem_loss = None
    if cfg.MODEL.ROI_HEADS.OHEM == 1:
        ohem_loss = OHEMLoss(cfg.MODEL.ROI_HEADS.OHEM1_NEG_RATE)
    elif cfg.MODEL.ROI_HEADS.OHEM == 2:
        ohem_loss = OHEM2Loss(cfg.MODEL.ROI_HEADS.OHEM2_BATCH_SIZE_PER_IM * cfg.SOLVER.IMS_PER_BATCH // cfg.SOLVER.NUM_GPU,
                              cfg.MODEL.ROI_HEADS.OHEM2_POSITIVE_FRACTION,
                              hard_rate=cfg.MODEL.ROI_HEADS.OHEM2_HARD_RATE)

    loss_evaluator = FastRCNNLossComputation(matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg, ohem_loss)
    # ################################################################################################################

    return loss_evaluator
Beispiel #14
0
    def __init__(self,
                 data_dir,
                 split,
                 temporal_saved_path,
                 use_difficult=False,
                 transforms=None):
        self.root = data_dir
        self.image_set = split
        self.keep_difficult = use_difficult
        self.transforms = transforms

        self._annopath = os.path.join(self.root, "Annotations", "%s.xml")
        self._imgpath = os.path.join(self.root, "JPEGImages", "%s.jpg")
        self._imgsetpath = os.path.join(self.root, "ImageSets", "Main",
                                        "%s.txt")

        with open(self._imgsetpath % self.image_set) as f:
            self.ids = f.readlines()
        self.ids = [x.strip("\n") for x in self.ids]
        self.id_to_img_map = {k: v for k, v in enumerate(self.ids)}

        cls = PascalVOCDataset.CLASSES
        self.class_to_ind = dict(zip(cls, range(len(cls))))
        self.categories = dict(zip(range(len(cls)), cls))
        self.temporal_saved_path = temporal_saved_path
        self.postprocessor = PostProcessor(
            score_thresh=0.05,
            nms=0.5,
            detections_per_img=100,
            box_coder=BoxCoder(weights=(10.0, 10.0, 5.0, 5.0)),
            cls_agnostic_bbox_reg=False,
            bbox_aug_enabled=False)

        self.__getitem__(0)
        self.get_img_info(0)
Beispiel #15
0
def make_roi_box_loss_evaluator(cfg):
    matcher = Matcher(
        cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD,  # 0.5,前面RPN出来的都是0.7了这里是0.5真的好吗:
        # 为了划分出一部分背景,这里的正样本阈值被降低,也可能是留出一些阈值给训练前期容错
        cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD,  # 0.5
        allow_low_quality_matches=False,
    )

    bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS
    box_coder = BoxCoder(weights=bbox_reg_weights)

    fg_bg_sampler = BalancedPositiveNegativeSampler(
        cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION
    )

    cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG

    loss_evaluator = FastRCNNLossComputation(
        matcher,
        fg_bg_sampler,
        box_coder,
        cls_agnostic_bbox_reg
    )

    return loss_evaluator
    def __init__(self, cfg, in_channels):
        super(RPNModule, self).__init__()

        self.cfg = cfg.clone()
        try:
            self.save_features = self.cfg.SAVE_FEATURES_RPN
        except:
            self.save_features = False

        anchor_generator = make_anchor_generator(self.cfg)

        rpn_head = registry.RPN_HEADS[self.cfg.MODEL.RPN.RPN_HEAD]
        head = rpn_head(self.cfg, in_channels,
                        anchor_generator.num_anchors_per_location()[0])

        rpn_box_coder = BoxCoder(weights=(1.0, 1.0, 1.0, 1.0))

        box_selector_train = make_rpn_postprocessor(self.cfg,
                                                    rpn_box_coder,
                                                    is_train=True)
        box_selector_test = make_rpn_postprocessor(self.cfg,
                                                   rpn_box_coder,
                                                   is_train=False)

        loss_evaluator = make_rpn_loss_evaluator(self.cfg, rpn_box_coder)

        self.anchor_generator = anchor_generator
        self.head = head
        self.box_selector_train = box_selector_train
        self.box_selector_test = box_selector_test
        self.loss_evaluator = loss_evaluator

        self.initialize_online_rpn_params()
Beispiel #17
0
    def __init__(
        self,
        cfg,
        box_coder=None,
    ):
        """
        Arguments:
            box_coder (BoxCoder)
        """
        super(PostProcessor, self).__init__()
        self.cfg = cfg
        self.score_thresh = cfg.MODEL.ROI_HEADS.SCORE_THRESH
        self.nms = cfg.MODEL.ROI_HEADS.NMS
        self.detections_per_img = cfg.MODEL.ROI_HEADS.DETECTIONS_PER_IMG
        self.min_detections_per_img = cfg.MODEL.ROI_HEADS.MIN_DETECTIONS_PER_IMG
        if box_coder is None:
            box_coder = BoxCoder(weights=(10., 10., 5., 5.))
        self.box_coder = box_coder
        self.cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG
        self.bbox_aug_enabled = cfg.TEST.BBOX_AUG.ENABLED
        self.output_feature = cfg.TEST.OUTPUT_FEATURE
        if self.output_feature:
            # needed to extract features when they have not been pooled yet
            self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.force_boxes = cfg.MODEL.ROI_BOX_HEAD.FORCE_BOXES
        self.ignore_box_regression = cfg.TEST.IGNORE_BOX_REGRESSION

        self.filter_method = self.filter_results
        if self.cfg.MODEL.ROI_HEADS.NMS_FILTER == 1:
            self.filter_method = self.filter_results_peter
        elif self.cfg.MODEL.ROI_HEADS.NMS_FILTER == 2:
            self.filter_method = self.filter_results_fast
Beispiel #18
0
    def __init__(self, cfg, in_channels):
        super(RPNModule, self).__init__()

        self.cfg = cfg.clone()

        anchor_generator = make_anchor_generator(cfg)  # 创建AnchorGenerator类

        rpn_head = registry.RPN_HEADS[cfg.MODEL.RPN.RPN_HEAD]  # 链接到rpn_head类
        head = rpn_head(
            cfg, in_channels,
            anchor_generator.num_anchors_per_location()[0])  # 创建rpn_head类

        rpn_box_coder = BoxCoder(weights=(1.0, 1.0, 1.0, 1.0))

        box_selector_train = make_rpn_postprocessor(
            cfg, rpn_box_coder, is_train=True)  # 创建RPNPostProcessor类
        box_selector_test = make_rpn_postprocessor(cfg,
                                                   rpn_box_coder,
                                                   is_train=False)

        loss_evaluator = make_rpn_loss_evaluator(
            cfg, rpn_box_coder)  # 创建RPNLossComputation类

        self.anchor_generator = anchor_generator
        self.head = head
        self.box_selector_train = box_selector_train
        self.box_selector_test = box_selector_test
        self.loss_evaluator = loss_evaluator
Beispiel #19
0
def make_roi_box_post_processor(cfg):
    use_fpn = cfg.MODEL.ROI_HEADS.USE_FPN

    # Default weights on (dx, dy, dw, dh) for normalizing bbox regression targets
    # These are empirically chosen to approximately lead to unit variance targets
    # _C.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS = (10., 10., 5., 5.)
    bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS

    box_coder = BoxCoder(weights=bbox_reg_weights)

    # Only used on test mode
    # Minimum score threshold (assuming scores in a [0, 1] range); a value chosen to
    # balance obtaining high recall with not having too many low precision
    # detections that will slow down inference post processing steps (like NMS)
    score_thresh = cfg.MODEL.ROI_HEADS.SCORE_THRESH  # 0.05

    nms_thresh = cfg.MODEL.ROI_HEADS.NMS  # 0.5

    # Maximum number of detections to return per image (100 is based on the limit
    # established for the COCO dataset)
    detections_per_img = cfg.MODEL.ROI_HEADS.DETECTIONS_PER_IMG  # 100
    cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG  # False

    postprocessor = PostProcessor(score_thresh, nms_thresh, detections_per_img,
                                  box_coder, cls_agnostic_bbox_reg)
    return postprocessor
Beispiel #20
0
    def __init__(
        self,
        pre_nms_top_n,
        post_nms_top_n,
        nms_thresh,
        min_size,
        box_coder=None,
        fpn_post_nms_top_n=None,
    ):
        """
        Arguments:
            two nms operation
            pre_nms_top_n (int)  for single feature level
            post_nms_top_n (int) for all feature level, image wise nms
            nms_thresh (float)
            min_size (int)
            box_coder (BoxCoder)
            fpn_post_nms_top_n (int)
        """
        super(RPNPostProcessor, self).__init__()
        self.pre_nms_top_n = pre_nms_top_n
        self.post_nms_top_n = post_nms_top_n
        self.nms_thresh = nms_thresh
        self.min_size = min_size

        if box_coder is None:
            box_coder = BoxCoder(weights=(1.0, 1.0, 1.0, 1.0))
        self.box_coder = box_coder

        if fpn_post_nms_top_n is None:
            fpn_post_nms_top_n = post_nms_top_n
        self.fpn_post_nms_top_n = fpn_post_nms_top_n
Beispiel #21
0
def make_roi_box_loss_evaluator(cfg):
    matcher = Matcher(
        cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD,
        cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD,
        allow_low_quality_matches=False,
    )

    bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS
    box_coder = BoxCoder(weights=bbox_reg_weights)
    class_weights = cfg.MODEL.ROI_BOX_HEAD.CLASS_WEIGHT
    class_compete = cfg.MODEL.ROI_BOX_HEAD.CLASS_COMPETE
    if len(class_weights) == 1:
        class_weights = class_weights * cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES

    fg_bg_sampler = BalancedPositiveNegativeSampler(
        cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE,
        cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION)

    cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG
    use_focal_loss = cfg.MODEL.ROI_BOX_HEAD.USE_FOCAL_LOSS
    sigmoid_focal_loss = SigmoidFocalLoss(cfg.MODEL.RETINANET.LOSS_GAMMA,
                                          cfg.MODEL.RETINANET.LOSS_ALPHA)

    loss_evaluator = FastRCNNLossComputation(matcher, fg_bg_sampler, box_coder,
                                             class_weights, class_compete,
                                             cls_agnostic_bbox_reg,
                                             use_focal_loss,
                                             sigmoid_focal_loss)

    return loss_evaluator
Beispiel #22
0
def make_roi_box_loss_evaluator(cfg):
    matcher = Matcher(
        # Overlap threshold for an RoI to be considered foreground (if >= FG_IOU_THRESHOLD)
        cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD,  # 0.5
        # Overlap threshold for an RoI to be considered background
        # (class = 0 if overlap in [0, BG_IOU_THRESHOLD))
        cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD,  # 0.5
        allow_low_quality_matches=False,
    )

    bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS  # (10., 10., 5., 5.)
    box_coder = BoxCoder(weights=bbox_reg_weights)

    fg_bg_sampler = BalancedPositiveNegativeSampler(
        cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE,  # 512
        cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION  # 0.25
    )

    cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG  # False

    loss_evaluator = FastRCNNLossComputation(
        matcher,
        fg_bg_sampler,
        box_coder,
        cls_agnostic_bbox_reg
    )

    return loss_evaluator
Beispiel #23
0
    def __init__(self, cfg):
        super(RetinaNetModule, self).__init__()

        self.cfg = cfg.clone()

        anchor_generator = make_anchor_generator_retinanet(cfg)
        head = RetinaNetHead(cfg)
        box_coder = BoxCoder(weights=(10., 10., 5., 5.))

        if self.cfg.MODEL.SPARSE_MASK_ON:
            box_selector_test = make_retinanet_detail_postprocessor(
                cfg, 100, box_coder)
        else:
            box_selector_test = make_retinanet_postprocessor(
                cfg, 100, box_coder)
        box_selector_train = None
        if self.cfg.MODEL.MASK_ON or self.cfg.MODEL.SPARSE_MASK_ON:
            box_selector_train = make_retinanet_postprocessor(
                cfg, 100, box_coder)

        loss_evaluator = make_free_anchor_loss_evaluator(cfg, box_coder) if cfg.FREEANCHOR.FREEANCHOR_ON \
            else make_retinanet_loss_evaluator(cfg, box_coder)

        self.anchor_generator = anchor_generator
        self.head = head
        self.box_selector_test = box_selector_test
        self.box_selector_train = box_selector_train
        self.loss_evaluator = loss_evaluator
Beispiel #24
0
def make_roi_box_post_processor(cfg):
    use_fpn = cfg.MODEL.ROI_HEADS.USE_FPN

    bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS
    box_coder = BoxCoder(weights=bbox_reg_weights)

    score_thresh = cfg.MODEL.ROI_HEADS.SCORE_THRESH
    nms_thresh = cfg.MODEL.ROI_HEADS.NMS_THRESH
    nms_method = cfg.MODEL.ROI_HEADS.NMS_METHOD
    nms_extra_argv = {}
    if nms_method == "soft_nms":
        nms_extra_argv["sigma"] = cfg.MODEL.ROI_HEADS.NMS_SIGMA
        nms_extra_argv["min_score"] = cfg.MODEL.ROI_HEADS.NMS_MIN_SCORE
    detections_per_img = cfg.MODEL.ROI_HEADS.DETECTIONS_PER_IMG
    cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG
    post_process_on = cfg.MODEL.ROI_HEADS.POST_PROCESS_ON

    postprocessor = PostProcessor(
        score_thresh,
        nms_thresh,
        detections_per_img,
        box_coder,
        cls_agnostic_bbox_reg,
        post_process_on,
        nms_method,
        nms_extra_argv,
    )
    return postprocessor
Beispiel #25
0
 def __init__(
     self,
     cfg,
     score_thresh=0.05,
     nms=0.5,
     detections_per_img=10000,
     box_coder=None,
     cls_agnostic_bbox_reg=False
 ):
     """
     Arguments:
         score_thresh (float)
         nms (float)
         detections_per_img (int)
         box_coder (BoxCoder)
     """
     super(PostProcessor, self).__init__()
     self.score_thresh = score_thresh
     self.nms = nms
     self.cfg=cfg
     self.detections_per_img = detections_per_img
     if box_coder is None:
         box_coder = BoxCoder(weights=(10., 10., 5., 5.))
     self.box_coder = box_coder
     self.cls_agnostic_bbox_reg = cls_agnostic_bbox_reg
Beispiel #26
0
 def __init__(
     self,
     score_thresh=0.05,
     nms=0.5,
     detections_per_img=100,
     box_coder=None,
     cls_agnostic_bbox_reg=False,
     post_process_on=True,
     nms_method='vanilla',
     nms_sigma=0.3,
     nms_min_score=0.1,
 ):
     """
     Arguments:
         score_thresh (float)
         nms (float)
         detections_per_img (int)
         box_coder (BoxCoder)
     """
     super(PostProcessor, self).__init__()
     self.score_thresh = score_thresh
     self.nms = nms
     self.detections_per_img = detections_per_img
     if box_coder is None:
         box_coder = BoxCoder(weights=(10., 10., 5., 5.))
     self.box_coder = box_coder
     self.cls_agnostic_bbox_reg = cls_agnostic_bbox_reg
     self.post_process_on = post_process_on
     self.nms_method = nms_method
     self.nms_sigma = nms_sigma
     self.nms_min_score = nms_min_score
    def __init__(
        self,
        pre_nms_thresh,
        pre_nms_top_n,
        nms_thresh,
        fpn_post_nms_top_n,
        min_size,
        box_coder=None,
    ):
        """
        Arguments:
            pre_nms_thresh (float)
            pre_nms_top_n (int)
            nms_thresh (float)
            fpn_post_nms_top_n (int)
            min_size (int)
            box_coder (BoxCoder)
        """
        super(RetinaNetPostProcessor, self).__init__()
        self.pre_nms_thresh = pre_nms_thresh
        self.pre_nms_top_n = pre_nms_top_n
        self.nms_thresh = nms_thresh
        self.fpn_post_nms_top_n = fpn_post_nms_top_n
        self.min_size = min_size

        if box_coder is None:
            box_coder = BoxCoder(weights=(10., 10., 5., 5.))
        self.box_coder = box_coder
Beispiel #28
0
    def __init__(self, cfg, in_channels):
        super(RPNModule, self).__init__()

        self.cfg = cfg.clone()
        RPN_HEADS={'SingleConvRPNHead':RPNHead}
        anchor_generator = make_anchor_generator(cfg)
        print(cfg.MODEL.RPN.RPN_HEAD)
#         rpn_head = registry.RPN_HEADS[cfg.MODEL.RPN.RPN_HEAD]
        rpn_head=RPN_HEADS[cfg.MODEL.RPN.RPN_HEAD]
        head = rpn_head(
            cfg, in_channels, anchor_generator.num_anchors_per_location()[0]
        )

        rpn_box_coder = BoxCoder(weights=(1.0, 1.0, 1.0, 1.0))

        box_selector_train = make_rpn_postprocessor(cfg, rpn_box_coder, is_train=True)
        box_selector_test = make_rpn_postprocessor(cfg, rpn_box_coder, is_train=False)

        loss_evaluator = make_rpn_loss_evaluator(cfg, rpn_box_coder)

        self.anchor_generator = anchor_generator
        self.head = head
        self.box_selector_train = box_selector_train
        self.box_selector_test = box_selector_test
        self.loss_evaluator = loss_evaluator
Beispiel #29
0
    def __init__(self, cfg):
        super(RPNModule, self).__init__()

        self.cfg = cfg.clone()

        anchor_generator = make_anchor_generator(cfg)

        in_channels = cfg.MODEL.BACKBONE.OUT_CHANNELS
        rpn_head = registry.RPN_HEADS[cfg.MODEL.RPN.RPN_HEAD]
        head = rpn_head(
            cfg, in_channels, anchor_generator.num_anchors_per_location()[0]
        )

        rpn_box_coder = BoxCoder(weights=(1.0, 1.0, 1.0, 1.0))

        box_selector_train = make_rpn_postprocessor(cfg, rpn_box_coder, is_train=True)
        box_selector_test = make_rpn_postprocessor(cfg, rpn_box_coder, is_train=False)

        loss_evaluator = make_rpn_loss_evaluator(cfg, rpn_box_coder)

        self.anchor_generator = anchor_generator
        self.head = head
        self.box_selector_train = box_selector_train
        self.box_selector_test = box_selector_test
        self.loss_evaluator = loss_evaluator
Beispiel #30
0
    def __init__(self, cfg, in_channels):
        super(RPNModule, self).__init__()

        self.cfg = cfg.clone()

        anchor_generator = make_anchor_generator(cfg)

        rpn_head = registry.RPN_HEADS[cfg.MODEL.RPN.RPN_HEAD]
        head = rpn_head(cfg, in_channels,
                        anchor_generator.num_anchors_per_location()[0])

        if self.cfg.MODEL.RPN.FREEZE_WEIGHT:
            for p in head.parameters():
                p.requires_grad = False

        rpn_box_coder = BoxCoder(weights=(1.0, 1.0, 1.0, 1.0))

        box_selector_train = make_rpn_postprocessor(cfg,
                                                    rpn_box_coder,
                                                    is_train=True)
        box_selector_test = make_rpn_postprocessor(cfg,
                                                   rpn_box_coder,
                                                   is_train=False)

        loss_evaluator = make_rpn_loss_evaluator(cfg, rpn_box_coder)

        self.anchor_generator = anchor_generator
        self.head = head
        self.box_selector_train = box_selector_train
        self.box_selector_test = box_selector_test
        self.loss_evaluator = loss_evaluator