def _match_priors_gt(self, priors, gt, thresh, num_boxes): batch_size = gt.size(0) num_priors = priors.size(0) overlaps = bbox_overlaps_batch(priors, gt) # [b, num_objects] best prior for each ground truth best_prior_overlap, best_prior_idx = overlaps.max(1) # [b, num_priors] best ground truth for each prior best_truth_overlap, best_truth_idx = overlaps.max(2) matches = torch.zeros(batch_size, num_priors, 5).type_as(priors) for num in range(batch_size): # select valid best prior idx best_prior_idx_valid = best_prior_idx[num][:num_boxes[num]] best_truth_overlap[num].index_fill_(0, best_prior_idx_valid, 2) # ensure best prior # TODO refactor: index best_prior_idx with long tensor # ensure every gt matches with its prior of max overlap for j in range(best_prior_idx_valid.size(0)): best_truth_idx[num][best_prior_idx_valid[j]] = j matches[num] = gt[num][best_truth_idx[num]] loc = matches[:, :, :-1] # Shape: [bs, num_priors,4] conf = matches[:, :, -1] # Shape: [bs, num_priors] conf[best_truth_overlap < thresh] = 0 # label as background encoded_loc = bbox_transform_batch(priors, loc) if cfg.TRAIN.COMMON.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev encoded_loc = ((encoded_loc - self.BBOX_NORMALIZE_MEANS.expand_as(encoded_loc)) / self.BBOX_NORMALIZE_STDS.expand_as(encoded_loc)) return encoded_loc, conf
def _compute_targets_pytorch(self, ex_rois, gt_rois): assert ex_rois.size(1) == gt_rois.size(1) assert ex_rois.size(2) == 4 assert gt_rois.size(2) == 4 batch_size = ex_rois.size(0) rois_per_image = ex_rois.size(1) targets = bbox_transform_batch(ex_rois, gt_rois) if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev targets = ( (targets - self.BBOX_NORMALIZE_MEANS.expand_as(targets)) / self.BBOX_NORMALIZE_STDS.expand_as(targets)) return targets
def _compute_targets_batch(ex_rois, gt_rois): """Compute bounding-box regression targets for an image.""" return bbox_transform_batch(ex_rois, gt_rois[:, :, :4])