|
|
import torch |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_center_value(arr): |
|
|
|
|
|
distances = np.abs(arr[:, np.newaxis] - arr[np.newaxis, :]) |
|
|
|
|
|
|
|
|
sum_distances = np.sum(distances, axis=1) |
|
|
|
|
|
|
|
|
center_index = np.argmin(sum_distances) |
|
|
|
|
|
|
|
|
center_value = arr[center_index] |
|
|
|
|
|
return center_value |
|
|
|
|
|
|
|
|
def compute_overlap(center_t, boundary_t, center_t_minus_1, boundary_t_minus_1): |
|
|
""" |
|
|
Compute the overlap of boundaries between time t and t-1 for each element in the arrays. |
|
|
|
|
|
Args: |
|
|
- center_t: numpy array representing the center at time t with shape [N,] |
|
|
- boundary_t: numpy array representing the boundary at time t with shape [N,1, candidates] |
|
|
- center_t_minus_1: numpy array representing the center at time t-1 with shape [N,] |
|
|
- boundary_t_minus_1: numpy array representing the boundary at time t-1 with shape [N,] |
|
|
|
|
|
Returns: |
|
|
- overlap: numpy array representing the overlap of boundaries with shape [N,] |
|
|
""" |
|
|
|
|
|
boundary_t = boundary_t.squeeze(1) |
|
|
boundary_t_minus_1 = boundary_t_minus_1.squeeze(1) |
|
|
center_t = center_t[:, np.newaxis] |
|
|
|
|
|
center_t_minus_1 = center_t_minus_1[:, np.newaxis] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
start_t = center_t - 0.5 * boundary_t |
|
|
end_t = center_t + 0.5 * boundary_t |
|
|
start_t_minus_1 = center_t_minus_1 - 0.5 * boundary_t_minus_1 |
|
|
end_t_minus_1 = center_t_minus_1 + 0.5 * boundary_t_minus_1 |
|
|
|
|
|
|
|
|
intersection = np.maximum(0, np.minimum(end_t, end_t_minus_1) - np.maximum(start_t, start_t_minus_1)) |
|
|
union = boundary_t + boundary_t_minus_1 - intersection |
|
|
|
|
|
|
|
|
overlap = intersection / union |
|
|
|
|
|
return overlap |