A detailed interpretation of the Cvtrimweights function

Source: Internet
Author: User

Cvtrimweights source in the OpenCV cvboost.cpp file, the specific content and some comments are as follows

/* *cvtrimweights * Role: The weight of less than a certain threshold culling, because the smaller weight of the sample on the training results have little impact, after culling, so that training samples can shorten the training time */cv_boost_implcvmat* cvtrimweights ( cvmat* weights,//Training Sample weight matrix  cvmat* idx,//Training sample index sequence matrix float factor)//culling    The fraction of the sample remaining after a small weight is calculated based on this parameter to calculate the threshold value {cvmat* ptr = 0) that needs to be excluded from the smaller sample.    Cv_funcname ("Cvtrimweights");    __begin__;    int i, index, NUM;    float sum_weights;    uchar* Wdata;    size_t Wstep;    int wnum;    float threshold;    int count;    float* sorted_weights;    Cv_assert (Cv_mat_type (weights->type) = = CV_32FC1);    ptr = idx;    Sorted_weights = NULL;        if (Factor > 0.0F && Factor < 1.0F) {size_t data_size;        Cv_mat2vec (*weights, Wdata, Wstep, wnum); num = (idx = = NULL)?        Wnum:max (Idx->rows, idx->cols);        data_size = num * sizeof (*sorted_weights);        Sorted_weights = (float*) cvalloc (data_size);        memset (sorted_weights, 0, data_size);        Sum_weights = 0.0F; for (i = 0; i < num; i++) {index = Icvgetidxat (idx, I);//Remove the sample sequence from the first position in the matrix idx sorted_weights[i] = * ((float*) (  Wdata + index * wstep));//Remove the weight of the IDX I position sum_weights + = sorted_weights[i];//sample Weights and} icvsort_32f (      sorted_weights, num, 0);//Sort Samples sum_weights *= (1.0f-factor);        Based on the total weight and factor of the training samples, the weights and i =-1 of the small weight training samples which need to be rejected are calculated. do {sum_weights-= Sorted_weights[++i];}        The sorted sample is subtracted from the sample with the smallest small weight until the condition ends while the while (Sum_weights > 0.0F && i < (num-1));             threshold = Sorted_weights[i]; /* After jumping out of the Do-while loop, assign the weight of the sample with the smallest weight to threshold without being stripped of the weight. External performance: If the weight of the sample is less than this threshold, the direct culling * * while (i > 0 && sorted_weights[i-1] = = threshold) i--;//The small weight of the sample, and then To the original culling of the portion of the threshold equal to the sample picked back, very humane ha if (i > 0 | |        (idx = NULL && cv_mat_type (idx->type)! = CV_32FC1))            {Cv_call (ptr = Cvcreatemat (1, Num-i, CV_32FC1));            Count = 0; for (i = 0; i < num;                i++) {index = Icvgetidxat (idx, i); if (* (* (float*) (Wdata + index * wstep)) >= threshold)//weight operation only for threshold >threshold {cv_m                    At_elem (*ptr, float, 0, count) = (float) index;                count++;        }} assert (count = = Ptr->cols);    } cvfree (&sorted_weights);    } __end__;     return ptr; Only samples with weights greater than threshold are returned}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A detailed interpretation of the Cvtrimweights function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.