예제 #1
0
        public static int getClassification(Matrix featureValues, AdaBoostRespons[] adaBoostResponses)
        {
            double sum = 0;
            double alphaSum = 0;
            foreach (AdaBoostRespons adaBoostRespons in adaBoostResponses)
            {
                sum += adaBoostRespons.alpha * getWeakClassification(featureValues[adaBoostRespons.featureIndex.j], adaBoostRespons.weakClassifier.parity, adaBoostRespons.weakClassifier.threshold);
                alphaSum += adaBoostRespons.alpha;
            }

            return sum > (alphaSum / 2) ? 1 : 0;
        }
예제 #2
0
        public static AdaBoostRespons[] executeAdaBoost(Matrix[] integralImages, Feature[] allFeatures, Matrix isFaceList, int nrNegative, int nrWeakClassifiers)
        {
            FeatureType[] featureTypes =
                (from feature in allFeatures
                 select feature.type).Distinct().ToArray();

            InitiateFeatureValues(integralImages, allFeatures, featureTypes);

            AdaBoostRespons[] adaBoostRespons = new AdaBoostRespons[nrWeakClassifiers];

            Matrix weights = getInitializedWeights(isFaceList.nr_vals - nrNegative, nrNegative);

            for (int t = 0; t < nrWeakClassifiers; t++)
            {
                weights = weights / weights.getSum();

                FeatureIndex bestFeatureIndex = new FeatureIndex(FeatureType.type1, int.MinValue);
                WeakClassifier bestClassifier = new WeakClassifier(int.MaxValue, double.MaxValue, double.MaxValue);

                foreach (FeatureType featureType in featureTypes)
                {
                    Matrix allFeatureValuesOfSameType = Matrix.DeserializeFromXML("FeatureValues_" + featureType.ToString() + ".xml");

                    for (int j = 1; j <= allFeatureValuesOfSameType.nr_rows; j++)
                    {
                        WeakClassifier weakClassifier = learnWeakClassifier(allFeatureValuesOfSameType, weights, isFaceList, j);
                        if (weakClassifier.error < bestClassifier.error)
                        {
                            bestClassifier = weakClassifier;
                            bestFeatureIndex.featureType = featureType;
                            bestFeatureIndex.j = j;
                        }
                    }
                }

                double beta = bestClassifier.error / (1 - bestClassifier.error);
                double alpha = Math.Log(1 / beta);

                Matrix featureValuesOfSameType = Matrix.DeserializeFromXML("FeatureValues_" + bestFeatureIndex.featureType.ToString() + ".xml");
                for (int i = 1; i <= featureValuesOfSameType.nr_cols; i++)
                {
                    int classification = getWeakClassification(featureValuesOfSameType[i, bestFeatureIndex.j], bestClassifier.parity, bestClassifier.threshold);
                    weights[i] = Math.Abs(classification - isFaceList[i]) == 0 ? weights[i] * beta : weights[i];
                }

                adaBoostRespons[t] = new AdaBoostRespons(bestClassifier, alpha, bestFeatureIndex);
            }

            return adaBoostRespons;
        }