def forward(self, param, p_c, priors, times): # param.view(param.size(0), -1, config["num_motion_model_param"]), # parameter predicts # # self.softmax(p_m.view(p_m.size(0), -1, 2)), # motion possibility # self.softmax(p_c.view(p_c.size(0), -1, self.num_classes)), # classification possiblity # self.priors # default boxes loc_datas = MotionModel.get_bbox_by_frames_pytorch(param, times) out = [] for i in range(times.shape[1]): loc_data = loc_datas[:, i, :] conf_data = p_c[:, i, :] out += [self.forward_one(loc_data, conf_data, priors)] return torch.stack(out, dim=1)
def convert_to_bboxes(self, parameters, times): """ current bbox's format is (cx, cy, w, h) """ # N_{ba} x N_{fn} x N_{tr} x 4 return MotionModel.get_bbox_by_frames_pytorch(parameters, times)
def forward(self, param, p_c, p_e, priors, times): # param.view(param.size(0), -1, config["num_motion_model_param"]), # parameter predicts # # self.softmax(p_c.view(p_c.size(0), -1, 2)), # motion possibility # self.softmax(p_e.view(p_e.size(0), -1, self.num_classes)), # classification possiblity # self.priors # default boxes loc_datas = MotionModel.get_bbox_by_frames_pytorch(param, times) # find the times scale if loc_datas.size(1) % p_e.size(1) != 0: raise AssertionError( "time scales should be the int in nms_with_frames") time_scales = loc_datas.size(1) // p_e.size(1) if time_scales > 1: p_e = p_e[:, :, None, :, :].repeat(1, 1, time_scales, 1, 1)\ .view(p_e.size(0), -1, 1, p_e.size(2), p_e.size(3)).squeeze(2) num = loc_datas.size(0) # batch size num_priors = priors.size(0) num_frames = times.size(1) param_shape_1 = param.size(2) param_shape_2 = param.size(3) output_boxes = torch.zeros(num, self.num_classes, num_frames, self.top_k, 4) output_p_e = torch.zeros(num, 2, num_frames, self.top_k) output_params = torch.zeros(num, self.num_classes, self.top_k, param_shape_1, param_shape_2) output_p_c = torch.zeros(num, self.num_classes, self.top_k) conf_preds = p_c.squeeze(1).view(num, num_priors, self.num_classes).transpose(2, 1) conf_exists = p_e.transpose(3, 2) # Decode predictions into bboxes. decoded_locs = decode_with_frames(loc_datas, priors, self.variance) for i in range(num): # For each class, perform nms conf_scores = conf_preds[i] decoded_boxes = decoded_locs[i, :] for cl in range(1, self.num_classes): # filter boxes by confidence c_mask = conf_scores[cl].gt(self.conf_thresh) if c_mask.sum() == 0: continue scores = conf_scores[cl][c_mask] exists = conf_exists[i, :, 1, c_mask] boxes = decoded_boxes[:, c_mask, :] # filter boxes by visibility v_mask = (exists > self.exist_thresh).sum( dim=0) / exists.shape[0] >= self.min_valid_node_rate if v_mask.sum() == 0: continue scores = scores[v_mask] exists = exists[:, v_mask] boxes = boxes[:, v_mask, :] # if there are exists the reasonable boxes. # print(c_mask.sum().item()) # if c_mask.sum().item() > 0: # idx of highest scoring and non-overlapping boxes per class ids, count = nms_with_frames(boxes, scores, exists, self.nms_thresh, self.top_k, self.exist_thresh) if count > 0: output_boxes[i, cl, :, :count, :] = boxes[:, ids[:count]] output_p_e[i, 1, :, :count] = exists[:, ids[:count]] output_params[i, cl, :count, :] = param[i, c_mask, :][ v_mask, :][ids[:count], :] output_p_c[i, cl, :count] = scores[ids[:count]] output_p_c_1 = output_p_c.contiguous().view(num, -1) _, idx = output_p_c_1.sort(1, descending=True) _, rank = idx.sort(1) mask = rank > self.top_k output_p_c_1.masked_fill_(mask, 0) return (output_params, output_p_c, output_p_e, output_boxes)