def test_fol(fol_model, test_gen): ''' Validate future vehicle localization module Params: fol_model: The fol model as nn.Module test_gen: test data generator Returns: ''' fol_model.eval() # Sets the module in training mode. fol_loss = 0 loader = tqdm(test_gen, total=len(test_gen)) FDE = 0 ADE = 0 FIOU = 0 with torch.set_grad_enabled(False): for batch_idx, data in enumerate(loader): input_bbox, input_flow, input_ego_motion, target_bbox, target_ego_motion = data # run forward fol_predictions,_ = fol_model(input_bbox, input_flow) # convert to numpy array, use [0] since batchsize if 1 for test fol_predictions = fol_predictions.to('cpu').numpy()[0] input_bbox = input_bbox.to('cpu').numpy()[0] target_bbox = target_bbox.to('cpu').numpy()[0] # compute FDE, ADE and FIOU metrics used in FVL2019ICRA paper input_bbox = np.expand_dims(input_bbox, axis=1) input_bbox = bbox_denormalize(input_bbox, W=1280, H=640) fol_predictions = bbox_denormalize(fol_predictions, W=1280, H=640) target_bbox = bbox_denormalize(target_bbox, W=1280, H=640) fol_predictions_xyxy = cxcywh_to_x1y1x2y2(fol_predictions) target_bbox_xyxy = cxcywh_to_x1y1x2y2(target_bbox) ADE += np.mean(np.sqrt(np.sum((target_bbox_xyxy[:,:,:2] - fol_predictions_xyxy[:,:,:2]) ** 2, axis=-1))) FDE += np.mean(np.sqrt(np.sum((target_bbox_xyxy[:,-1,:2] - fol_predictions_xyxy[:,-1,:2]) ** 2, axis=-1))) tmp_FIOU = [] for i in range(target_bbox_xyxy.shape[0]): tmp_FIOU.append(compute_IOU(target_bbox_xyxy[i,-1,:], fol_predictions_xyxy[i,-1,:], format='x1y1x2y2')) FIOU += np.mean(tmp_FIOU) print("FDE: %4f; ADE: %4f; FIOU: %4f" % (FDE, ADE, FIOU)) ADE /= len(test_gen.dataset) FDE /= len(test_gen.dataset) FIOU /= len(test_gen.dataset) print("FDE: %4f; ADE: %4f; FIOU: %4f" % (FDE, ADE, FIOU))
def mask_iou_metrics(all_pred_boxes_t, all_observed_boxes, W, H, multi_box='latest'): ''' Compute a 0/1 mask for all tracked object and another mask for all observed boxes, return the difference of the mask as anomaly measures Params: all_pred_boxes_t: a dictionary saves all pred_box_t all_observed_boxes: a dictionary saves all bbox multi_box: 'latest', 'mverage', 'union' Return: mask_iou: ''' pred_mask = np.zeros([H, W]) observed_mask = np.zeros([H, W]) if len(all_observed_boxes) > 0: for tarcker_id, bbox in all_observed_boxes.items(): converted_box = cxcywh_to_x1y1x2y2(bbox) converted_box = bbox_denormalize(converted_box, W=W, H=H)[0].astype(int) observed_mask[converted_box[1]:converted_box[3], converted_box[0]:converted_box[2]] = 1 if len(all_pred_boxes_t) > 0: for tracker_id, pred_boxes_t in all_pred_boxes_t.items(): if len(pred_boxes_t) <= 0: continue if multi_box == 'union': converted_boxes = cxcywh_to_x1y1x2y2(pred_boxes_t) converted_boxes = bbox_denormalize(converted_boxes) converted_boxes = converted_boxes.astype(int) for box in converted_boxes: pred_mask[box[1]:box[3], box[0]:box[2]] = 1 elif multi_box == 'latest': pred_box_t = pred_boxes_t[0:1, :] converted_box = cxcywh_to_x1y1x2y2(pred_box_t) converted_box = bbox_denormalize(converted_box, W=W, H=H) converted_box = converted_box.astype(int) box = converted_box[0, :] pred_mask[box[1]:box[3], box[0]:box[2]] = 1 # mask_diff = np.srqrt(np.mean((pred_mask - observed_mask)**2)) mask_iou = compute_mask_iou(np.expand_dims(pred_mask, 0), np.expand_dims(observed_mask, 0)) return 1 - mask_iou, pred_mask, observed_mask #mask_diff
def boxes_union(boxes): new_boxes = cxcywh_to_x1y1x2y2(boxes) xmin = np.min(new_boxes[:, 0]) ymin = np.min(new_boxes[:, 1]) xmax = np.max(new_boxes[:, 2]) ymax = np.max(new_boxes[:, 3]) union = np.sum(boxes[:, 2] * boxes[:, 3]) union_box = np.array([(xmin + xmax) / 2, (ymin + ymax) / 2, xmax - xmin, ymax - ymin]) return union, union_box
def boxes_union(boxes): ''' Givent an array where each row is a box (Cx, Cy, w, h) Return: intersection: Box intersection area inter_box: the intersected rectangle box in (cx, cy, w, h) ''' new_boxes = cxcywh_to_x1y1x2y2(boxes) xmin = np.min(new_boxes[:, 0]) ymin = np.min(new_boxes[:, 1]) xmax = np.max(new_boxes[:, 2]) ymax = np.max(new_boxes[:, 3]) union = np.sum(boxes[:, 2] * boxes[:, 3]) union_box = np.array([(xmin + xmax) / 2, (ymin + ymax) / 2, xmax - xmin, ymax - ymin]) return union, union_box
def boxes_intersection(boxes): boxes = cxcywh_to_x1y1x2y2(boxes) xmin = np.max(boxes[:, 0]) ymin = np.min(boxes[:, 1]) xmax = np.max(boxes[:, 2]) ymax = np.min(boxes[:, 3]) w_inter = np.max([0, xmax - xmin]) h_inter = np.max([0, ymax - ymin]) intersection = w_inter * h_inter if intersection == 0: inter_box = np.array([[0, 0, 0, 0]]) else: inter_box = np.array([(xmin + xmax) / 2, (ymin + ymax) / 2, xmax - xmin, ymax - ymin]) return intersection
def boxes_intersection(boxes): ''' Givent an array where each row is a box (Cx, Cy, w, h) Return: intersection: Box intersection area inter_box: the intersected rectangle box in (cx, cy, w, h) ''' boxes = cxcywh_to_x1y1x2y2(boxes) xmin = np.max(boxes[:, 0]) ymin = np.min(boxes[:, 1]) xmax = np.max(boxes[:, 2]) ymax = np.min(boxes[:, 3]) w_inter = np.max([0, xmax - xmin]) h_inter = np.max([0, ymax - ymin]) intersection = w_inter * h_inter if intersection == 0: inter_box = np.array([[0, 0, 0, 0]]) else: inter_box = np.array([(xmin + xmax) / 2, (ymin + ymax) / 2, xmax - xmin, ymax - ymin]) return intersection