Commit 4f46567ad0b928395702b1f500bdd50525584734

Authored by Scott Klum
1 parent 74cfaa03

More

Showing 1 changed file with 38 additions and 28 deletions
openbr/core/boost.cpp
1 1 #include <opencv2/imgproc/imgproc.hpp>
  2 +#include <openbr/core/opencvutils.h>
2 3 #include <QDebug>
3 4  
4 5 #include "boost.h"
... ... @@ -132,10 +133,10 @@ static CvMat* cvPreprocessIndexArray( const CvMat* idx_arr, int data_arr_size, b
132 133  
133 134 //------------------------------------- FeatureEvaluator ---------------------------------------
134 135  
135   -void FeatureEvaluator::init(Representation *_representation, int _maxSampleCount)
  136 +void FeatureEvaluator::init(Representation *_representation, int maxSampleCount)
136 137 {
137 138 representation = _representation;
138   - cls.create( (int)_maxSampleCount, 1, CV_32FC1 );
  139 + cls.create(maxSampleCount, 1, CV_32FC1);
139 140 }
140 141  
141 142 void FeatureEvaluator::setImage(const Template &src, uchar clsLabel, int idx)
... ... @@ -174,6 +175,8 @@ struct CascadeBoostTrainData : CvDTreeTrainData
174 175 virtual void setData(const FeatureEvaluator* _featureEvaluator,
175 176 int _numSamples, int _precalcValBufSize, int _precalcIdxBufSize,
176 177 const CvDTreeParams& _params=CvDTreeParams());
  178 +
  179 + void initVarType();
177 180 void precalculate();
178 181  
179 182 virtual CvDTreeNode* subsample_data(const CvMat* _subsample_idx);
... ... @@ -371,6 +374,19 @@ CascadeBoostTrainData::CascadeBoostTrainData(const FeatureEvaluator* _featureEva
371 374 shared = true;
372 375 set_params( _params );
373 376 max_c_count = MAX( 2, featureEvaluator->getMaxCatCount() );
  377 +
  378 + initVarType();
  379 +
  380 + int maxSplitSize = cvAlign(sizeof(CvDTreeSplit) + (MAX(0,max_c_count - 33)/32)*sizeof(int),sizeof(void*));
  381 + int treeBlockSize = MAX((int)sizeof(CvDTreeNode)*8, maxSplitSize);
  382 + treeBlockSize = MAX(treeBlockSize + BlockSizeDelta, MinBlockSize);
  383 + tree_storage = cvCreateMemStorage( treeBlockSize );
  384 + node_heap = cvCreateSet( 0, sizeof(node_heap[0]), sizeof(CvDTreeNode), tree_storage );
  385 + split_heap = cvCreateSet( 0, sizeof(split_heap[0]), maxSplitSize, tree_storage );
  386 +}
  387 +
  388 +void CascadeBoostTrainData::initVarType()
  389 +{
374 390 var_type = cvCreateMat( 1, var_count + 2, CV_32SC(channels) );
375 391 if ( featureEvaluator->getMaxCatCount() > 0 )
376 392 {
... ... @@ -393,13 +409,6 @@ CascadeBoostTrainData::CascadeBoostTrainData(const FeatureEvaluator* _featureEva
393 409 }
394 410 var_type->data.i[var_count] = cat_var_count;
395 411 var_type->data.i[var_count+1] = cat_var_count+1;
396   -
397   - int maxSplitSize = cvAlign(sizeof(CvDTreeSplit) + (MAX(0,max_c_count - 33)/32)*sizeof(int),sizeof(void*));
398   - int treeBlockSize = MAX((int)sizeof(CvDTreeNode)*8, maxSplitSize);
399   - treeBlockSize = MAX(treeBlockSize + BlockSizeDelta, MinBlockSize);
400   - tree_storage = cvCreateMemStorage( treeBlockSize );
401   - node_heap = cvCreateSet( 0, sizeof(node_heap[0]), sizeof(CvDTreeNode), tree_storage );
402   - split_heap = cvCreateSet( 0, sizeof(split_heap[0]), maxSplitSize, tree_storage );
403 412 }
404 413  
405 414 CascadeBoostTrainData::CascadeBoostTrainData(const FeatureEvaluator* _featureEvaluator,
... ... @@ -450,15 +459,15 @@ void CascadeBoostTrainData::setData( const FeatureEvaluator* _featureEvaluator,
450 459 if (sample_count < 65536)
451 460 is_buf_16u = true;
452 461  
  462 + // 1048576 is the number of bytes in a megabyte
453 463 numPrecalcVal = min( cvRound((double)_precalcValBufSize*1048576. / (sizeof(float)*sample_count)), var_count );
454   - numPrecalcIdx = min( cvRound((double)_precalcIdxBufSize*1048576. /
455   - ((is_buf_16u ? sizeof(unsigned short) : sizeof (int))*sample_count)), var_count );
  464 + numPrecalcIdx = min( cvRound((double)_precalcIdxBufSize*1048576. / ((is_buf_16u ? sizeof(unsigned short) : sizeof (int))*sample_count)), var_count );
456 465  
457 466 assert( numPrecalcIdx >= 0 && numPrecalcVal >= 0 );
458 467  
459 468 valCache.create( numPrecalcVal, sample_count, CV_32FC1 );
460 469 var_type = cvCreateMat( 1, var_count + 2, CV_32SC(channels) );
461   -
  470 +
462 471 if ( featureEvaluator->getMaxCatCount() > 0 )
463 472 {
464 473 numPrecalcIdx = 0;
... ... @@ -480,13 +489,16 @@ void CascadeBoostTrainData::setData( const FeatureEvaluator* _featureEvaluator,
480 489 }
481 490 var_type->data.i[var_count] = cat_var_count;
482 491 var_type->data.i[var_count+1] = cat_var_count+1;
  492 +
  493 + initVarType();
  494 +
483 495 work_var_count = ( cat_var_count ? 0 : numPrecalcIdx ) + 1/*cv_lables*/;
484 496 buf_count = 2;
485 497  
486 498 buf_size = -1; // the member buf_size is obsolete
487 499  
488 500 effective_buf_size = (uint64)(work_var_count + 1)*(uint64)sample_count * buf_count; // this is the total size of "CvMat buf" to be allocated
489   -
  501 +
490 502 effective_buf_width = sample_count;
491 503 effective_buf_height = work_var_count+1;
492 504 if (effective_buf_width >= effective_buf_height)
... ... @@ -592,6 +604,7 @@ const int* CascadeBoostTrainData::get_sample_indices( CvDTreeNode* n, int* indic
592 604 indicesBuf[i] = short_values[i];
593 605 cat_values = indicesBuf;
594 606 }
  607 +
595 608 return cat_values;
596 609 }
597 610  
... ... @@ -682,7 +695,8 @@ void CascadeBoostTrainData::get_ord_var_data( CvDTreeNode* n, int vi, float* ord
682 695 const int* CascadeBoostTrainData::get_cat_var_data( CvDTreeNode* n, int vi, int* catValuesBuf )
683 696 {
684 697 int nodeSampleCount = n->sample_count;
685   - int* sampleIndicesBuf = catValuesBuf; //
  698 + int* sampleIndicesBuf = catValuesBuf;
  699 +
686 700 const int* sampleIndices = get_sample_indices(n, sampleIndicesBuf);
687 701  
688 702 if ( vi < numPrecalcVal )
... ... @@ -738,9 +752,9 @@ struct FeatureIdxOnlyPrecalc : ParallelLoopBody
738 752 *(idst + fi*sample_count + si) = si;
739 753 }
740 754 if ( is_buf_16u )
741   - icvSortUShAux( udst + fi*sample_count, sample_count, valCachePtr );
  755 + icvSortUShAux( udst + (uint64)fi*sample_count, sample_count, valCachePtr );
742 756 else
743   - icvSortIntAux( idst + fi*sample_count, sample_count, valCachePtr );
  757 + icvSortIntAux( idst + (uint64)fi*sample_count, sample_count, valCachePtr );
744 758 }
745 759 }
746 760 const FeatureEvaluator* featureEvaluator;
... ... @@ -810,6 +824,8 @@ void CascadeBoostTrainData::precalculate()
810 824 {
811 825 int minNum = MIN( numPrecalcVal, numPrecalcIdx);
812 826  
  827 + qDebug() << "Starting precalculation...";
  828 +
813 829 QTime time;
814 830 time.start();
815 831  
... ... @@ -931,10 +947,8 @@ void CascadeBoostTree::split_node_data( CvDTreeNode* node )
931 947 else
932 948 {
933 949 int *ldst, *rdst;
934   - ldst = buf->data.i + left->buf_idx*length_buf_row +
935   - vi*scount + left->offset;
936   - rdst = buf->data.i + right->buf_idx*length_buf_row +
937   - vi*scount + right->offset;
  950 + ldst = buf->data.i + left->buf_idx*length_buf_row + (uint64)(vi*scount) + left->offset;
  951 + rdst = buf->data.i + right->buf_idx*length_buf_row + (uint64)(vi*scount) + right->offset;
938 952  
939 953 // split sorted
940 954 for( int i = 0; i < n1; i++ )
... ... @@ -989,10 +1003,8 @@ void CascadeBoostTree::split_node_data( CvDTreeNode* node )
989 1003 }
990 1004 else
991 1005 {
992   - int *ldst = buf->data.i + left->buf_idx*length_buf_row +
993   - (workVarCount-1)*scount + left->offset;
994   - int *rdst = buf->data.i + right->buf_idx*length_buf_row +
995   - (workVarCount-1)*scount + right->offset;
  1006 + int *ldst = buf->data.i + left->buf_idx*length_buf_row + (uint64)(workVarCount-1)*scount + left->offset;
  1007 + int *rdst = buf->data.i + right->buf_idx*length_buf_row + (uint64)(workVarCount-1)*scount + right->offset;
996 1008  
997 1009 for( int i = 0; i < n; i++ )
998 1010 {
... ... @@ -1040,10 +1052,8 @@ void CascadeBoostTree::split_node_data( CvDTreeNode* node )
1040 1052 }
1041 1053 else
1042 1054 {
1043   - int* ldst = buf->data.i + left->buf_idx*length_buf_row +
1044   - workVarCount*scount + left->offset;
1045   - int* rdst = buf->data.i + right->buf_idx*length_buf_row +
1046   - workVarCount*scount + right->offset;
  1055 + int* ldst = buf->data.i + left->buf_idx*length_buf_row + (uint64)workVarCount*scount + left->offset;
  1056 + int* rdst = buf->data.i + right->buf_idx*length_buf_row + (uint64)workVarCount*scount + right->offset;
1047 1057 for (int i = 0; i < n; i++)
1048 1058 {
1049 1059 int idx = tempBuf[i];
... ...