-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcs_fn_ransac_estimator.py
530 lines (282 loc) · 18 KB
/
dcs_fn_ransac_estimator.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 9 10:18:08 2019
@author: pohsuanh
Class Implementaion of the RANSAC-SVM.
(Originally dcs_fn_ransac_svc_module.py)
'''
RBF SVM parameters
Intuitively, the gamma parameter defines how far the influence of a single
training example reaches, with low values meaning ‘far’ and high values meaning
‘close’. The gamma parameters can be seen as the inverse of the radius of
influence of samples selected by the model as support vectors.
The C parameter trades off correct classification of training examples against
maximization of the decision function’s margin. For larger values of C, a
smaller margin will be accepted if the decision function is better at
classifying all training points correctly. A lower C will encourage a larger
margin, therefore a simpler decision function, at the cost of training
accuracy.In other words``C`` behaves as a regularization parameter in the SVM.
'''
This class allows various classifiers
"""
import numpy as np
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
import pathos.multiprocessing as mp
from multiprocessing import Process, Queue
from copy import deepcopy
from sklearn.metrics import precision_recall_curve, average_precision_score
import time
def normalize(v):
norm = np.linalg.norm(v)
if norm == 0:
return v
return v / norm
class SVC_(object):
def __init__(self):
self.estimator = SVC(gamma = 1.0, C = 100.0, class_weight = 'balanced',probability=True)
def fit(self, X, Y):
Xtrain = X[np.argwhere(Y != -1)].squeeze()
Xelse = X[np.argwhere(Y == -1)].squeeze()
Ytrain = Y[np.argwhere(Y != -1)].squeeze()
return self.estimator.fit(Xtrain,Ytrain)
def predict(self, X):
self.pred = self.estimator.predict(X)
return self.pred
def predict_prob(self, X):
self.y_score =self.estimator.predict_proba(X)[:,0]
self.y_score =self.estimator.decision_function(X)
return self.y_score
def fit_predict(self, Xtrain,Ytrain, Xval):
self.pred = self.estimator.fit(Xtrain,Ytrain).predict(Xval)
return self.pred
def score(self, Yval):
return average_precision_score(Yval, self.pred)
#%% Global Matching : Random Sample and Concensus (RANSAC)
class RANSAC_SVC(object):
def __init__(self, gamma = 'auto', C = 1, class_weight = 'balanced', estimator=None, kernel = 'svc'):
self.id = 1
self.results = []
self.result_queue = Queue()
self.Xtrain_init = None
self.match_thres = 0.2
self.learning_constant = 0.05
self.n_iter = 30
self.gamma = gamma
self.C =C
self.class_weight = class_weight
if estimator == None :
if kernel == 'svc' :
self.estimator = SVC( C = self.C, gamma = self.gamma, class_weight = self.class_weight, probability=True)
elif kernel == 'knn' :
self.estimator = KNeighborsClassifier()
else :
self.estimator = estimator
self.best_estimator = None
self.best_AP = 0.
def fit_paralall(self, X, Y):
""" X is the input data, Y is the class annotation.
Unlabelled data are annotated '-1'.
"""
def _work(Xtrain, Ytrain, Xelse, result_queue):
result = ransac_processor().fit(Xtrain, Ytrain, Xelse)
result_queue.put(result)
Xtrain = X[np.argwhere(Y != -1)].squeeze()
Xelse = X[np.argwhere(Y == -1)].squeeze()
Ytrain = Y[np.argwhere(Y != -1)].squeeze().astype(int)
jobs = []
global start0
start0 = time.time()
i = 0
while i < (self.n_iter) :
if self.result_queue.qsize() < mp.cpu_count()-4 :
p = Process(target = _work, args = (Xtrain, Ytrain, Xelse, self.result_queue))
jobs.append(p)
p.start()
print( 'ChildProcess...',i)
i += 1
else :
time.sleep(1)
for p in jobs:
p.join()
p.close()
self.results = [ self.result_queue.get() for i in range(self.result_queue.qsize())]
best_aps, best_estimators = list(zip(*self.results))
self.best_estimator = best_estimators[ np.argmax(best_aps) ]
self.best_AP = np.max(best_aps)
def predict(self, X):
self.pred = self.best_estimator.predict(X)
return self.pred
def predict_class(self, X):
return self.predict(X)
def predict_proba(self,X):
self.y_score = self.best_estimator.predict_proba(X)
return self.y_score
def score(self, Xval, Yval):
return self.estimator.score(Xval, Yval)
def eval_score(self, Xval, Yval):
pred = self.predict_class(Xval)
self.validation_average_precision = average_precision_score(Yval, pred)
# print('validation_average_precision: ', average_precision_score(Yval, pred) )
y_score = self.predict_score(Xval)
self.average_precision = average_precision_score(Yval, y_score)
# print('Average precision-recall score: {0:0.2f}'.format(average_precision))
self.precision, self.recall, self.thresholds = precision_recall_curve(Yval, y_score)
def sample(self, Xtrain, Ytrain) :
if type(self.Xtrain_init) != np.ndarray :
self.Xtrain_init = Xtrain.copy()
self.train_size_init = Xtrain.shape[0]
self.train_size = Xtrain.shape[0]
self.num_samples = int( 0.9 * len(Xtrain) )
self.idx_sample = np.arange(self.train_size)
np.random.shuffle(self.idx_sample)
self.idx_sample = self.idx_sample[:self.num_samples]
self.Xsample, self.Ysample = Xtrain[self.idx_sample], Ytrain[self.idx_sample]
self.idx_all = np.arange(self.train_size)
self.idx_res = np.setdiff1d(self.idx_all, self.idx_sample)
self.Xres, self.Yres = Xtrain[list(self.idx_res)], Ytrain[list(self.idx_res)]
def fit_sub(self, Xtrain, Ytrain, Xelse):
self.Ysample = 1
while len(np.unique(self.Ysample)) == 1 : # ensure drawn samples contains both classes
# print('Sampling...')
self.sample(Xtrain, Ytrain)
n = 0
while True :
# print('label_propagation...')
# Find lablled data that matches this fit
self.estimator.fit(self.Xsample, self.Ysample)
pred = self.estimator.predict(self.Xres)
AP = average_precision_score(self.Yres, pred)
# if enough mathces are found, declare it to be a good estimate,
# refit the estimator to the expanded set.
if AP >= self.match_thres + n * self.learning_constant :
n += 1
if AP > self.best_AP :
self.best_AP = AP
print('Best average precision :',self.best_AP)
self.best_estimator = deepcopy(self.estimator) # deep copy
# Rest of the labelled data
if hasattr(self.estimator, "decision_function"):
confidence = self.estimator.decision_function(self.Xres)
confidence = normalize(confidence) + 0.5
self.idx_sample = np.union1d(self.idx_sample ,self.idx_res[ (confidence >= 0.9) | (confidence <= -0.9) ])
self.idx_res = np.setdiff1d( self.idx_res ,self.idx_res[ (confidence >= 0.9) | (confidence <= -0.9) ] )
else:
confidence = self.estimator.predict_proba(self.Xres).T
self.idx_sample = np.union1d(self.idx_sample ,self.idx_res[ (confidence[0] >= 0.9) | (confidence[1] <= 0.1) ])
self.idx_res = np.setdiff1d( self.idx_res ,self.idx_res[ (confidence[0] >= 0.9) | (confidence[1] <= 0.1) ] )
self.Xsample, self.Ysample = Xtrain[self.idx_sample], Ytrain[self.idx_sample]
self.Xres, self.Yres = Xtrain[self.idx_res], Ytrain[self.idx_res]
# print('training %',(train_size_init - len(group1_idx_res))/ train_size_init)
if(self.train_size_init - len(self.idx_res))/ self.train_size_init == 1.0 : # training finished.
print('Propagation finished')
break
if Xelse.size != 0 and (self.train_size_init - len(self.idx_res))/ self.train_size_init > 0.0 :
print(' Unitilizing Unlabelled Data...')
Yelse = self.estimator.predict(Xelse)
if hasattr(self.estimator, "decision_function"): # calculate confidence score of the unlablled data
confidence = self.estimator.decision_function(Xelse)
else:
confidence = self.estimator.predict_proba(Xelse)
confidence = (confidence - np.mean(confidence) )/np.std(confidence) + 0.5
X_inliers, Y_inliers = Xelse[ np.abs(confidence) >= 0.9 ], Yelse[ np.abs(confidence) >= 0.9 ]
X_outliers, Y_outliers = Xelse[ np.abs(confidence) < 0.1 ], Yelse[ np.abs(confidence) < 0.1 ]
# expande the dataset with the new lablled data
train_size = Xtrain.shape[0]
Xtrain, Ytrain = np.concatenate((Xtrain,X_inliers)), np.concatenate((Ytrain, Y_inliers))
# Xtrain, Ytrain = np.concatenate((Xtrain,X_outliers)), np.concatenate((Ytrain, Y_outliers))
expanded_size = Xtrain.shape[0]
self.idx_sample = np.union1d(self.idx_sample, np.arange(train_size, expanded_size))
self.Xsample, self.Ysample = Xtrain[self.idx_sample], Ytrain[self.idx_sample]
else :
# print(' cannnot find good estimator, best average precision : ', self.best_AP)
break
return (self.best_AP, self.best_estimator, )
def fit(self, X, Y) :
Xtrain = X[np.argwhere(Y != -1)].squeeze()
Xelse = X[np.argwhere(Y == -1)].squeeze()
Ytrain = Y[np.argwhere(Y != -1)].squeeze()
for i in range(self.n_iter) :
result = self.fit_sub(Xtrain, Ytrain, Xelse)
self.result_queue.put(result)
self.results = [ self.result_queue.get() for i in range(self.result_queue.qsize())]
best_aps, best_estimators = list(zip(*self.results))
self.best_estimator = best_estimators[ np.argmax(best_aps) ]
self.best_AP = np.max(best_aps)
class ransac_processor(RANSAC_SVC):
def __init__(self) :
super(ransac_processor, self).__init__()
print('Initializing...')
assert self.best_AP == 0., 'not a deep copy.'
# self.id += self.id
#
# print('selfid :', self.id)
if self.estimator == None :
print('no estimator, continue ?')
if k == 'n' :
import os
os.sys.exit()
# else :
# print('estimator :', self.estimator)
def sample(self, Xtrain, Ytrain) :
if type(self.Xtrain_init) != np.ndarray :
self.Xtrain_init = Xtrain.copy()
self.train_size_init = Xtrain.shape[0]
self.train_size = Xtrain.shape[0]
self.num_samples = 4 # 10
self.idx_sample = np.arange(self.train_size)
np.random.shuffle(self.idx_sample)
self.idx_sample = self.idx_sample[:self.num_samples]
self.Xsample, self.Ysample = Xtrain[self.idx_sample], Ytrain[self.idx_sample]
self.idx_all = np.arange(self.train_size)
self.idx_res = np.setdiff1d(self.idx_all, self.idx_sample)
self.Xres, self.Yres = Xtrain[list(self.idx_res)], Ytrain[list(self.idx_res)]
def fit(self, Xtrain, Ytrain, Xelse):
self.Ysample = 1
while len(np.unique(self.Ysample)) == 1 : # ensure drawn samples contains both classes
# print('Sampling...')
self.sample(Xtrain, Ytrain)
n = 0
while True :
# print('label_propagation...')
# Find lablled data that matches this fit
self.estimator.fit(self.Xsample, self.Ysample)
pred = self.estimator.predict(self.Xres)
AP = average_precision_score(self.Yres, pred)
# if enough mathces are found, declare it to be a good estimate,
# refit the estimator to the expanded set.
if AP >= self.match_thres + n * self.learning_constant :
n += 1
if AP > self.best_AP :
self.best_AP = AP
# print('Best average precision :',self.best_AP)
self.best_estimator = deepcopy(self.estimator) # deep copy
# Rest of the labelled data
confidence = self.estimator.decision_function(self.Xres)
# confidence = (confidence - np.mean(confidence) )/np.std(confidence) + 0.5
self.idx_sample = np.union1d(self.idx_sample ,self.idx_res[ (confidence >= 0.9) | (confidence <= -0.9) ])
self.idx_res = np.setdiff1d( self.idx_res ,self.idx_res[ (confidence >= 0.9) | (confidence <= -0.9) ] )
self.Xsample, self.Ysample = Xtrain[self.idx_sample], Ytrain[self.idx_sample]
self.Xres, self.Yres = Xtrain[self.idx_res], Ytrain[self.idx_res]
# print('training %',(train_size_init - len(group1_idx_res))/ train_size_init)
if(self.train_size_init - len(self.idx_res))/ self.train_size_init == 1.0 : # training finished.
print('Propagation finished')
break
if Xelse.size != 0 and (self.train_size_init - len(self.idx_res))/ self.train_size_init > 0.0 :
# print(' Unitilizing Unlabelled Data...')
Yelse = self.estimator.predict(Xelse)
confidence = self.estimator.decision_function(Xelse) # calculate confidence score of the unlablled data
# confidence = (confidence - np.mean(confidence) )/np.std(confidence) + 0.5
X_inliers, Y_inliers = Xelse[ np.abs(confidence) >= 0.9 ], Yelse[ np.abs(confidence) >= 0.9 ]
X_outliers, Y_outliers = Xelse[ np.abs(confidence) < 0.1 ], Yelse[ np.abs(confidence) < 0.1 ]
# expande the dataset with the new lablled data
train_size = Xtrain.shape[0]
Xtrain, Ytrain = np.concatenate((Xtrain,X_inliers)), np.concatenate((Ytrain, Y_inliers))
# Xtrain, Ytrain = np.concatenate((Xtrain,X_outliers)), np.concatenate((Ytrain, Y_outliers))
expanded_size = Xtrain.shape[0]
self.idx_sample = np.union1d(self.idx_sample, np.arange(train_size, expanded_size))
self.Xsample, self.Ysample = Xtrain[self.idx_sample], Ytrain[self.idx_sample]
else :
break
return (self.best_AP,self.best_estimator)