GPHIKRegressionMex.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. const NICE::ConverterMatlabToNICE converterMtoNICE;
  23. const NICE::ConverterNICEToMatlab converterNICEtoM;
  24. using namespace std; //C basics
  25. using namespace NICE; // nice-core
  26. NICE::Config parseParametersGPHIKRegression(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 == "uncertaintyPredictionForRegression") ||
  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("GPHIKRegression", variable, true);
  61. else
  62. conf.sB("GPHIKRegression", variable, false);
  63. }
  64. else if ( mxIsLogical( prhs[i+1] ) )
  65. {
  66. bool value = converterMtoNICE.convertMatlabToBool( prhs[i+1] );
  67. conf.sB("GPHIKRegression", 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("GPHIKRegression", variable, (int) value);
  85. }
  86. else if ( mxIsInt32( prhs[i+1] ) )
  87. {
  88. int value = converterMtoNICE.convertMatlabToInt32(prhs[i+1]);
  89. conf.sI("GPHIKRegression", 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("GPHIKRegression", 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("GPHIKRegression", 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("GPHIKRegression", 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("GPHIKRegression", 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("GPHIKRegression", 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("GPHIKRegression", 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("GPHIKRegression", 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 = parseParametersGPHIKRegression(prhs+1,nrhs-1);
  203. // create class instance
  204. NICE::GPHIKRegression * regressor = new NICE::GPHIKRegression ( &conf, "GPHIKRegression" /*sectionName in config*/ );
  205. // handle to the C++ instance
  206. plhs[0] = convertPtr2Mat<NICE::GPHIKRegression>( regressor );
  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::GPHIKRegression>(prhs[1]);
  218. return;
  219. }
  220. // get the class instance pointer from the second input
  221. // every following function needs the regressor object
  222. NICE::GPHIKRegression * regressor = convertMat2Ptr<NICE::GPHIKRegression>(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 yValuesTrain;
  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. yValuesTrain = converterMtoNICE.convertDoubleVectorToNice(prhs[3]);
  254. //----------------- train our regressor -------------
  255. regressor->train ( examplesTrain , yValuesTrain );
  256. //----------------- clean up -------------
  257. for(int i=0;i<examplesTrain.size();i++)
  258. delete examplesTrain[i];
  259. return;
  260. }
  261. // perform regression
  262. if ( !strcmp("estimate", 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. double result;
  271. double uncertainty;
  272. if ( mxIsSparse( prhs[2] ) )
  273. {
  274. NICE::SparseVector * example;
  275. example = new NICE::SparseVector ( converterMtoNICE.convertSparseVectorToNice( prhs[2] ) );
  276. regressor->estimate ( example, result, uncertainty );
  277. //----------------- clean up -------------
  278. delete example;
  279. }
  280. else
  281. {
  282. NICE::Vector * example;
  283. example = new NICE::Vector ( converterMtoNICE.convertDoubleVectorToNice(prhs[2]) );
  284. regressor->estimate ( example, result, uncertainty );
  285. //----------------- clean up -------------
  286. delete example;
  287. }
  288. // output
  289. plhs[0] = mxCreateDoubleScalar( result );
  290. if(nlhs >= 2)
  291. {
  292. plhs[1] = mxCreateDoubleScalar( uncertainty );
  293. }
  294. return;
  295. }
  296. // Uncertainty prediction
  297. if ( !strcmp("uncertainty", cmd.c_str() ) )
  298. {
  299. // Check parameters
  300. if ( (nlhs < 0) || (nrhs < 2) )
  301. {
  302. mexErrMsgTxt("Test: Unexpected arguments.");
  303. }
  304. double uncertainty;
  305. //------------- read the data --------------
  306. if ( mxIsSparse( prhs[2] ) )
  307. {
  308. NICE::SparseVector * example;
  309. example = new NICE::SparseVector ( converterMtoNICE.convertSparseVectorToNice( prhs[2] ) );
  310. regressor->predictUncertainty( example, uncertainty );
  311. //----------------- clean up -------------
  312. delete example;
  313. }
  314. else
  315. {
  316. NICE::Vector * example;
  317. example = new NICE::Vector ( converterMtoNICE.convertDoubleVectorToNice(prhs[2]) );
  318. regressor->predictUncertainty( example, uncertainty );
  319. //----------------- clean up -------------
  320. delete example;
  321. }
  322. // output
  323. plhs[0] = mxCreateDoubleScalar( uncertainty );
  324. return;
  325. }
  326. // Test - evaluate regressor on whole test set
  327. if ( !strcmp("testL2loss", cmd.c_str() ) )
  328. {
  329. // Check parameters
  330. if (nlhs < 0 || nrhs < 3)
  331. mexErrMsgTxt("Test: Unexpected arguments.");
  332. //------------- read the data --------------
  333. bool dataIsSparse ( mxIsSparse( prhs[2] ) );
  334. std::vector< const NICE::SparseVector *> dataTest_sparse;
  335. NICE::Matrix dataTest_dense;
  336. if ( dataIsSparse )
  337. {
  338. dataTest_sparse = converterMtoNICE.convertSparseMatrixToNice( prhs[2] );
  339. }
  340. else
  341. {
  342. dataTest_dense = converterMtoNICE.convertDoubleMatrixToNice(prhs[2]);
  343. }
  344. NICE::Vector yValuesTest;
  345. yValuesTest = converterMtoNICE.convertDoubleVectorToNice(prhs[3]);
  346. int i_numTestSamples ( yValuesTest.size() );
  347. double l2loss ( 0.0 );
  348. NICE::Vector scores;
  349. NICE::Vector::iterator itScores;
  350. if ( nlhs >= 2 )
  351. {
  352. scores.resize( i_numTestSamples );
  353. itScores = scores.begin();
  354. }
  355. // ------------------------------------------
  356. // ------------- REGRESSION --------------
  357. // ------------------------------------------
  358. NICE::Timer t;
  359. double testTime (0.0);
  360. for (int i = 0; i < i_numTestSamples; i++)
  361. {
  362. //----------------- convert data to sparse data structures ---------
  363. double result;
  364. if ( dataIsSparse )
  365. {
  366. // and perform regression
  367. t.start();
  368. regressor->estimate( dataTest_sparse[ i ], result);
  369. t.stop();
  370. testTime += t.getLast();
  371. }
  372. else
  373. {
  374. NICE::Vector example ( dataTest_dense.getRow(i) );
  375. // and perform regression
  376. t.start();
  377. regressor->estimate( &example, result );
  378. t.stop();
  379. testTime += t.getLast();
  380. }
  381. l2loss += pow ( yValuesTest[i] - result, 2);
  382. if ( nlhs >= 2 )
  383. {
  384. *itScores = result;
  385. itScores++;
  386. }
  387. }
  388. std::cerr << "Time for testing: " << testTime << std::endl;
  389. // clean up
  390. if ( dataIsSparse )
  391. {
  392. for ( std::vector<const NICE::SparseVector *>::iterator it = dataTest_sparse.begin(); it != dataTest_sparse.end(); it++)
  393. delete *it;
  394. }
  395. plhs[0] = mxCreateDoubleScalar( l2loss );
  396. if(nlhs >= 2)
  397. plhs[1] = converterNICEtoM.convertVectorFromNice(scores);
  398. return;
  399. }
  400. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  401. // interface specific methods for incremental extensions
  402. ///////////////////// INTERFACE ONLINE LEARNABLE /////////////////////
  403. // addExample
  404. if ( !strcmp("addExample", cmd.c_str() ) )
  405. {
  406. // Check parameters
  407. if ( (nlhs < 0) || (nrhs < 4) )
  408. {
  409. mexErrMsgTxt("Test: Unexpected arguments.");
  410. }
  411. //------------- read the data --------------
  412. NICE::SparseVector * newExample;
  413. double newLabel;
  414. if ( mxIsSparse( prhs[2] ) )
  415. {
  416. newExample = new NICE::SparseVector ( converterMtoNICE.convertSparseVectorToNice( prhs[2] ) );
  417. }
  418. else
  419. {
  420. NICE::Vector * example;
  421. example = new NICE::Vector ( converterMtoNICE.convertDoubleVectorToNice(prhs[2]) );
  422. newExample = new NICE::SparseVector ( *example );
  423. //----------------- clean up -------------
  424. delete example;
  425. }
  426. newLabel = converterMtoNICE.convertMatlabToDouble( prhs[3] );
  427. // setting performOptimizationAfterIncrement is optional
  428. if ( nrhs > 4 )
  429. {
  430. bool performOptimizationAfterIncrement;
  431. performOptimizationAfterIncrement = converterMtoNICE.convertMatlabToBool( prhs[4] );
  432. regressor->addExample ( newExample, newLabel, performOptimizationAfterIncrement );
  433. }
  434. else
  435. {
  436. regressor->addExample ( newExample, newLabel );
  437. }
  438. //----------------- clean up -------------
  439. delete newExample;
  440. return;
  441. }
  442. // addMultipleExamples
  443. if ( !strcmp("addMultipleExamples", cmd.c_str() ) )
  444. {
  445. // Check parameters
  446. if ( (nlhs < 0) || (nrhs < 4) )
  447. {
  448. mexErrMsgTxt("Test: Unexpected arguments.");
  449. }
  450. //------------- read the data --------------
  451. std::vector< const NICE::SparseVector *> newExamples;
  452. NICE::Vector newLabels;
  453. if ( mxIsSparse( prhs[2] ) )
  454. {
  455. newExamples = converterMtoNICE.convertSparseMatrixToNice( prhs[2] );
  456. }
  457. else
  458. {
  459. NICE::Matrix newData;
  460. newData = converterMtoNICE.convertDoubleMatrixToNice(prhs[2]);
  461. //----------------- convert data to sparse data structures ---------
  462. newExamples.resize( newData.rows() );
  463. std::vector< const NICE::SparseVector *>::iterator exTrainIt = newExamples.begin();
  464. for (int i = 0; i < (int)newData.rows(); i++, exTrainIt++)
  465. {
  466. *exTrainIt = new NICE::SparseVector( newData.getRow(i) );
  467. }
  468. }
  469. newLabels = converterMtoNICE.convertDoubleVectorToNice(prhs[3]);
  470. // setting performOptimizationAfterIncrement is optional
  471. if ( nrhs > 4 )
  472. {
  473. bool performOptimizationAfterIncrement;
  474. performOptimizationAfterIncrement = converterMtoNICE.convertMatlabToBool( prhs[4] );
  475. regressor->addMultipleExamples ( newExamples, newLabels, performOptimizationAfterIncrement );
  476. }
  477. else
  478. {
  479. regressor->addMultipleExamples ( newExamples, newLabels );
  480. }
  481. //----------------- clean up -------------
  482. for ( std::vector< const NICE::SparseVector *>::iterator exIt = newExamples.begin();
  483. exIt != newExamples.end(); exIt++
  484. )
  485. {
  486. delete *exIt;
  487. }
  488. return;
  489. }
  490. ///////////////////// INTERFACE PERSISTENT /////////////////////
  491. // interface specific methods for store and restore
  492. ///////////////////// INTERFACE PERSISTENT /////////////////////
  493. // store the regressor to an external file
  494. if ( !strcmp("store", cmd.c_str() ) || !strcmp("save", cmd.c_str() ) )
  495. {
  496. // Check parameters
  497. if ( nrhs < 3 )
  498. mexErrMsgTxt("store: no destination given.");
  499. std::string s_destination = converterMtoNICE.convertMatlabToString( prhs[2] );
  500. std::filebuf fb;
  501. fb.open ( s_destination.c_str(), ios::out );
  502. std::ostream os(&fb);
  503. //
  504. regressor->store( os );
  505. //
  506. fb.close();
  507. return;
  508. }
  509. // load regressor from external file
  510. if ( !strcmp("restore", cmd.c_str() ) || !strcmp("load", cmd.c_str() ) )
  511. {
  512. // Check parameters
  513. if ( nrhs < 3 )
  514. mexErrMsgTxt("restore: no destination given.");
  515. std::string s_destination = converterMtoNICE.convertMatlabToString( prhs[2] );
  516. std::cerr << " aim at restoring the regressor from " << s_destination << std::endl;
  517. std::filebuf fbIn;
  518. fbIn.open ( s_destination.c_str(), ios::in );
  519. std::istream is (&fbIn);
  520. //
  521. regressor->restore( is );
  522. //
  523. fbIn.close();
  524. return;
  525. }
  526. // Got here, so command not recognized
  527. std::string errorMsg (cmd.c_str() );
  528. errorMsg += " -- command not recognized.";
  529. mexErrMsgTxt( errorMsg.c_str() );
  530. }