GPHIKRegressionMex.cpp 22 KB

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