-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathactive_utils.py
69 lines (53 loc) · 2.11 KB
/
active_utils.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from __future__ import print_function
import numpy as np
import time
'''
This file contains all the implemented query functions
'''
def random(model,points=2000):
'''
This function randomly selects indices in range([pool size])
:param model: the trained model f, used only to get the number of points in the pool
:param points: number of points to query
:return: array of indices of the queried points
'''
m = model.pool_idx.SHAPE[0]
all_points = np.arange(m)
np.random.shuffle(all_points)
return all_points[:points]
def softmax_response(model, points=5000):
'''
:param model: the trained model f, used to calculate the softmax responses
:param points: number of points to query
:return: array of indices of the queried points
'''
confidence_score = model.softmax_response() #calculate the softmax responses of the pool
argsort_confidence = np.argsort(confidence_score)
return argsort_confidence[:points]
def coreset(model, points=5000):
'''
:param model: the trained model f, used to calculate the softmax responses
:param points: number of points to query
:return: array of indices of the queried points
'''
m = model.pool_idx.SHAPE[0]
all_points = np.arange(m)
dist_mat, pool_mat = model.coreset_mat() #return two distance matrices, train to pool and pool to pool
dist_mat_min = np.min(dist_mat,0)
new_points_dist = dist_mat_min
selected_points = []
for i in range(points):
idx = np.argmax(np.minimum(dist_mat_min[all_points], new_points_dist[all_points]))
selected_points.append(all_points[idx])
new_points_dist = np.minimum(new_points_dist, pool_mat[idx])
np.delete(all_points,(idx),0)
return np.array(selected_points)
def mc_dropout(model, points=5000):
'''
:param model: the trained model f, used to calculate the softmax responses
:param points: number of points to query
:return: array of indices of the queried points
'''
confidence_score = model.mc_dropout()
argsort_confidence = np.argsort(confidence_score)
return argsort_confidence[:points]