|
|
| import numpy as np |
| import scipy.spatial.distance as dist |
| import utilities |
| import matplotlib.pyplot as plt |
| from matplotlib.colors import LinearSegmentedColormap |
| from PIL import Image |
|
|
| |
| |
| |
|
|
| def compute_mse(gt_keypoints, pred_keypoints): |
| assert gt_keypoints.shape == pred_keypoints.shape, "The ground truth list has not the same shape of the predicted list" |
|
|
| |
| squared_diff = np.square(gt_keypoints - pred_keypoints) |
|
|
| |
| mse = np.mean(squared_diff) |
|
|
| return mse |
|
|
| |
| |
| |
| def compute_oks_heatmaps(ground_truth, prediction, sigma): |
| distance = dist.cdist(ground_truth, prediction, 'euclidean') |
| scale = 1 |
| oks = np.exp(-1 * (distance ** 2) / (2 * (sigma**2) * (scale ** 2))) |
| return oks |
|
|
| def compute_map_heatmaps(ground_truth_heatmaps, predicted_heatmaps, sigma=0.1, thresholds=np.arange(0.5, 1.0, 0.05)): |
| aps = [] |
|
|
| assert ground_truth_heatmaps.shape == predicted_heatmaps.shape, "Heatmaps should have the same shape" |
|
|
| oks = compute_oks_heatmaps(ground_truth_heatmaps, predicted_heatmaps, sigma) |
| for threshold in thresholds: |
| tp = np.sum(oks >= threshold) |
| fp = np.sum(oks < threshold) |
| precision = tp / (tp + fp) |
| aps.append(precision) |
| map_value = np.mean(aps) |
|
|
| return map_value |
|
|
|
|
| |
| |
| |
| def compute_oks_keypoints(ground_truth, prediction, sigma): |
| |
| distance = np.sqrt(np.sum((ground_truth - prediction)**2, axis=1)) |
|
|
| |
| |
| scale = 1 |
| |
| oks = np.exp(-1 * (distance ** 2) / (2 * (sigma**2) * (scale ** 2))) |
| return oks |
|
|
|
|
| def compute_map_keypoints(ground_truth_keypoints, predicted_keypoints, sigma=0.1, thresholds=np.arange(0.5, 1.0, 0.05)): |
| aps = [] |
|
|
| |
| oks = compute_oks_keypoints(ground_truth_keypoints, predicted_keypoints, sigma) |
| for threshold in thresholds: |
| |
| tp = np.sum(oks >= threshold) |
| fp = np.sum(oks < threshold) |
| precision = tp / (tp + fp) |
| aps.append(precision) |
| |
| map_value = np.mean(aps) |
| return map_value |
|
|
|
|
|
|
| |
| |
| |
|
|
| def compute_iou_heatmaps(heatmap1, heatmap2): |
|
|
| assert heatmap1.shape == heatmap2.shape, "Heatmaps should have the same shape" |
|
|
| overlap = np.logical_and(heatmap1, heatmap2) |
| union = np.logical_or(heatmap1, heatmap2) |
| overlap_area = np.sum(overlap) |
| union_area = np.sum(union) |
| IoU = overlap_area / union_area |
| return IoU |
|
|
|
|
|
|
| |
| |
| |
|
|
| from collections.abc import Iterable |
|
|
| def radial(pt1, pt2, factor=1): |
| if not isinstance(factor,Iterable): |
| factor = [factor]*len(pt1) |
| return sum(((i-j)*s)**2 for i, j,s in zip(pt1, pt2, factor))**0.5 |
|
|
| def cal_all_distance(points, gt_points, factor=1): |
| ''' |
| points: [(x,y,z...)] |
| gt_points: [(x,y,z...)] |
| return : [d1,d2, ...] |
| ''' |
| n1 = len(points) |
| n2 = len(gt_points) |
| if n1 == 0: |
| print("[Warning]: Empty input for calculating mean and std") |
| return 0, 0 |
| if n1 != n2: |
| raise Exception("Error: lengthes dismatch, {}<>{}".format(n1, n2)) |
| return [radial(p, q, factor) for p, q in zip(points, gt_points)] |
|
|
|
|
| |
| |
| |
|
|
| """ |
| MRE (Mean Radial Error): |
| This measures the average euclidean distance between predicted landmarks and ground truth landmarks. |
| It is calculated by taking the mean of the list of distances (cal_all_distance). |
| """ |
|
|
| def compute_mre(distance_list): |
| return np.mean(distance_list) |
|
|
| |
| |
| |
| """ |
| SDR (Successful Detection Rate): |
| This measures the percentage of predicted landmarks that are within a threshold distance of the ground truth. |
| It is calculated by get_sdr which counts the number of distances below each threshold and divides by the total number of landmarks. |
| """ |
|
|
| def compute_sdr(distance_list, threshold=[2, 2.5, 3, 4, 6, 9, 10]): |
| """ |
| Compute Successful Detection Rate (SDR) in pixel for a given list of distances and thresholds. |
| The SDR is the proportion of predicted points that fall within a certain distance threshold from the ground truth points. |
| """ |
| sdr = {} |
| n = len(distance_list) |
|
|
| for th in threshold: |
| sdr[th] = sum(d <= th for d in distance_list) / n |
| return sdr |
|
|
| |
| |
| |
|
|
|
|
| def compute_batch_metrics(gt_batch_keypoints, gt_batch_heatmaps, pred_batch, image_size, num_landmarks, useHeatmaps, sigma): |
|
|
| batch_size = pred_batch.shape[0] |
| mse_list = [] |
| map_list1 = [] |
| map_list2 = [] |
| iou_list = [] |
| distance_list = [] |
|
|
| |
| sigma = 5 |
| |
| |
| for i in range(batch_size): |
| single_gt_keypoints = gt_batch_keypoints[i, :, :].numpy() |
| single_gt_heatmaps = gt_batch_heatmaps[i, :, :].numpy() |
| single_prediction = pred_batch[i, :, :].numpy() |
| single_image_size = tuple(image_size[i].int().tolist()) |
|
|
| |
| single_gt_keypoints = utilities.extract_landmarks(single_gt_heatmaps, num_landmarks) |
|
|
| |
| single_gt_heatmaps_fused = utilities.points_to_heatmap(single_gt_keypoints, img_size=single_image_size, sigma=sigma, fuse=True) |
| |
|
|
| if useHeatmaps: |
| |
| single_pred_keypoints = utilities.extract_landmarks(single_prediction, num_landmarks) |
|
|
| |
| single_pred_heatmaps = utilities.points_to_heatmap(single_pred_keypoints, img_size=single_image_size, sigma=sigma, fuse=True) |
| else: |
| single_pred_keypoints = single_prediction |
| single_pred_heatmaps = utilities.points_to_heatmap(single_pred_keypoints, img_size=single_image_size, sigma=sigma, fuse=True) |
|
|
| gt_scaled_points = np.array(utilities.scale_points(single_gt_keypoints, single_image_size)) |
| pred_scaled_points = np.array(utilities.scale_points(single_pred_keypoints, single_image_size)) |
|
|
| |
| if num_landmarks == 6: |
| physical_factor = 1 |
| elif num_landmarks == 19: |
| physical_factor = np.array([2400/single_image_size[0], 1935/single_image_size[1]]) * 0.1 |
| |
| |
| elif num_landmarks == 37: |
| physical_factor = 50/radial(gt_scaled_points[0], gt_scaled_points[4]) |
| else: |
| raise Exception("Error: Unknown number of landmarks") |
|
|
| cur_distance_list = cal_all_distance(pred_scaled_points, gt_scaled_points, physical_factor) |
| distance_list += cur_distance_list |
|
|
| |
| mse = compute_mse(gt_scaled_points, pred_scaled_points) |
| mse_list.append(mse) |
|
|
| |
| map2 = compute_map_keypoints(gt_scaled_points, pred_scaled_points) |
| map_list2.append(map2) |
|
|
| |
| map1 = compute_map_heatmaps(single_gt_heatmaps_fused, single_pred_heatmaps) |
| map_list1.append(map1) |
|
|
| |
| iou = compute_iou_heatmaps(single_gt_heatmaps_fused, single_pred_heatmaps) |
| iou_list.append(iou) |
| |
| return mse_list, map_list1, map_list2, iou_list, distance_list |
|
|