(self, example,  refine_weight=2)
module's forward should always accept dict and return loss.
module's forward should always accept dict and return loss.
module s forward should always accept dict and return loss .
def forward(self, example,  refine_weight=2):
        """module's forward should always accept dict and return loss.
        """
        #print('refine_weight:', refine_weight)
        voxels = example["voxels"]
        num_points = example["num_points"]
        coors = example["coordinates"]
        batch_anchors = example["anchors"]
        batch_size_dev = batch_anchors.shape[0]
        t = time.time()
        # features: [num_voxels, max_num_points_per_voxel, 7]
        # num_points: [num_voxels]
        # coors: [num_voxels, 4]
        voxel_features = self.voxel_feature_extractor(voxels, num_points, coors)
        if self._use_sparse_rpn:
            preds_dict = self.sparse_rpn(voxel_features, coors, batch_size_dev)
        else:
            spatial_features = self.middle_feature_extractor(
                voxel_features, coors, batch_size_dev)
            if self._use_bev:
                preds_dict = self.rpn(spatial_features, example["bev_map"])
            else:
                preds_dict = self.rpn(spatial_features)
        # preds_dict["voxel_features"] = voxel_features
        # preds_dict["spatial_features"] = spatial_features
        box_preds = preds_dict["box_preds"]
        cls_preds = preds_dict["cls_preds"]
        self._total_forward_time += time.time() - t
        if self.training:
            labels = example['labels']
            reg_targets = example['reg_targets']

            cls_weights, reg_weights, cared = prepare_loss_weights(
                labels,
                pos_cls_weight=self._pos_cls_weight,
                neg_cls_weight=self._neg_cls_weight,
                loss_norm_type=self._loss_norm_type,
                dtype=voxels.dtype)
            cls_targets = labels * cared.type_as(labels)
            cls_targets = cls_targets.unsqueeze(-1)

            loc_loss, cls_loss = create_loss(
                self._loc_loss_ftor,
                self._cls_loss_ftor,
                box_preds=box_preds,
                cls_preds=cls_preds,
                cls_targets=cls_targets,
                cls_weights=cls_weights,
                reg_targets=reg_targets,
                reg_weights=reg_weights,
                num_class=self._num_class,
                encode_rad_error_by_sin=self._encode_rad_error_by_sin,
                encode_background_as_zeros=self._encode_background_as_zeros,
                box_code_size=self._box_coder.code_size,
            )

            loc_loss_reduced = loc_loss.sum() / batch_size_dev
            loc_loss_reduced *= self._loc_loss_weight
            cls_pos_loss, cls_neg_loss = _get_pos_neg_loss(cls_loss, labels)
            cls_pos_loss /= self._pos_cls_weight
            cls_neg_loss /= self._neg_cls_weight
            cls_loss_reduced = cls_loss.sum() / batch_size_dev
            cls_loss_reduced *= self._cls_loss_weight
            coarse_loss = loc_loss_reduced + cls_loss_reduced


            if self._use_direction_classifier:
                dir_targets = get_direction_target(example['anchors'],
                                                   reg_targets)
                dir_logits = preds_dict["dir_cls_preds"].view(
                    batch_size_dev, -1, 2)


                weights = (labels > 0).type_as(dir_logits)
                weights /= torch.clamp(weights.sum(-1, keepdim=True), min=1.0)

                ### compute coarse dir loss
                coarse_dir_loss = self._dir_loss_ftor(dir_logits, dir_targets, weights=weights)
                dir_loss = coarse_dir_loss.sum() / batch_size_dev
                coarse_loss += dir_loss * self._direction_loss_weight


            if self.rpn_class_name == "PSA" or self.rpn_class_name == "RefineDet":
                refine_box_preds = preds_dict["Refine_loc_preds"]
                refine_cls_preds = preds_dict["Refine_cls_preds"]

                positives = labels > 0
                reg_weights_ori = positives.type(torch.float32)

                refine_loc_loss, refine_cls_loss = create_refine_loss(
                                 self._loc_loss_ftor,
                                 self._cls_loss_ftor,
                                 example,
                                 coarse_box_preds=box_preds,
                                 coarse_cls_preds=cls_preds,
                                 refine_box_preds=refine_box_preds,
                                 refine_cls_preds=refine_cls_preds,
                                 cls_targets=cls_targets,
                                 cls_weights=cls_weights,
                                 reg_targets=reg_targets,
                                 reg_weights=reg_weights,
                                 num_class=self._num_class,
                                 encode_background_as_zeros=True,
                                 encode_rad_error_by_sin=True,
                                 box_code_size=7,
                                 reg_weights_ori = reg_weights_ori)

                '''
                refine_loc_loss, refine_cls_loss = create_refine_loss_V2(self._loc_loss_ftor,
                                  self._cls_loss_ftor,
                                  example,
                                  coarse_box_batch_preds = box_preds,
                                  coarse_cls_batch_preds = cls_preds,
                                  refine_box_batch_preds = refine_box_preds,
                                  refine_cls_batch_preds = refine_box_preds,
                                  num_class=self._num_class,
                                  loss_norm_type=self._loss_norm_type,
                                  encode_background_as_zeros=True,
                                  encode_rad_error_by_sin=True,
                                  box_code_size=7, max_voxels=12000,
                                  anchor_area_threshold=1.0,
                                  grid_size=[352, 400, 1], voxel_size=[0.2, 0.2, 4],
                                  pc_range=[0.0,-40.0,-3.0,70.4,40.0, 1.0])
                '''


                refine_loc_loss = refine_weight * refine_loc_loss
                refine_cls_loss = refine_weight * refine_cls_loss

                refine_loc_loss_reduced = refine_loc_loss.sum() / batch_size_dev
                refine_loc_loss_reduced *= self._loc_loss_weight  # self._loc_loss_weight = 2.0
                refine_cls_pos_loss, refine_cls_neg_loss = _get_pos_neg_loss(refine_cls_loss, labels)
                refine_cls_pos_loss /= self._pos_cls_weight
                refine_cls_neg_loss /= self._neg_cls_weight
                refine_cls_loss_reduced = refine_cls_loss.sum() / batch_size_dev
                refine_cls_loss_reduced *= self._cls_loss_weight

                refine_loss = refine_loc_loss_reduced + refine_cls_loss_reduced  # + refine_iou_loss

                if self._use_direction_classifier:
                    refine_dir_logits = preds_dict["Refine_dir_preds"].view(batch_size_dev, -1, 2)
                    ### compute refine dir loss
                    refine_dir_loss = self._dir_loss_ftor(refine_dir_logits, dir_targets, weights=weights)
                    refine_dir_loss = refine_dir_loss.sum() / batch_size_dev

                    ### compute refine loss   self._direction_loss_weight = 0.2
                    refine_loss += refine_dir_loss * self._direction_loss_weight

                total_loss = coarse_loss + refine_loss

                return {
                    "loss": total_loss,
                    "coarse_loss": coarse_loss,
                    "refine_loss": refine_loss,
                    "cls_loss": cls_loss,
                    "loc_loss": loc_loss,
                    "cls_pos_loss": cls_pos_loss,
                    "cls_neg_loss": cls_neg_loss,
                    "refine_cls_loss_reduced": refine_cls_loss_reduced,
                    "refine_loc_loss_reduced": refine_loc_loss_reduced,
                    "refine_dir_loss_reduced": refine_dir_loss,
                    "cls_preds": refine_cls_preds,  ##cls_preds,
                    "dir_loss_reduced": dir_loss,  # dir_loss,
                    "cls_loss_reduced": cls_loss_reduced,
                    "loc_loss_reduced": loc_loss_reduced,
                    "cared": cared,
                    # "iou_loss": coarse_iou_loss,
                    # "refine_iou_loss": refine_iou_loss,
                }


            else:
                return {
                    "loss": coarse_loss,
                    "cls_loss": cls_loss,
                    "loc_loss": loc_loss,
                    "cls_pos_loss": cls_pos_loss,
                    "cls_neg_loss": cls_neg_loss,
                    "cls_preds": cls_preds,
                    "dir_loss_reduced": dir_loss,
                    "cls_loss_reduced": cls_loss_reduced,
                    "loc_loss_reduced": loc_loss_reduced,
                    "cared": cared,
            }
        else:
            if self.rpn_class_name == "PSA" or self.rpn_class_name == "RefineDet":
                coarse_output = self.predict_coarse(example, preds_dict)
                refine_output = self.predict_refine(example, preds_dict)
                return coarse_output, refine_output
            else:
                return self.predict_coarse(example, preds_dict)
def forward ( self , example , refine_weight = 2 ) : #print('refine_weight:', refine_weight) voxels = example [ "voxels" ] num_points = example [ "num_points" ] coors = example [ "coordinates" ] batch_anchors = example [ "anchors" ] batch_size_dev = batch_anchors . shape [ 0 ] t = time . time ( ) # features: [num_voxels, max_num_points_per_voxel, 7] # num_points: [num_voxels] # coors: [num_voxels, 4] voxel_features = self . voxel_feature_extractor ( voxels , num_points , coors ) if self . _use_sparse_rpn : preds_dict = self . sparse_rpn ( voxel_features , coors , batch_size_dev ) else : spatial_features = self . middle_feature_extractor ( voxel_features , coors , batch_size_dev ) if self . _use_bev : preds_dict = self . rpn ( spatial_features , example [ "bev_map" ] ) else : preds_dict = self . rpn ( spatial_features ) # preds_dict["voxel_features"] = voxel_features # preds_dict["spatial_features"] = spatial_features box_preds = preds_dict [ "box_preds" ] cls_preds = preds_dict [ "cls_preds" ] self . _total_forward_time += time . time ( ) - t if self . training : labels = example [ 'labels' ] reg_targets = example [ 'reg_targets' ] cls_weights , reg_weights , cared = prepare_loss_weights ( labels , pos_cls_weight = self . _pos_cls_weight , neg_cls_weight = self . _neg_cls_weight , loss_norm_type = self . _loss_norm_type , dtype = voxels . dtype ) cls_targets = labels * cared . type_as ( labels ) cls_targets = cls_targets . unsqueeze ( - 1 ) loc_loss , cls_loss = create_loss ( self . _loc_loss_ftor , self . _cls_loss_ftor , box_preds = box_preds , cls_preds = cls_preds , cls_targets = cls_targets , cls_weights = cls_weights , reg_targets = reg_targets , reg_weights = reg_weights , num_class = self . _num_class , encode_rad_error_by_sin = self . _encode_rad_error_by_sin , encode_background_as_zeros = self . _encode_background_as_zeros , box_code_size = self . _box_coder . code_size , ) loc_loss_reduced = loc_loss . sum ( ) / batch_size_dev loc_loss_reduced *= self . _loc_loss_weight cls_pos_loss , cls_neg_loss = _get_pos_neg_loss ( cls_loss , labels ) cls_pos_loss /= self . _pos_cls_weight cls_neg_loss /= self . _neg_cls_weight cls_loss_reduced = cls_loss . sum ( ) / batch_size_dev cls_loss_reduced *= self . _cls_loss_weight coarse_loss = loc_loss_reduced + cls_loss_reduced if self . _use_direction_classifier : dir_targets = get_direction_target ( example [ 'anchors' ] , reg_targets ) dir_logits = preds_dict [ "dir_cls_preds" ] . view ( batch_size_dev , - 1 , 2 ) weights = ( labels > 0 ) . type_as ( dir_logits ) weights /= torch . clamp ( weights . sum ( - 1 , keepdim = True ) , min = 1.0 ) ### compute coarse dir loss coarse_dir_loss = self . _dir_loss_ftor ( dir_logits , dir_targets , weights = weights ) dir_loss = coarse_dir_loss . sum ( ) / batch_size_dev coarse_loss += dir_loss * self . _direction_loss_weight if self . rpn_class_name == "PSA" or self . rpn_class_name == "RefineDet" : refine_box_preds = preds_dict [ "Refine_loc_preds" ] refine_cls_preds = preds_dict [ "Refine_cls_preds" ] positives = labels > 0 reg_weights_ori = positives . type ( torch . float32 ) refine_loc_loss , refine_cls_loss = create_refine_loss ( self . _loc_loss_ftor , self . _cls_loss_ftor , example , coarse_box_preds = box_preds , coarse_cls_preds = cls_preds , refine_box_preds = refine_box_preds , refine_cls_preds = refine_cls_preds , cls_targets = cls_targets , cls_weights = cls_weights , reg_targets = reg_targets , reg_weights = reg_weights , num_class = self . _num_class , encode_background_as_zeros = True , encode_rad_error_by_sin = True , box_code_size = 7 , reg_weights_ori = reg_weights_ori ) '''
                refine_loc_loss, refine_cls_loss = create_refine_loss_V2(self._loc_loss_ftor,
                                  self._cls_loss_ftor,
                                  example,
                                  coarse_box_batch_preds = box_preds,
                                  coarse_cls_batch_preds = cls_preds,
                                  refine_box_batch_preds = refine_box_preds,
                                  refine_cls_batch_preds = refine_box_preds,
                                  num_class=self._num_class,
                                  loss_norm_type=self._loss_norm_type,
                                  encode_background_as_zeros=True,
                                  encode_rad_error_by_sin=True,
                                  box_code_size=7, max_voxels=12000,
                                  anchor_area_threshold=1.0,
                                  grid_size=[352, 400, 1], voxel_size=[0.2, 0.2, 4],
                                  pc_range=[0.0,-40.0,-3.0,70.4,40.0, 1.0])
                ''' refine_loc_loss = refine_weight * refine_loc_loss refine_cls_loss = refine_weight * refine_cls_loss refine_loc_loss_reduced = refine_loc_loss . sum ( ) / batch_size_dev refine_loc_loss_reduced *= self . _loc_loss_weight # self._loc_loss_weight = 2.0 refine_cls_pos_loss , refine_cls_neg_loss = _get_pos_neg_loss ( refine_cls_loss , labels ) refine_cls_pos_loss /= self . _pos_cls_weight refine_cls_neg_loss /= self . _neg_cls_weight refine_cls_loss_reduced = refine_cls_loss . sum ( ) / batch_size_dev refine_cls_loss_reduced *= self . _cls_loss_weight refine_loss = refine_loc_loss_reduced + refine_cls_loss_reduced # + refine_iou_loss if self . _use_direction_classifier : refine_dir_logits = preds_dict [ "Refine_dir_preds" ] . view ( batch_size_dev , - 1 , 2 ) ### compute refine dir loss refine_dir_loss = self . _dir_loss_ftor ( refine_dir_logits , dir_targets , weights = weights ) refine_dir_loss = refine_dir_loss . sum ( ) / batch_size_dev ### compute refine loss   self._direction_loss_weight = 0.2 refine_loss += refine_dir_loss * self . _direction_loss_weight total_loss = coarse_loss + refine_loss return { "loss" : total_loss , "coarse_loss" : coarse_loss , "refine_loss" : refine_loss , "cls_loss" : cls_loss , "loc_loss" : loc_loss , "cls_pos_loss" : cls_pos_loss , "cls_neg_loss" : cls_neg_loss , "refine_cls_loss_reduced" : refine_cls_loss_reduced , "refine_loc_loss_reduced" : refine_loc_loss_reduced , "refine_dir_loss_reduced" : refine_dir_loss , "cls_preds" : refine_cls_preds , ##cls_preds, "dir_loss_reduced" : dir_loss , # dir_loss, "cls_loss_reduced" : cls_loss_reduced , "loc_loss_reduced" : loc_loss_reduced , "cared" : cared , # "iou_loss": coarse_iou_loss, # "refine_iou_loss": refine_iou_loss, } else : return { "loss" : coarse_loss , "cls_loss" : cls_loss , "loc_loss" : loc_loss , "cls_pos_loss" : cls_pos_loss , "cls_neg_loss" : cls_neg_loss , "cls_preds" : cls_preds , "dir_loss_reduced" : dir_loss , "cls_loss_reduced" : cls_loss_reduced , "loc_loss_reduced" : loc_loss_reduced , "cared" : cared , } else : if self . rpn_class_name == "PSA" or self . rpn_class_name == "RefineDet" : coarse_output = self . predict_coarse ( example , preds_dict ) refine_output = self . predict_refine ( example , preds_dict ) return coarse_output , refine_output else : return self . predict_coarse ( example , preds_dict )