old_ml_precomp.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // Intel License Agreement
  11. //
  12. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  13. // Third party copyrights are property of their respective owners.
  14. //
  15. // Redistribution and use in source and binary forms, with or without modification,
  16. // are permitted provided that the following conditions are met:
  17. //
  18. // * Redistribution's of source code must retain the above copyright notice,
  19. // this list of conditions and the following disclaimer.
  20. //
  21. // * Redistribution's in binary form must reproduce the above copyright notice,
  22. // this list of conditions and the following disclaimer in the documentation
  23. // and/or other materials provided with the distribution.
  24. //
  25. // * The name of Intel Corporation may not be used to endorse or promote products
  26. // derived from this software without specific prior written permission.
  27. //
  28. // This software is provided by the copyright holders and contributors "as is" and
  29. // any express or implied warranties, including, but not limited to, the implied
  30. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  31. // In no event shall the Intel Corporation or contributors be liable for any direct,
  32. // indirect, incidental, special, exemplary, or consequential damages
  33. // (including, but not limited to, procurement of substitute goods or services;
  34. // loss of use, data, or profits; or business interruption) however caused
  35. // and on any theory of liability, whether in contract, strict liability,
  36. // or tort (including negligence or otherwise) arising in any way out of
  37. // the use of this software, even if advised of the possibility of such damage.
  38. //
  39. //M*/
  40. #ifndef OPENCV_PRECOMP_H
  41. #define OPENCV_PRECOMP_H
  42. #include "opencv2/core.hpp"
  43. #include "old_ml.hpp"
  44. #include "opencv2/core/core_c.h"
  45. #include "opencv2/core/utility.hpp"
  46. #include "opencv2/core/private.hpp"
  47. #include <assert.h>
  48. #include <float.h>
  49. #include <limits.h>
  50. #include <math.h>
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #include <string.h>
  54. #include <time.h>
  55. #define ML_IMPL CV_IMPL
  56. #define __BEGIN__ __CV_BEGIN__
  57. #define __END__ __CV_END__
  58. #define EXIT __CV_EXIT__
  59. #define CV_MAT_ELEM_FLAG( mat, type, comp, vect, tflag ) \
  60. (( tflag == CV_ROW_SAMPLE ) \
  61. ? (CV_MAT_ELEM( mat, type, comp, vect )) \
  62. : (CV_MAT_ELEM( mat, type, vect, comp )))
  63. /* Convert matrix to vector */
  64. #define ICV_MAT2VEC( mat, vdata, vstep, num ) \
  65. if( MIN( (mat).rows, (mat).cols ) != 1 ) \
  66. CV_ERROR( CV_StsBadArg, "" ); \
  67. (vdata) = ((mat).data.ptr); \
  68. if( (mat).rows == 1 ) \
  69. { \
  70. (vstep) = CV_ELEM_SIZE( (mat).type ); \
  71. (num) = (mat).cols; \
  72. } \
  73. else \
  74. { \
  75. (vstep) = (mat).step; \
  76. (num) = (mat).rows; \
  77. }
  78. /* get raw data */
  79. #define ICV_RAWDATA( mat, flags, rdata, sstep, cstep, m, n ) \
  80. (rdata) = (mat).data.ptr; \
  81. if( CV_IS_ROW_SAMPLE( flags ) ) \
  82. { \
  83. (sstep) = (mat).step; \
  84. (cstep) = CV_ELEM_SIZE( (mat).type ); \
  85. (m) = (mat).rows; \
  86. (n) = (mat).cols; \
  87. } \
  88. else \
  89. { \
  90. (cstep) = (mat).step; \
  91. (sstep) = CV_ELEM_SIZE( (mat).type ); \
  92. (n) = (mat).rows; \
  93. (m) = (mat).cols; \
  94. }
  95. #define ICV_IS_MAT_OF_TYPE( mat, mat_type) \
  96. (CV_IS_MAT( mat ) && CV_MAT_TYPE( mat->type ) == (mat_type) && \
  97. (mat)->cols > 0 && (mat)->rows > 0)
  98. /*
  99. uchar* data; int sstep, cstep; - trainData->data
  100. uchar* classes; int clstep; int ncl;- trainClasses
  101. uchar* tmask; int tmstep; int ntm; - typeMask
  102. uchar* missed;int msstep, mcstep; -missedMeasurements...
  103. int mm, mn; == m,n == size,dim
  104. uchar* sidx;int sistep; - sampleIdx
  105. uchar* cidx;int cistep; - compIdx
  106. int k, l; == n,m == dim,size (length of cidx, sidx)
  107. int m, n; == size,dim
  108. */
  109. #define ICV_DECLARE_TRAIN_ARGS() \
  110. uchar* data; \
  111. int sstep, cstep; \
  112. uchar* classes; \
  113. int clstep; \
  114. int ncl; \
  115. uchar* tmask; \
  116. int tmstep; \
  117. int ntm; \
  118. uchar* missed; \
  119. int msstep, mcstep; \
  120. int mm, mn; \
  121. uchar* sidx; \
  122. int sistep; \
  123. uchar* cidx; \
  124. int cistep; \
  125. int k, l; \
  126. int m, n; \
  127. \
  128. data = classes = tmask = missed = sidx = cidx = NULL; \
  129. sstep = cstep = clstep = ncl = tmstep = ntm = msstep = mcstep = mm = mn = 0; \
  130. sistep = cistep = k = l = m = n = 0;
  131. #define ICV_TRAIN_DATA_REQUIRED( param, flags ) \
  132. if( !ICV_IS_MAT_OF_TYPE( (param), CV_32FC1 ) ) \
  133. { \
  134. CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
  135. } \
  136. else \
  137. { \
  138. ICV_RAWDATA( *(param), (flags), data, sstep, cstep, m, n ); \
  139. k = n; \
  140. l = m; \
  141. }
  142. #define ICV_TRAIN_CLASSES_REQUIRED( param ) \
  143. if( !ICV_IS_MAT_OF_TYPE( (param), CV_32FC1 ) ) \
  144. { \
  145. CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
  146. } \
  147. else \
  148. { \
  149. ICV_MAT2VEC( *(param), classes, clstep, ncl ); \
  150. if( m != ncl ) \
  151. { \
  152. CV_ERROR( CV_StsBadArg, "Unmatched sizes" ); \
  153. } \
  154. }
  155. #define ICV_ARG_NULL( param ) \
  156. if( (param) != NULL ) \
  157. { \
  158. CV_ERROR( CV_StsBadArg, #param " parameter must be NULL" ); \
  159. }
  160. #define ICV_MISSED_MEASUREMENTS_OPTIONAL( param, flags ) \
  161. if( param ) \
  162. { \
  163. if( !ICV_IS_MAT_OF_TYPE( param, CV_8UC1 ) ) \
  164. { \
  165. CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
  166. } \
  167. else \
  168. { \
  169. ICV_RAWDATA( *(param), (flags), missed, msstep, mcstep, mm, mn ); \
  170. if( mm != m || mn != n ) \
  171. { \
  172. CV_ERROR( CV_StsBadArg, "Unmatched sizes" ); \
  173. } \
  174. } \
  175. }
  176. #define ICV_COMP_IDX_OPTIONAL( param ) \
  177. if( param ) \
  178. { \
  179. if( !ICV_IS_MAT_OF_TYPE( param, CV_32SC1 ) ) \
  180. { \
  181. CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
  182. } \
  183. else \
  184. { \
  185. ICV_MAT2VEC( *(param), cidx, cistep, k ); \
  186. if( k > n ) \
  187. CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
  188. } \
  189. }
  190. #define ICV_SAMPLE_IDX_OPTIONAL( param ) \
  191. if( param ) \
  192. { \
  193. if( !ICV_IS_MAT_OF_TYPE( param, CV_32SC1 ) ) \
  194. { \
  195. CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
  196. } \
  197. else \
  198. { \
  199. ICV_MAT2VEC( *sampleIdx, sidx, sistep, l ); \
  200. if( l > m ) \
  201. CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
  202. } \
  203. }
  204. /****************************************************************************************/
  205. #define ICV_CONVERT_FLOAT_ARRAY_TO_MATRICE( array, matrice ) \
  206. { \
  207. CvMat a, b; \
  208. int dims = (matrice)->cols; \
  209. int nsamples = (matrice)->rows; \
  210. int type = CV_MAT_TYPE((matrice)->type); \
  211. int i, offset = dims; \
  212. \
  213. CV_ASSERT( type == CV_32FC1 || type == CV_64FC1 ); \
  214. offset *= ((type == CV_32FC1) ? sizeof(float) : sizeof(double));\
  215. \
  216. b = cvMat( 1, dims, CV_32FC1 ); \
  217. cvGetRow( matrice, &a, 0 ); \
  218. for( i = 0; i < nsamples; i++, a.data.ptr += offset ) \
  219. { \
  220. b.data.fl = (float*)array[i]; \
  221. CV_CALL( cvConvert( &b, &a ) ); \
  222. } \
  223. }
  224. /****************************************************************************************\
  225. * Auxiliary functions declarations *
  226. \****************************************************************************************/
  227. /* Generates a set of classes centers in quantity <num_of_clusters> that are generated as
  228. uniform random vectors in parallelepiped, where <data> is concentrated. Vectors in
  229. <data> should have horizontal orientation. If <centers> != NULL, the function doesn't
  230. allocate any memory and stores generated centers in <centers>, returns <centers>.
  231. If <centers> == NULL, the function allocates memory and creates the matrice. Centers
  232. are supposed to be oriented horizontally. */
  233. CvMat* icvGenerateRandomClusterCenters( int seed,
  234. const CvMat* data,
  235. int num_of_clusters,
  236. CvMat* centers CV_DEFAULT(0));
  237. /* Fills the <labels> using <probs> by choosing the maximal probability. Outliers are
  238. fixed by <oulier_tresh> and have cluster label (-1). Function also controls that there
  239. weren't "empty" clusters by filling empty clusters with the maximal probability vector.
  240. If probs_sums != NULL, fills it with the sums of probabilities for each sample (it is
  241. useful for normalizing probabilities' matrice of FCM) */
  242. void icvFindClusterLabels( const CvMat* probs, float outlier_thresh, float r,
  243. const CvMat* labels );
  244. typedef struct CvSparseVecElem32f
  245. {
  246. int idx;
  247. float val;
  248. }
  249. CvSparseVecElem32f;
  250. /* Prepare training data and related parameters */
  251. #define CV_TRAIN_STATMODEL_DEFRAGMENT_TRAIN_DATA 1
  252. #define CV_TRAIN_STATMODEL_SAMPLES_AS_ROWS 2
  253. #define CV_TRAIN_STATMODEL_SAMPLES_AS_COLUMNS 4
  254. #define CV_TRAIN_STATMODEL_CATEGORICAL_RESPONSE 8
  255. #define CV_TRAIN_STATMODEL_ORDERED_RESPONSE 16
  256. #define CV_TRAIN_STATMODEL_RESPONSES_ON_OUTPUT 32
  257. #define CV_TRAIN_STATMODEL_ALWAYS_COPY_TRAIN_DATA 64
  258. #define CV_TRAIN_STATMODEL_SPARSE_AS_SPARSE 128
  259. int
  260. cvPrepareTrainData( const char* /*funcname*/,
  261. const CvMat* train_data, int tflag,
  262. const CvMat* responses, int response_type,
  263. const CvMat* var_idx,
  264. const CvMat* sample_idx,
  265. bool always_copy_data,
  266. const float*** out_train_samples,
  267. int* _sample_count,
  268. int* _var_count,
  269. int* _var_all,
  270. CvMat** out_responses,
  271. CvMat** out_response_map,
  272. CvMat** out_var_idx,
  273. CvMat** out_sample_idx=0 );
  274. void
  275. cvSortSamplesByClasses( const float** samples, const CvMat* classes,
  276. int* class_ranges, const uchar** mask CV_DEFAULT(0) );
  277. void
  278. cvCombineResponseMaps (CvMat* _responses,
  279. const CvMat* old_response_map,
  280. CvMat* new_response_map,
  281. CvMat** out_response_map);
  282. void
  283. cvPreparePredictData( const CvArr* sample, int dims_all, const CvMat* comp_idx,
  284. int class_count, const CvMat* prob, float** row_sample,
  285. int as_sparse CV_DEFAULT(0) );
  286. /* copies clustering [or batch "predict"] results
  287. (labels and/or centers and/or probs) back to the output arrays */
  288. void
  289. cvWritebackLabels( const CvMat* labels, CvMat* dst_labels,
  290. const CvMat* centers, CvMat* dst_centers,
  291. const CvMat* probs, CvMat* dst_probs,
  292. const CvMat* sample_idx, int samples_all,
  293. const CvMat* comp_idx, int dims_all );
  294. #define cvWritebackResponses cvWritebackLabels
  295. #define XML_FIELD_NAME "_name"
  296. CvFileNode* icvFileNodeGetChild(CvFileNode* father, const char* name);
  297. CvFileNode* icvFileNodeGetChildArrayElem(CvFileNode* father, const char* name,int index);
  298. CvFileNode* icvFileNodeGetNext(CvFileNode* n, const char* name);
  299. void cvCheckTrainData( const CvMat* train_data, int tflag,
  300. const CvMat* missing_mask,
  301. int* var_all, int* sample_all );
  302. CvMat* cvPreprocessIndexArray( const CvMat* idx_arr, int data_arr_size, bool check_for_duplicates=false );
  303. CvMat* cvPreprocessVarType( const CvMat* type_mask, const CvMat* var_idx,
  304. int var_all, int* response_type );
  305. CvMat* cvPreprocessOrderedResponses( const CvMat* responses,
  306. const CvMat* sample_idx, int sample_all );
  307. CvMat* cvPreprocessCategoricalResponses( const CvMat* responses,
  308. const CvMat* sample_idx, int sample_all,
  309. CvMat** out_response_map, CvMat** class_counts=0 );
  310. const float** cvGetTrainSamples( const CvMat* train_data, int tflag,
  311. const CvMat* var_idx, const CvMat* sample_idx,
  312. int* _var_count, int* _sample_count,
  313. bool always_copy_data=false );
  314. namespace cv
  315. {
  316. struct DTreeBestSplitFinder
  317. {
  318. DTreeBestSplitFinder(){ splitSize = 0, tree = 0; node = 0; }
  319. DTreeBestSplitFinder( CvDTree* _tree, CvDTreeNode* _node);
  320. DTreeBestSplitFinder( const DTreeBestSplitFinder& finder, Split );
  321. virtual ~DTreeBestSplitFinder() {}
  322. virtual void operator()(const BlockedRange& range);
  323. void join( DTreeBestSplitFinder& rhs );
  324. Ptr<CvDTreeSplit> bestSplit;
  325. Ptr<CvDTreeSplit> split;
  326. int splitSize;
  327. CvDTree* tree;
  328. CvDTreeNode* node;
  329. };
  330. struct ForestTreeBestSplitFinder : DTreeBestSplitFinder
  331. {
  332. ForestTreeBestSplitFinder() : DTreeBestSplitFinder() {}
  333. ForestTreeBestSplitFinder( CvForestTree* _tree, CvDTreeNode* _node );
  334. ForestTreeBestSplitFinder( const ForestTreeBestSplitFinder& finder, Split );
  335. virtual void operator()(const BlockedRange& range);
  336. };
  337. }
  338. #endif /* __ML_H__ */