def assign_all(self, anchors, gt_boxes, anchors_mask=None, gt_classes=None, matched_thresholds=None, unmatched_thresholds=None, importance=None): if anchors_mask is not None: prune_anchor_fn = lambda _: np.where(anchors_mask)[0] else: prune_anchor_fn = None def similarity_fn(anchors, gt_boxes): anchors_rbv = anchors[:, [0, 1, 3, 4, 6]] gt_boxes_rbv = gt_boxes[:, [0, 1, 3, 4, 6]] return self._sim_calcs[0].compare(anchors_rbv, gt_boxes_rbv) def box_encoding_fn(boxes, anchors): return self._box_coder.encode(boxes, anchors) return create_target_np(anchors, gt_boxes, similarity_fn, box_encoding_fn, prune_anchor_fn=prune_anchor_fn, gt_classes=gt_classes, matched_threshold=matched_thresholds, unmatched_threshold=unmatched_thresholds, positive_fraction=self._positive_fraction, rpn_batch_size=self._sample_size, norm_by_num_examples=False, box_code_size=self.box_coder.code_size, gt_importance=importance)
def assign(self, anchors, gt_boxes, anchors_mask=None, gt_classes=None, matched_thresholds=None, unmatched_thresholds=None): if anchors_mask is not None: prune_anchor_fn = lambda _: np.where(anchors_mask)[ 0] # 返回非0元素的坐标,这里的lambda很多余,实际上不需要任何参数 else: prune_anchor_fn = None def similarity_fn(anchors, gt_boxes): # 根据鸟瞰图计算IOU,嵌套函数第一时间不执行 anchors_rbv = anchors[:, [0, 1, 3, 4, 6]] gt_boxes_rbv = gt_boxes[:, [0, 1, 3, 4, 6]] return self._region_similarity_calculator.compare( anchors_rbv, gt_boxes_rbv) def box_encoding_fn(boxes, anchors): # 锚框编码,嵌套函数可以第一时间不传参数 return self._box_coder.encode(boxes, anchors) return create_target_np( # 创建训练目标的核心函数 anchors, # (248*216*2,7) gt_boxes, # (gt_num, 7) similarity_fn, # func box_encoding_fn, # func prune_anchor_fn=prune_anchor_fn, # func gt_classes=gt_classes, # (gt_num,)从1开始标志不同真值的类别 matched_threshold=matched_thresholds, # 0.6,(248*216*2,) unmatched_threshold=unmatched_thresholds, # 0.45,(248*216*2,) positive_fraction=self._positive_fraction, # None rpn_batch_size=self._sample_size, # 512 norm_by_num_examples=False, box_code_size=self.box_coder.code_size) # 7
def assign(self, anchors, gt_boxes, anchors_mask=None, gt_classes=None, matched_thresholds=None, unmatched_thresholds=None): if anchors_mask is not None: prune_anchor_fn = lambda _: np.where(anchors_mask)[0] else: prune_anchor_fn = None def similarity_fn(anchors, gt_boxes): anchors_rbv = anchors[:, [0, 1, 3, 4, 6]] ## anchors的第7位是? gt_boxes_rbv = gt_boxes[:, [0, 1, 3, 4, 6]] return self._region_similarity_calculator.compare( anchors_rbv, gt_boxes_rbv)## 计算rbv锚框的相似度 def box_encoding_fn(boxes, anchors): return self._box_coder.encode(boxes, anchors)## box和锚框需要编码成? return create_target_np( anchors, gt_boxes[gt_classes == class_name], similarity_fn, box_encoding_fn, prune_anchor_fn=prune_anchor_fn, gt_classes=gt_classes[gt_classes == class_name], matched_threshold=matched_thresholds, unmatched_threshold=unmatched_thresholds, positive_fraction=self._positive_fraction, rpn_batch_size=self._sample_size, norm_by_num_examples=False, box_code_size=self.box_coder.code_size)
def assign_v2(self, anchors_dict, gt_boxes, anchors_mask=None, gt_classes=None, gt_names=None): if anchors_mask is not None: prune_anchor_fn = lambda _: np.where(anchors_mask)[0] else: prune_anchor_fn = None def similarity_fn(anchors, gt_boxes): anchors_rbv = anchors[:, [0, 1, 3, 4, 6]] gt_boxes_rbv = gt_boxes[:, [0, 1, 3, 4, 6]] return self._region_similarity_calculator.compare( anchors_rbv, gt_boxes_rbv) def box_encoding_fn(boxes, anchors): return self._box_coder.encode(boxes, anchors) targets_list = [] for class_name, anchor_dict in anchors_dict.items(): mask = np.array([c == class_name for c in gt_names], dtype=np.bool_) targets = create_target_np( anchor_dict["anchors"].reshape(-1, self.box_coder.code_size), gt_boxes[mask], similarity_fn, box_encoding_fn, prune_anchor_fn=prune_anchor_fn, gt_classes=gt_classes[mask], matched_threshold=anchor_dict["matched_thresholds"], unmatched_threshold=anchor_dict["unmatched_thresholds"], positive_fraction=self._positive_fraction, rpn_batch_size=self._sample_size, norm_by_num_examples=False, box_code_size=self.box_coder.code_size) targets_list.append(targets) feature_map_size = anchor_dict["anchors"].shape[:3] targets_dict = { "labels": [t["labels"] for t in targets_list], "bbox_targets": [t["bbox_targets"] for t in targets_list], "bbox_outside_weights": [t["bbox_outside_weights"] for t in targets_list], } targets_dict["bbox_targets"] = np.concatenate([v.reshape(*feature_map_size, -1, self.box_coder.code_size) for v in targets_dict["bbox_targets"]], axis=-2) targets_dict["bbox_targets"] = targets_dict["bbox_targets"].reshape(-1, self.box_coder.code_size) targets_dict["labels"] = np.concatenate([v.reshape(*feature_map_size, -1) for v in targets_dict["labels"]], axis=-1) targets_dict["bbox_outside_weights"] = np.concatenate([v.reshape(*feature_map_size, -1) for v in targets_dict["bbox_outside_weights"]], axis=-1) targets_dict["labels"] = targets_dict["labels"].reshape(-1) targets_dict["bbox_outside_weights"] = targets_dict["bbox_outside_weights"].reshape(-1) return targets_dict
def assign_per_class(self, anchors_dict, gt_boxes, anchors_mask=None, gt_classes=None, gt_names=None, importance=None): """this function assign target individally for each class. recommend for multi-class network. """ def box_encoding_fn(boxes, anchors): return self._box_coder.encode(boxes, anchors) targets_list = [] anchor_loc_idx = 0 anchor_gene_idx = 0 for class_name, anchor_dict in anchors_dict.items(): def similarity_fn(anchors, gt_boxes): anchors_rbv = anchors[:, [0, 1, 3, 4, 6]] gt_boxes_rbv = gt_boxes[:, [0, 1, 3, 4, 6]] return self._sim_calcs[anchor_gene_idx].compare( anchors_rbv, gt_boxes_rbv) mask = np.array([c == class_name for c in gt_names], dtype=np.bool_) feature_map_size = anchor_dict["anchors"].shape[:3] num_loc = anchor_dict["anchors"].shape[-2] if anchors_mask is not None: anchors_mask = anchors_mask.reshape(-1) a_range = self.anchors_range(class_name) anchors_mask_class = anchors_mask[ a_range[0]:a_range[1]].reshape(-1) prune_anchor_fn = lambda _: np.where(anchors_mask_class)[0] else: prune_anchor_fn = None # print(f"num of {class_name}:", np.sum(mask)) targets = create_target_np( anchor_dict["anchors"].reshape(-1, self.box_ndim), gt_boxes[mask], similarity_fn, box_encoding_fn, prune_anchor_fn=prune_anchor_fn, gt_classes=gt_classes[mask], matched_threshold=anchor_dict["matched_thresholds"], unmatched_threshold=anchor_dict["unmatched_thresholds"], positive_fraction=self._positive_fraction, rpn_batch_size=self._sample_size, norm_by_num_examples=False, box_code_size=self.box_coder.code_size, gt_importance=importance) # print(f"num of positive:", np.sum(targets["labels"] == self.classes.index(class_name) + 1)) anchor_loc_idx += num_loc targets_list.append(targets) anchor_gene_idx += 1 targets_dict = { "labels": [t["labels"] for t in targets_list], "bbox_targets": [t["bbox_targets"] for t in targets_list], "importance": [t["importance"] for t in targets_list], } targets_dict["bbox_targets"] = np.concatenate([ v.reshape(-1, self.box_coder.code_size) for v in targets_dict["bbox_targets"] ], axis=0) targets_dict["bbox_targets"] = targets_dict["bbox_targets"].reshape( -1, self.box_coder.code_size) targets_dict["labels"] = np.concatenate( [v.reshape(-1) for v in targets_dict["labels"]], axis=0) targets_dict["importance"] = np.concatenate( [v.reshape(-1) for v in targets_dict["importance"]], axis=0) targets_dict["labels"] = targets_dict["labels"].reshape(-1) targets_dict["importance"] = targets_dict["importance"].reshape(-1) return targets_dict