def get_pos_anchors(self, fpn_feats, gt_bboxes, gt_labels, img_metas, cfg, gt_bboxes_ignore=None): featmap_sizes = [featmap.size()[-2:] for featmap in fpn_feats] assert len(featmap_sizes) == len(self.anchor_generators) device = fpn_feats[0].device img_metas = [img_metas] anchor_list, valid_flag_list = self.get_anchors( featmap_sizes, img_metas, device=device) for i in range(len(img_metas)): assert len(anchor_list[i]) == len(valid_flag_list[i]) anchor_list[i] = torch.cat(anchor_list[i]) valid_flag_list[i] = torch.cat(valid_flag_list[i]) anchor_valid = anchor_list[0][valid_flag_list[0],:] overlaps = bbox_overlaps(gt_bboxes, anchor_valid) assign_result = self.assign_wrt_overlaps(overlaps, gt_labels) bbox_sampler = PseudoSampler() sampling_result = bbox_sampler.sample(assign_result, anchor_valid, gt_bboxes) pos_inds = sampling_result.pos_inds #labels = anchor_valid.new_zeros(anchor_valid.shape[0], dtype=torch.long) labels_pos = gt_labels[sampling_result.pos_assigned_gt_inds] anchor_pos = anchor_valid[pos_inds] return anchor_pos, labels_pos
def anchor_target_single(self, anchors, gt_bboxes, gt_bboxes_ignore, gt_labels, img_meta, target_means, target_stds, cfg, label_channels=1, sampling=True, unmap_outputs=True): if sampling: assign_result, sampling_result = assign_and_sample( anchors, gt_bboxes, gt_bboxes_ignore, None, cfg) else: bbox_assigner = build_assigner(cfg.assigner) assign_result = bbox_assigner.assign(anchors, gt_bboxes, gt_bboxes_ignore, gt_labels) bbox_sampler = PseudoSampler() sampling_result = bbox_sampler.sample(assign_result, anchors, gt_bboxes) num_valid_anchors = anchors.shape[0] bbox_targets = torch.zeros_like(anchors) bbox_weights = torch.zeros_like(anchors) labels = anchors.new_zeros(num_valid_anchors, dtype=torch.long) label_weights = anchors.new_zeros(num_valid_anchors, dtype=torch.float) pos_inds = sampling_result.pos_inds neg_inds = sampling_result.neg_inds if len(pos_inds) > 0: pos_bbox_targets = bbox2delta(sampling_result.pos_bboxes, sampling_result.pos_gt_bboxes, target_means, target_stds) bbox_targets[pos_inds, :] = pos_bbox_targets bbox_weights[pos_inds, :] = 1.0 if gt_labels is None: labels[pos_inds] = 1 else: labels[pos_inds] = gt_labels[ sampling_result.pos_assigned_gt_inds] if cfg.pos_weight <= 0: label_weights[pos_inds] = 1.0 else: label_weights[pos_inds] = cfg.pos_weight if len(neg_inds) > 0: label_weights[neg_inds] = 1.0 return (labels, label_weights, bbox_targets, bbox_weights, pos_inds, neg_inds)
def forward_loss(self, fpn, bbox_list, gt_bboxes, gt_labels, gt_bboxes_ignore, train_cfg): cur_bbox, cur_labels, cur_score_vec = bbox_list[-1] ##### # Transformer! ##### src_feat, ind_list = self.get_src_feat(fpn, bbox_list) # [T*N_i, 1, 256] num_tgt = len(ind_list[-1]) tgt_feat = src_feat[-num_tgt:] tgt_out = self.forward_seq_model(tgt_feat, src_feat) # [N, 1, 256] cur_score_vec = self.remap_scores(cur_score_vec, ind_list[-1], tgt_out) ####################### # Loss Part ####################### gt_bboxes = gt_bboxes[-1] gt_labels = gt_labels[-1] gt_bboxes_ignore = gt_bboxes_ignore[ -1] if gt_bboxes_ignore is not None else None bbox_assigner = build_assigner(train_cfg.assigner) assign_result = bbox_assigner.assign(cur_bbox, gt_bboxes, gt_bboxes_ignore, gt_labels) sampling_result = PseudoSampler().sample(assign_result, cur_bbox[:, :4], gt_bboxes) # Sample according to sampling result cur_score_vec = torch.cat([ cur_score_vec[sampling_result.pos_inds], cur_score_vec[sampling_result.neg_inds] ], dim=0) losses = dict() bbox_targets = self.get_target([sampling_result], gt_bboxes, gt_labels, train_cfg) loss_score = self.loss(cur_score_vec, None, *bbox_targets) losses.update(loss_score) ####################### # End of loss Part ####################### return losses
def anchor_target_single(flat_anchors, valid_flags, gt_bboxes, gt_bboxes_ignore, gt_labels, img_meta, target_means, target_stds, cfg, label_channels=1, sampling=True, unmap_outputs=True): inside_flags = anchor_inside_flags(flat_anchors, valid_flags, img_meta['img_shape'][:2], cfg.allowed_border) if not inside_flags.any(): return (None, ) * 6 # assign gt and sample anchors anchors = flat_anchors[inside_flags, :] if sampling: assign_result, sampling_result = assign_and_sample( anchors, gt_bboxes, gt_bboxes_ignore, None, cfg) else: bbox_assigner = build_assigner(cfg.assigner) assign_result = bbox_assigner.assign(anchors, gt_bboxes, gt_bboxes_ignore, gt_labels) bbox_sampler = PseudoSampler() sampling_result = bbox_sampler.sample(assign_result, anchors, gt_bboxes) num_valid_anchors = anchors.shape[0] bbox_targets = torch.zeros_like(anchors) bbox_weights = torch.zeros_like(anchors) labels = anchors.new_zeros(num_valid_anchors, dtype=torch.long) label_weights = anchors.new_zeros(num_valid_anchors, dtype=torch.float) pos_inds = sampling_result.pos_inds neg_inds = sampling_result.neg_inds # import pdb # print('in anchor target') # pdb.set_trace() if len(pos_inds) > 0: pos_bbox_targets = bbox2delta(sampling_result.pos_bboxes, sampling_result.pos_gt_bboxes, target_means, target_stds) bbox_targets[pos_inds, :] = pos_bbox_targets bbox_weights[pos_inds, :] = 1.0 if gt_labels is None: labels[pos_inds] = 1 else: labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds] if cfg.pos_weight <= 0: label_weights[pos_inds] = 1.0 else: label_weights[pos_inds] = cfg.pos_weight if len(neg_inds) > 0: label_weights[neg_inds] = 1.0 # map up to original set of anchors if unmap_outputs: num_total_anchors = flat_anchors.size(0) labels = unmap(labels, num_total_anchors, inside_flags) label_weights = unmap(label_weights, num_total_anchors, inside_flags) bbox_targets = unmap(bbox_targets, num_total_anchors, inside_flags) bbox_weights = unmap(bbox_weights, num_total_anchors, inside_flags) return (labels, label_weights, bbox_targets, bbox_weights, pos_inds, neg_inds)
def anchor_target_rbbox_single(flat_anchors, valid_flags, gt_bboxes, gt_masks, gt_bboxes_ignore, gt_labels, img_meta, target_means, target_stds, cfg, label_channels=1, sampling=True, unmap_outputs=True, with_module=True, hbb_trans='hbb2obb_v2'): inside_flags = anchor_inside_flags(flat_anchors, valid_flags, img_meta['img_shape'][:2], cfg.allowed_border) if not inside_flags.any(): return (None, ) * 6 # assign gt and sample anchors # import pdb # print('in anchor target rbbox single') # pdb.set_trace() anchors = flat_anchors[inside_flags, :] if sampling: assign_result, sampling_result = assign_and_sample( anchors, gt_bboxes, gt_bboxes_ignore, None, cfg) else: bbox_assigner = build_assigner(cfg.assigner) assign_result = bbox_assigner.assign(anchors, gt_bboxes, gt_bboxes_ignore, gt_labels) bbox_sampler = PseudoSampler() sampling_result = bbox_sampler.sample(assign_result, anchors, gt_bboxes) num_valid_anchors = anchors.shape[0] # anchors shape, [num_anchors, 4] # bbox_targets = torch.zeros_like(anchors) # bbox_weights = torch.zeros_like(anchors) bbox_targets = torch.zeros(num_valid_anchors, 5).to(anchors.device) bbox_weights = torch.zeros(num_valid_anchors, 5).to(anchors.device) labels = anchors.new_zeros(num_valid_anchors, dtype=torch.long) label_weights = anchors.new_zeros(num_valid_anchors, dtype=torch.float) pos_inds = sampling_result.pos_inds neg_inds = sampling_result.neg_inds # TODO: copy the code in mask target to here. trans gt_masks to gt_rbboxes pos_assigned_gt_inds = sampling_result.pos_assigned_gt_inds # implementation A # pos_gt_masks = gt_masks[pos_assigned_gt_inds.cpu().numpy()] # pos_gt_obbs = gt_mask_bp_obbs(pos_gt_masks) # pos_gt_obbs_ts = torch.from_numpy(pos_gt_obbs).to(sampling_result.pos_bboxes.device) # implementation B gt_obbs = gt_mask_bp_obbs(gt_masks, with_module) gt_obbs_ts = torch.from_numpy(gt_obbs).to(sampling_result.pos_bboxes.device) pos_gt_obbs_ts = gt_obbs_ts[pos_assigned_gt_inds] if len(pos_inds) > 0: # pos_bbox_targets = bbox2delta(sampling_result.pos_bboxes, # sampling_result.pos_gt_bboxes, # target_means, target_stds) # if hbb_trans == 'hbb2obb': # pos_ext_bboxes = hbb2obb(sampling_result.pos_bboxes) # elif hbb_trans == 'hbbpolyobb': # pos_ext_bboxes = hbbpolyobb(sampling_result.pos_bboxes) # elif hbb_trans == 'hbb2obb_v2': # pos_ext_bboxes = hbb2obb_v2(sampling_result.pos_bboxes) # else: # print('no such hbb2obb trans function') # raise Exception pos_ext_bboxes = hbb2obb_v2(sampling_result.pos_bboxes) if with_module: pos_bbox_targets = dbbox2delta(pos_ext_bboxes, pos_gt_obbs_ts, target_means, target_stds) else: pos_bbox_targets = dbbox2delta_v3(pos_ext_bboxes, pos_gt_obbs_ts, target_means, target_stds) bbox_targets[pos_inds, :] = pos_bbox_targets bbox_weights[pos_inds, :] = 1.0 if gt_labels is None: labels[pos_inds] = 1 else: labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds] if cfg.pos_weight <= 0: label_weights[pos_inds] = 1.0 else: label_weights[pos_inds] = cfg.pos_weight if len(neg_inds) > 0: label_weights[neg_inds] = 1.0 # map up to original set of anchors if unmap_outputs: num_total_anchors = flat_anchors.size(0) labels = unmap(labels, num_total_anchors, inside_flags) label_weights = unmap(label_weights, num_total_anchors, inside_flags) bbox_targets = unmap(bbox_targets, num_total_anchors, inside_flags) bbox_weights = unmap(bbox_weights, num_total_anchors, inside_flags) return (labels, label_weights, bbox_targets, bbox_weights, pos_inds, neg_inds)
def radius_target_single(flat_anchors, valid_flags, bbox_pred, gt_bboxes, gt_cheby, gt_skeleton, gt_bboxes_ignore, gt_labels, img_meta, target_means, target_stds, num_coords, cfg, label_channels=1, sampling=True, unmap_outputs=True): inside_flags = anchor_inside_flags(flat_anchors, valid_flags, img_meta['img_shape'][:2], cfg.allowed_border) if not inside_flags.any(): return (None, ) * 6 # assign gt and sample anchors anchors = flat_anchors[inside_flags, :] # print('at cheby_target, gt_bboxes_ignore:', gt_bboxes_ignore) if sampling: assign_result, sampling_result = assign_and_sample( anchors, gt_bboxes, gt_cheby, gt_skeleton, gt_bboxes_ignore, None, cfg) else: bbox_assigner = build_assigner(cfg.assigner) assign_result = bbox_assigner.assign(anchors, gt_bboxes, gt_bboxes_ignore, gt_labels) bbox_sampler = PseudoSampler() sampling_result = bbox_sampler.sample(assign_result, anchors, gt_bboxes) num_valid_anchors = anchors.shape[0] labels = anchors.new_zeros(num_valid_anchors, dtype=torch.long) label_weights = anchors.new_zeros(num_valid_anchors, dtype=torch.float) bbox_targets = torch.zeros((num_valid_anchors, num_coords - 3), dtype=torch.float).cuda() bbox_weights = torch.zeros((num_valid_anchors, num_coords - 3), dtype=torch.float).cuda() ctr_targets = torch.zeros((num_valid_anchors, 3), dtype=torch.float).cuda() ctr_weights = torch.zeros((num_valid_anchors, 3), dtype=torch.float).cuda() pos_inds = sampling_result.pos_inds neg_inds = sampling_result.neg_inds if len(pos_inds) > 0: deltas, weights = bbox2radius(sampling_result.pos_bboxes, sampling_result.pos_gt_bboxes, sampling_result.pos_gt_skeleton, num_coords, target_means, target_stds) bbox_targets[pos_inds, :] = deltas[:, :-3] bbox_weights[pos_inds, :] = weights.unsqueeze( 1) if cfg.use_centerness else 1.0 ctr_targets[pos_inds, :] = deltas[:, -3:] ctr_weights[pos_inds, :] = weights.unsqueeze( 1) if cfg.use_centerness else 1.0 if gt_labels is None: labels[pos_inds] = 1 else: labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds] if cfg.pos_weight <= 0: label_weights[pos_inds] = weights if cfg.use_centerness else 1.0 else: label_weights[pos_inds] = cfg.pos_weight # print("pos:", len(pos_inds), "neg:", len(neg_inds), weights) if len(neg_inds) > 0: label_weights[neg_inds] = 1.0 # map up to original set of anchors if unmap_outputs: num_total_anchors = flat_anchors.size(0) labels = unmap(labels, num_total_anchors, inside_flags) label_weights = unmap(label_weights, num_total_anchors, inside_flags) bbox_targets = unmap(bbox_targets, num_total_anchors, inside_flags) bbox_weights = unmap(bbox_weights, num_total_anchors, inside_flags) ctr_targets = unmap(ctr_targets, num_total_anchors, inside_flags) ctr_weights = unmap(ctr_weights, num_total_anchors, inside_flags) return (labels, label_weights, bbox_targets, bbox_weights, ctr_targets, ctr_weights, pos_inds, neg_inds)
def dense_reppoints_target_sinle(flat_proposals, flat_proposals_pts, num_level_proposals, valid_flags, gt_bboxes, gt_masks, gt_bboxes_ignore, gt_labels, cfg, label_channels=1, sampling=True, unmap_outputs=True, num_pts=49): inside_flags = valid_flags num_level_proposals_inside = get_num_level_proposals_inside( num_level_proposals, inside_flags) if not inside_flags.any(): return (None, ) * 8 # assign gt and sample points proposals = flat_proposals[inside_flags, :] proposals_pts = flat_proposals_pts[inside_flags, :] if sampling: assign_result, sampling_result = assign_and_sample( proposals, gt_bboxes, gt_bboxes_ignore, None, cfg) else: bbox_assigner = build_assigner(cfg.assigner) if cfg.assigner.type != "ATSSAssigner": assign_result = bbox_assigner.assign(proposals, gt_bboxes, None, gt_labels) else: assign_result = bbox_assigner.assign(proposals, num_level_proposals_inside, gt_bboxes, None, gt_labels) bbox_sampler = PseudoSampler() sampling_result = bbox_sampler.sample(assign_result, proposals, gt_bboxes) gt_ind = sampling_result.pos_assigned_gt_inds.cpu().numpy() sample_func = cfg.get('sample_func', 'distance_sample_pts') gt_pts_numpy = eval(sample_func)(gt_bboxes, gt_masks, cfg, num_pts) pts_label_list = [] proposals_pos_pts = proposals_pts[ sampling_result.pos_inds, :].detach().cpu().numpy().round().astype( np.long) for i in range(len(gt_ind)): gt_mask = gt_masks[gt_ind[i]] h, w = gt_mask.shape pts_long = proposals_pos_pts[i] _pts_label = gt_mask[pts_long[1::2].clip(0, h - 1), pts_long[0::2].clip(0, w - 1)] pts_label_list.append(_pts_label) del proposals_pos_pts if len(gt_ind) != 0: gt_pts = gt_bboxes.new_tensor(gt_pts_numpy) pos_gt_pts = gt_pts[gt_ind] pts_label = np.stack(pts_label_list, 0) pos_gt_pts_label = gt_bboxes.new_tensor(pts_label) else: pos_gt_pts = None pos_gt_pts_label = None num_valid_proposals = proposals.shape[0] bbox_gt = proposals.new_zeros([num_valid_proposals, 4]) mask_gt = proposals.new_zeros([0, num_pts * 2]) mask_gt_label = proposals.new_zeros([0, num_pts]).long() mask_gt_index = proposals.new_zeros([ num_valid_proposals, ], dtype=torch.long) pos_proposals = torch.zeros_like(proposals) proposals_weights = proposals.new_zeros([num_valid_proposals, 4]) labels = proposals.new_zeros(num_valid_proposals, dtype=torch.long) label_weights = proposals.new_zeros(num_valid_proposals, dtype=torch.float) pos_inds = sampling_result.pos_inds neg_inds = sampling_result.neg_inds if len(pos_inds) > 0: pos_gt_bboxes = sampling_result.pos_gt_bboxes bbox_gt[pos_inds, :] = pos_gt_bboxes if pos_gt_pts is not None: mask_gt = pos_gt_pts.type(bbox_gt.type()) mask_gt_index[pos_inds] = torch.arange( len(pos_inds)).long().cuda() + 1 if pos_gt_pts_label is not None: mask_gt_label = pos_gt_pts_label.long() pos_proposals[pos_inds, :] = proposals[pos_inds, :] proposals_weights[pos_inds, :] = 1.0 if gt_labels is None: labels[pos_inds] = 1 else: labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds] if cfg.pos_weight <= 0: label_weights[pos_inds] = 1.0 else: label_weights[pos_inds] = cfg.pos_weight if len(neg_inds) > 0: label_weights[neg_inds] = 1.0 # map up to original set of grids if unmap_outputs: num_total_proposals = flat_proposals.size(0) labels = unmap(labels, num_total_proposals, inside_flags) label_weights = unmap(label_weights, num_total_proposals, inside_flags) bbox_gt = unmap(bbox_gt, num_total_proposals, inside_flags) mask_gt_index = unmap(mask_gt_index, num_total_proposals, inside_flags) pos_proposals = unmap(pos_proposals, num_total_proposals, inside_flags) proposals_weights = unmap(proposals_weights, num_total_proposals, inside_flags) return (labels, label_weights, bbox_gt, mask_gt_index, mask_gt, mask_gt_label, pos_proposals, proposals_weights, pos_inds, neg_inds)
def offset_target_single(flat_anchors, valid_flags, bbox_pred, gt_bboxes, gt_cheby, gt_skeleton, gt_bboxes_ignore, gt_labels, img_meta, target_means, target_stds, num_coords, cfg, label_channels=1, sampling=True, unmap_outputs=True): inside_flags = anchor_inside_flags(flat_anchors, valid_flags, img_meta['img_shape'][:2], cfg.allowed_border) if not inside_flags.any(): return (None, ) * 6 # assign gt and sample anchors anchors = flat_anchors[inside_flags, :] if sampling: assign_result, sampling_result = assign_and_sample( anchors, gt_bboxes, gt_cheby, gt_skeleton, gt_bboxes_ignore, None, cfg) else: bbox_assigner = build_assigner(cfg.assigner) assign_result = bbox_assigner.assign(anchors, gt_bboxes, gt_bboxes_ignore, gt_labels) bbox_sampler = PseudoSampler() sampling_result = bbox_sampler.sample(assign_result, anchors, gt_bboxes) num_valid_anchors = anchors.shape[0] labels = anchors.new_zeros(num_valid_anchors, dtype=torch.long) label_weights = anchors.new_zeros(num_valid_anchors, dtype=torch.float) bbox_targets = torch.zeros((num_valid_anchors, num_coords), dtype=torch.float).cuda() bbox_weights = torch.zeros((num_valid_anchors, num_coords), dtype=torch.float).cuda() pos_inds = sampling_result.pos_inds neg_inds = sampling_result.neg_inds if len(pos_inds) > 0: deltas, weights = bbox2offset(sampling_result.pos_bboxes, sampling_result.pos_gt_bboxes, target_means, target_stds) bbox_targets[pos_inds, :] = deltas bbox_weights[pos_inds, :] = weights.unsqueeze( 1) if cfg.use_centerness else 1.0 if gt_labels is None: labels[pos_inds] = 1 else: labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds] if cfg.pos_weight <= 0: label_weights[pos_inds] = weights if cfg.use_centerness else 1.0 else: label_weights[pos_inds] = cfg.pos_weight # start adaptive weights use_adaptive_weights = False if use_adaptive_weights: pos_weights = torch.norm(pos_bbox_targets[:, -2:], dim=1) pos_weights = 1.0 / (0.5 + pos_weights) bbox_weights[pos_inds, :] = pos_weights.reshape(-1, 1) label_weights[pos_inds] = pos_weights # end of adaptive weights if len(neg_inds) > 0: label_weights[neg_inds] = 1.0 # map up to original set of anchors if unmap_outputs: num_total_anchors = flat_anchors.size(0) labels = unmap(labels, num_total_anchors, inside_flags) label_weights = unmap(label_weights, num_total_anchors, inside_flags) bbox_targets = unmap(bbox_targets, num_total_anchors, inside_flags) bbox_weights = unmap(bbox_weights, num_total_anchors, inside_flags) return (labels, label_weights, bbox_targets, bbox_weights, pos_inds, neg_inds)