File size: 1,136 Bytes
ce0d4fb
 
 
 
 
1865e20
ce0d4fb
 
 
 
 
1865e20
 
ce0d4fb
 
 
 
 
 
 
1865e20
ce0d4fb
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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