⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boosting.cpp

📁 这是一个实现Adaboost算法的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/*******************************************************\
Function description:
Framework to do Adaboost

History:

\*******************************************************/
void Boosting::AdaBoost(int stage)
{
        static double TargetFalseAlarm[10] = {0.00000030, 0.45, 0.60, 0.70, 0.80,
                0.85, 0.85, 0.90, 0.90, 0.95};
        double AccuracyWanted = 0.9987;
        double FalseAlarmWanted = 0;
        int	   i;

        int	   tempResult;
        double s, beta;
        double PresentAccuracy0,  PresentFalseAlarm0,
                PresentAccuracy = 0, PresentFalseAlarm = 1,
                PresentThreshold;
        double FA = 1.0;

        for (i=0; i<PositiveNum; i++)
        {
                //		if (pSampleFlag[i] == 1)
                //		if (PositiveNum - i < 1900)
                //			Weight[i] = 0.5 / double(PositiveNum) * 0.7;
                //		else
                Weight[i] = 0.5 / double(PositiveNum);//正负样本各占权重0.5
        }
        for (i=PositiveNum; i<TotalSampleNum; i++)
        {
                //		if (pSampleFlag[i] == 1)
                Weight[i] = 0.5 / double(curNegNum);
        }

        memset(VotedValue, 0, TotalSampleNum * sizeof(double));

        int    iteration = 0;
        FinalThreshold = 0;
        printf("================= stage%d ==================\n", stage);
        while (   iteration<MaxIteration 
                && (PresentFalseAlarm>TargetFalseAlarm[stage-1] 
                || PresentAccuracy<AccuracyWanted)  )
                {
                        s = 0;
                        for (i=0; i<TotalSampleNum; i++)
                        {
                                //			if (pSampleFlag[i] == 1)
                                s += Weight[i];
                        }
                        if (s == 0) s = 1;
                        for (i=0; i<TotalSampleNum; i++)
                        {
                                //			if (pSampleFlag[i] == 1)
                                Weight[i] /= s;
                        }

                        //=============Select the most discriminate feature=============//
                        double MinCriterion;
                        UpdateHistClassify(&MinCriterion, &SelectedFea[iteration], 
                                &DiscretThreshold[iteration], &Sign[iteration]);

                        //================print to screen====================//
                        printf("stage%-2d:Iteration=%d   Feature=%d  NegNum = %d  ",
                                stage, iteration, SelectedFea[iteration], curNegNum);

                        //=======Get "alpha" and "error rate"========//
                        beta = MinCriterion / (1 - MinCriterion);
                        apha[iteration] = log( (1 - MinCriterion) / MinCriterion );
                        m_m_Error[iteration] = MinCriterion;

                        //==========re-weight===========//
                        for (i=0; i<TotalSampleNum; i++)
                        {
                                //			if (pSampleFlag[i] == 0) continue;
                                tempResult = SimpleDiscretClassify(i, iteration);

                                if (tempResult == 1)
                                {
                                        VotedValue[i] += apha[iteration];
                                }

                                if (tempResult == Label[i])
                                {
                                        Weight[i] *= beta;
                                }

                        }

                        printf("Weighted Error= %lf\n", MinCriterion);
                        FinalThreshold += apha[iteration] / 2.0;
                        double tempThreshold = FinalThreshold;
                        WeakLearnerNum = iteration;

                        AdjustThrehold(AccuracyWanted, FalseAlarmWanted,
                                &PresentAccuracy0, &PresentFalseAlarm0,
                                &PresentAccuracy, &PresentFalseAlarm,
                                &PresentThreshold);

                        double testAccuracy, testFalseAlarm;
                        CheckAccuracy(&testAccuracy, &testFalseAlarm, PresentThreshold);
                        FinalThreshold = PresentThreshold;

                        printf("Detection Rate = %lf    False Alarm Rate = %lf  Total FA= %.8f\n",
                                PresentAccuracy, PresentFalseAlarm, FA);

                        WriteIterationResult(iteration, MinCriterion,
                                PresentAccuracy0, PresentFalseAlarm0,
                                PresentAccuracy, PresentFalseAlarm, stage);

                        WriteModelResult("mResultFile", iteration, PresentAccuracy, stage, iteration);

                        printf("finalthreshold=%lf   FinalThreshold = %lf\n", tempThreshold, FinalThreshold);
                        printf("----------------------------------------------\n", stage);

                        iteration++;
                }
                //	markSampleFlag(FinalThreshold, pSampleFlag);

}

/////////////////////////////////////////////////////////////////////////////////////////////////
void Boosting::WriteIterationResult(int i,double WeightedError,
                                    double Recall1, double FalseAlarm1,
                                    double Recall2, double FalseAlarm2, int stage)
{

        CString filename = SAVEPATH + "Temp.txt";
        FILE *stream = fopen(filename, "at");		
        fprintf(stream,"stage=%d T=%3d Fea=%5d Error=%.2f  Re=%.4f FA=%.4f      Re=%.4f Fa=%.8f NegNum=%d\n",
                stage, i, SelectedFea[i], WeightedError, Recall1, FalseAlarm1, 
                Recall2, FalseAlarm2, curNegNum);
        fclose(stream);
}


///////////////////////////////////////////////////////////////////////////////////////////////////
void Boosting::WriteModelResult(CString ResultFile, int nId, double recall, int stage, int newstage)
{
        CString filename;
        FILE *stream;		
        filename.Format("%sModel.txt", SAVEPATH);
        stream = fopen(filename, "at");

        int index = SelectedFea[nId];

        fprintf(stream,"%d %d %d %d %d %d %lf %lf %lf %lf\n",
                subWinInfo[index].type,
                subWinInfo[index].size.x, subWinInfo[index].size.y,
                subWinInfo[index].pos.x, subWinInfo[index].pos.y,
                Sign[nId],
                GetRawFeaValue(index, DiscretThreshold[nId]),
                apha[nId], FinalThreshold, recall);

        fclose(stream);
}

////////////////////////////////////////////////////////////////////
inline double Boosting::GetRawFeaValue(int FeaNo, int BinNo)
{
        return (BinWidth[FeaNo] * BinNo + BinMin[FeaNo]);
}

////////////////////////////////////////////////////////////////////
void Boosting::GetAllSubWinInfo()
{
        POINT imageSize;
        POINT FeatureStPos, FeatureScale;
        int   FeatureType;

        imageSize.x = SRCIMAGEX;
        imageSize.y = SRCIMAGEY;

        unsigned char *pFlag = new unsigned char[TOTAL_FEATURE_NUM];
        //	FILE *fFlag = fopen(SAVEPATH + "flag", "rb");
        //	fread(pFlag, sizeof(unsigned char), TOTAL_FEATURE_NUM, fFlag);
        //	fclose(fFlag);
        memset(pFlag, 1, TOTAL_FEATURE_NUM);

        long feaCount = 0;
        long count = -1;
        FeatureStPos.y = 1;
        while (FeatureStPos.y < imageSize.y-1)//行扫描
        {
                FeatureStPos.x = 1;
                while (FeatureStPos.x < imageSize.x-1)//列扫描
                {
                        //====== 计算A类特征 ======
                        FeatureScale.y = 2;
                        FeatureType = 0;
                        while (FeatureStPos.y + FeatureScale.y < imageSize.y)//特征y方向放大
                        {
                                FeatureScale.x = 2; 
                                while (FeatureStPos.x + 2 * FeatureScale.x < imageSize.x)//特征x方向放大
                                {
                                        count++;
                                        if (pFlag[count] == 1) 
                                        {
                                                subWinInfo[feaCount].imageSize.x = imageSize.x;
                                                subWinInfo[feaCount].imageSize.y = imageSize.y;
                                                subWinInfo[feaCount].pos.x = FeatureStPos.x;
                                                subWinInfo[feaCount].pos.y = FeatureStPos.y;
                                                subWinInfo[feaCount].size.x = FeatureScale.x;
                                                subWinInfo[feaCount].size.y = FeatureScale.y;
                                                subWinInfo[feaCount].type = FeatureType;
                                                feaCount++;
                                        }
                                        FeatureScale.x += 1;
                                }
                                FeatureScale.y += 1;
                        }
                        //========= 计算B类特征 ==========
                        FeatureScale.y = 2;
                        FeatureType = 1;
                        while (FeatureStPos.y + 2 * FeatureScale.y < imageSize.y)//特征y方向放大
                        {
                                FeatureScale.x = 2;
                                while (FeatureStPos.x + FeatureScale.x < imageSize.x)//特征x方向放大
                                {
                                        count++;
                                        if (pFlag[count] == 1)
                                        {
                                                subWinInfo[feaCount].imageSize.x = imageSize.x;
                                                subWinInfo[feaCount].imageSize.y = imageSize.y;
                                                subWinInfo[feaCount].pos.x = FeatureStPos.x;
                                                subWinInfo[feaCount].pos.y = FeatureStPos.y;
                                                subWinInfo[feaCount].size.x = FeatureScale.x;
                                                subWinInfo[feaCount].size.y = FeatureScale.y;
                                                subWinInfo[feaCount].type = FeatureType;
                                                feaCount++;
                                        }
                                        FeatureScale.x += 1;
                                }
                                FeatureScale.y += 1; 
                        }
                        //========= 计算C类特征 ==========
                        FeatureScale.y = 2;
                        FeatureType = 2;
                        while (FeatureStPos.y + FeatureScale.y < imageSize.y)
                        {
                                FeatureScale.x = 2; 
                                while (FeatureStPos.x+3*FeatureScale.x < imageSize.x)
                                {
                                        count++;
                                        if (pFlag[count] == 1)
                                        {
                                                subWinInfo[feaCount].imageSize.x = imageSize.x;
                                                subWinInfo[feaCount].imageSize.y = imageSize.y;
                                                subWinInfo[feaCount].pos.x = FeatureStPos.x;
                                                subWinInfo[feaCount].pos.y = FeatureStPos.y;
                                                subWinInfo[feaCount].size.x = FeatureScale.x;
                                                subWinInfo[feaCount].size.y = FeatureScale.y;
                                                subWinInfo[feaCount].type = FeatureType;
                                                feaCount++;
                                        }
                                        FeatureScale.x += 1;
                                }
                                FeatureScale.y += 1;
                        }
                        //========= 计算D类特征 ==========
                        FeatureScale.y = 2;
                        FeatureType = 3;
                        while (FeatureStPos.y + 2 * FeatureScale.y < imageSize.y)
                        {
                                FeatureScale.x = 2;
                                while (FeatureStPos.x + 2 * FeatureScale.x < imageSize.x)
                                {
                                        count++;
                                        if (pFlag[count] == 1)
                                        {
                                                subWinInfo[feaCount].imageSize.x = imageSize.x;
                                                subWinInfo[feaCount].imageSize.y = imageSize.y;
                                                subWinInfo[feaCount].pos.x = FeatureStPos.x;
                                                subWinInfo[feaCount].pos.y = FeatureStPos.y;
                                                subWinInfo[feaCount].size.x = FeatureScale.x;
                                                subWinInfo[feaCount].size.y = FeatureScale.y;
                                                subWinInfo[feaCount].type = FeatureType;
                                                feaCount++;
                                        }
                                        FeatureScale.x += 1;
                                }
                                FeatureScale.y += 1;
                        }
                        //========= 计算E类特征 ==========
                        FeatureScale.y = 2;
                        FeatureType = 4;
                        while (FeatureStPos.y + 3 * FeatureScale.y < imageSize.y)
                        {
                                FeatureScale.x = 2; 
                                while (FeatureStPos.x + FeatureScale.x < imageSize.x)
                                {
                                        count++;
                                        if (pFlag[count] == 1)
                                        {
                                                subWinInfo[feaCount].imageSize.x = imageSize.x;
                                                subWinInfo[feaCount].imageSize.y = imageSize.y;
                                                subWinInfo[feaCount].pos.x = FeatureStPos.x;
                                                subWinInfo[feaCount].pos.y = FeatureStPos.y;
                                                subWinInfo[feaCount].size.x = FeatureScale.x;
                                                subWinInfo[feaCount].size.y = FeatureScale.y;
                                                subWinInfo[feaCount].type = FeatureType;
                                                feaCount++;
                                        }
                                        FeatureScale.x += 1;
                                }
                                FeatureScale.y += 1;
                        }
                        FeatureStPos.x += 1;
                }
                FeatureStPos.y += 1;
        }

        delete []pFlag;
        return;
}


////////////////////////////////////////////////////////////////////////////
void Boosting::markSampleFlag(double Threshold, unsigned char* &pSampleFlag)
{

        for (int i=0; i<TotalSampleNum; i++)
        {
                if (pSampleFlag[i] == 0 || Label[i] == 1) continue;

                if ( VotedValue[i] < Threshold )
                {
                        curNegNum--;
                        pSampleFlag[i] = 0;
                }
        }

        return;
}

////////////////////////////////////////////////////////////////////////////
void Boosting::TrainCascade()
{
        int stageNum = 1;
        int stage = 0;

        pSampleFlag = new unsigned char[TotalSampleNum];
        memset(pSampleFlag, 1, TotalSampleNum);

        curNegNum = NegativeNum;

        for (int i=0; i<stageNum; i++)
        {
                AdaBoost(i+1);
        }

        delete []pSampleFlag;
        pSampleFlag = NULL;

        return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -