-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathransac_simple_new.py
472 lines (327 loc) · 14.2 KB
/
ransac_simple_new.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 8 02:15:14 2020
@author: pohsuanh
Ransac Simple Algorithm2 (fork from ransac_simple.py)
RANSAC semisupervised learning utilizing unlabeled data.
"""
import numpy as np
from sklearn.svm import SVC
from copy import deepcopy
from sklearn.metrics import precision_recall_curve, average_precision_score, accuracy_score
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import logging
import argparse
def intersect_id(a,b):
a1_rows = a.view([('', a.dtype)] * a.shape[1])
a2_rows = b.view([('', b.dtype)] * b.shape[1])
sec, comm1, comm2 = np.intersect1d(a1_rows, a2_rows, return_indices=True)
return comm1, comm2
def setdiff(a, b):
a1_rows = a.view([('', a.dtype)] * a.shape[1])
a2_rows = b.view([('', b.dtype)] * b.shape[1])
return np.setdiff1d(a1_rows, a2_rows).view(a.dtype).reshape(-1, a.shape[1])
class RANSAC(object):
""" Randomly sample data from the training set and fit N estimators.
The consensus of the predictions of the N estimators are used as labels on the
unlablled data set X.
"""
def __init__(self, estimator, n=30, Epochs = 30, sampling_rate = 0.5 ,verbose = False, save_iteration_consensus = False, save_decision_maps = False ):
'''
Parameters
----------
estimator : sk-learn classifier
DESCRIPTION.
n : int, optional
numer of base estimators. The default is 30.
verbose : TYPE, optional
print debugging information. The default is False.
save_iteration_consensus : Boolean, optional
Used for plotting 2D decision contours. Save inliers of each iteration of the best epoch. The default is False.
save_decision_maps : TYPE, optional
Used for plotting 2D decision contours. Save decision maps and grids of deicision maps of each iteration of the best epoch. The default is False.
Returns
-------
None.
'''
self.num_estimators = n
self.sampling_rate = sampling_rate
self.Epochs = Epochs
self.epoch = 0
self.estimator = estimator
self.estimators = []
self.x_consensus = []
self.y_consensus = []
if verbose:
def verboseprint(self,*args):
# Print each argument separately so caller doesn't need to
# stuff everything to be printed into a single string
for arg in args:
print(arg),
print()
else:
self.verboseprint = lambda *a: None # do-nothing function
if save_iteration_consensus :
self.iteration_consensus = []
if save_decision_maps :
self.decision_maps = []
self.decision_maps_grids = []
def sample_gen( self, X, Y):
resample = True
rate = self.sampling_rate
num_totoal = len(X)
total_ids = np.arange(num_totoal)
while resample == True :
np.random.shuffle(total_ids)
sample_ids = total_ids[:int(num_totoal*rate)]
X_s, Y_s = X[sample_ids], Y[sample_ids]
if len(np.unique(Y_s)) < 2: # only one class in the samples, resample
self.verboseprint('resample training set because only one class presented.')
u, c = np.unique(Y, return_counts = True)
self.verboseprint('training set')
self.verboseprint('class{} : class{}'.format(u[0],u[1]))
self.verboseprint('counts {} : {}'.format(c[0], c[1]))
self.verboseprint('in the samples')
u, c = np.unique(Y_s, return_counts = True)
self.verboseprint('class{} '.format(u[0]))
self.verboseprint('counts {} '.format(c[0]))
else :
resample = False
return X_s, Y_s
def _fit(self, Xtrain, Ytrain) :
"""fit the base estimators of the ensemble within iteration
"""
# Sampling Phase
if self.epoch == 0 :
for i in range(self.num_estimators) :
x, y = self.sample_gen(Xtrain,Ytrain)
self.estimator.fit(x,y.ravel())
self.estimators.append(deepcopy(self.estimator))
else :
for i in range(self.num_estimators) :
self.estimators[i].fit(Xtrain,Ytrain.ravel())
def _predict(self, X) :
""" predict on unlabled data with estmators
"""
self.verboseprint('_predict: ',X.shape[0])
# Consensus Phase
preds = []
for i in range(self.num_estimators):
estimator = self.estimators[i]
pred = estimator.predict(X)
preds.append(pred)
self.verboseprint('done predict')
x_consensus = []
y_consensus = []
preds = np.asarray(preds)
for n in range(len(X)) :
pred = np.unique(preds[:,n])
if len(pred) == 1 : # censensus on seach data point
x_consensus.append(X[n])
y_consensus.append(pred)
return np.asarray(x_consensus), np.asarray(y_consensus)
def fit_predict(self, Xtrain, Ytrain, Xelse ):
"""
Set up X_init by sampling 90% of the labeled data.
"""
self.Xtrain_init = Xtrain
self.Xtrain_epoch = Xtrain
if len(Ytrain.shape) < 2 :
self.Ytrain_init = Ytrain[:,np.newaxis]
self.Ytrain_epoch = Ytrain[:,np.newaxis]
else :
self.Ytrain_init = Ytrain
self.Ytrain_epoch = Ytrain
self.X_epoch = Xelse
try :
assert np.size(self.X_epoch) > 0 , 'unlabeled data array empty.'
except AssertionError :
self._fit(self.Xtrain_epoch, self.Ytrain_epoch)
return self.Xtrain_epoch, self.Ytrain_epoch
# propagation
for self.epoch in range(self.Epochs): # add the consensus to training set
self._fit(self.Xtrain_epoch, self.Ytrain_epoch)
x, y = self._predict(self.X_epoch)
if len(y) == 0 : # no more consensus
break
else :
if hasattr(self, 'iteration_consensus') :
self.verboseprint('append iter')
self.iteration_consensus.append((x,y))
if hasattr(self, 'decision_maps') :
self.verboseprint('append decision map')
# Create mesh for contour plot
h = 0.06 # step size in the mesh
x1_min, x1_max = self.Xtrain_epoch[:, 0].min() - .5, self.Xtrain_epoch[:, 0].max() + .5
x2_min, x2_max = self.Xtrain_epoch[:, 1].min() - .5, self.Xtrain_epoch[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x1_min, x1_max, h),
np.arange(x2_min, x2_max, h))
if hasattr(self.estimator, "decision_function"):
decision_map = self.decision_function(np.c_[xx.ravel(), yy.ravel()])
else:
decision_map = self.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1] - 0.5
decision_map= decision_map.reshape(xx.shape)
self.decision_maps.append(decision_map)
self.decision_maps_grids.append((xx,yy))
self.Xtrain_epoch = np.concatenate((self.Xtrain_epoch, x), axis = 0) # update training set
self.Ytrain_epoch = np.concatenate((self.Ytrain_epoch, y), axis = 0)
self.X_epoch = setdiff(self.X_epoch,x) #update unlabeled set
# self.verboseprint('size of x epoch', self.X_epoch,shape)
if len(self.X_epoch) == 0:
break
return self.Xtrain_epoch[len(self.Xtrain_init):], self.Ytrain_epoch[len(self.Ytrain_init):]
def predict_proba(self, X):
'''
Predict class probabilities for unlabled data.
Parameters
----------
X : TYPE
Unlabeled samples to be predicted.
Returns
-------
None.
'''
# X_, y_ = self.predict(X)
preds = []
for i in range(self.num_estimators):
estimator = self.estimators[i]
preds.append(estimator.predict_proba(X))
preds = np.mean(np.asarray(preds), axis = 0 )
return preds
def score(self, X):
preds = self.predict_proba(X)
return np.argmax(preds, axis = 1)
def decision_function(self, X):
'''
Predict decision function value for unlabled data.
Parameters
----------
X : TYPE
Unlabeled samples to be predicted.
Returns
-------
None.
'''
# X_, y_ = self.predict(X)
if hasattr(self.estimator, 'decision_function') :
preds = []
for i in range(self.num_estimators):
estimator = self.estimators[i]
preds.append(estimator.decision_function(X))
preds = np.mean(np.asarray(preds), axis = 0 )
return preds
else :
preds = self.predict_proba(X)[:,1]
return preds -0.5
def score(self, X):
preds = self.predict_proba(X)
return np.argmax(preds, axis = 1)
if __name__ == '__main__' :
n_samples = 300
X, y = make_classification(n_samples = n_samples, n_features=2, n_redundant=0, n_informative=2,
random_state=1, n_clusters_per_class=1)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(size=X.shape)
linearly_separable = (X, y)
datasets = [make_moons(n_samples = n_samples, noise=0.1, random_state=0),
make_circles(n_samples = n_samples, noise=0.1, factor=0.5, random_state=1),
linearly_separable
]
## corrutped annotations
def add_noise(targets):
y_noise = targets
noise_indices = []
classes = np.unique(targets)
# level of noise in targets
noise_percent = np.ones(len(classes)) * 0.4
if all(noise_percent != 0.) :
for i, c in enumerate(classes) :
indices = np.where(targets == c)[0]
indices = np.random.choice( indices, int(len(indices)*noise_percent[i]))
noise_indices.extend(indices.tolist())
other_labels = [ cl for cl in classes if cl != c]
for j in np.asarray(indices, dtype= int) :
y_noise[j] = np.random.choice(other_labels)
return y_noise, noise_indices, noise_percent
else :
return targets, None, 0
i = 1
figure = plt.figure(figsize=(9, 9))
for ds_cnt, ds in enumerate(datasets):
X, y = ds
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.9, random_state=42)
y_train_gt = deepcopy(y_train)
y_train, noise_indices, noise_percent = add_noise(y_train)
est =SVC(gamma='auto', C=1, probability =True)
# est = MLPClassifier(hidden_layer_sizes=(10,10), solver='lbfgs' )
est.fit(X_train, y_train_gt)
score_clean = est.score(X_test, y_test)
est.fit(X_train, y_train)
score_noise = est.score(X_test, y_test)
rac = RANSAC(est)
rac.fit_predict(X_train, y_train)
# predict() classification
x_pred, y_pred = rac.predict(X_test)
ind1, ind2 = intersect_id(X_test, x_pred)
score_denoise = accuracy_score(y_test[ind1].ravel(),y_pred.ravel())
# probability prediction
preds = rac.predict_proba(X_test)
score_denoise = accuracy_score(y_test.ravel(), np.argmax(preds, axis = 1).ravel())
h = .02
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
cm = plt.cm.RdBu
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
# Plot the training points
ax = plt.subplot(len(datasets), 3, i*3 -2)
ax.set_xticks(())
ax.set_yticks(())
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
if ds_cnt == 0:
ax.set_title("training data")
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright,
alpha = 0.6)
# Plot the testing points
ax = plt.subplot(len(datasets), 3, i*3 - 1 )
ax.set_xticks(())
ax.set_yticks(())
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
if ds_cnt == 0:
ax.set_title("testing data")
ax.scatter(X_test[:, 0], X_test[:, 1], facecolors =None, cmap=cm_bright,
edgecolors='k',alpha = 0.6)
# Plot the prdiction points
ax = plt.subplot(len(datasets), 3, i*3 )
ax.set_xticks(())
ax.set_yticks(())
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
if ds_cnt == 0:
ax.set_title("prediction")
# non-concensus
# X_no = setdiff(X_test, x_pred)
# ax.scatter(X_no[:,0], X_no[:,1], facecolors =None, cmap=cm_bright,
# edgecolors='k',alpha = 0.2)
# concensus
# ax.scatter(x_pred[:, 0], x_pred[:, 1], c = y_pred.ravel() ,cmap=cm_bright,
# edgecolors='k',alpha = 0.2)
ax.scatter(X_test[:, 0], X_test[:, 1], c = np.argmax(preds, axis =1).ravel() ,cmap=cm_bright,
edgecolors='k',alpha = 0.2)
ax.text(xx.max() - 3.8, yy.min() + 0.3, ('%.2f' % score_clean).lstrip('0'), c = 'k',
size=15, horizontalalignment='right')
ax.text(xx.max() - 2.8, yy.min() + 0.3, ('%.2f' % score_noise).lstrip('0'), c = 'b',
size=15, horizontalalignment='right')
ax.text(xx.max() - 1.8, yy.min() + 0.3, ('%.2f' % score_denoise).lstrip('0'), c = 'purple',
size=15, horizontalalignment='right')
i+=1