Commit da980eca4c85876b4b00a83306a0ec3269657000
Merge branch 'boost'
Showing
1 changed file
with
83 additions
and
227 deletions
openbr/core/boost.cpp
| ... | ... | @@ -21,7 +21,6 @@ logRatio( double val ) |
| 21 | 21 | |
| 22 | 22 | #define CV_CMP_NUM_IDX(i,j) (aux[i] < aux[j]) |
| 23 | 23 | static CV_IMPLEMENT_QSORT_EX( icvSortIntAux, int, CV_CMP_NUM_IDX, const float* ) |
| 24 | -static CV_IMPLEMENT_QSORT_EX( icvSortUShAux, unsigned short, CV_CMP_NUM_IDX, const float* ) | |
| 25 | 24 | |
| 26 | 25 | #define CV_THRESHOLD_EPS (0.00001F) |
| 27 | 26 | |
| ... | ... | @@ -73,7 +72,7 @@ static CvMat* cvPreprocessIndexArray( const CvMat* idx_arr, int data_arr_size, b |
| 73 | 72 | |
| 74 | 73 | if( idx_selected == 0 ) |
| 75 | 74 | CV_ERROR( CV_StsOutOfRange, "No components/input_variables is selected!" ); |
| 76 | - } else if (type == CV_32SC(channels)) { | |
| 75 | + } else if (type == CV_32SC1) { | |
| 77 | 76 | // idx_arr is array of integer indices of selected components |
| 78 | 77 | if( idx_total > data_arr_size ) |
| 79 | 78 | CV_ERROR( CV_StsOutOfRange, |
| ... | ... | @@ -95,10 +94,10 @@ static CvMat* cvPreprocessIndexArray( const CvMat* idx_arr, int data_arr_size, b |
| 95 | 94 | "(it should be 8uC1, 8sC1 or 32sC1)" ); |
| 96 | 95 | } |
| 97 | 96 | |
| 98 | - CV_CALL( idx = cvCreateMat( 1, idx_selected, CV_32SC(channels) )); | |
| 97 | + CV_CALL( idx = cvCreateMat( 1, idx_selected, CV_32SC1 )); | |
| 99 | 98 | dsti = idx->data.i; |
| 100 | 99 | |
| 101 | - if( type < CV_32SC(channels) ) | |
| 100 | + if( type < CV_32SC1 ) | |
| 102 | 101 | { |
| 103 | 102 | for( i = 0; i < idx_total; i++ ) |
| 104 | 103 | if( srcb[i*step] ) |
| ... | ... | @@ -181,7 +180,12 @@ struct CascadeBoostTrainData : CvDTreeTrainData |
| 181 | 180 | |
| 182 | 181 | virtual CvDTreeNode* subsample_data(const CvMat* _subsample_idx); |
| 183 | 182 | |
| 184 | - virtual const int* getBufferValues(CvDTreeNode* n, int* labelsBuf, uint64_t vi); | |
| 183 | + inline uint64_t getLength() const | |
| 184 | + { | |
| 185 | + return ((uint64_t)work_var_count + 1) * sample_count; | |
| 186 | + } | |
| 187 | + | |
| 188 | + virtual const int* getBufferValues(CvDTreeNode* n, uint64_t vi); | |
| 185 | 189 | virtual const int* get_class_labels(CvDTreeNode* n, int* labelsBuf); |
| 186 | 190 | virtual const int* get_cv_labels(CvDTreeNode* n, int* labelsBuf); |
| 187 | 191 | virtual const int* get_sample_indices(CvDTreeNode* n, int* indicesBuf); |
| ... | ... | @@ -254,12 +258,12 @@ CvDTreeNode* CascadeBoostTrainData::subsample_data( const CvMat* _subsample_idx |
| 254 | 258 | int* sidx = isubsample_idx->data.i; |
| 255 | 259 | // co - array of count/offset pairs (to handle duplicated values in _subsample_idx) |
| 256 | 260 | int* co, cur_ofs = 0; |
| 257 | - int workVarCount = get_work_var_count(); | |
| 261 | + uint64_t workVarCount = get_work_var_count(); | |
| 258 | 262 | int count = isubsample_idx->rows + isubsample_idx->cols - 1; |
| 259 | 263 | |
| 260 | 264 | root = new_node( 0, count, 1, 0 ); |
| 261 | 265 | |
| 262 | - CV_Assert( (subsample_co = cvCreateMat( 1, sample_count*2, CV_32SC(channels) )) != 0); | |
| 266 | + CV_Assert( (subsample_co = cvCreateMat( 1, sample_count*2, CV_32SC1 )) != 0); | |
| 263 | 267 | cvZero( subsample_co ); |
| 264 | 268 | co = subsample_co->data.i; |
| 265 | 269 | for( int i = 0; i < count; i++ ) |
| ... | ... | @@ -293,67 +297,29 @@ CvDTreeNode* CascadeBoostTrainData::subsample_data( const CvMat* _subsample_idx |
| 293 | 297 | int num_valid = data_root->get_num_valid(vi); |
| 294 | 298 | CV_Assert( num_valid == sample_count ); |
| 295 | 299 | |
| 296 | - if (is_buf_16u) | |
| 300 | + int* idst_idx = buf->data.i + root->buf_idx*getLength() + (uint64_t)vi*sample_count + root->offset; | |
| 301 | + for( int i = 0; i < num_valid; i++ ) | |
| 297 | 302 | { |
| 298 | - unsigned short* udst_idx = (unsigned short*)(buf->data.s + root->buf_idx*get_length_subbuf() + | |
| 299 | - vi*sample_count + data_root->offset); | |
| 300 | - for( int i = 0; i < num_valid; i++ ) | |
| 301 | - { | |
| 302 | - idx = src_idx[i]; | |
| 303 | - count_i = co[idx*2]; | |
| 304 | - if( count_i ) | |
| 305 | - for( cur_ofs = co[idx*2+1]; count_i > 0; count_i--, j++, cur_ofs++ ) | |
| 306 | - udst_idx[j] = (unsigned short)cur_ofs; | |
| 307 | - } | |
| 308 | - } | |
| 309 | - else | |
| 310 | - { | |
| 311 | - int* idst_idx = buf->data.i + root->buf_idx*get_length_subbuf() + | |
| 312 | - vi*sample_count + root->offset; | |
| 313 | - for( int i = 0; i < num_valid; i++ ) | |
| 314 | - { | |
| 315 | - idx = src_idx[i]; | |
| 316 | - count_i = co[idx*2]; | |
| 317 | - if( count_i ) | |
| 318 | - for( cur_ofs = co[idx*2+1]; count_i > 0; count_i--, j++, cur_ofs++ ) | |
| 319 | - idst_idx[j] = cur_ofs; | |
| 320 | - } | |
| 303 | + idx = src_idx[i]; | |
| 304 | + count_i = co[idx*2]; | |
| 305 | + if( count_i ) | |
| 306 | + for( cur_ofs = co[idx*2+1]; count_i > 0; count_i--, j++, cur_ofs++ ) | |
| 307 | + idst_idx[j] = cur_ofs; | |
| 321 | 308 | } |
| 322 | 309 | } |
| 323 | 310 | |
| 324 | 311 | // subsample cv_lables |
| 325 | 312 | const int* src_lbls = get_cv_labels(data_root, (int*)(uchar*)inn_buf); |
| 326 | - if (is_buf_16u) | |
| 327 | - { | |
| 328 | - unsigned short* udst = (unsigned short*)(buf->data.s + root->buf_idx*get_length_subbuf() + | |
| 329 | - (workVarCount-1)*sample_count + root->offset); | |
| 330 | - for( int i = 0; i < count; i++ ) | |
| 331 | - udst[i] = (unsigned short)src_lbls[sidx[i]]; | |
| 332 | - } | |
| 333 | - else | |
| 334 | - { | |
| 335 | - int* idst = buf->data.i + root->buf_idx*get_length_subbuf() + | |
| 336 | - (workVarCount-1)*sample_count + root->offset; | |
| 337 | - for( int i = 0; i < count; i++ ) | |
| 338 | - idst[i] = src_lbls[sidx[i]]; | |
| 339 | - } | |
| 313 | + int* idst = buf->data.i + root->buf_idx*getLength() + | |
| 314 | + (workVarCount-1)*sample_count + root->offset; | |
| 315 | + for( int i = 0; i < count; i++ ) | |
| 316 | + idst[i] = src_lbls[sidx[i]]; | |
| 340 | 317 | |
| 341 | 318 | // subsample sample_indices |
| 342 | 319 | const int* sample_idx_src = get_sample_indices(data_root, (int*)(uchar*)inn_buf); |
| 343 | - if (is_buf_16u) | |
| 344 | - { | |
| 345 | - unsigned short* sample_idx_dst = (unsigned short*)(buf->data.s + root->buf_idx*get_length_subbuf() + | |
| 346 | - workVarCount*sample_count + root->offset); | |
| 347 | - for( int i = 0; i < count; i++ ) | |
| 348 | - sample_idx_dst[i] = (unsigned short)sample_idx_src[sidx[i]]; | |
| 349 | - } | |
| 350 | - else | |
| 351 | - { | |
| 352 | - int* sample_idx_dst = buf->data.i + root->buf_idx*get_length_subbuf() + | |
| 353 | - workVarCount*sample_count + root->offset; | |
| 354 | - for( int i = 0; i < count; i++ ) | |
| 355 | - sample_idx_dst[i] = sample_idx_src[sidx[i]]; | |
| 356 | - } | |
| 320 | + int* sample_idx_dst = buf->data.i + root->buf_idx*getLength() + workVarCount*sample_count + root->offset; | |
| 321 | + for( int i = 0; i < count; i++ ) | |
| 322 | + sample_idx_dst[i] = sample_idx_src[sidx[i]]; | |
| 357 | 323 | |
| 358 | 324 | for( int vi = 0; vi < var_count; vi++ ) |
| 359 | 325 | root->set_num_valid(vi, count); |
| ... | ... | @@ -390,7 +356,7 @@ CascadeBoostTrainData::CascadeBoostTrainData(const FeatureEvaluator* _featureEva |
| 390 | 356 | |
| 391 | 357 | void CascadeBoostTrainData::initVarType() |
| 392 | 358 | { |
| 393 | - var_type = cvCreateMat( 1, var_count + 2, CV_32SC(channels) ); | |
| 359 | + var_type = cvCreateMat( 1, var_count + 2, CV_32SC1 ); | |
| 394 | 360 | if ( featureEvaluator->getMaxCatCount() > 0 ) |
| 395 | 361 | { |
| 396 | 362 | numPrecalcIdx = 0; |
| ... | ... | @@ -459,17 +425,15 @@ void CascadeBoostTrainData::setData( const FeatureEvaluator* _featureEvaluator, |
| 459 | 425 | sample_count = _numSamples; |
| 460 | 426 | |
| 461 | 427 | is_buf_16u = false; |
| 462 | - if (sample_count < 65536) | |
| 463 | - is_buf_16u = true; | |
| 464 | 428 | |
| 465 | 429 | // 1048576 is the number of bytes in a megabyte |
| 466 | 430 | numPrecalcVal = min( cvRound((double)_precalcValBufSize*1048576. / (sizeof(float)*sample_count)), var_count ); |
| 467 | - numPrecalcIdx = min( cvRound((double)_precalcIdxBufSize*1048576. / ((is_buf_16u ? sizeof(unsigned short) : sizeof (int))*sample_count)), var_count ); | |
| 431 | + numPrecalcIdx = min( cvRound((double)_precalcIdxBufSize*1048576. / (sizeof (int)*sample_count)), var_count ); | |
| 468 | 432 | |
| 469 | 433 | assert( numPrecalcIdx >= 0 && numPrecalcVal >= 0 ); |
| 470 | 434 | |
| 471 | 435 | valCache.create( numPrecalcVal, sample_count, CV_32FC1 ); |
| 472 | - var_type = cvCreateMat( 1, var_count + 2, CV_32SC(channels) ); | |
| 436 | + var_type = cvCreateMat( 1, var_count + 2, CV_32SC1 ); | |
| 473 | 437 | |
| 474 | 438 | if ( featureEvaluator->getMaxCatCount() > 0 ) |
| 475 | 439 | { |
| ... | ... | @@ -500,7 +464,7 @@ void CascadeBoostTrainData::setData( const FeatureEvaluator* _featureEvaluator, |
| 500 | 464 | |
| 501 | 465 | buf_size = -1; // the member buf_size is obsolete |
| 502 | 466 | |
| 503 | - effective_buf_size = (work_var_count + 1)*sample_count * buf_count; // this is the total size of "CvMat buf" to be allocated | |
| 467 | + effective_buf_size = getLength()*buf_count; | |
| 504 | 468 | |
| 505 | 469 | effective_buf_width = sample_count; |
| 506 | 470 | effective_buf_height = work_var_count+1; |
| ... | ... | @@ -510,15 +474,11 @@ void CascadeBoostTrainData::setData( const FeatureEvaluator* _featureEvaluator, |
| 510 | 474 | effective_buf_width *= buf_count; |
| 511 | 475 | |
| 512 | 476 | if (effective_buf_width * effective_buf_height != effective_buf_size) |
| 513 | - { | |
| 514 | 477 | CV_Error(CV_StsBadArg, "The memory buffer cannot be allocated since its size exceeds integer fields limit"); |
| 515 | - } | |
| 516 | - if ( is_buf_16u ) | |
| 517 | - buf = cvCreateMat( effective_buf_height, effective_buf_width, CV_16UC(channels) ); | |
| 518 | - else | |
| 519 | - buf = cvCreateMat( effective_buf_height, effective_buf_width, CV_32SC(channels) ); | |
| 520 | 478 | |
| 521 | - cat_count = cvCreateMat( 1, cat_var_count + 1, CV_32SC(channels) ); | |
| 479 | + buf = cvCreateMat( effective_buf_height, effective_buf_width, CV_32SC1 ); | |
| 480 | + | |
| 481 | + cat_count = cvCreateMat( 1, cat_var_count + 1, CV_32SC1 ); | |
| 522 | 482 | |
| 523 | 483 | // precalculate valCache and set indices in buf |
| 524 | 484 | precalculate(); |
| ... | ... | @@ -542,10 +502,7 @@ void CascadeBoostTrainData::setData( const FeatureEvaluator* _featureEvaluator, |
| 542 | 502 | data_root = new_node( 0, sample_count, 0, 0 ); |
| 543 | 503 | |
| 544 | 504 | // set sample labels |
| 545 | - if (is_buf_16u) | |
| 546 | - udst = (unsigned short*)(buf->data.s + (uint64_t)work_var_count*sample_count); | |
| 547 | - else | |
| 548 | - idst = buf->data.i + (uint64_t)work_var_count*sample_count; | |
| 505 | + idst = buf->data.i + (uint64_t)work_var_count*sample_count; | |
| 549 | 506 | |
| 550 | 507 | for (int si = 0; si < sample_count; si++) |
| 551 | 508 | { |
| ... | ... | @@ -568,9 +525,9 @@ void CascadeBoostTrainData::setData( const FeatureEvaluator* _featureEvaluator, |
| 568 | 525 | priors = cvCreateMat( 1, get_num_classes(), CV_64F ); |
| 569 | 526 | cvSet(priors, cvScalar(1)); |
| 570 | 527 | priors_mult = cvCloneMat( priors ); |
| 571 | - counts = cvCreateMat( 1, get_num_classes(), CV_32SC(channels) ); | |
| 572 | - direction = cvCreateMat( 1, sample_count, CV_8UC(channels) ); | |
| 573 | - split_buf = cvCreateMat( 1, sample_count, CV_32SC(channels) );//TODO: make a pointer | |
| 528 | + counts = cvCreateMat( 1, get_num_classes(), CV_32SC1 ); | |
| 529 | + direction = cvCreateMat( 1, sample_count, CV_8UC1 ); | |
| 530 | + split_buf = cvCreateMat( 1, sample_count, CV_32SC1 );//TODO: make a pointer | |
| 574 | 531 | } |
| 575 | 532 | |
| 576 | 533 | void CascadeBoostTrainData::free_train_data() |
| ... | ... | @@ -594,29 +551,21 @@ const int* CascadeBoostTrainData::get_class_labels( CvDTreeNode* n, int* labelsB |
| 594 | 551 | return labelsBuf; |
| 595 | 552 | } |
| 596 | 553 | |
| 597 | -const int* CascadeBoostTrainData::getBufferValues(CvDTreeNode* n, int* indicesBuf, uint64_t vi) | |
| 554 | +const int* CascadeBoostTrainData::getBufferValues(CvDTreeNode* n, uint64_t vi) | |
| 598 | 555 | { |
| 599 | - const int* cat_values = 0; | |
| 600 | - if (!is_buf_16u) | |
| 601 | - cat_values = buf->data.i + n->buf_idx*get_length_subbuf() + vi*sample_count + n->offset; | |
| 602 | - else { | |
| 603 | - const unsigned short* short_values = (const unsigned short*)(buf->data.s + n->buf_idx*get_length_subbuf() + vi*sample_count + n->offset); | |
| 604 | - for (int i = 0; i < n->sample_count; i++) | |
| 605 | - indicesBuf[i] = short_values[i]; | |
| 606 | - cat_values = indicesBuf; | |
| 607 | - } | |
| 608 | - | |
| 609 | - return cat_values; | |
| 556 | + return buf->data.i + n->buf_idx*getLength() + vi*sample_count + n->offset; | |
| 610 | 557 | } |
| 611 | 558 | |
| 612 | -const int* CascadeBoostTrainData::get_sample_indices( CvDTreeNode* n, int* indicesBuf ) | |
| 559 | +const int* CascadeBoostTrainData::get_sample_indices(CvDTreeNode* n, int* indicesBuf) | |
| 613 | 560 | { |
| 614 | - return getBufferValues(n,indicesBuf,get_work_var_count()); | |
| 561 | + Q_UNUSED(indicesBuf) | |
| 562 | + return getBufferValues(n,get_work_var_count()); | |
| 615 | 563 | } |
| 616 | 564 | |
| 617 | -const int* CascadeBoostTrainData::get_cv_labels( CvDTreeNode* n, int* indicesBuf ) | |
| 565 | +const int* CascadeBoostTrainData::get_cv_labels(CvDTreeNode* n, int* indicesBuf) | |
| 618 | 566 | { |
| 619 | - return getBufferValues(n,indicesBuf,get_work_var_count()-1); | |
| 567 | + Q_UNUSED(indicesBuf) | |
| 568 | + return getBufferValues(n,get_work_var_count()-1); | |
| 620 | 569 | } |
| 621 | 570 | |
| 622 | 571 | void CascadeBoostTrainData::get_ord_var_data( CvDTreeNode* n, int vi, float* ordValuesBuf, int* sortedIndicesBuf, const float** ordValues, const int** sortedIndices, int* sampleIndicesBuf ) |
| ... | ... | @@ -629,14 +578,7 @@ void CascadeBoostTrainData::get_ord_var_data( CvDTreeNode* n, int vi, float* ord |
| 629 | 578 | // For this feature (this code refers to features as values, hence vi == value index), |
| 630 | 579 | // have we precalculated (presorted) the training samples by their feature response? |
| 631 | 580 | if (vi < numPrecalcIdx) { |
| 632 | - if (!is_buf_16u) | |
| 633 | - *sortedIndices = buf->data.i + n->buf_idx*get_length_subbuf() + (uint64_t)vi*sample_count + n->offset; | |
| 634 | - else { | |
| 635 | - const unsigned short* shortIndices = (const unsigned short*)(buf->data.s + n->buf_idx*get_length_subbuf() + (uint64_t)vi*sample_count + n->offset ); | |
| 636 | - for (int i = 0; i < nodeSampleCount; i++) | |
| 637 | - sortedIndicesBuf[i] = shortIndices[i]; | |
| 638 | - *sortedIndices = sortedIndicesBuf; | |
| 639 | - } | |
| 581 | + *sortedIndices = getBufferValues(n, vi); | |
| 640 | 582 | |
| 641 | 583 | // For this feature, have we precalculated all of the feature responses? |
| 642 | 584 | if (vi < numPrecalcVal) { |
| ... | ... | @@ -731,26 +673,20 @@ struct IndexPrecalc : Precalc |
| 731 | 673 | { |
| 732 | 674 | int* idst; |
| 733 | 675 | unsigned short* udst; |
| 734 | - bool isBufShort; | |
| 735 | 676 | |
| 736 | - IndexPrecalc(const FeatureEvaluator* featureEvaluator, CvMat* buf, int sampleCount, bool isBufShort) : | |
| 677 | + IndexPrecalc(const FeatureEvaluator* featureEvaluator, CvMat* buf, int sampleCount) : | |
| 737 | 678 | Precalc(featureEvaluator, sampleCount), |
| 738 | - isBufShort(isBufShort) | |
| 739 | - { | |
| 740 | - udst = (unsigned short*)buf->data.s; | |
| 741 | - idst = buf->data.i; | |
| 742 | - } | |
| 679 | + idst(buf->data.i) | |
| 680 | + {} | |
| 743 | 681 | |
| 744 | 682 | void setBuffer(uint64_t fi, uint64_t si) const |
| 745 | 683 | { |
| 746 | - if (isBufShort) *(udst + fi*sampleCount + si) = (unsigned short)si; | |
| 747 | - else *(idst + fi*sampleCount + si) = si; | |
| 684 | + *(idst + fi*sampleCount + si) = si; | |
| 748 | 685 | } |
| 749 | 686 | |
| 750 | 687 | void sortBuffer(uint64_t fi, float *valCachePtr) const |
| 751 | 688 | { |
| 752 | - if (isBufShort) icvSortUShAux(udst + fi*sampleCount, sampleCount, valCachePtr); | |
| 753 | - else icvSortIntAux(idst + fi*sampleCount, sampleCount, valCachePtr); | |
| 689 | + icvSortIntAux(idst + fi*sampleCount, sampleCount, valCachePtr); | |
| 754 | 690 | } |
| 755 | 691 | |
| 756 | 692 | virtual void operator()(const Range& range) const |
| ... | ... | @@ -771,8 +707,8 @@ struct FeatureAndIndexPrecalc : IndexPrecalc |
| 771 | 707 | { |
| 772 | 708 | Mat *valCache; |
| 773 | 709 | |
| 774 | - FeatureAndIndexPrecalc(const FeatureEvaluator* featureEvaluator, CvMat* buf, Mat* valCache, int sampleCount, bool isBufShort) : | |
| 775 | - IndexPrecalc(featureEvaluator, buf, sampleCount, isBufShort), | |
| 710 | + FeatureAndIndexPrecalc(const FeatureEvaluator* featureEvaluator, CvMat* buf, Mat* valCache, int sampleCount) : | |
| 711 | + IndexPrecalc(featureEvaluator, buf, sampleCount), | |
| 776 | 712 | valCache(valCache) |
| 777 | 713 | {} |
| 778 | 714 | |
| ... | ... | @@ -816,11 +752,11 @@ void CascadeBoostTrainData::precalculate() |
| 816 | 752 | |
| 817 | 753 | // Compute features and sort training samples for feature indices we are not going to cache |
| 818 | 754 | parallel_for_(Range(numPrecalcVal, numPrecalcIdx), |
| 819 | - IndexPrecalc(featureEvaluator, buf, sample_count, is_buf_16u!=0)); | |
| 755 | + IndexPrecalc(featureEvaluator, buf, sample_count)); | |
| 820 | 756 | |
| 821 | 757 | // Compute features and sort training samples for features indices we are going to cache |
| 822 | 758 | parallel_for_(Range(0, minPrecalc), |
| 823 | - FeatureAndIndexPrecalc(featureEvaluator, buf, &valCache, sample_count, is_buf_16u!=0)); | |
| 759 | + FeatureAndIndexPrecalc(featureEvaluator, buf, &valCache, sample_count)); | |
| 824 | 760 | |
| 825 | 761 | // Compute feature values for feature indices for which we are not going to sort training samples |
| 826 | 762 | parallel_for_(Range(minPrecalc, numPrecalcVal), |
| ... | ... | @@ -874,14 +810,13 @@ void CascadeBoostTree::split_node_data( CvDTreeNode* node ) |
| 874 | 810 | int* newIdx = data->split_buf->data.i; |
| 875 | 811 | int newBufIdx = data->get_child_buf_idx( node ); |
| 876 | 812 | CvMat* buf = data->buf; |
| 877 | - size_t length_buf_row = data->get_length_subbuf(); | |
| 813 | + size_t length_buf_row = ((CascadeBoostTrainData*)data)->getLength(); | |
| 878 | 814 | cv::AutoBuffer<uchar> inn_buf(nodeSampleCount*(3*sizeof(int)+sizeof(float))); |
| 879 | 815 | int* tempBuf = (int*)(uchar*)inn_buf; |
| 880 | 816 | |
| 881 | 817 | complete_node_dir(node); |
| 882 | 818 | |
| 883 | - for (uint64_t i = nLeft = nRight = 0; i < nodeSampleCount; i++) | |
| 884 | - { | |
| 819 | + for (uint64_t i = nLeft = nRight = 0; i < nodeSampleCount; i++) { | |
| 885 | 820 | int d = dir[i]; |
| 886 | 821 | // initialize new indices for splitting ordered variables |
| 887 | 822 | newIdx[i] = (nLeft & (d-1)) | (nRight & -d); // d ? ri : li |
| ... | ... | @@ -900,59 +835,27 @@ void CascadeBoostTree::split_node_data( CvDTreeNode* node ) |
| 900 | 835 | if( ci >= 0 || !splitInputData ) |
| 901 | 836 | continue; |
| 902 | 837 | |
| 903 | - int n1 = node->get_num_valid(vi); | |
| 904 | - float *src_val_buf = (float*)(tempBuf + nodeSampleCount); | |
| 905 | - int *src_sorted_idx_buf = (int*)(src_val_buf + nodeSampleCount); | |
| 906 | - int *src_sample_idx_buf = src_sorted_idx_buf + nodeSampleCount; | |
| 907 | - const int* src_sorted_idx = 0; | |
| 908 | - const float* src_val = 0; | |
| 909 | - | |
| 910 | - data->get_ord_var_data(node, vi, src_val_buf, src_sorted_idx_buf, &src_val, &src_sorted_idx, src_sample_idx_buf); | |
| 911 | - | |
| 838 | + const int* src_sorted_idx = ((CascadeBoostTrainData*)data)->getBufferValues(node, vi); | |
| 912 | 839 | for(uint64_t i = 0; i < nodeSampleCount; i++) |
| 913 | 840 | tempBuf[i] = src_sorted_idx[i]; |
| 914 | 841 | |
| 915 | - if (data->is_buf_16u) { | |
| 916 | - ushort *ldst, *rdst; | |
| 917 | - ldst = (ushort*)(buf->data.s + left->buf_idx*length_buf_row + vi*sampleCount + left->offset); | |
| 918 | - rdst = (ushort*)(ldst + nLeft); | |
| 919 | - | |
| 920 | - // split sorted | |
| 921 | - for (int i = 0; i < n1; i++) { | |
| 922 | - int idx = tempBuf[i]; | |
| 923 | - int d = dir[idx]; | |
| 924 | - idx = newIdx[idx]; | |
| 925 | - if (d) { | |
| 926 | - *rdst = (ushort)idx; | |
| 927 | - rdst++; | |
| 928 | - } else { | |
| 929 | - *ldst = (ushort)idx; | |
| 930 | - ldst++; | |
| 931 | - } | |
| 932 | - } | |
| 933 | - } | |
| 934 | - else | |
| 935 | - { | |
| 936 | - int *ldst, *rdst; | |
| 937 | - ldst = buf->data.i + left->buf_idx*length_buf_row + vi*sampleCount + left->offset; | |
| 938 | - rdst = buf->data.i + right->buf_idx*length_buf_row + vi*sampleCount + right->offset; | |
| 842 | + int *ldst, *rdst; | |
| 843 | + ldst = buf->data.i + left->buf_idx*length_buf_row + vi*sampleCount + left->offset; | |
| 844 | + rdst = buf->data.i + right->buf_idx*length_buf_row + vi*sampleCount + right->offset; | |
| 939 | 845 | |
| 940 | - // split sorted | |
| 941 | - for( int i = 0; i < n1; i++ ) | |
| 942 | - { | |
| 943 | - int idx = tempBuf[i]; | |
| 944 | - int d = dir[idx]; | |
| 945 | - idx = newIdx[idx]; | |
| 946 | - if (d) | |
| 947 | - { | |
| 948 | - *rdst = idx; | |
| 949 | - rdst++; | |
| 950 | - } | |
| 951 | - else | |
| 952 | - { | |
| 953 | - *ldst = idx; | |
| 954 | - ldst++; | |
| 955 | - } | |
| 846 | + int n1 = node->get_num_valid(vi); | |
| 847 | + | |
| 848 | + // split sorted | |
| 849 | + for (int i = 0; i < n1; i++) { | |
| 850 | + int idx = tempBuf[i]; | |
| 851 | + int d = dir[idx]; | |
| 852 | + idx = newIdx[idx]; | |
| 853 | + if (d) { | |
| 854 | + *rdst = idx; | |
| 855 | + rdst++; | |
| 856 | + } else { | |
| 857 | + *ldst = idx; | |
| 858 | + ldst++; | |
| 956 | 859 | } |
| 957 | 860 | } |
| 958 | 861 | } |
| ... | ... | @@ -964,37 +867,16 @@ void CascadeBoostTree::split_node_data( CvDTreeNode* node ) |
| 964 | 867 | for(uint64_t i = 0; i < nodeSampleCount; i++) |
| 965 | 868 | tempBuf[i] = src_lbls[i]; |
| 966 | 869 | |
| 967 | - if (data->is_buf_16u) { | |
| 968 | - unsigned short *ldst = (unsigned short *)(buf->data.s + left->buf_idx*length_buf_row + (workVarCount-1)*sampleCount + left->offset); | |
| 969 | - unsigned short *rdst = (unsigned short *)(buf->data.s + right->buf_idx*length_buf_row + (workVarCount-1)*sampleCount + right->offset); | |
| 970 | - | |
| 971 | - for( uint64_t i = 0; i < nodeSampleCount; i++ ) { | |
| 972 | - int idx = tempBuf[i]; | |
| 973 | - if (dir[i]) { | |
| 974 | - *rdst = (unsigned short)idx; | |
| 975 | - rdst++; | |
| 976 | - } else { | |
| 977 | - *ldst = (unsigned short)idx; | |
| 978 | - ldst++; | |
| 979 | - } | |
| 980 | - } | |
| 981 | - | |
| 982 | - } | |
| 983 | - else | |
| 984 | 870 | { |
| 985 | 871 | int *ldst = buf->data.i + left->buf_idx*length_buf_row + (workVarCount-1)*sampleCount + left->offset; |
| 986 | 872 | int *rdst = buf->data.i + right->buf_idx*length_buf_row + (workVarCount-1)*sampleCount + right->offset; |
| 987 | 873 | |
| 988 | - for( uint64_t i = 0; i < nodeSampleCount; i++ ) | |
| 989 | - { | |
| 874 | + for (uint64_t i = 0; i < nodeSampleCount; i++) { | |
| 990 | 875 | int idx = tempBuf[i]; |
| 991 | - if (dir[i]) | |
| 992 | - { | |
| 876 | + if (dir[i]) { | |
| 993 | 877 | *rdst = idx; |
| 994 | 878 | rdst++; |
| 995 | - } | |
| 996 | - else | |
| 997 | - { | |
| 879 | + } else { | |
| 998 | 880 | *ldst = idx; |
| 999 | 881 | ldst++; |
| 1000 | 882 | } |
| ... | ... | @@ -1008,35 +890,15 @@ void CascadeBoostTree::split_node_data( CvDTreeNode* node ) |
| 1008 | 890 | for(uint64_t i = 0; i < nodeSampleCount; i++) |
| 1009 | 891 | tempBuf[i] = sampleIdx_src[i]; |
| 1010 | 892 | |
| 1011 | - if (data->is_buf_16u) { | |
| 1012 | - unsigned short* ldst = (unsigned short*)(buf->data.s + left->buf_idx*length_buf_row + workVarCount*sampleCount + left->offset); | |
| 1013 | - unsigned short* rdst = (unsigned short*)(buf->data.s + right->buf_idx*length_buf_row + workVarCount*sampleCount + right->offset); | |
| 1014 | - for (uint64_t i = 0; i < nodeSampleCount; i++) { | |
| 1015 | - unsigned short idx = (unsigned short)tempBuf[i]; | |
| 1016 | - if (dir[i]) { | |
| 1017 | - *rdst = idx; | |
| 1018 | - rdst++; | |
| 1019 | - } else { | |
| 1020 | - *ldst = idx; | |
| 1021 | - ldst++; | |
| 1022 | - } | |
| 1023 | - } | |
| 1024 | - } | |
| 1025 | - else | |
| 1026 | 893 | { |
| 1027 | 894 | int* ldst = buf->data.i + left->buf_idx*length_buf_row + workVarCount*sampleCount + left->offset; |
| 1028 | 895 | int* rdst = buf->data.i + right->buf_idx*length_buf_row + workVarCount*sampleCount + right->offset; |
| 1029 | - for (uint64_t i = 0; i < nodeSampleCount; i++) | |
| 1030 | - { | |
| 896 | + for (uint64_t i = 0; i < nodeSampleCount; i++) { | |
| 1031 | 897 | int idx = tempBuf[i]; |
| 1032 | - if (dir[i]) | |
| 1033 | - { | |
| 1034 | - | |
| 898 | + if (dir[i]) { | |
| 1035 | 899 | *rdst = idx; |
| 1036 | 900 | rdst++; |
| 1037 | - } | |
| 1038 | - else | |
| 1039 | - { | |
| 901 | + } else { | |
| 1040 | 902 | *ldst = idx; |
| 1041 | 903 | ldst++; |
| 1042 | 904 | } |
| ... | ... | @@ -1143,7 +1005,7 @@ void CascadeBoost::update_weights(CvBoostTree* tree) |
| 1143 | 1005 | } |
| 1144 | 1006 | |
| 1145 | 1007 | CvMat* buf = data->buf; |
| 1146 | - size_t length_buf_row = data->get_length_subbuf(); | |
| 1008 | + uint64_t length_buf_row = ((CascadeBoostTrainData*)data)->getLength(); | |
| 1147 | 1009 | if( !tree ) // before training the first tree, initialize weights and other parameters |
| 1148 | 1010 | { |
| 1149 | 1011 | int* classLabelsBuf = (int*)cur_inn_buf_pos; cur_inn_buf_pos = (uchar*)(classLabelsBuf + n); |
| ... | ... | @@ -1167,15 +1029,9 @@ void CascadeBoost::update_weights(CvBoostTree* tree) |
| 1167 | 1029 | |
| 1168 | 1030 | // set the labels to find (from within weak tree learning proc) |
| 1169 | 1031 | // the particular sample weight, and where to store the response. |
| 1170 | - if (data->is_buf_16u) { | |
| 1171 | - unsigned short* labels = (unsigned short*)(buf->data.s + data->data_root->buf_idx*length_buf_row + data->data_root->offset + (uint64_t)(data->work_var_count-1)*data->sample_count); | |
| 1172 | - for (int i = 0; i < n; i++) | |
| 1173 | - labels[i] = (unsigned short)i; | |
| 1174 | - } else { | |
| 1175 | - int* labels = buf->data.i + data->data_root->buf_idx*length_buf_row + data->data_root->offset + (uint64_t)(data->work_var_count-1)*data->sample_count; | |
| 1176 | - for( int i = 0; i < n; i++ ) | |
| 1177 | - labels[i] = i; | |
| 1178 | - } | |
| 1032 | + int* labels = buf->data.i + data->data_root->buf_idx*length_buf_row + data->data_root->offset + (uint64_t)(data->work_var_count-1)*data->sample_count; | |
| 1033 | + for( int i = 0; i < n; i++ ) | |
| 1034 | + labels[i] = i; | |
| 1179 | 1035 | |
| 1180 | 1036 | for (int i = 0; i < n; i++) { |
| 1181 | 1037 | // save original categorical responses {0,1}, convert them to {-1,1} | ... | ... |