(self, cfg)
| 38 | """ |
| 39 | |
| 40 | def __init__(self, cfg): |
| 41 | super().__init__() |
| 42 | |
| 43 | self.device = torch.device(cfg.MODEL.DEVICE) |
| 44 | |
| 45 | self.in_features = cfg.MODEL.ROI_HEADS.IN_FEATURES |
| 46 | self.num_classes = cfg.MODEL.SparseRCNN.NUM_CLASSES |
| 47 | self.num_proposals = cfg.MODEL.SparseRCNN.NUM_PROPOSALS |
| 48 | self.hidden_dim = cfg.MODEL.SparseRCNN.HIDDEN_DIM |
| 49 | self.num_heads = cfg.MODEL.SparseRCNN.NUM_HEADS |
| 50 | |
| 51 | # Build Backbone. |
| 52 | self.backbone = build_backbone(cfg) |
| 53 | self.size_divisibility = self.backbone.size_divisibility |
| 54 | |
| 55 | # Build Proposals. |
| 56 | self.init_proposal_features = nn.Embedding(self.num_proposals, self.hidden_dim) |
| 57 | self.init_proposal_boxes = nn.Embedding(self.num_proposals, 4) |
| 58 | nn.init.constant_(self.init_proposal_boxes.weight[:, :2], 0.5) |
| 59 | nn.init.constant_(self.init_proposal_boxes.weight[:, 2:], 1.0) |
| 60 | |
| 61 | # Build Dynamic Head. |
| 62 | self.head = DynamicHead(cfg=cfg, roi_input_shape=self.backbone.output_shape()) |
| 63 | |
| 64 | # Loss parameters: |
| 65 | class_weight = cfg.MODEL.SparseRCNN.CLASS_WEIGHT |
| 66 | giou_weight = cfg.MODEL.SparseRCNN.GIOU_WEIGHT |
| 67 | l1_weight = cfg.MODEL.SparseRCNN.L1_WEIGHT |
| 68 | no_object_weight = cfg.MODEL.SparseRCNN.NO_OBJECT_WEIGHT |
| 69 | self.deep_supervision = cfg.MODEL.SparseRCNN.DEEP_SUPERVISION |
| 70 | self.use_focal = cfg.MODEL.SparseRCNN.USE_FOCAL |
| 71 | |
| 72 | # Build Criterion. |
| 73 | matcher = HungarianMatcher(cfg=cfg, |
| 74 | cost_class=class_weight, |
| 75 | cost_bbox=l1_weight, |
| 76 | cost_giou=giou_weight, |
| 77 | use_focal=self.use_focal) |
| 78 | weight_dict = {"loss_ce": class_weight, "loss_bbox": l1_weight, "loss_giou": giou_weight} |
| 79 | if self.deep_supervision: |
| 80 | aux_weight_dict = {} |
| 81 | for i in range(self.num_heads - 1): |
| 82 | aux_weight_dict.update({k + f"_{i}": v for k, v in weight_dict.items()}) |
| 83 | weight_dict.update(aux_weight_dict) |
| 84 | |
| 85 | losses = ["labels", "boxes"] |
| 86 | |
| 87 | self.criterion = SetCriterion(cfg=cfg, |
| 88 | num_classes=self.num_classes, |
| 89 | matcher=matcher, |
| 90 | weight_dict=weight_dict, |
| 91 | eos_coef=no_object_weight, |
| 92 | losses=losses, |
| 93 | use_focal=self.use_focal) |
| 94 | |
| 95 | pixel_mean = torch.Tensor(cfg.MODEL.PIXEL_MEAN).to(self.device).view(3, 1, 1) |
| 96 | pixel_std = torch.Tensor(cfg.MODEL.PIXEL_STD).to(self.device).view(3, 1, 1) |
| 97 | self.normalizer = lambda x: (x - pixel_mean) / pixel_std |
nothing calls this directly
no test coverage detected