MCPcopy Create free account
hub / github.com/biometrics/openbr / BR_PROPERTY

Method BR_PROPERTY

openbr/plugins/classification/svm.cpp:80–171  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78 BR_PROPERTY(Kernel, kernel, Linear)
79 BR_PROPERTY(Type, type, C_SVC)
80 BR_PROPERTY(float, C, -1)
81 BR_PROPERTY(float, gamma, -1)
82 BR_PROPERTY(QString, inputVariable, "Label")
83 BR_PROPERTY(QString, outputVariable, "")
84 BR_PROPERTY(bool, returnDFVal, false)
85 BR_PROPERTY(int, termCriteria, 1000)
86 BR_PROPERTY(int, folds, 5)
87 BR_PROPERTY(bool, balanceFolds, false)
88
89 SVM svm;
90 QHash<QString, int> labelMap;
91 QHash<int, QVariant> reverseLookup;
92
93 void train(const TemplateList &_data)
94 {
95 Mat data = OpenCVUtils::toMat(_data.data());
96 Mat lab;
97 // If we are doing regression, the input variable should have float
98 // values
99 if (type == EPS_SVR || type == NU_SVR) {
100 lab = OpenCVUtils::toMat(File::get<float>(_data, inputVariable));
101 }
102 // If we are doing classification, we should be dealing with discrete
103 // values. Map them and store the mapping data
104 else {
105 QList<int> dataLabels = _data.indexProperty(inputVariable, labelMap, reverseLookup);
106 lab = OpenCVUtils::toMat(dataLabels);
107 }
108
109 if (data.type() != CV_32FC1)
110 qFatal("Expected single channel floating point training data.");
111
112 CvSVMParams params;
113 params.kernel_type = kernel;
114 params.svm_type = type;
115 params.p = 0.1;
116 params.nu = 0.5;
117 params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, termCriteria, FLT_EPSILON);
118
119 if ((C == -1) || ((gamma == -1) && (kernel == RBF))) {
120 try {
121 svm.train_auto(data, lab, Mat(), Mat(), params, folds,
122 CvSVM::get_default_grid(CvSVM::C),
123 CvSVM::get_default_grid(CvSVM::GAMMA),
124 CvSVM::get_default_grid(CvSVM::P),
125 CvSVM::get_default_grid(CvSVM::NU),
126 CvSVM::get_default_grid(CvSVM::COEF),
127 CvSVM::get_default_grid(CvSVM::DEGREE),
128 balanceFolds);
129 } catch (...) {
130 qWarning("Some classes do not contain sufficient examples or are not discriminative enough for accurate SVM classification.");
131 svm.train(data, lab, Mat(), Mat(), params);
132 }
133 } else {
134 params.C = C;
135 params.gamma = gamma;
136 svm.train(data, lab, Mat(), Mat(), params);
137 }

Callers

nothing calls this directly

Calls 8

storeModelFunction · 0.85
indexPropertyMethod · 0.80
predictMethod · 0.80
projectFunction · 0.70
dataMethod · 0.45
trainMethod · 0.45
sizeMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected