GPHIKRegressionMex.cpp 22 KB

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