GPHIKClassifierMex.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /**
  2. * @file GPHIKClassifierMex.cpp
  3. * @author Alexander Freytag
  4. * @date 07-01-2014 (dd-mm-yyyy)
  5. * @brief Matlab-Interface of our GPHIKClassifier, allowing for training, classification, optimization, variance prediction, incremental learning, and storing/re-storing.
  6. */
  7. // STL includes
  8. #include <math.h>
  9. #include <matrix.h>
  10. #include <mex.h>
  11. // NICE-core includes
  12. #include <core/basics/Config.h>
  13. #include <core/basics/Timer.h>
  14. #include <core/vector/MatrixT.h>
  15. #include <core/vector/VectorT.h>
  16. // gp-hik-core includes
  17. #include "gp-hik-core/GPHIKClassifier.h"
  18. // Interface for conversion between Matlab and C objects
  19. #include "gp-hik-core/matlab/classHandleMtoC.h"
  20. #include "gp-hik-core/matlab/ConverterMatlabToNICE.h"
  21. #include "gp-hik-core/matlab/ConverterNICEToMatlab.h"
  22. const NICE::ConverterMatlabToNICE converterMtoNICE;
  23. const NICE::ConverterNICEToMatlab converterNICEtoM;
  24. using namespace std; //C basics
  25. using namespace NICE; // nice-core
  26. NICE::Config parseParametersGPHIKClassifier(const mxArray *prhs[], int nrhs)
  27. {
  28. NICE::Config conf;
  29. // if first argument is the filename of an existing config file,
  30. // read the config accordingly
  31. int i_start ( 0 );
  32. std::string variable = converterMtoNICE.convertMatlabToString(prhs[i_start]);
  33. if(variable == "conf")
  34. {
  35. conf = NICE::Config ( converterMtoNICE.convertMatlabToString( prhs[i_start+1] ) );
  36. i_start = i_start+2;
  37. }
  38. // now run over all given parameter specifications
  39. // and add them to the config
  40. for( int i=i_start; i < nrhs; i+=2 )
  41. {
  42. std::string variable = converterMtoNICE.convertMatlabToString(prhs[i]);
  43. /////////////////////////////////////////
  44. // READ STANDARD BOOLEAN VARIABLES
  45. /////////////////////////////////////////
  46. if( (variable == "verboseTime") || (variable == "verbose") ||
  47. (variable == "optimize_noise") || (variable == "uncertaintyPredictionForClassification") ||
  48. (variable == "use_quantization") || (variable == "ils_verbose")
  49. )
  50. {
  51. if ( mxIsChar( prhs[i+1] ) )
  52. {
  53. string value = converterMtoNICE.convertMatlabToString( prhs[i+1] );
  54. if ( (value != "true") && (value != "false") )
  55. {
  56. std::string errorMsg = "Unexpected parameter value for \'" + variable + "\'. In string modus, \'true\' or \'false\' expected.";
  57. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  58. }
  59. if( value == "true" )
  60. conf.sB("GPHIKClassifier", variable, true);
  61. else
  62. conf.sB("GPHIKClassifier", variable, false);
  63. }
  64. else if ( mxIsLogical( prhs[i+1] ) )
  65. {
  66. bool value = converterMtoNICE.convertMatlabToBool( prhs[i+1] );
  67. conf.sB("GPHIKClassifier", variable, value);
  68. }
  69. else
  70. {
  71. std::string errorMsg = "Unexpected parameter value for \'" + variable + "\'. \'true\', \'false\', or logical expected.";
  72. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  73. }
  74. }
  75. /////////////////////////////////////////
  76. // READ STANDARD INT VARIABLES
  77. /////////////////////////////////////////
  78. if ( (variable == "nrOfEigenvaluesToConsiderForVarApprox")
  79. )
  80. {
  81. if ( mxIsDouble( prhs[i+1] ) )
  82. {
  83. double value = converterMtoNICE.convertMatlabToDouble(prhs[i+1]);
  84. conf.sI("GPHIKClassifier", variable, (int) value);
  85. }
  86. else if ( mxIsInt32( prhs[i+1] ) )
  87. {
  88. int value = converterMtoNICE.convertMatlabToInt32(prhs[i+1]);
  89. conf.sI("GPHIKClassifier", variable, value);
  90. }
  91. else
  92. {
  93. std::string errorMsg = "Unexpected parameter value for \'" + variable + "\'. Int32 or Double expected.";
  94. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  95. }
  96. }
  97. /////////////////////////////////////////
  98. // READ STRICT POSITIVE INT VARIABLES
  99. /////////////////////////////////////////
  100. if ( (variable == "num_bins") || (variable == "ils_max_iterations")
  101. )
  102. {
  103. if ( mxIsDouble( prhs[i+1] ) )
  104. {
  105. double value = converterMtoNICE.convertMatlabToDouble(prhs[i+1]);
  106. if( value < 1 )
  107. {
  108. std::string errorMsg = "Expected parameter value larger than 0 for \'" + variable + "\'.";
  109. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  110. }
  111. conf.sI("GPHIKClassifier", variable, (int) value);
  112. }
  113. else if ( mxIsInt32( prhs[i+1] ) )
  114. {
  115. int value = converterMtoNICE.convertMatlabToInt32(prhs[i+1]);
  116. if( value < 1 )
  117. {
  118. std::string errorMsg = "Expected parameter value larger than 0 for \'" + variable + "\'.";
  119. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  120. }
  121. conf.sI("GPHIKClassifier", variable, value);
  122. }
  123. else
  124. {
  125. std::string errorMsg = "Unexpected parameter value for \'" + variable + "\'. Int32 or Double expected.";
  126. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  127. }
  128. }
  129. /////////////////////////////////////////
  130. // READ POSITIVE DOUBLE VARIABLES
  131. /////////////////////////////////////////
  132. if ( (variable == "ils_min_delta") || (variable == "ils_min_residual") ||
  133. (variable == "noise")
  134. )
  135. {
  136. if ( mxIsDouble( prhs[i+1] ) )
  137. {
  138. double value = converterMtoNICE.convertMatlabToDouble(prhs[i+1]);
  139. if( value < 0.0 )
  140. {
  141. std::string errorMsg = "Expected parameter value larger than 0 for \'" + variable + "\'.";
  142. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  143. }
  144. conf.sD("GPHIKClassifier", variable, value);
  145. }
  146. else
  147. {
  148. std::string errorMsg = "Unexpected parameter value for \'" + variable + "\'. Double expected.";
  149. mexErrMsgIdAndTxt( "mexnice:error", errorMsg.c_str() );
  150. }
  151. }
  152. /////////////////////////////////////////
  153. // READ REMAINING SPECIFIC VARIABLES
  154. /////////////////////////////////////////
  155. if(variable == "ils_method")
  156. {
  157. string value = converterMtoNICE.convertMatlabToString(prhs[i+1]);
  158. if(value != "CG" && value != "CGL" && value != "SYMMLQ" && value != "MINRES")
  159. mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'ils_method\'. \'CG\', \'CGL\', \'SYMMLQ\' or \'MINRES\' expected.");
  160. conf.sS("GPHIKClassifier", variable, value);
  161. }
  162. if(variable == "optimization_method")
  163. {
  164. string value = converterMtoNICE.convertMatlabToString(prhs[i+1]);
  165. if(value != "greedy" && value != "downhillsimplex" && value != "none")
  166. mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'optimization_method\'. \'greedy\', \'downhillsimplex\' or \'none\' expected.");
  167. conf.sS("GPHIKClassifier", variable, value);
  168. }
  169. if(variable == "transform")
  170. {
  171. string value = converterMtoNICE.convertMatlabToString( prhs[i+1] );
  172. if(value != "absexp" && value != "exp" && value != "MKL" && value != "WeightedDim")
  173. mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'transform\'. \'absexp\', \'exp\' , \'MKL\' or \'WeightedDim\' expected.");
  174. conf.sS("GPHIKClassifier", variable, value);
  175. }
  176. if(variable == "varianceApproximation")
  177. {
  178. string value = converterMtoNICE.convertMatlabToString(prhs[i+1]);
  179. if(value != "approximate_fine" && value != "approximate_rough" && value != "exact" && value != "none")
  180. mexErrMsgIdAndTxt("mexnice:error","Unexpected parameter value for \'varianceApproximation\'. \'approximate_fine\', \'approximate_rough\', \'none\' or \'exact\' expected.");
  181. conf.sS("GPHIKClassifier", variable, value);
  182. }
  183. }
  184. return conf;
  185. }
  186. // MAIN MATLAB FUNCTION
  187. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  188. {
  189. // get the command string specifying what to do
  190. if (nrhs < 1)
  191. mexErrMsgTxt("No commands and options passed... Aborting!");
  192. if( !mxIsChar( prhs[0] ) )
  193. mexErrMsgTxt("First argument needs to be the command, ie.e, the class method to call... Aborting!");
  194. std::string cmd = converterMtoNICE.convertMatlabToString( prhs[0] );
  195. // create object
  196. if ( !strcmp("new", cmd.c_str() ) )
  197. {
  198. // check output variable
  199. if (nlhs != 1)
  200. mexErrMsgTxt("New: One output expected.");
  201. // read config settings
  202. NICE::Config conf = parseParametersGPHIKClassifier(prhs+1,nrhs-1);
  203. // create class instance
  204. NICE::GPHIKClassifier * classifier = new NICE::GPHIKClassifier ( &conf, "GPHIKClassifier" /*sectionName in config*/ );
  205. // handle to the C++ instance
  206. plhs[0] = convertPtr2Mat<NICE::GPHIKClassifier>( classifier );
  207. return;
  208. }
  209. // in all other cases, there should be a second input,
  210. // which the be the class instance handle
  211. if (nrhs < 2)
  212. mexErrMsgTxt("Second input should be a class instance handle.");
  213. // delete object
  214. if ( !strcmp("delete", cmd.c_str() ) )
  215. {
  216. // Destroy the C++ object
  217. destroyObject<NICE::GPHIKClassifier>(prhs[1]);
  218. return;
  219. }
  220. // get the class instance pointer from the second input
  221. // every following function needs the classifier object
  222. NICE::GPHIKClassifier * classifier = convertMat2Ptr<NICE::GPHIKClassifier>(prhs[1]);
  223. ////////////////////////////////////////
  224. // Check which class method to call //
  225. ////////////////////////////////////////
  226. // standard train - assumes initialized object
  227. if (!strcmp("train", cmd.c_str() ))
  228. {
  229. // Check parameters
  230. if (nlhs < 0 || nrhs < 4)
  231. {
  232. mexErrMsgTxt("Train: Unexpected arguments.");
  233. }
  234. //------------- read the data --------------
  235. std::vector< const NICE::SparseVector *> examplesTrain;
  236. NICE::Vector yMultiTrain;
  237. if ( mxIsSparse( prhs[2] ) )
  238. {
  239. examplesTrain = converterMtoNICE.convertSparseMatrixToNice( prhs[2] );
  240. }
  241. else
  242. {
  243. NICE::Matrix dataTrain;
  244. dataTrain = converterMtoNICE.convertDoubleMatrixToNice(prhs[2]);
  245. //----------------- convert data to sparse data structures ---------
  246. examplesTrain.resize( dataTrain.rows() );
  247. std::vector< const NICE::SparseVector *>::iterator exTrainIt = examplesTrain.begin();
  248. for (int i = 0; i < (int)dataTrain.rows(); i++, exTrainIt++)
  249. {
  250. *exTrainIt = new NICE::SparseVector( dataTrain.getRow(i) );
  251. }
  252. }
  253. yMultiTrain = converterMtoNICE.convertDoubleVectorToNice(prhs[3]);
  254. //----------------- train our classifier -------------
  255. classifier->train ( examplesTrain , yMultiTrain );
  256. //----------------- clean up -------------
  257. for(int i=0;i<examplesTrain.size();i++)
  258. delete examplesTrain[i];
  259. return;
  260. }
  261. // Classify
  262. if ( !strcmp("classify", cmd.c_str() ) )
  263. {
  264. // Check parameters
  265. if ( (nlhs < 0) || (nrhs < 2) )
  266. {
  267. mexErrMsgTxt("Test: Unexpected arguments.");
  268. }
  269. //------------- read the data --------------
  270. int result;
  271. NICE::SparseVector scores;
  272. double uncertainty;
  273. if ( mxIsSparse( prhs[2] ) )
  274. {
  275. NICE::SparseVector * example;
  276. example = new NICE::SparseVector ( converterMtoNICE.convertSparseVectorToNice( prhs[2] ) );
  277. classifier->classify ( example, result, scores, uncertainty );
  278. //----------------- clean up -------------
  279. delete example;
  280. }
  281. else
  282. {
  283. NICE::Vector * example;
  284. example = new NICE::Vector ( converterMtoNICE.convertDoubleVectorToNice(prhs[2]) );
  285. classifier->classify ( example, result, scores, uncertainty );
  286. //----------------- clean up -------------
  287. delete example;
  288. }
  289. // output
  290. plhs[0] = mxCreateDoubleScalar( result );
  291. if(nlhs >= 2)
  292. {
  293. plhs[1] = converterNICEtoM.convertSparseVectorFromNice( scores, true /*b_adaptIndex*/);
  294. }
  295. if(nlhs >= 3)
  296. {
  297. plhs[2] = mxCreateDoubleScalar( uncertainty );
  298. }
  299. return;
  300. }
  301. // Uncertainty prediction
  302. if ( !strcmp("uncertainty", cmd.c_str() ) )
  303. {
  304. // Check parameters
  305. if ( (nlhs < 0) || (nrhs < 2) )
  306. {
  307. mexErrMsgTxt("Test: Unexpected arguments.");
  308. }
  309. double uncertainty;
  310. //------------- read the data --------------
  311. if ( mxIsSparse( prhs[2] ) )
  312. {
  313. NICE::SparseVector * example;
  314. example = new NICE::SparseVector ( converterMtoNICE.convertSparseVectorToNice( prhs[2] ) );
  315. classifier->predictUncertainty( example, uncertainty );
  316. //----------------- clean up -------------
  317. delete example;
  318. }
  319. else
  320. {
  321. NICE::Vector * example;
  322. example = new NICE::Vector ( converterMtoNICE.convertDoubleVectorToNice(prhs[2]) );
  323. classifier->predictUncertainty( example, uncertainty );
  324. //----------------- clean up -------------
  325. delete example;
  326. }
  327. // output
  328. plhs[0] = mxCreateDoubleScalar( uncertainty );
  329. return;
  330. }
  331. // Test - evaluate classifier on whole test set
  332. if ( !strcmp("test", cmd.c_str() ) )
  333. {
  334. // Check parameters
  335. if (nlhs < 0 || nrhs < 4)
  336. mexErrMsgTxt("Test: Unexpected arguments.");
  337. //------------- read the data --------------
  338. bool dataIsSparse ( mxIsSparse( prhs[2] ) );
  339. std::vector< const NICE::SparseVector *> dataTest_sparse;
  340. NICE::Matrix dataTest_dense;
  341. if ( dataIsSparse )
  342. {
  343. dataTest_sparse = converterMtoNICE.convertSparseMatrixToNice( prhs[2] );
  344. }
  345. else
  346. {
  347. dataTest_dense = converterMtoNICE.convertDoubleMatrixToNice(prhs[2]);
  348. }
  349. NICE::Vector yMultiTest;
  350. yMultiTest = converterMtoNICE.convertDoubleVectorToNice(prhs[3]);
  351. // ------------------------------------------
  352. // ------------- PREPARATION --------------
  353. // ------------------------------------------
  354. // determine classes known during training and corresponding mapping
  355. // thereby allow for non-continous class labels
  356. std::set<int> classesKnownTraining = classifier->getKnownClassNumbers();
  357. int noClassesKnownTraining ( classesKnownTraining.size() );
  358. std::map<int,int> mapClNoToIdxTrain;
  359. std::set<int>::const_iterator clTrIt = classesKnownTraining.begin();
  360. for ( int i=0; i < noClassesKnownTraining; i++, clTrIt++ )
  361. mapClNoToIdxTrain.insert ( std::pair<int,int> ( *clTrIt, i ) );
  362. // determine classes known during testing and corresponding mapping
  363. // thereby allow for non-continous class labels
  364. std::set<int> classesKnownTest;
  365. classesKnownTest.clear();
  366. // determine which classes we have in our label vector
  367. // -> MATLAB: myClasses = unique(y);
  368. for ( NICE::Vector::const_iterator it = yMultiTest.begin(); it != yMultiTest.end(); it++ )
  369. {
  370. if ( classesKnownTest.find ( *it ) == classesKnownTest.end() )
  371. {
  372. classesKnownTest.insert ( *it );
  373. }
  374. }
  375. int noClassesKnownTest ( classesKnownTest.size() );
  376. std::map<int,int> mapClNoToIdxTest;
  377. std::set<int>::const_iterator clTestIt = classesKnownTest.begin();
  378. for ( int i=0; i < noClassesKnownTest; i++, clTestIt++ )
  379. mapClNoToIdxTest.insert ( std::pair<int,int> ( *clTestIt, i ) );
  380. int i_numTestSamples;
  381. if ( dataIsSparse )
  382. i_numTestSamples = dataTest_sparse.size();
  383. else
  384. i_numTestSamples = (int) dataTest_dense.rows();
  385. NICE::Matrix confusionMatrix( noClassesKnownTraining, noClassesKnownTest, 0.0);
  386. NICE::Matrix scores( i_numTestSamples, noClassesKnownTraining, 0.0);
  387. // ------------------------------------------
  388. // ------------- CLASSIFICATION --------------
  389. // ------------------------------------------
  390. NICE::Timer t;
  391. double testTime (0.0);
  392. for (int i = 0; i < i_numTestSamples; i++)
  393. {
  394. //----------------- convert data to sparse data structures ---------
  395. int result;
  396. NICE::SparseVector exampleScoresSparse;
  397. if ( dataIsSparse )
  398. {
  399. // and classify
  400. t.start();
  401. classifier->classify( dataTest_sparse[ i ], result, exampleScoresSparse );
  402. t.stop();
  403. testTime += t.getLast();
  404. }
  405. else
  406. {
  407. NICE::Vector example ( dataTest_dense.getRow(i) );
  408. // and classify
  409. t.start();
  410. classifier->classify( &example, result, exampleScoresSparse );
  411. t.stop();
  412. testTime += t.getLast();
  413. }
  414. confusionMatrix( mapClNoToIdxTrain.find(result)->second, mapClNoToIdxTest.find(yMultiTest[i])->second ) += 1.0;
  415. int scoreCnt ( 0 );
  416. for ( NICE::SparseVector::const_iterator scoreIt = exampleScoresSparse.begin(); scoreIt != exampleScoresSparse.end(); scoreIt++, scoreCnt++ )
  417. {
  418. scores(i,scoreCnt) = scoreIt->second;
  419. }
  420. }
  421. std::cerr << "Time for testing: " << testTime << std::endl;
  422. // clean up
  423. if ( dataIsSparse )
  424. {
  425. for ( std::vector<const NICE::SparseVector *>::iterator it = dataTest_sparse.begin(); it != dataTest_sparse.end(); it++)
  426. delete *it;
  427. }
  428. confusionMatrix.normalizeColumnsL1();
  429. double recRate = confusionMatrix.trace()/confusionMatrix.cols();
  430. plhs[0] = mxCreateDoubleScalar( recRate );
  431. if(nlhs >= 2)
  432. plhs[1] = converterNICEtoM.convertMatrixFromNice(confusionMatrix);
  433. if(nlhs >= 3)
  434. plhs[2] = converterNICEtoM.convertMatrixFromNice(scores);
  435. return;
  436. }
  437. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  438. // interface specific methods for incremental extensions
  439. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  440. // addExample
  441. if ( !strcmp("addExample", cmd.c_str() ) )
  442. {
  443. // Check parameters
  444. if ( (nlhs < 0) || (nrhs < 4) )
  445. {
  446. mexErrMsgTxt("Test: Unexpected arguments.");
  447. }
  448. //------------- read the data --------------
  449. NICE::SparseVector * newExample;
  450. double newLabel;
  451. if ( mxIsSparse( prhs[2] ) )
  452. {
  453. newExample = new NICE::SparseVector ( converterMtoNICE.convertSparseVectorToNice( prhs[2] ) );
  454. }
  455. else
  456. {
  457. NICE::Vector * example;
  458. example = new NICE::Vector ( converterMtoNICE.convertDoubleVectorToNice(prhs[2]) );
  459. newExample = new NICE::SparseVector ( *example );
  460. //----------------- clean up -------------
  461. delete example;
  462. }
  463. newLabel = converterMtoNICE.convertMatlabToDouble( prhs[3] );
  464. // setting performOptimizationAfterIncrement is optional
  465. if ( nrhs > 4 )
  466. {
  467. bool performOptimizationAfterIncrement;
  468. performOptimizationAfterIncrement = converterMtoNICE.convertMatlabToBool( prhs[4] );
  469. classifier->addExample ( newExample, newLabel, performOptimizationAfterIncrement );
  470. }
  471. else
  472. {
  473. classifier->addExample ( newExample, newLabel );
  474. }
  475. //----------------- clean up -------------
  476. delete newExample;
  477. return;
  478. }
  479. // addMultipleExamples
  480. if ( !strcmp("addMultipleExamples", cmd.c_str() ) )
  481. {
  482. // Check parameters
  483. if ( (nlhs < 0) || (nrhs < 4) )
  484. {
  485. mexErrMsgTxt("Test: Unexpected arguments.");
  486. }
  487. //------------- read the data --------------
  488. std::vector< const NICE::SparseVector *> newExamples;
  489. NICE::Vector newLabels;
  490. if ( mxIsSparse( prhs[2] ) )
  491. {
  492. newExamples = converterMtoNICE.convertSparseMatrixToNice( prhs[2] );
  493. }
  494. else
  495. {
  496. NICE::Matrix newData;
  497. newData = converterMtoNICE.convertDoubleMatrixToNice(prhs[2]);
  498. //----------------- convert data to sparse data structures ---------
  499. newExamples.resize( newData.rows() );
  500. std::vector< const NICE::SparseVector *>::iterator exTrainIt = newExamples.begin();
  501. for (int i = 0; i < (int)newData.rows(); i++, exTrainIt++)
  502. {
  503. *exTrainIt = new NICE::SparseVector( newData.getRow(i) );
  504. }
  505. }
  506. newLabels = converterMtoNICE.convertDoubleVectorToNice(prhs[3]);
  507. // setting performOptimizationAfterIncrement is optional
  508. if ( nrhs > 4 )
  509. {
  510. bool performOptimizationAfterIncrement;
  511. performOptimizationAfterIncrement = converterMtoNICE.convertMatlabToBool( prhs[4] );
  512. classifier->addMultipleExamples ( newExamples, newLabels, performOptimizationAfterIncrement );
  513. }
  514. else
  515. {
  516. classifier->addMultipleExamples ( newExamples, newLabels );
  517. }
  518. //----------------- clean up -------------
  519. for ( std::vector< const NICE::SparseVector *>::iterator exIt = newExamples.begin();
  520. exIt != newExamples.end(); exIt++
  521. )
  522. {
  523. delete *exIt;
  524. }
  525. return;
  526. }
  527. ///////////////////// INTERFACE PERSISTENT /////////////////////
  528. // interface specific methods for store and restore
  529. ///////////////////// INTERFACE PERSISTENT /////////////////////
  530. // store the classifier to an external file
  531. if ( !strcmp("store", cmd.c_str() ) || !strcmp("save", cmd.c_str() ) )
  532. {
  533. // Check parameters
  534. if ( nrhs < 3 )
  535. mexErrMsgTxt("store: no destination given.");
  536. std::string s_destination = converterMtoNICE.convertMatlabToString( prhs[2] );
  537. std::filebuf fb;
  538. fb.open ( s_destination.c_str(), ios::out );
  539. std::ostream os(&fb);
  540. //
  541. classifier->store( os );
  542. //
  543. fb.close();
  544. return;
  545. }
  546. // load classifier from external file
  547. if ( !strcmp("restore", cmd.c_str() ) || !strcmp("load", cmd.c_str() ) )
  548. {
  549. // Check parameters
  550. if ( nrhs < 3 )
  551. mexErrMsgTxt("restore: no destination given.");
  552. std::string s_destination = converterMtoNICE.convertMatlabToString( prhs[2] );
  553. std::cerr << " aim at restoring the classifier from " << s_destination << std::endl;
  554. std::filebuf fbIn;
  555. fbIn.open ( s_destination.c_str(), ios::in );
  556. std::istream is (&fbIn);
  557. //
  558. classifier->restore( is );
  559. //
  560. fbIn.close();
  561. return;
  562. }
  563. // Got here, so command not recognized
  564. std::string errorMsg (cmd.c_str() );
  565. errorMsg += " -- command not recognized.";
  566. mexErrMsgTxt( errorMsg.c_str() );
  567. }