コード例 #1
0
ファイル: detector_builder.py プロジェクト: taksau/GPS-Net
    def __init__(self):
        super().__init__()

        # For cache
        self.mapping_to_detectron = None
        self.orphans_in_detectron = None

        # Backbone for feature extraction
        self.Conv_Body = get_func(cfg.MODEL.CONV_BODY)()

        # Region Proposal Network
        if cfg.RPN.RPN_ON:
            self.RPN = get_func(cfg.RPN.HEAD)(
                self.Conv_Body.dim_out, self.Conv_Body.spatial_scale)

        if cfg.FPN.FPN_ON:
            # Only supports case when RPN and ROI min levels are the same
            assert cfg.FPN.RPN_MIN_LEVEL == cfg.FPN.ROI_MIN_LEVEL
            # RPN max level can be >= to ROI max level
            assert cfg.FPN.RPN_MAX_LEVEL >= cfg.FPN.ROI_MAX_LEVEL
            # FPN RPN max level might be > FPN ROI max level in which case we
            # need to discard some leading conv blobs (blobs are ordered from
            # max/coarsest level to min/finest level)
            self.num_roi_levels = cfg.FPN.ROI_MAX_LEVEL - cfg.FPN.ROI_MIN_LEVEL + 1

            # Retain only the spatial scales that will be used for RoI heads. `Conv_Body.spatial_scale`
            # may include extra scales that are used for RPN proposals, but not for RoI heads.
            self.Conv_Body.spatial_scale = self.Conv_Body.spatial_scale[-self.num_roi_levels:]

        # BBOX Branch
        self.Box_Head = get_func(cfg.FAST_RCNN.ROI_BOX_HEAD)(
            self.RPN.dim_out, self.roi_feature_transform, self.Conv_Body.spatial_scale)
        self.Box_Outs = fast_rcnn_heads.fast_rcnn_outputs(
            self.Box_Head.dim_out)
        
        # self.Prd_RCNN = copy.deepcopy(self)
        # del self.Prd_RCNN.RPN
        # del self.Prd_RCNN.Box_Outs
        # del self.Prd_RCNN.Box_Head

        # self.RelPN = relpn_heads.generic_relpn_outputs()
        
            
         # rel pyramid connection
        if cfg.MODEL.USE_REL_PYRAMID:
            assert cfg.FPN.FPN_ON
            self.RelPyramid = rel_pyramid_module.rel_pyramid_module(self.num_roi_levels)
        
        

        self._init_modules()
コード例 #2
0
    def __init__(self):
        super().__init__()

        # For cache
        self.mapping_to_detectron = None
        self.orphans_in_detectron = None

        # Backbone for feature extraction
        self.Conv_Body = get_func(cfg.MODEL.CONV_BODY)()

        # Region Proposal Network
        if cfg.RPN.RPN_ON:
            self.RPN = rpn_heads.generic_rpn_outputs(
                self.Conv_Body.dim_out, self.Conv_Body.spatial_scale)

        if cfg.FPN.FPN_ON:
            # Only supports case when RPN and ROI min levels are the same
            assert cfg.FPN.RPN_MIN_LEVEL == cfg.FPN.ROI_MIN_LEVEL
            # RPN max level can be >= to ROI max level
            assert cfg.FPN.RPN_MAX_LEVEL >= cfg.FPN.ROI_MAX_LEVEL
            # FPN RPN max level might be > FPN ROI max level in which case we
            # need to discard some leading conv blobs (blobs are ordered from
            # max/coarsest level to min/finest level)
            self.num_roi_levels = cfg.FPN.ROI_MAX_LEVEL - cfg.FPN.ROI_MIN_LEVEL + 1

            # Retain only the spatial scales that will be used for RoI heads. `Conv_Body.spatial_scale`
            # may include extra scales that are used for RPN proposals, but not for RoI heads.
            self.Conv_Body.spatial_scale = self.Conv_Body.spatial_scale[
                -self.num_roi_levels:]

        # BBOX Branch
        self.Box_Head = get_func(cfg.FAST_RCNN.ROI_BOX_HEAD)(
            self.RPN.dim_out, self.roi_feature_transform,
            self.Conv_Body.spatial_scale)
        self.Box_Outs = fast_rcnn_heads.fast_rcnn_outputs(
            self.Box_Head.dim_out)

        self.ori_embed = get_ort_embeds(cfg.MODEL.NUM_CLASSES, 200)

        # rel pyramid connection
        if cfg.MODEL.USE_REL_PYRAMID:
            assert cfg.FPN.FPN_ON
            self.RelPyramid = rel_pyramid_module.rel_pyramid_module(
                self.num_roi_levels)

        # RelPN
        self.RelPN = relpn_heads.generic_relpn_outputs()

        self.Box_Head_sg = copy.deepcopy(self.Box_Head)
        self.Box_Head_prd = get_func(cfg.FAST_RCNN.ROI_BOX_HEAD_PRD)(
            self.RPN.dim_out, self.roi_feature_transform,
            self.Conv_Body.spatial_scale)
        self.union_mask = reldn_heads.union_mask(self.RPN.dim_out)

        self.obj_dim = self.Box_Head.dim_out
        self.merge_obj_feats = Merge_OBJ_Feats(self.obj_dim, 200, 512)
        self.obj_mps1 = Message_Passing4OBJ(512)
        self.obj_mps2 = Message_Passing4OBJ(512)
        self.ObjClassifier = nn.Linear(512, cfg.MODEL.NUM_CLASSES)

        self.EdgePN = relpn_heads.single_scale_pairs_pn_outputs(False)
        self.get_phr_feats = nn.Linear(self.obj_dim, 512)

        self.sbj_map = nn.Linear(self.obj_dim + 200 + 5, self.obj_dim)
        self.sbj_map.weight = torch.nn.init.xavier_normal_(self.sbj_map.weight,
                                                           gain=1.0)
        self.obj_map = nn.Linear(self.obj_dim + 200 + 5, self.obj_dim)
        self.obj_map.weight = torch.nn.init.xavier_normal_(self.obj_map.weight,
                                                           gain=1.0)

        if cfg.MODEL.USE_BG:
            self.num_prd_classes = cfg.MODEL.NUM_PRD_CLASSES + 1
        else:
            self.num_prd_classes = cfg.MODEL.NUM_PRD_CLASSES

        self.rel_compress = nn.Linear(self.obj_dim, self.num_prd_classes)
        self.rel_compress.weight = torch.nn.init.xavier_normal_(
            self.rel_compress.weight, gain=1.0)

        if cfg.MODEL.USE_FREQ_BIAS:
            # Assume we are training/testing on only one dataset
            if len(cfg.TRAIN.DATASETS):
                self.freq_bias = FrequencyBias_Fix(cfg.TRAIN.DATASETS[0])
            else:
                self.freq_bias = FrequencyBias_Fix(cfg.TEST.DATASETS[0])

        self._init_modules()