-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabeler.py
279 lines (249 loc) · 12.4 KB
/
labeler.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import numpy as np
from joint_optimisation import do_optimization
from utils import compute_intersection_line_decision_boundary, to_vector
import traceback
from interface import LabelInterface, PointLabelInterface
__author__ = 'mhuijser'
class Labeler(object):
def __init__(self):
pass
def label(self, *args):
raise NotImplementedError
class IdealLabeler(Labeler):
"""
Provide the errorless/noiseless label to any feature vectors being queried.
Parameters
----------
dataset: Dataset object
Dataset object with the ground-truth label for each sample.
"""
def __init__(self, dataset, **kwargs):
super(IdealLabeler, self).__init__()
y = dataset.groundtruth_y # Dataset in original data space
# make sure the input dataset is fully labeled
assert (np.array(y) != np.array(dataset.unlabeled_class)).all()
self.y = y # Ground truth labels
self.dataset = dataset
def label(self, feature, **kwargs):
"""
Label feature (in original space)
:param feature:
:return:
"""
try:
found_id = np.where([np.array_equal(x, feature) for x in self.dataset.data["features"]])[0][0]
label = self.y[found_id]
except Exception as e:
traceback.print_exc()
raise e
return label
class PointLabeler(Labeler):
def __init__(self, dataset, pretrained_groundtruth, hyperparameters, **kwargs):
super(PointLabeler, self).__init__()
self.dataset = dataset
self.lambda_r = hyperparameters["lambda_r"]
self.gamma = hyperparameters["gamma"]
self.weight_hinge = hyperparameters["weight_hinge"]
self.learning_rate = hyperparameters["learning_rate"]
self.decay = hyperparameters["decay"]
self.interval = hyperparameters["interval"]
self.factor = hyperparameters["factor"]
self.max_epochs = hyperparameters["max_epochs"]
self.batch_size = hyperparameters["batch_size"]
self.no_class = self.dataset.unlabeled_class
self.classes = np.unique(self.dataset.groundtruth_y)
if pretrained_groundtruth is not None:
self.w, self.b0 = pretrained_groundtruth["w"], pretrained_groundtruth["b0"]
else:
self.w, self.b0 = self.__train_groundtruth_model()
self.decision_function = lambda x: self.w.T.dot(x) + self.b0
self.trained = self.w is not None
@property
def groundtruth_model(self):
return {"w": self.w, "b0": self.b0}
def __train_groundtruth_model(self):
print "Training ground truth model for labeler"
X_original_space, labels = self.dataset.data["features"], self.dataset.groundtruth_y
if self.no_class in labels:
raise Exception("Model can only be trained on labeled data. Unlabeled data encountered!")
X_scaled = self.dataset.scaling_transformation.transform(X_original_space)
X_val_scaled, labels_val = self.dataset.validation_data_format_keras()
y = np.copy(labels)
y[y == min(self.classes)] = -1
y[y == max(self.classes)] = 1
y_val = None
if labels_val is not None:
y_val = np.copy(labels_val)
y_val[y_val == min(self.classes)] = -1
y_val[y_val == max(self.classes)] = 1
history, (w, b) = do_optimization(X_scaled, y, None, None, Xj_val=X_val_scaled, yj_val=y_val,
lambda_r=self.lambda_r,
gamma=self.gamma, weight_hinge=self.weight_hinge,
learning_rate=self.learning_rate, decay=self.decay,
interval=self.interval, factor=self.factor, max_epochs=self.max_epochs,
batch_size=self.batch_size)
del X_scaled, y
return w, float(b)
def predict(self, sample):
"""
:param sample: n_samples x n_features
:return:
"""
if not self.trained:
raise Exception("Cannot predict with an untrained model.")
scores = self.decision_function(sample.T).T
scores[scores == 0] = self.no_class
scores[scores > 0] = max(self.classes)
scores[scores < 0] = min(self.classes)
labels = scores
# return labels.flatten()
return labels
def label(self, sample, sample_already_scaled=False, *args):
# Transform from original data space to ground truth data space = scaled space.
sample = to_vector(sample).T
if not sample_already_scaled:
sample = self.dataset.scaling_transformation.transform(sample)
label = self.predict(sample)
return label
class HumanPointLabeler(PointLabeler):
def __init__(self, dataset, ALI_model, pretrained_groundtruth=None, hyperparameters=None, **kwargs):
"""
Human oracle interface to label point queries (traditional active learning).
GPU required because of image generation.
:param dataset:
:param ALI_model:
:param pretrained_groundtruth:
:param hyperparameters:
:param kwargs:
"""
super(HumanPointLabeler, self).__init__(dataset, pretrained_groundtruth=pretrained_groundtruth,
hyperparameters=hyperparameters, **kwargs)
self.ali = ALI_model
def label(self, sample, sample_already_scaled=False, *args):
# Transform from original data space to ground truth data space = scaled space.
sample = to_vector(sample)
if sample_already_scaled:
sample = self.dataset.scaling_transformation.inverse_transform(sample.T).T
point_query_image = self.ali.decode(sample.T)
oracle = PointLabelInterface(point_query_image, list(self.dataset.classes),
classes_dictionary=self.dataset.classes_dictionary)
return oracle.label_point_query
class LineLabeler(PointLabeler):
def __init__(self, dataset, pretrained_groundtruth=None, hyperparameters=None,
use_groundtruth_labels=False, **kwargs):
"""
Automatic line labeler, uses a ground truth model to label.
:param dataset: Dataset object with desired dataset loaded in.
:param pretrained_groundtruth: optional, pass pretrained ground truth model to serve as labeler.
If not provided, a ground truth model is trained with all ground truth labels.
:param hyperparameters: hyperparameters (e.g. for optimization).
:param use_groundtruth_labels: use ground truth labels to label query samples,
instead of using ground truth model to classify.
:param kwargs:
"""
super(LineLabeler, self).__init__(dataset, pretrained_groundtruth, hyperparameters)
self.use_groundtruth_labels = use_groundtruth_labels
self.ideal_labeler = None
if self.use_groundtruth_labels:
self.ideal_labeler = IdealLabeler(dataset)
def label(self, sample, line=None, line_segment=None, sample_already_scaled=False, intersection_point_cdb=None):
"""
This function labels a query line with its decision boundary point (= intersection line with decision boundary)
and the label of the query point from which the query line was created.
:param sample: query point: (n_features, 1) in original dataspace
:param line: query line in scaled data space
:param line_segment: can be used to convert to human understandable line query. In scaled data space
:return:
"""
sample = to_vector(sample).T
if self.ideal_labeler is None:
if not sample_already_scaled:
# Transform from original data space to ground truth data space = scaled space.
sample = self.dataset.scaling_transformation.transform(sample)
label = self.predict(sample)
else:
label = self.ideal_labeler.label(sample.squeeze())
A, B = line(0), line(1) # a + (b-a)*0 = a and a+(b-a)*1 = b
db_point = compute_intersection_line_decision_boundary(A, B, self.w, self.b0)
assert db_point.shape == A.shape
return label, db_point
class HumanLineLabeler(LineLabeler):
def __init__(self, dataset, ALI_model, hyperparameters=None, **kwargs):
"""
Human oracle interface to label line queries (traditional active learning).
GPU required because of image generation.
:param dataset:
:param ALI_model:
:param hyperparameters:
:param kwargs:
"""
super(HumanLineLabeler, self).__init__(dataset, hyperparameters=hyperparameters, **kwargs)
self.ali = ALI_model
def label(self, sample, line=None, line_segment=None, sample_already_scaled=False, intersection_point_cdb=None):
"""
NB ONLY HANDLES LINES FROM UNCERTAINTY STRATEGY (for clustercentroids, check if everything is in correct space!)
:param sample: for uncertainty strategy in original ALI space
:param line: lambda function in scaled space
:param line_segment: scaled space
:param sample_already_scaled:
:return:
"""
sample = to_vector(sample) # original space if uncertainty strategy
if sample_already_scaled:
sample = self.dataset.scaling_transformation.inverse_transform(sample.T).T
line_segment_original_space = self.dataset.scaling_transformation.inverse_transform(line_segment)
line_images = self.generate_images(line_segment_original_space)
point_query_image = self.generate_images(sample.T)
intersection_point_cdb_original_space = self.dataset.scaling_transformation.inverse_transform(
intersection_point_cdb.T).T
oracle = LabelInterface(line_segment_original_space, line_images, point_query=sample,
point_query_image=point_query_image,
intersection_point_cdb=intersection_point_cdb_original_space,
classes=list(self.dataset.classes),
classes_dictionary=self.dataset.classes_dictionary)
db_point_original_space = oracle.chosen_point
if db_point_original_space is not None:
db_point_scaled_space = self.dataset.scaling_transformation.transform(db_point_original_space.T).T
else:
db_point_scaled_space = None
label_point_query = oracle.label_point_query
print "Chosen label", label_point_query
return label_point_query, db_point_scaled_space
def generate_images(self, line_segment_original_space):
"""
:param line_segment_original_space: in original space
:return:
"""
try:
decoded_images = self.ali.decode(
line_segment_original_space) # Transform to original space, in which ALI operates.
except Exception as e:
print "EXCEPTION:", traceback.format_exc()
raise e
return decoded_images
class NoisyLineLabeler(LineLabeler):
def __init__(self, dataset, std_noise, pretrained_groundtruth=None, hyperparameters=None,
use_groundtruth_labels=False, **kwargs):
"""
:param dataset:
:param std_noise:
:param pretrained_groundtruth:
:param hyperparameters:
:param use_groundtruth_labels: use ground truth labels to label query samples,
instead of using ground truth model to classify.
:param kwargs:
"""
super(NoisyLineLabeler, self).__init__(dataset, pretrained_groundtruth=pretrained_groundtruth,
hyperparameters=hyperparameters,
use_groundtruth_labels=use_groundtruth_labels, **kwargs)
self.std_noise = std_noise
def label(self, sample, line=None, line_segment=None, sample_already_scaled=False, intersection_point_cdb=None):
label, db_point = super(NoisyLineLabeler, self).label(sample, line, line_segment,
sample_already_scaled=sample_already_scaled)
a, b = line(0), line(1) # a + (b-a)*0 = a and a+(b-a)*1 = b
if self.std_noise == 0:
return label, db_point
noise = np.random.normal(0, self.std_noise)
noisy_db_point = db_point + noise * (
(b - a) / np.linalg.norm(b - a))
return label, noisy_db_point