20 m_Model(nullptr),m_Parameter(nullptr)
44 ReadYValues(&problem, Y);
45 ReadXValues(&problem, &xSpace,X);
46 ReadWValues(&problem);
48 const char * error_msg =
nullptr;
59 m_Model =
svm_train(&problem, m_Parameter);
70 mitkThrow() <<
"No Model is trained. Train or load a model before predicting new values.";
72 auto noOfPoints =
static_cast<int>(X.rows());
73 auto noOfFeatures =
static_cast<int>(X.cols());
75 Eigen::MatrixXi result(noOfPoints,1);
77 auto * xVector =
static_cast<svm_node *
>(malloc(
sizeof(
svm_node) * (noOfFeatures+1)));
78 for (
int point = 0; point < noOfPoints; ++point)
80 for (
int feature = 0; feature < noOfFeatures; ++feature)
82 xVector[feature].
index = feature+1;
83 xVector[feature].value = X(point, feature);
85 xVector[noOfFeatures].index = -1;
101 if(!this->
GetPropertyList()->Get(
"classifier.svm.nu",this->m_Parameter->
nu)) this->m_Parameter->
nu = 0.5;
103 if(!this->
GetPropertyList()->Get(
"classifier.svm.c",this->m_Parameter->
C)) this->m_Parameter->
C = 1.0;
104 if(!this->
GetPropertyList()->Get(
"classifier.svm.eps",this->m_Parameter->
eps)) this->m_Parameter->
eps = 1e-3;
105 if(!this->
GetPropertyList()->Get(
"classifier.svm.p",this->m_Parameter->
p)) this->m_Parameter->
p = 0.1;
144 this->
GetPropertyList()->SetIntProperty(
"classifier.svm.probability",val);
149 this->
GetPropertyList()->SetIntProperty(
"classifier.svm.shrinking",val);
154 this->
GetPropertyList()->SetIntProperty(
"classifier.svm.nr_weight",val);
179 this->
GetPropertyList()->SetDoubleProperty(
"classifier.svm.cache-size",val);
184 this->
GetPropertyList()->SetIntProperty(
"classifier.svm.svm-type",val);
189 this->
GetPropertyList()->SetIntProperty(
"classifier.svm.kernel-type",val);
199 this->
GetPropertyList()->SetDoubleProperty(
"classifier.svm.gamma",val);
204 this->
GetPropertyList()->SetDoubleProperty(
"classifier.svm.coef0",val);
209 if(this->m_Parameter ==
nullptr)
211 MITK_WARN(
"LibSVMClassifier") <<
"Parameters are not initialized. Please call ConvertParameter() first!";
219 str <<
"classifier.svm.svm-type\tNOT SET (default " << this->m_Parameter->
svm_type <<
")" <<
"\n";
221 str <<
"classifier.svm.svm-type\t" << this->m_Parameter->
svm_type <<
"\n";
224 str <<
"classifier.svm.kernel-type\tNOT SET (default " << this->m_Parameter->
kernel_type <<
")" <<
"\n";
226 str <<
"classifier.svm.kernel-type\t" << this->m_Parameter->
kernel_type <<
"\n";
229 str <<
"classifier.svm.degree\t\tNOT SET (default " << this->m_Parameter->
degree <<
")" <<
"\n";
231 str <<
"classifier.svm.degree\t\t" << this->m_Parameter->
degree <<
"\n";
234 str <<
"classifier.svm.gamma\t\tNOT SET (default " << this->m_Parameter->
gamma <<
")" <<
"\n";
236 str <<
"classifier.svm.gamma\t\t" << this->m_Parameter->
gamma <<
"\n";
239 str <<
"classifier.svm.coef0\t\tNOT SET (default " << this->m_Parameter->
coef0 <<
")" <<
"\n";
241 str <<
"classifier.svm.coef0\t\t" << this->m_Parameter->
coef0 <<
"\n";
244 str <<
"classifier.svm.nu\t\tNOT SET (default " << this->m_Parameter->
nu <<
")" <<
"\n";
246 str <<
"classifier.svm.nu\t\t" << this->m_Parameter->
nu <<
"\n";
249 str <<
"classifier.svm.cache-size\tNOT SET (default " << this->m_Parameter->
cache_size <<
")" <<
"\n";
251 str <<
"classifier.svm.cache-size\t" << this->m_Parameter->
cache_size <<
"\n";
254 str <<
"classifier.svm.c\t\tNOT SET (default " << this->m_Parameter->
C <<
")" <<
"\n";
256 str <<
"classifier.svm.c\t\t" << this->m_Parameter->
C <<
"\n";
259 str <<
"classifier.svm.eps\t\tNOT SET (default " << this->m_Parameter->
eps <<
")" <<
"\n";
261 str <<
"classifier.svm.eps\t\t" << this->m_Parameter->
eps <<
"\n";
264 str <<
"classifier.svm.p\t\tNOT SET (default " << this->m_Parameter->
p <<
")" <<
"\n";
266 str <<
"classifier.svm.p\t\t" << this->m_Parameter->
p <<
"\n";
269 str <<
"classifier.svm.shrinking\tNOT SET (default " << this->m_Parameter->
shrinking <<
")" <<
"\n";
271 str <<
"classifier.svm.shrinking\t" << this->m_Parameter->
shrinking <<
"\n";
274 str <<
"classifier.svm.probability\tNOT SET (default " << this->m_Parameter->
probability <<
")" <<
"\n";
276 str <<
"classifier.svm.probability\t" << this->m_Parameter->
probability <<
"\n";
279 str <<
"classifier.svm.nr-weight\tNOT SET (default " << this->m_Parameter->
nr_weight <<
")" <<
"\n";
281 str <<
"classifier.svm.nr-weight\t" << this->m_Parameter->
nr_weight <<
"\n";
285 void mitk::LibSVMClassifier::ReadXValues(
svm_problem * problem,
svm_node** xSpace,
const Eigen::MatrixXd &X)
287 auto noOfPoints =
static_cast<int>(X.rows());
288 auto features =
static_cast<int>(X.cols());
293 for (
int row = 0; row < noOfPoints; ++row)
295 for (
int col = 0; col <
features; ++col)
297 (*xSpace)[row*features + col].
index = col;
298 (*xSpace)[row*features + col].value = X(row,col);
300 (*xSpace)[row*features +
features].index = -1;
302 problem->
x[row] = &((*xSpace)[row*
features]);
306 void mitk::LibSVMClassifier::ReadYValues(
svm_problem * problem,
const Eigen::MatrixXi &Y)
308 problem->
l =
static_cast<int>(Y.rows());
309 problem->
y =
static_cast<double *
>(malloc(
sizeof(
double) * problem->
l));
311 for (
int i = 0; i < problem->
l; ++i)
313 problem->
y[i] = Y(i,0);
317 void mitk::LibSVMClassifier::ReadWValues(
svm_problem * problem)
320 int noOfPoints = problem->
l;
321 problem->
W =
static_cast<double *
>(malloc(
sizeof(
double) * noOfPoints));
325 for (
int i = 0; i < noOfPoints; ++i)
327 problem->
W[i] = W(i,0);
331 for (
int i = 0; i < noOfPoints; ++i)
virtual bool IsUsingPointWiseWeight()
IsUsingPointWiseWeight.
void SetProbability(int val)
void Train(const Eigen::MatrixXd &X, const Eigen::MatrixXi &Y) override
Build a forest of trees from the training set (X, y).
void svm_destroy_param(struct svm_parameter *param)
const char * svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param)
virtual Eigen::MatrixXd & GetPointWiseWeight()
GetPointWiseWeightCopy.
void SetCoef0(double val)
void PrintParameter(std::ostream &str)
void SetNrWeight(int val)
virtual void UsePointWiseWeight(bool value)
UsePointWiseWeight.
void SetShrinking(int val)
~LibSVMClassifier() override
void SetKernelType(int val)
mitk::PropertyList::Pointer GetPropertyList() const
Get the data's property list.
Eigen::MatrixXi Predict(const Eigen::MatrixXd &X) override
Predict class for X.
struct svm_model * svm_train(const struct svm_problem *prob, const struct svm_parameter *param)
double svm_predict(const struct svm_model *model, const struct svm_node *x)
virtual void SetPointWiseWeight(const Eigen::MatrixXd &W)
SetPointWiseWeight.
void SetGamma(double val)
void SetCacheSize(double val)
void svm_free_and_destroy_model(struct svm_model **model_ptr_ptr)