Convert Rotation matrix to quaternion, then calculate the location errors. original from PoseNet Paper
(dl, model, loss, args, vis=True)
| 98 | |
| 99 | @torch.no_grad() |
| 100 | def eval_model(dl, model, loss, args, vis=True): |
| 101 | """ Convert Rotation matrix to quaternion, then calculate the location errors. original from PoseNet Paper """ |
| 102 | model.eval() |
| 103 | val_losses = [] |
| 104 | errors_trans = [] |
| 105 | errors_rot = [] |
| 106 | pred_trans = [] |
| 107 | directions = [] |
| 108 | for data, pose, _, _ in tqdm(dl, desc="Validating"): |
| 109 | data = data.to(args.device) # input |
| 110 | pose = pose.to(args.device) # label |
| 111 | |
| 112 | gt_pose = pose.float() |
| 113 | with autocast(args.device, enabled=args.amp, dtype=args.amp_dtype): |
| 114 | _, pred_pose = model(data) |
| 115 | val_loss = loss(gt_pose, pred_pose) |
| 116 | |
| 117 | val_losses.append(val_loss.item()) |
| 118 | |
| 119 | pose = pose.reshape((-1, 3, 4)) |
| 120 | pred_pose = pred_pose.reshape((-1, 3, 4)).double() |
| 121 | |
| 122 | # R_torch = pred_pose[:, :3, :3] |
| 123 | # u, s, v = torch.svd(R_torch) |
| 124 | # Rs = torch.matmul(u, v.transpose(-2, -1)) |
| 125 | # pred_pose[:, :3, :3] = Rs |
| 126 | |
| 127 | error_trans, error_rot = get_pose_error(pose, pred_pose) |
| 128 | errors_trans.append(error_trans.cpu().numpy()) |
| 129 | errors_rot.append(error_rot.cpu().numpy()) |
| 130 | # print ('Iteration: {} Error XYZ (m): {} Error Q (degrees): {}'.format(i, error_x, theta)) |
| 131 | |
| 132 | if vis: |
| 133 | pred_trans.append(pred_pose[:, :3, 3].cpu().numpy()) |
| 134 | directions.append(pred_pose[:, :3, 2].cpu().numpy()) |
| 135 | |
| 136 | mean_loss = np.mean(val_losses) |
| 137 | |
| 138 | errors_trans = np.hstack(errors_trans) |
| 139 | errors_rot = np.hstack(errors_rot) |
| 140 | |
| 141 | median_trans, median_rot = np.median(errors_trans), np.median(errors_rot) |
| 142 | mean_trans, mean_rot = np.mean(errors_trans), np.mean(errors_rot) |
| 143 | max_trans, max_rot = np.max(errors_trans), np.max(errors_rot) |
| 144 | min_trans, min_rot = np.min(errors_trans), np.min(errors_rot) |
| 145 | |
| 146 | success_condition_5 = (errors_trans < 0.05) & (errors_rot < 5) |
| 147 | success_condition_2 = (errors_trans < 0.02) & (errors_rot < 2) |
| 148 | successful_count_5 = np.sum(success_condition_5) |
| 149 | successful_count_2 = np.sum(success_condition_2) |
| 150 | success_rate_5 = successful_count_5 / errors_trans.shape[0] |
| 151 | success_rate_2 = successful_count_2 / errors_trans.shape[0] |
| 152 | |
| 153 | save_dir = args.logbase |
| 154 | save_name = f"{args.run_name}/" |
| 155 | if args.pretrained_model_path: |
| 156 | save_dir = os.path.dirname(args.pretrained_model_path) or "." |
| 157 | save_name = f"{os.path.splitext(os.path.basename(args.pretrained_model_path))[0]}_" |
no test coverage detected