def match_targets_to_proposals(self, proposal, target): masks_field = "masks" rrects_field = "rrects" # anchor_tensor = anchor.get_field(rrects_field) proposal_tensor = get_boxlist_rotated_rect_tensor( proposal, masks_field, rrects_field) target_tensor = get_boxlist_rotated_rect_tensor( target, masks_field, rrects_field) if not target.has_field( rrects_field): # add rrects to gt if not already added target.add_field(rrects_field, target_tensor) match_quality_matrix = rotate_iou(target_tensor, proposal_tensor) matched_idxs = self.proposal_matcher(match_quality_matrix) # Mask RCNN needs "labels" and "masks "fields for creating the targets target = target.copy_with_fields(["labels", "masks"]) # get the targets corresponding GT for each proposal # NB: need to clamp the indices because we can have a single # GT in the image, and matched_idxs can be -2, which goes # out of bounds matched_targets = target[matched_idxs.clamp(min=0)] matched_targets.add_field("matched_idxs", matched_idxs) return matched_targets
def add_gt_proposals(self, proposals, targets): """ Arguments: proposals: list[BoxList] targets: list[BoxList] """ # Get the device we're operating on device = proposals[0].bbox.device masks_field = "masks" rrects_field = "rrects" gt_boxes = [target.copy_with_fields([]) for target in targets] # later cat of bbox requires all fields to be present for all bbox # so we need to add a dummy for objectness that's missing for ix, gt_box in enumerate(gt_boxes): target = targets[ix] gt_rrect = get_boxlist_rotated_rect_tensor(target, masks_field, rrects_field) aug_rrect = gt_rrect.clone() N = len(gt_rrect) multiplier = int(4) for m in range(multiplier - 1): aug_rrect[:, :2] += torch.randint(-7, 7, (N, 2), dtype=torch.float32, device=device) aug_rrect[:, 2:4] *= torch.randint( 94, 106, (N, 2), dtype=torch.float32, device=device) / 100 aug_rrect[:, -1] += torch.randint(-5, 5, (N, ), dtype=torch.float32, device=device) gt_rrect = torch.cat((gt_rrect, aug_rrect)) # convert anchor rects to bboxes bboxes = convert_rects_to_bboxes(gt_rrect, torch) boxlist = BoxList(bboxes, gt_box.size, mode="xyxy") boxlist.add_field("rrects", gt_rrect) boxlist.add_field("objectness", torch.ones(len(gt_rrect), device=device)) gt_boxes[ix] = boxlist proposals = [ cat_boxlist((proposal, gt_box)) for proposal, gt_box in zip(proposals, gt_boxes) ] return proposals
def match_targets_to_anchors(self, anchor, target, copied_fields=[]): """ :param anchor: BoxList :param target: BoxList :param copied_fields: list :return: """ masks_field = "masks" rrects_field = "rrects" # anchor_tensor = anchor.get_field(rrects_field) anchor_tensor = get_boxlist_rotated_rect_tensor( anchor, masks_field, rrects_field) target_tensor = get_boxlist_rotated_rect_tensor( target, masks_field, rrects_field) if not target.has_field( rrects_field): # add rrects to gt if not already added target.add_field(rrects_field, target_tensor) match_quality_matrix = rotate_iou(target_tensor, anchor_tensor) matched_idxs = self.proposal_matcher(match_quality_matrix) # RPN doesn't need any fields from target # for creating the labels, so clear them all target = target.copy_with_fields(copied_fields + [rrects_field]) # get the targets corresponding GT for each anchor # NB: need to clamp the indices because we can have a single # GT in the image, and matched_idxs can be -2, which goes # out of bounds matched_idxs_clamped = matched_idxs.clamp(min=0) matched_targets = target[matched_idxs_clamped] # matched_ious = match_quality_matrix[matched_idxs_clamped, # torch.arange(len(anchor_tensor), device=anchor_tensor.device)] matched_targets.add_field("matched_idxs", matched_idxs) # matched_targets.add_field("matched_ious", matched_ious) return matched_targets
# # imtt = im1.unsqueeze(0) # grid = F.affine_grid(theta.unsqueeze(0), imtt.size()) # im1_warped = F.grid_sample(imtt, grid) # im1_warped = im1_warped.squeeze().numpy().astype(np.uint8) # im1_warped = np.transpose(im1_warped, [1,2,0]) # CHW to HWC # # cv2.imshow("im_warped", im1_warped) # cv2.waitKey(0) m_field = t1.get_field("masks") labels = t1.get_field('labels') bboxes = t1.bbox if is_rotated: rrects = get_boxlist_rotated_rect_tensor(t1, "masks", "rrects") for ix, label in enumerate(labels): seg_mask = m_field[ix] sm = seg_mask.get_mask_tensor().numpy() p = m_field.instances.polygons[ix] m = p.convert_to_binarymask() # visualize_mask(m.numpy()) m = m.numpy() # uint8 format, 0-1 m *= 255 m = cv2.cvtColor(m, cv2.COLOR_GRAY2BGR) if is_rotated: rr = rrects[ix] proposal = rr.numpy().copy() # proposal[:2] += 15
# # top_iou_ids = torch.sort(pos_matched_gt_ious[nz], descending=True)[1][:MAX_GT_NUM] # # inds = nz[top_iou_ids] # label_weights[inds] = total_pos / MAX_GT_NUM # = total_pos / label_cnts.to(dtype=torch.float32) for ix, cnt in enumerate(anchors_cnt): gt = targets[ix] gt_bbox = np.round(gt.bbox.cpu().numpy()) # gt_mask_instance = gt.get_field("masks") # gt_polygons = [p.polygons for p in gt_mask_instance.instances.polygons] # for gx,gtp in enumerate(gt_polygons): # gtp = [p.view(-1, 2).cpu().numpy() for p in gtp] # gt_polygons[gx] = gtp gt_rrects = get_boxlist_rotated_rect_tensor(gt, "masks").cpu().numpy() img_t = img_tensors[ix] inds = sampled_pos_inds[cumu_cnt < sampled_pos_inds] inds = inds[inds < (cumu_cnt + cnt)] pos_anchors = anchors[ix][inds - cumu_cnt] reg_targets = regression_targets[inds] reg_target_gt_inds = matched_gt_ids[inds] # reg_target_gt_ious = matched_gt_ious[inds] cumu_cnt += cnt anchor_rrects = pos_anchors.get_field("rrects") rr = anchor_rrects.cpu().numpy() # print(rr)