-
Notifications
You must be signed in to change notification settings - Fork 13
/
suggest_classifier_svm.cpp
838 lines (745 loc) · 35 KB
/
suggest_classifier_svm.cpp
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//**********************************************************************
//* This file is a part of the CANUPO project, a set of programs for *
//* classifying automatically 3D point clouds according to the local *
//* multi-scale dimensionality at each point. *
//* *
//* Author & Copyright: Nicolas Brodu <nicolas.brodu@numerimoire.net> *
//* *
//* This project is free software; you can redistribute it and/or *
//* modify it under the terms of the GNU Lesser General Public *
//* License as published by the Free Software Foundation; either *
//* version 2.1 of the License, or (at your option) any later version. *
//* *
//* This library is distributed in the hope that it will be useful, *
//* but WITHOUT ANY WARRANTY; without even the implied warranty of *
//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
//* Lesser General Public License for more details. *
//* *
//* You should have received a copy of the GNU Lesser General Public *
//* License along with this library; if not, write to the Free *
//* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
//* MA 02110-1301 USA *
//* *
//**********************************************************************/
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <limits>
#include <boost/format.hpp>
// graphics lib
#include <cairo/cairo.h>
#include <strings.h>
#include <math.h>
#include "points.hpp"
#include "base64.hpp"
#include "linearSVM.hpp"
#include "pngutil.hpp"
using namespace std;
typedef LinearSVM::sample_type sample_type;
static const int svgSize = 800;
static const int halfSvgSize = svgSize / 2;
int help(const char* errmsg = 0) {
cout << "\
suggest_classifier [=N] outfile.svg [ unlabeled.msc ...] : class1.msc ... - class2.msc ...\n\
input: class1.msc ... - class2.msc ... # the multiscale files for each class, separated by -\n\
output: outfile.svg # a svg file in which to write a classifier definition. This file may be edited graphically (ex: with inkscape) so as to add more points in the path that separates both classes. So long as there is only one path consisting only of line segments it shall be recognised.\n\
\n\
input(optional): unlabeled.msc # additionnal multiscale files for the scene that are not classified. These provide more points for semi-supervised learning. The corresponding points will be displayed in grey in the output file. If these are not given a Linear Discriminant Analysis is performed in the projected space to separate the classes. To perform semi-supervised learning with density estimation even when no additionnal unlabelled data is available simply repeat the class1 and class2 data as unlabeled.\n\
input(optional): =N # Size of the search grid for cross-validating the SVM. By default N=1 is used, with a local search for the best parameters around a default value hopefully adequate most of the time. Use N>1 in order to increase the quality of the training at the expense of a larger computation time.\n\
"<<endl;
if (errmsg) cout << "Error: " << errmsg << endl;
return 0;
}
bool fpeq(FloatType a, FloatType b) {
static const FloatType epsilon = 1e-6;
if (b==0) return fabs(a)<epsilon;
FloatType ratio = a/b;
return ratio>1-epsilon && ratio<1+epsilon;
}
// if vector is empty, fill it
// otherwise check the vectors match
int read_msc_header(ifstream& mscfile, vector<FloatType>& scales, int& ptnparams) {
int npts;
mscfile.read((char*)&npts,sizeof(npts));
if (npts<=0) {
cerr << "invalid msc file (negative or null number of points)" << endl;
exit(1);
}
int nscales_thisfile;
mscfile.read((char*)&nscales_thisfile, sizeof(nscales_thisfile));
if (nscales_thisfile<=0) {
cerr << "invalid msc file (negative or null number of scales)" << endl;
exit(1);
}
#ifndef MAX_SCALES_IN_MSC_FILE
#define MAX_SCALES_IN_MSC_FILE 1000000
#endif
if (nscales_thisfile>MAX_SCALES_IN_MSC_FILE) {
cerr << "This msc file claims to contain more than " << MAX_SCALES_IN_MSC_FILE << " scales. Aborting, this is probably a mistake. If not, simply recompile with a different value for MAX_SCALES_IN_MSC_FILE." << endl;
exit(1);
}
vector<FloatType> scales_thisfile(nscales_thisfile);
for (int si=0; si<nscales_thisfile; ++si) mscfile.read((char*)&scales_thisfile[si], sizeof(FloatType));
// all files must be consistant
if (scales.size() == 0) {
scales = scales_thisfile;
} else {
if (scales.size() != nscales_thisfile) {cerr<<"input file mismatch"<<endl; exit(1);}
for (int si=0; si<scales.size(); ++si) if (!fpeq(scales[si],scales_thisfile[si])) {cerr<<"input file mismatch"<<endl; exit(1);}
}
// TODO: check consistency of ptnparams
mscfile.read((char*)&ptnparams, sizeof(int));
return npts;
}
void read_msc_data(ifstream& mscfile, int nscales, int npts, sample_type* data, int ptnparams) {
for (int pt=0; pt<npts; ++pt) {
// we do not care for the point coordinates and other parameters
for (int i=0; i<ptnparams; ++i) {
FloatType param;
mscfile.read((char*)¶m, sizeof(FloatType));
}
for (int s=0; s<nscales; ++s) {
FloatType a,b;
mscfile.read((char*)(&a), sizeof(FloatType));
mscfile.read((char*)(&b), sizeof(FloatType));
FloatType c = 1 - a - b;
// project in the equilateral triangle a*(0,0) + b*(1,0) + c*(1/2,sqrt3/2)
// equivalently to the triangle formed by the three components unit vector
// (1,0,0), (0,1,0) and (0,0,1) when considering a,b,c in 3D
// so each a,b,c = dimensionality of the data is given equal weight
// is this necessary ? => not for linear classifiers, but plan ahead...
FloatType x = b + c / 2;
FloatType y = c * sqrt(3)/2;
(*data)(s*2) = x;
(*data)(s*2+1) = y;
}
// we do not care for number of neighbors and average dist between nearest neighbors
// TODO: take this info into account to weight the samples and improve the classifier
int fooi;
for (int i=0; i<nscales; ++i) mscfile.read((char*)&fooi, sizeof(int));
/* FloatType foof;
for (int i=0; i<nscales; ++i) mscfile.read((char*)&foof, sizeof(FloatType));*/
++data;
}
}
void GramSchmidt(dlib::matrix<dlib::matrix<double,0,1>,0,1>& basis, dlib::matrix<double,0,1>& newX) {
using namespace dlib;
// goal: find a basis so that the given vector is the new X
// principle: at least one basis vector is not orthogonal with newX (except if newX is null but we suppose this is not the case)
// => use the max dot product vector, and replace it by newX. this forms a set of
// linearly independent vectors.
// then apply the Gram-Schmidt process
int dim = basis.size();
double maxabsdp = -1; int selectedCoord = 0;
for (int i=0; i<dim; ++i) {
double absdp = fabs(dot(basis(i),newX));
if (absdp > maxabsdp) {
absdp = maxabsdp;
selectedCoord = i;
}
}
// swap basis vectors to use the selected coord as the X vector, then replaced by newX
basis(selectedCoord) = basis(0);
basis(0) = newX;
// Gram-Schmidt process to re-orthonormalise the basis.
// Thanks Wikipedia for the stabilized version
for (int j = 0; j < dim; ++j) {
for (int i = 0; i < j; ++i) {
basis(j) -= (dot(basis(j),basis(i)) / dot(basis(i),basis(i))) * basis(i);
}
basis(j) /= sqrt(dot(basis(j),basis(j)));
}
}
int dichosearch(const vector<double>& series, double x) {
int dichofirst = 0;
int dicholast = series.size();
int dichomed;
while (true) {
dichomed = (dichofirst + dicholast) / 2;
if (dichomed==dichofirst) break;
if (x==series[dichomed]) break;
if (x<series[dichomed]) { dicholast = dichomed; continue;}
dichofirst = dichomed;
}
return dichomed;
}
int main(int argc, char** argv) {
if (argc<5) return help();
int grid_size = 1;
int arg_shift = 0;
string first_arg = argv[1];
if (first_arg[0]=='=') {
grid_size = atoi(first_arg.substr(1).c_str());
if (grid_size<1) return help();
++arg_shift;
}
ofstream svgfile(argv[arg_shift+1]);
int arg_class1 = argc;
for (int argi = arg_shift+2; argi<argc; ++argi) if (!strcmp(argv[argi],":")) {
arg_class1 = argi+1;
break;
}
if (arg_class1>=argc) return help();
int arg_class2 = argc;
for (int argi = arg_class1+1; argi<argc; ++argi) if (!strcmp(argv[argi],"-")) {
arg_class2 = argi+1;
break;
}
if (arg_class2>=argc) return help();
sample_type undefsample;
int ptnparams;
cout << "Loading unlabeled files" << endl;
// neutral files, if any
int ndata_unlabeled = 0;
vector<FloatType> scales;
for (int argi = arg_shift+2; argi<arg_class1-1; ++argi) {
ifstream mscfile(argv[argi], ifstream::binary);
// read the file header
int npts = read_msc_header(mscfile, scales, ptnparams);
mscfile.close();
ndata_unlabeled += npts;
}
int nscales = scales.size();
int fdim = nscales * 2;
if (nscales) undefsample.set_size(fdim,1);
// fill data
vector<sample_type> data_unlabeled(ndata_unlabeled, undefsample);
int base_pt = 0;
for (int argi = arg_shift+2; argi<arg_class1-1; ++argi) {
ifstream mscfile(argv[argi], ifstream::binary);
// read the file header (again)
int npts = read_msc_header(mscfile, scales, ptnparams);
// read data
read_msc_data(mscfile,nscales,npts,&data_unlabeled[base_pt], ptnparams);
mscfile.close();
base_pt += npts;
}
cout << "Loading class files" << endl;
// class1 files
int ndata_class1 = 0;
for (int argi = arg_class1; argi<arg_class2-1; ++argi) {
ifstream mscfile(argv[argi], ifstream::binary);
int npts = read_msc_header(mscfile, scales, ptnparams);
mscfile.close();
ndata_class1 += npts;
}
// class2 files
int ndata_class2 = 0;
for (int argi = arg_class2; argi<argc; ++argi) {
ifstream mscfile(argv[argi], ifstream::binary);
int npts = read_msc_header(mscfile, scales, ptnparams);
mscfile.close();
ndata_class2 += npts;
}
nscales = scales.size(); // in case there is no unlabeled data
fdim = nscales * 2;
undefsample.set_size(fdim,1);
int nsamples = ndata_class1+ndata_class2;
vector<sample_type> samples(nsamples, undefsample);
vector<double> labels(nsamples, 0);
for (int i=0; i<ndata_class1; ++i) labels[i] = -1;
for (int i=ndata_class1; i<nsamples; ++i) labels[i] = 1;
base_pt = 0;
for (int argi = arg_class1; argi<arg_class2-1; ++argi) {
ifstream mscfile(argv[argi], ifstream::binary);
int npts = read_msc_header(mscfile, scales, ptnparams);
read_msc_data(mscfile,nscales,npts,&samples[base_pt], ptnparams);
mscfile.close();
base_pt += npts;
}
for (int argi = arg_class2; argi<argc; ++argi) {
ifstream mscfile(argv[argi], ifstream::binary);
int npts = read_msc_header(mscfile, scales, ptnparams);
read_msc_data(mscfile,nscales,npts,&samples[base_pt], ptnparams);
mscfile.close();
base_pt += npts;
}
cout << "Computing the two best projection directions" << endl;
LinearSVM classifier(grid_size);
// shuffle before cross-validating to spread instances of each class
dlib::randomize_samples(samples, labels);
FloatType nu = classifier.crossValidate(10, samples, labels);
cout << "Training" << endl;
classifier.train(10, nu, samples, labels);
// get the projections of each sample on the first classifier direction
vector<FloatType> proj1(nsamples);
for (int i=0; i<nsamples; ++i) proj1[i] = classifier.predict(samples[i]);
// we now have the first hyperplane and corresponding decision boundary
// projection onto the orthogonal subspace and repeat SVM to get a 2D plot
// The procedure is a bit like PCA except we seek the successive directions of maximal
// separability instead of maximal variance
// perform a real projection with reduced dimension to help the SVM a bit
dlib::matrix<dlib::matrix<double,0,1>,0,1> basis;
basis.set_size(fdim);
for (int i=0; i<fdim; ++i) {
basis(i).set_size(fdim);
for (int j=0; j<fdim; ++j) basis(i)(j) = 0;
basis(i)(i) = 1;
}
dlib::matrix<double,0,1> w_vect;
w_vect.set_size(fdim);
for (int i=0; i<fdim; ++i) w_vect(i) = classifier.weights[i];
GramSchmidt(basis,w_vect);
vector<sample_type> samples_reduced(nsamples);
for (int i=0; i<nsamples; ++i) samples_reduced[i].set_size(fdim-1);
// project the data onto the hyperplane so as to get the second direction
for (int si=0; si<nsamples; ++si) {
for(int i=1; i<fdim; ++i) samples_reduced[si](i-1) = dlib::dot(samples[si], basis(i));
}
// already shuffled, and do not change order for the proj1 anyway
LinearSVM ortho_classifier(grid_size);
nu = ortho_classifier.crossValidate(10, samples_reduced, labels);
cout << "Training" << endl;
ortho_classifier.train(10, nu, samples_reduced, labels);
// convert back the classifier weights into the original space
ortho_classifier.weights.resize(fdim+1);
ortho_classifier.weights[fdim] = ortho_classifier.weights[fdim-1];
for(int i=0; i<fdim; ++i) w_vect(i) = 0;
for(int i=1; i<fdim; ++i) w_vect += ortho_classifier.weights[i-1] * basis(i);
for(int i=0; i<fdim; ++i) ortho_classifier.weights[i] = w_vect(i);
vector<FloatType> proj2(nsamples);
for (int i=0; i<nsamples; ++i) proj2[i] = ortho_classifier.predict(samples[i]);
// compute the reference points for orienting the classifier boundaries
// pathological cases are possible where an arbitrary point in the (>0,>0)
// quadrant is not in the +1 class for example
// here, just use the mean of the classes
Point refpt_pos(0,0,0);
Point refpt_neg(0,0,0);
for (int i=0; i<nsamples; ++i) {
if (labels[i]>0) refpt_pos += Point(proj1[i], proj2[i], 1);
else refpt_neg += Point(proj1[i], proj2[i], 1);
}
refpt_pos /= refpt_pos.z;
refpt_neg /= refpt_neg.z;
FloatType scaleFactor = 0;
FloatType axis_scale_ratio = 1;
// Experimental: dilatation to highlight the internal data structure
if (true) {
Point2D e1(refpt_pos.x-refpt_neg.x, refpt_pos.y-refpt_neg.y);
e1/=e1.norm();
Point2D e2(-e1.y, e1.x);
Point2D ori = Point2D(refpt_pos.x+refpt_neg.x, refpt_pos.y+refpt_neg.y) * 0.5;
FloatType m11=0, m21=0, m12=0, m22=0; // m12, m22 null by construction
FloatType v11=0, v12=0, v21=0, v22=0;
for (int i=0; i<nsamples; ++i) {
Point2D p(proj1[i], proj2[i]);
p -= ori;
FloatType p1 = p.dot(e1);
FloatType p2 = p.dot(e2);
if (labels[i]<0) {
m11 += p1; v11 += p1*p1;
m12 += p2; v12 += p2*p2;
}
else {
m21 += p1; v21 += p1*p1;
m22 += p2; v22 += p2*p2;
}
}
m11 /= ndata_class1;
v11 = (v11 - m11*m11*ndata_class1) / (ndata_class1-1);
m21 /= ndata_class2;
v21 = (v21 - m21*m21*ndata_class2) / (ndata_class2-1);
m12 /= ndata_class1;
v12 = (v12 - m12*m12*ndata_class1) / (ndata_class1-1);
m22 /= ndata_class2;
v22 = (v22 - m22*m22*ndata_class2) / (ndata_class2-1);
double d1 = sqrt(v11/v12);
double d2 = sqrt(v21/v22);
axis_scale_ratio = sqrt(d1*d2);
using namespace dlib;
matrix<double,2,2> bd;
bd = e1.x, e1.y, e2.x/axis_scale_ratio, e2.y/axis_scale_ratio;
matrix<double,2,2> bi;
bi = e1.x, e2.x, e1.y, e2.y;
//matrix<double,2,2> c = bi * bd;
matrix<double,2,2> c = inv(trans(bd));
std::vector<double>& w1 = classifier.weights;
std::vector<double>& w2 = ortho_classifier.weights;
// first shift so the center of the figure is at the midpoint
w1[fdim] -= ori.x;
w2[fdim] -= ori.y;
// now transform / scale along e2
std::vector<double> wn1(fdim+1), wn2(fdim+1);
for (int i=0; i<=fdim; ++i) {
wn1[i] = c(0,0) * w1[i] + c(0,1) * w2[i];
wn2[i] = c(1,0) * w1[i] + c(1,1) * w2[i];
}
for (int i=0; i<=fdim; ++i) cout << wn1[i] << " "; cout << endl;
for (int i=0; i<=fdim; ++i) cout << wn2[i] << " "; cout << endl;
classifier.weights = wn1;
ortho_classifier.weights = wn2;
// reset projections
for (int i=0; i<nsamples; ++i) {
proj1[i] = classifier.predict(samples[i]);
proj2[i] = ortho_classifier.predict(samples[i]);
}
refpt_pos = 0;
refpt_neg = 0;
for (int i=0; i<nsamples; ++i) {
if (labels[i]>0) refpt_pos += Point(proj1[i], proj2[i], 1);
else refpt_neg += Point(proj1[i], proj2[i], 1);
}
refpt_pos /= refpt_pos.z;
refpt_neg /= refpt_neg.z;
scaleFactor = halfSvgSize / max(fabs(m11-sqrt(v11)*3),fabs(m21+sqrt(v21)*3));
}
FloatType xming = numeric_limits<FloatType>::max();
FloatType xmaxg = -numeric_limits<FloatType>::max();
FloatType yming = numeric_limits<FloatType>::max();
FloatType ymaxg = -numeric_limits<FloatType>::max();
for (int i=0; i<data_unlabeled.size(); ++i) {
FloatType x = classifier.predict(data_unlabeled[i]);
FloatType y = ortho_classifier.predict(data_unlabeled[i]);
xming = min(xming, x);
xmaxg = max(xmaxg, x);
yming = min(yming, y);
ymaxg = max(ymaxg, y);
}
FloatType xminc = numeric_limits<FloatType>::max();
FloatType xmaxc = -numeric_limits<FloatType>::max();
FloatType yminc = numeric_limits<FloatType>::max();
FloatType ymaxc = -numeric_limits<FloatType>::max();
for (int i=0; i<nsamples; ++i) {
xminc = min(xminc, proj1[i]);
xmaxc = max(xmaxc, proj1[i]);
yminc = min(yminc, proj2[i]);
ymaxc = max(ymaxc, proj2[i]);
}
xming = min(xming, xminc);
xmaxg = max(xmaxg, xmaxc);
yming = min(yming, yminc);
ymaxg = max(ymaxg, ymaxc);
FloatType minX = numeric_limits<FloatType>::max();
FloatType maxX = -minX;
FloatType minY = minX;
FloatType maxY = -minX;
for (int i=0; i<nsamples; ++i) {
minX = min(minX, proj1[i]);
maxX = max(maxX, proj1[i]);
minY = min(minY, proj2[i]);
maxY = max(maxY, proj2[i]);
}
FloatType absmaxXY = fabs(max(max(max(-minX,maxX),-minY),maxY));
if (scaleFactor==0) scaleFactor = halfSvgSize / absmaxXY;
else {
FloatType sf2 = halfSvgSize / absmaxXY;
if (scaleFactor<sf2) scaleFactor=sf2;
else absmaxXY = halfSvgSize / scaleFactor;
}
PointCloud<Point2D> cloud2D;
cloud2D.prepare(xming,xmaxg,yming,ymaxg,nsamples+data_unlabeled.size());
for (int i=0; i<data_unlabeled.size(); ++i) cloud2D.insert(Point2D(
classifier.predict(data_unlabeled[i]),
ortho_classifier.predict(data_unlabeled[i])
));
for (int i=0; i<nsamples; ++i) {
cloud2D.insert(Point2D(proj1[i],proj2[i]));
}
FloatType absxymax = fabs(max(max(max(-xming,xmaxg),-yming),ymaxg));
int nsearchpointm1 = 25;
// radius from probabilistic SVM, diameter = 90% chance of correct classif
FloatType radius = -log(1.0/0.9 - 1.0) / 2;
FloatType wx = 0, wy = 0, wc = 0, minspcx = 0, minspcy = 0;
if (ndata_unlabeled) {
int minsumd = numeric_limits<int>::max();
FloatType minvx = 0, minvy = 0;
cout << "Finding the line with least density" << flush;
for (int spci = 0; spci <= nsearchpointm1; ++spci) {
cout << "." << flush;
FloatType spcx = refpt_neg.x + spci * (refpt_pos.x - refpt_neg.x) / nsearchpointm1;
FloatType spcy = refpt_neg.y + spci * (refpt_pos.y - refpt_neg.y) / nsearchpointm1;
// now we swipe a decision boundary in each direction around the point
// and look for the lowest overall density along the boundary
int nsearchdir = 90; // each 2 degree, as we swipe from 0 to 180 (unoriented lines)
FloatType incr = max(xmaxg-xming, ymaxg-yming) / nsearchpointm1;
vector<FloatType> sumds(nsearchdir);
#pragma omp parallel for
for(int sd = 0; sd < nsearchdir; ++sd) {
// use the parametric P = P0 + alpha*V formulation of a line
// unit vector in the direction of the line
FloatType vx = cos(M_PI * sd / nsearchdir);
FloatType vy = sin(M_PI * sd / nsearchdir);
sumds[sd] = 0;
for(int sp = -nsearchpointm1/2; sp < nsearchpointm1/2; ++sp) {
int s = sp * incr;
FloatType x = vx * s + spcx;
FloatType y = vy * s + spcy;
vector<DistPoint<Point2D> > neighbors;
cloud2D.findNeighbors(back_inserter(neighbors), Point2D(x,y), radius);
sumds[sd] += neighbors.size();
}
}
for(int sd = 0; sd < nsearchdir; ++sd) {
if (sumds[sd]<minsumd) {
minsumd = sumds[sd];
minvx = cos(M_PI * sd / nsearchdir);
minvy = sin(M_PI * sd / nsearchdir);
minspcx = spcx;
minspcy = spcy;
}
}
}
cout << endl;
// so we finally have the decision boundary in this 2D space
// P = P0 + alpha * V : px-p0x = alpha * vx and py-p0y = alpha * vy,
// alpha = (px-p0x) / vx; // if vx is null see below
// py-p0y = (px-p0x) * vy / vx
// py = px * vy/vx + p0y - p0x * vy / vx
// px * vy/vx - py + p0y - p0x * vy / vx = 0
// equa: wx * px + wy * py + c = 0
// with: wx = vy/vx; wy = -1; c = p0y - p0x * vy / vx
// null vx just reverse roles as vy is then !=0 (v is unit vec)
if (minvx!=0) { wx = minvy / minvx; wy = -1; wc = minspcy - minspcx * wx;}
else {wx = -1; wy = minvx / minvy; wc = minspcx - minspcy * wy;}
} else {
Point2D c1(0,0), c2(0,0);
for (int i=0; i<nsamples; ++i) {
if (labels[i]<0) c1 += Point2D(proj1[i],proj2[i]);
else c2 += Point2D(proj1[i],proj2[i]);
}
c1 /= ndata_class1;
c2 /= ndata_class2;
Point2D w_vect = c2 - c1;
w_vect /= w_vect.norm();
Point2D w_orth(-w_vect.y,w_vect.x);
double cump_diff_min = 2.0;
double cba2_max = 0;
for(int sd = 1; sd < 180; ++sd) {
FloatType vx = cos(sd * M_PI / 180.0);
FloatType vy = sin(sd * M_PI / 180.0);
dlib::matrix<double,2,2> basis;
Point2D base_vec1 = w_vect;
Point2D base_vec2 = vx * w_vect + vy * w_orth;
basis(0,0) = base_vec1.x; basis(0,1) = base_vec2.x;
basis(1,0) = base_vec1.y; basis(1,1) = base_vec2.y;
basis = inv(basis);
dlib::matrix<double,2,1> P;
double m1 = 0, m2 = 0;
vector<double> p1, p2;
for (int i=0; i<nsamples; ++i) {
P(0) = proj1[i];
P(1) = proj2[i];
P = basis * P;
double d = P(0); // projection on w_vect along the slanted direction
if (labels[i]<0) {p1.push_back(d); m1+=d;}
else {p2.push_back(d); m2+=d;}
}
m1 /= ndata_class1;
m2 /= ndata_class2;
// search for optimal separation
bool reversed = false;
double n1 = ndata_class1, n2 = ndata_class2;
if (m1 > m2) {
reversed = true;
swap(m1,m2);
p1.swap(p2);
}
sort(p1.begin(), p1.end());
sort(p2.begin(), p2.end());
for (int i=0; i<=100; ++i) {
double pos = m1 + i * (m2 - m1) / 100.0;
int idx1 = dichosearch(p1, pos);
int idx2 = dichosearch(p2, pos);
double pr1 = idx1 / (double)ndata_class1;
double pr2 = 1.0 - idx2 / (double)ndata_class2;
//double cump_diff = fabs(pr1 - pr2);
//if (cump_diff < cump_diff_min) {cump_diff_min = cump_diff;
double cba2 = fabs(pr1 + pr2);
if (cba2 > cba2_max) {cba2_max = cba2;
double r = (pos - m1) / (m2 - m1);
if (reversed) r = 1.0 - r;
Point2D center = c1 + r * (c2 - c1);
minspcx = center.x;
minspcy = center.y;
wx = -base_vec2.y;
wy = base_vec2.x;
// wx * cx + wy * cy + wc = 0
wc = -wx * center.x - wy * center.y;
}
}
}
}
cout << "Drawing image" << endl;
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, svgSize, svgSize);
cairo_t *cr = cairo_create(surface);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_set_line_width(cr, 0);
cairo_rectangle(cr, 0, 0, svgSize, svgSize);
cairo_fill(cr);
cairo_stroke(cr);
cairo_set_line_width(cr, 1);
// cumulating transluscent points to easily get a density estimate
cairo_set_source_rgba(cr, 0.4, 0.4, 0.4, 0.1);
// Plot points
// first the unlabeled data, if any
for (int i=0; i<ndata_unlabeled; ++i) {
// we have to project this data as this was not done above
FloatType x = classifier.predict(data_unlabeled[i]) * scaleFactor + halfSvgSize;
FloatType y = halfSvgSize - ortho_classifier.predict(data_unlabeled[i]) * scaleFactor;
cairo_arc(cr, x, y, 0.5, 0, 2*M_PI);
cairo_stroke(cr);
}
// now plot the reference data. It is very well that it was randomised so we do not have one class on top of the other
for (int i=0; i<nsamples; ++i) {
FloatType x = proj1[i]*scaleFactor + halfSvgSize;
FloatType y = halfSvgSize - proj2[i]*scaleFactor;
if (labels[i]==1) cairo_set_source_rgba(cr, 1, 0, 0, 0.75);
else cairo_set_source_rgba(cr, 0, 0, 1, 0.75);
cairo_arc(cr, x, y, 0.5, 0, 2*M_PI);
cairo_stroke(cr);
}
// specify scales at the bottom-right of the image, in a less-used quadrant
cairo_set_source_rgb(cr, 0.25,0.25,0.25);
cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 12);
cairo_text_extents_t extents;
FloatType dprob = -log(1.0/0.95 - 1.0) * scaleFactor;
const char* text = "p(classif)>95%";
cairo_text_extents(cr, text, &extents);
cairo_move_to(cr, svgSize - dprob - 20 - extents.width - extents.x_bearing, svgSize - 15 - extents.height/2 - extents.y_bearing);
cairo_show_text(cr, text);
cairo_move_to(cr, svgSize - dprob - 10, svgSize - 15);
cairo_line_to(cr, svgSize - 10, svgSize - 15);
string s_axis_scale_ratio = boost::str(boost::format("x%.1f") % axis_scale_ratio);
cairo_text_extents(cr, s_axis_scale_ratio.c_str(), &extents);
cairo_move_to(cr, svgSize - 20 - extents.width - extents.x_bearing, svgSize - 30 - extents.height - extents.y_bearing );
cairo_show_text(cr, s_axis_scale_ratio.c_str());
cairo_move_to(cr, svgSize - 10, svgSize - dprob - 30);
cairo_line_to(cr, svgSize - 10, svgSize - 30);
cairo_stroke(cr);
// draw lines on top of points
double dashes[2];
dashes[0] = dashes[1] = svgSize*0.01;
cairo_set_dash(cr, dashes, 2, svgSize*0.005);
cairo_set_source_rgb(cr, 0.25,0.25,0.25);
cairo_move_to(cr, 0,halfSvgSize);
cairo_line_to(cr, svgSize,halfSvgSize);
cairo_move_to(cr, halfSvgSize,0);
cairo_line_to(cr, halfSvgSize,svgSize);
cairo_stroke(cr);
cout << "Writing the svg file" << endl;
// output the svg file
svgfile << "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\""<< svgSize << "\" height=\""<< svgSize <<"\" >" << endl;
// Save the classifier parameters as an SVG comment so we can find them back later on
// Use base64 encoded binary to preserve full precision
vector<char> binary_parameters(
sizeof(int)
+ nscales*sizeof(FloatType)
+ (fdim+1)*sizeof(FloatType)
+ (fdim+1)*sizeof(FloatType)
+ sizeof(FloatType)
+ sizeof(FloatType)
+ sizeof(int)
+ sizeof(FloatType)
);
int bpidx = 0;
memcpy(&binary_parameters[bpidx],&nscales,sizeof(int)); bpidx += sizeof(int);
for (int i=0; i<nscales; ++i) {
memcpy(&binary_parameters[bpidx],&scales[i],sizeof(FloatType));
bpidx += sizeof(FloatType);
}
// Projections on the two 2D axis
for (int i=0; i<=fdim; ++i) {
memcpy(&binary_parameters[bpidx],&classifier.weights[i],sizeof(FloatType));
bpidx += sizeof(FloatType);
}
for (int i=0; i<=fdim; ++i) {
memcpy(&binary_parameters[bpidx],&ortho_classifier.weights[i],sizeof(FloatType));
bpidx += sizeof(FloatType);
}
// boundaries
memcpy(&binary_parameters[bpidx],&absmaxXY,sizeof(FloatType)); bpidx += sizeof(FloatType);
// conversion from svg to 2D space
memcpy(&binary_parameters[bpidx],&scaleFactor,sizeof(FloatType)); bpidx += sizeof(FloatType);
memcpy(&binary_parameters[bpidx],&halfSvgSize,sizeof(int)); bpidx += sizeof(int);
// axis scale ratio
memcpy(&binary_parameters[bpidx],&axis_scale_ratio,sizeof(FloatType)); bpidx += sizeof(FloatType);
base64 codec;
int nbytes;
std::vector<char> base64commentdata(codec.get_max_encoded_size(binary_parameters.size()));
nbytes = codec.encode(&binary_parameters[0], binary_parameters.size(), &base64commentdata[0]);
nbytes += codec.encode_end(&base64commentdata[nbytes]);
// comments work well and do not introduce any artifact in the resulting SVG
// but sometimes they are not preserved... use a hidden text then as workaround
#ifdef CANUPO_NO_SVG_COMMENT
svgfile << "<text style=\"font-size:1px;fill:#ffffff;fill-opacity:0;stroke:none\" x=\"20\" y=\"20\">params=" << &base64commentdata[0] << "</text>" << endl;
#else
svgfile << "<!-- params " << &base64commentdata[0] << " -->" << endl;
#endif
/*#ifdef CANUPO_NO_PNG
string filename = argv[arg_shift+1];
filename.replace(filename.size()-3,3,"ppm");
ppmwrite(surface,filename.c_str());
svgfile << "<image xlink:href=\""<< filename << "\" width=\""<<svgSize<<"\" height=\""<<svgSize<<"\" x=\"0\" y=\"0\" style=\"z-index:0\" />" << endl;
#else
*/
//cairo_surface_write_to_png (surface, argv[arg_shift+1]);
std::vector<char> pngdata;
// pngdata.reserve(800*800*3); // need only large enough init size
// cairo_surface_write_to_png_stream(surface, png_copier, &pngdata);
surface_to_png(surface, pngdata);
// encode the png data into base64
std::vector<char> base64pngdata(codec.get_max_encoded_size(pngdata.size()));
codec.reset_encoder();
nbytes = codec.encode(&pngdata[0], pngdata.size(), &base64pngdata[0]);
nbytes += codec.encode_end(&base64pngdata[nbytes]);
// include the image inline
svgfile << "<image xlink:href=\"data:image/png;base64,"<< &base64pngdata[0]
<< "\" width=\""<<svgSize<<"\" height=\""<<svgSize<<"\" x=\"0\" y=\"0\" style=\"z-index:0\" />" << endl;
//#endif
// include the reference points
svgfile << "<circle cx=\""<< (refpt_pos.x*scaleFactor+halfSvgSize) <<"\" cy=\""<< (halfSvgSize-refpt_pos.y*scaleFactor) <<"\" r=\"2\" style=\"fill:none;stroke:#000000;stroke-width:1px;z-index:1;\" />" << endl;
svgfile << "<circle cx=\""<< (refpt_neg.x*scaleFactor+halfSvgSize) <<"\" cy=\""<< (halfSvgSize-refpt_neg.y*scaleFactor) <<"\" r=\"2\" style=\"fill:none;stroke:#000000;stroke-width:1px;z-index:1;\" />" << endl;
// plot decision boundary as a path
// xy space in plane => scale and then reverse
// first find homogeneous equa in the 2D space
// convert the decision boundary to SVG space
// ori: wx * x + wy * y + wc = 0
// xsvg = x * scaleFactor + halfSvgSize; => x = (xsvg - halfSvgSize) / scaleFactor
// ysvg = halfSvgSize - y * scaleFactor; => y = (halfSvgSize - ysvg) / scaleFactor
// wxsvg * xsvg + wysvg * ysvg + csvg = 0
// wx * x + wy * y + wc = 0
// wx * (xsvg - halfSvgSize) / scaleFactor + wy * (halfSvgSize - ysvg) / scaleFactor + wc = 0
// wx * (xsvg - halfSvgSize) + wy * (halfSvgSize - ysvg) + wc * scaleFactor = 0
FloatType wxsvg = wx;
FloatType wysvg = -wy;
FloatType csvg = (wy-wx)*halfSvgSize + wc * scaleFactor;
FloatType minspcxsvg = minspcx * scaleFactor + halfSvgSize;
FloatType minspcysvg = halfSvgSize - minspcy * scaleFactor;
// now intersect to find xminsvg, yminsvg, and so on
// some may be NaN
FloatType xsvgy0 = -csvg / wxsvg; // at ysvg = 0
FloatType ysvgx0 = -csvg / wysvg; // at xsvg = 0
// wxsvg * xsvg + wysvg * ysvg + csvg = 0
FloatType xsvgymax = (-csvg -wysvg*svgSize) / wxsvg; // at ysvg = svgSize
FloatType ysvgxmax = (-csvg -wxsvg*svgSize) / wysvg; // at xsvg = svgSize
// NaN comparisons always fail, so use only positive tests and this is OK
bool useLeft = (ysvgx0 >= 0) && (ysvgx0 <= svgSize);
bool useRight = (ysvgxmax >= 0) && (ysvgxmax <= svgSize);
bool useTop = (xsvgy0 >= 0) && (xsvgy0 <= svgSize);
bool useBottom = (xsvgymax >= 0) && (xsvgymax <= svgSize);
int sidescount = useLeft + useRight + useTop + useBottom;
vector<Point2D> path;
// if (sidescount==2) {
svgfile << "<path style=\"fill:none;stroke:#000000;stroke-width:1px;z-index:1;\" d=\"M ";
if (useLeft) {
svgfile << 0 << "," << ysvgx0 << " L " << minspcxsvg<<","<<minspcysvg<<" L ";
if (useTop) svgfile << xsvgy0 << "," << 0 << " ";
if (useRight) svgfile << svgSize << "," << ysvgxmax << " ";
if (useBottom) svgfile << xsvgymax << "," << svgSize << " ";
}
else if (useTop) {
svgfile << xsvgy0 << "," << 0 << " L " << minspcxsvg<<","<<minspcysvg<<" L ";
if (useRight) svgfile << svgSize << "," << ysvgxmax << " ";
if (useBottom) svgfile << xsvgymax << "," << svgSize << " ";
}
svgfile << "\" />" << endl;
// }
svgfile << "</svg>" << endl;
svgfile.close();
cairo_surface_destroy(surface);
cairo_destroy(cr);
return 0;
}