def generate_label(prop_loc, sample_list, index): batch_size = prop_loc.shape[0] iou_labels = [] lens = [] for idx in range(batch_size): batch_prop_start = np.append([0.0], prop_loc[idx][:, 0]) batch_prop_end = np.append([0.0], prop_loc[idx][:, 1]) gt_start = sample_list[index[idx]][2] gt_end = sample_list[index[idx]][3] cur_labels = [] lens.append(len(gt_start) + 1) for i in range(len(gt_start)): tmp_iou_list = iou_with_anchors(batch_prop_start, batch_prop_end, gt_start[i], gt_end[i]) cur_labels.append(tmp_iou_list) end_prop = np.zeros([ len(batch_prop_start), ]) end_prop[0] = 1. cur_labels.append(end_prop) for i in range(len(gt_start) + 1, args.max_seq_len): cur_labels.append(np.zeros([ len(batch_prop_start), ])) iou_labels.append(np.array(cur_labels)) return np.array(iou_labels), np.array(lens)
def evaluate(props, sample_list, idx): prop_start, prop_end = [prop[0] for prop in props], [prop[1] for prop in props] prop_start, prop_end = np.array(prop_start), np.array(prop_end) gt_start = sample_list[idx][2] gt_end = sample_list[idx][3] prop_gt_iou = [] for i in range(len(props)): tmp_iou_list = iou_with_anchors(gt_start, gt_end, prop_start[i], prop_end[i]) prop_gt_iou.append(tmp_iou_list) prop_gt_iou = np.array(prop_gt_iou) p, r, f = [], [], [] for tiou in np.linspace(0.5, 0.95, 10): cur_iou = copy.deepcopy(prop_gt_iou) match = .0 matched = [] for i in range(len(props)): j = np.argmax(cur_iou[i]) if prop_gt_iou[i][j] > tiou and j not in matched: match += 1 matched.append(j) cur_iou[:, j] = -1 cur_p = match / len(props) cur_r = match / len(gt_start) p.append(cur_p) r.append(cur_r) f.append(2 * cur_p * cur_r / (cur_p + cur_r + 1e-10)) return np.array(p), np.array(r), np.array(f)
def soft_nms(prop, alpha, t1, t2): ''' prop: proposals generated by network, (score, start, end) for each row; alpha: alpha value of Gaussian decaying function; t1, t2: threshold for soft nms. ''' idx = np.argsort(-prop[:, 0], axis=0) prop = prop[idx] tscore, tstart, tend = map(list, [prop[:, 0], prop[:, 1], prop[:, 2]]) rstart = [] rend = [] rscore = [] while len(tscore) > 1 and len(rscore) < 101: max_index = tscore.index(max(tscore)) tmp_iou_list = iou_with_anchors(np.array(tstart), np.array(tend), tstart[max_index], tend[max_index]) for idx in range(0, len(tscore)): if idx != max_index: tmp_iou = tmp_iou_list[idx] tmp_width = (tend[max_index] - tstart[max_index]) / tscale if tmp_iou > t1 + (t2 - t1) * tmp_width: tscore[idx] = tscore[idx] * np.exp( -np.square(tmp_iou) / alpha) rstart.append(tstart[max_index]) rend.append(tend[max_index]) rscore.append(tscore[max_index]) tstart.pop(max_index) tend.pop(max_index) tscore.pop(max_index) newprop = np.stack([rscore, rstart, rend]).transpose() return newprop
def _get_train_label(self, start, end, anchor_xmin, anchor_xmax): gt_bbox = [] gt_iou_map = [] for tmp_start, tmp_end in zip(start, end): gt_bbox.append([tmp_start, tmp_end]) tmp_gt_iou_map = iou_with_anchors(self.match_map[:, 0], self.match_map[:, 1], tmp_start, tmp_end) tmp_gt_iou_map = np.reshape(tmp_gt_iou_map, [self.maxdur, self.tscale]) gt_iou_map.append(tmp_gt_iou_map) gt_iou_map = np.array(gt_iou_map) gt_iou_map = np.max(gt_iou_map, axis=0) gt_iou_map = torch.Tensor(gt_iou_map) # generate R_s and R_e gt_bbox = np.array(gt_bbox) gt_xmins = gt_bbox[:, 0] gt_xmaxs = gt_bbox[:, 1] gt_lens = gt_xmaxs - gt_xmins gt_len_small = 3 * self.temporal_gap # np.maximum(self.temporal_gap, self.boundary_ratio * gt_lens) gt_start_bboxs = np.stack((gt_xmins - gt_len_small / 2, gt_xmins + gt_len_small / 2), axis=1) gt_end_bboxs = np.stack((gt_xmaxs - gt_len_small / 2, gt_xmaxs + gt_len_small / 2), axis=1) # calculate the ioa for all timestamp match_score_start, match_score_end = [], [] for i in range(len(anchor_xmin)): match_score_start.append(np.max( ioa_with_anchors(anchor_xmin[i], anchor_xmax[i], gt_start_bboxs[:, 0], gt_start_bboxs[:, 1]))) match_score_end.append(np.max( ioa_with_anchors(anchor_xmin[i], anchor_xmax[i], gt_end_bboxs[:, 0], gt_end_bboxs[:, 1]))) match_score_start = torch.Tensor(match_score_start) match_score_end = torch.Tensor(match_score_end) return match_score_start, match_score_end, gt_iou_map