visdif / sampling_util.py
englert
update app.py
1865e20
raw history blame
No virus
1.14 kB
import numpy as np
from fastdist2 import euclidean_vector_to_matrix_distance
def furthest_neighbours(x, downsample_size, seed):
x = x.astype(np.float32)
np.random.seed(seed)
length = x.shape[0]
img_vecs_dims = x.shape[-1]
rv_indices = [np.random.randint(low=0, high=downsample_size - 1, size=1)[0]]
selected_points = np.zeros((downsample_size, img_vecs_dims), np.float32)
selected_points[0, :] = x[rv_indices[0], :]
distance_for_selected_min = np.ones((length,)) * 1e15
inactive_points = np.zeros(length, dtype=bool)
inactive_points[rv_indices[0]] = True
for i in (range(downsample_size - 1)):
distance_for_selected = euclidean_vector_to_matrix_distance(selected_points[i, :], x)
distance_for_selected_min = np.minimum(distance_for_selected_min, distance_for_selected)
furthest_point_idx = np.argmax(np.ma.array(distance_for_selected_min, mask=inactive_points))
rv_indices.append(furthest_point_idx)
selected_points[i + 1, :] = x[furthest_point_idx, :]
inactive_points[furthest_point_idx] = True
return rv_indices, selected_points