Commit 914912b1e997e0fbd2f6e012ade81b51cb4ba5e9

Authored by Scott Klum
1 parent ef7897a4

Memory leaks fixed, introducing enum for solver

Showing 1 changed file with 27 additions and 26 deletions
openbr/plugins/liblinear.cpp
@@ -16,12 +16,9 @@ namespace br @@ -16,12 +16,9 @@ namespace br
16 class LinearSVM : public Transform 16 class LinearSVM : public Transform
17 { 17 {
18 Q_OBJECT 18 Q_OBJECT
19 - Q_ENUMS(Kernel)  
20 - Q_ENUMS(Type)  
21 - Q_PROPERTY(Kernel kernel READ get_kernel WRITE set_kernel RESET reset_kernel STORED false)  
22 - Q_PROPERTY(Type type READ get_type WRITE set_type RESET reset_type STORED false) 19 + Q_ENUMS(Solver)
  20 + Q_PROPERTY(Solver solver READ get_solver WRITE set_solver RESET reset_solver STORED false)
23 Q_PROPERTY(float C READ get_C WRITE set_C RESET reset_C STORED false) 21 Q_PROPERTY(float C READ get_C WRITE set_C RESET reset_C STORED false)
24 - Q_PROPERTY(float gamma READ get_gamma WRITE set_gamma RESET reset_gamma STORED false)  
25 Q_PROPERTY(QString inputVariable READ get_inputVariable WRITE set_inputVariable RESET reset_inputVariable STORED false) 22 Q_PROPERTY(QString inputVariable READ get_inputVariable WRITE set_inputVariable RESET reset_inputVariable STORED false)
26 Q_PROPERTY(QString outputVariable READ get_outputVariable WRITE set_outputVariable RESET reset_outputVariable STORED false) 23 Q_PROPERTY(QString outputVariable READ get_outputVariable WRITE set_outputVariable RESET reset_outputVariable STORED false)
27 Q_PROPERTY(bool returnDFVal READ get_returnDFVal WRITE set_returnDFVal RESET reset_returnDFVal STORED false) 24 Q_PROPERTY(bool returnDFVal READ get_returnDFVal WRITE set_returnDFVal RESET reset_returnDFVal STORED false)
@@ -30,22 +27,21 @@ class LinearSVM : public Transform @@ -30,22 +27,21 @@ class LinearSVM : public Transform
30 Q_PROPERTY(bool balanceFolds READ get_balanceFolds WRITE set_balanceFolds RESET reset_balanceFolds STORED false) 27 Q_PROPERTY(bool balanceFolds READ get_balanceFolds WRITE set_balanceFolds RESET reset_balanceFolds STORED false)
31 28
32 public: 29 public:
33 - enum Kernel { Linear = CvSVM::LINEAR,  
34 - Poly = CvSVM::POLY,  
35 - RBF = CvSVM::RBF,  
36 - Sigmoid = CvSVM::SIGMOID };  
37 -  
38 - enum Type { C_SVC = CvSVM::C_SVC,  
39 - NU_SVC = CvSVM::NU_SVC,  
40 - ONE_CLASS = CvSVM::ONE_CLASS,  
41 - EPS_SVR = CvSVM::EPS_SVR,  
42 - NU_SVR = CvSVM::NU_SVR}; 30 + enum Solver { L2R_LR,
  31 + L2R_L2LOSS_SVC_DUAL,
  32 + L2R_L2LOSS_SVC,
  33 + L2R_L1LOSS_SVC_DUAL,
  34 + MCSVM_CS,
  35 + L1R_L2LOSS_SVC,
  36 + L1R_LR,
  37 + L2R_LR_DUAL,
  38 + L2R_L2LOSS_SVR,
  39 + L2R_L2LOSS_SVR_DUAL,
  40 + L2R_L1LOSS_SVR_DUAL };
43 41
44 private: 42 private:
45 - BR_PROPERTY(Kernel, kernel, Linear)  
46 - BR_PROPERTY(Type, type, C_SVC)  
47 - BR_PROPERTY(float, C, -1)  
48 - BR_PROPERTY(float, gamma, -1) 43 + BR_PROPERTY(Solver, solver, L2R_L2LOSS_SVC_DUAL)
  44 + BR_PROPERTY(float, C, 1)
49 BR_PROPERTY(QString, inputVariable, "Label") 45 BR_PROPERTY(QString, inputVariable, "Label")
50 BR_PROPERTY(QString, outputVariable, "") 46 BR_PROPERTY(QString, outputVariable, "")
51 BR_PROPERTY(bool, returnDFVal, false) 47 BR_PROPERTY(bool, returnDFVal, false)
@@ -88,9 +84,12 @@ private: @@ -88,9 +84,12 @@ private:
88 } 84 }
89 85
90 parameter param; 86 parameter param;
91 - param.C = 1; 87 + // TODO: Support grid search
  88 + param.C = C;
92 param.eps = FLT_EPSILON; 89 param.eps = FLT_EPSILON;
93 - param.solver_type = L2R_L2LOSS_SVC_DUAL; 90 + param.solver_type = solver;
  91 +
  92 + // TODO: Support weights
94 param.nr_weight = 0; 93 param.nr_weight = 0;
95 param.p = 1; 94 param.p = 1;
96 param.weight_label = NULL; 95 param.weight_label = NULL;
@@ -98,9 +97,9 @@ private: @@ -98,9 +97,9 @@ private:
98 97
99 m = train_svm(&prob, &param); 98 m = train_svm(&prob, &param);
100 99
101 - delete x_space;  
102 - delete prob.x;  
103 - delete prob.y; 100 + delete[] prob.y;
  101 + delete[] prob.x;
  102 + delete[] x_space;
104 } 103 }
105 104
106 void project(const Template &src, Template &dst) const 105 void project(const Template &src, Template &dst) const
@@ -108,7 +107,7 @@ private: @@ -108,7 +107,7 @@ private:
108 dst = src; 107 dst = src;
109 108
110 Mat sample = src.m().reshape(1,1); 109 Mat sample = src.m().reshape(1,1);
111 - feature_node *x_space = new feature_node[sample.cols]; 110 + feature_node *x_space = new feature_node[sample.cols+1];
112 111
113 // Assign the address of the ith instance to be the address of the jth feature 112 // Assign the address of the ith instance to be the address of the jth feature
114 for (int j=0; j<sample.cols; j++) { 113 for (int j=0; j<sample.cols; j++) {
@@ -117,12 +116,14 @@ private: @@ -117,12 +116,14 @@ private:
117 } 116 }
118 x_space[sample.cols].index = -1; 117 x_space[sample.cols].index = -1;
119 118
  119 + // TODO: Call appropriate function based on solver
120 double prob_estimates[1]; 120 double prob_estimates[1];
121 float prediction = predict_values(m,x_space,prob_estimates); 121 float prediction = predict_values(m,x_space,prob_estimates);
122 122
123 - delete x_space;  
124 dst.m() = Mat(1, 1, CV_32F); 123 dst.m() = Mat(1, 1, CV_32F);
125 dst.m().at<float>(0, 0) = prob_estimates[0]; 124 dst.m().at<float>(0, 0) = prob_estimates[0];
  125 +
  126 + delete[] x_space;
126 } 127 }
127 128
128 void store(QDataStream &stream) const 129 void store(QDataStream &stream) const