public LaplacianFaceRecognizer(List <Matrix> trainingSet, List <String> labels, int numOfComponents)
        {
            this.numOfComponents = numOfComponents;
            this.imgRows         = trainingSet[0].RowDimension;
            int n = trainingSet.Count; // sample size
            HashSet <String> tempSet = new HashSet <String>(labels);
            int c = tempSet.Count;     // class size

            // process in PCA
            EigenFaceRecognizer pca = new EigenFaceRecognizer(trainingSet, labels, numOfComponents);

            //construct the nearest graph
            Matrix S = constructGraph(pca.getWeights());
            Matrix D = constructD(S);
            Matrix L = D.Subtract(S);

            //reconstruct the trainingSet into required X;
            Matrix X    = constructTrainingMatrix(pca.getWeights());
            Matrix XLXT = X.Multiply(L).Multiply(X.Transpose());
            Matrix XDXT = X.Multiply(D).Multiply(X.Transpose());

            //calculate the eignevalues and eigenvectors of (XDXT)^-1 * (XLXT)
            XDXT.Inverse();
            Matrix targetForEigen           = XDXT.Inverse().Multiply(XLXT);
            EigenvalueDecomposition feature = targetForEigen.Eigen();

            double[] eigenValues = feature.RealEigenvalues;
            int[]    indexOfChosenEigenValues = this.getIndOfHigherEV(eigenValues, eigenValues.Length);//higher eigen values

            Matrix eigenVectors         = feature.GetV();
            Matrix selectedEigenVectors = eigenVectors.GetMatrix(0, eigenVectors.RowDimension - 1, indexOfChosenEigenValues);

            this.weightMatrix = pca.getWeightMatrix().Multiply(selectedEigenVectors);

            //Construct projectedTrainingMatrix
            this.weights = new List <ProjectMatrix>();
            for (int i = 0; i < trainingSet.Count(); i++)
            {
                ProjectMatrix pm = new ProjectMatrix(this.weightMatrix.Transpose().Multiply(trainingSet[i].Subtract(pca.getMeanMatrix())),
                                                     labels[i]);
                this.weights.Add(pm);
            }
            this.meanMatrix = pca.getMeanMatrix();
        }
        public FisherFaceRecognizer(List <Matrix> trainingSet, List <String> labels, int numOfComponents)
        {
            int n = trainingSet.Count; // sample size
            HashSet <String> tempSet = new HashSet <String>(labels);
            int c = tempSet.Count;     // class size

            // process in PCA
            EigenFaceRecognizer pca = new EigenFaceRecognizer(trainingSet, labels, numOfComponents);

            //construct the nearest neighbor graph
            Matrix S = constructNearestNeighborGraph(pca.getProjectSet());
            Matrix D = constructD(S);
            Matrix L = D.Subtract(S);

            //reconstruct the trainingSet into required X;
            Matrix X    = constructTrainingMatrix(pca.getProjectSet());
            Matrix XLXT = X.Multiply(L).Multiply(X.Transpose());
            Matrix XDXT = X.Multiply(D).Multiply(X.Transpose());

            //calculate the eignevalues and eigenvectors of (XDXT)^-1 * (XLXT)
            Matrix targetForEigen           = XDXT.Inverse().Multiply(XLXT);
            EigenvalueDecomposition feature = targetForEigen.Eigen();

            double[] d = feature.RealEigenvalues;
            //assert d.length >= c - 1 :"Ensure that the number of eigenvalues is larger than c - 1";///
            int[] indexes = getIndOfHigherEV(d, d.Length);

            Matrix eigenVectors         = feature.GetV();
            Matrix selectedEigenVectors = eigenVectors.GetMatrix(0, eigenVectors.RowDimension - 1, indexes);

            this.weightMatrix = pca.getWeightMatrix().Multiply(selectedEigenVectors);

            //Construct projectedTrainingMatrix
            this.projectSet = new List <ProjectMatrix>();
            for (int i = 0; i < trainingSet.Count(); i++)
            {
                ProjectMatrix ptm = new ProjectMatrix(this.weightMatrix.Transpose().Multiply(trainingSet[i].Subtract(pca.getMeanMatrix())), labels[i]);
                this.projectSet.Add(ptm);
            }
            this.meanMatrix = pca.getMeanMatrix();
        }
Esempio n. 3
0
        public LDA(List <Matrix> trainingSet, List <String> labels,
                   int numOfComponents)
        {
            int n = trainingSet.Count(); // sample size
            HashSet <string> tempSet = new HashSet <string>(labels);
            int c = tempSet.Count();     // class size

            /// deh mfrod used for debugging issues, so fakes for nw //////////////////////////
            //assert numOfComponents >= n - c : "the input components is smaller than n - c!";
            //assert n >= 2 * c : "n is smaller than 2c!";

            // process in PCA
            EigenFaceRecognizer pca = new EigenFaceRecognizer(trainingSet, labels, n - c);

            // classify
            Matrix meanTotal = new Matrix(n - c, 1);

            Dictionary <string, List <Matrix> > map = new Dictionary <string, List <Matrix> >();
            List <ProjectMatrix> pcaTrain           = pca.getProjectSet();

            for (int i = 0; i < pcaTrain.Count(); i++)
            {
                string key = pcaTrain[i].getLabel();
                meanTotal.AddEquals(pcaTrain[i].getImgMat());

                if (!map.ContainsKey(key))
                {
                    List <Matrix> temp = new List <Matrix>();
                    temp.Add(pcaTrain[i].getImgMat());
                    map.Add(key, temp);
                }
                else
                {
                    List <Matrix> temp = map[key];
                    temp.Add(pcaTrain[i].getImgMat());
                    map[key] = temp;
                }
            }
            meanTotal.Multiply((double)1 / n);

            // calculate Sw, Sb
            Matrix Sw = new Matrix(n - c, n - c);
            Matrix Sb = new Matrix(n - c, n - c);

            /*** !!! **/
            tempSet = new HashSet <string>(map.Keys);
            /*** !!! **/
            foreach (string s in tempSet)
            {
                //iterator<string> it = tempSet.iterator();
                //while (it.hasNext()) {
                //String s = (String)it.next();
                List <Matrix> matrixWithinThatClass = map[s];


                Matrix meanOfCurrentClass = getMean(matrixWithinThatClass);
                for (int i = 0; i < matrixWithinThatClass.Count(); i++)
                {
                    Matrix temp1 = matrixWithinThatClass[i].Subtract(meanOfCurrentClass);
                    temp1 = temp1.Multiply(temp1.Transpose());
                    Sw.AddEquals(temp1);
                }

                Matrix temp = meanOfCurrentClass.Subtract(meanTotal);
                temp = temp.Multiply(temp.Transpose()).Multiply(matrixWithinThatClass.Count());
                Sb.AddEquals(temp);
            }

            // calculate the eigenvalues and vectors of Sw^-1 * Sb
            Matrix targetForEigen           = Sw.Inverse().Multiply(Sb);
            EigenvalueDecomposition feature = targetForEigen.Eigen();

            double[] d = feature.RealEigenvalues;
            //assert d.length >= c - 1 : "Ensure that the number of eigenvalues is larger than c - 1";

            int[]  indexes              = getIndOfHigherEV(d, c - 1);
            Matrix eigenVectors         = feature.GetV();
            Matrix selectedEigenVectors = eigenVectors.GetMatrix(0, eigenVectors.RowDimension - 1, indexes);

            this.weightMatrix = pca.getWeightMatrix().Multiply(selectedEigenVectors);

            // Construct projectedTrainingMatrix
            this.projectSet = new List <ProjectMatrix>();
            for (int i = 0; i < trainingSet.Count(); i++)
            {
                ProjectMatrix ptm = new ProjectMatrix(this.weightMatrix
                                                      .Transpose()
                                                      .Multiply(trainingSet[i].Subtract(pca.getMeanMatrix())),
                                                      labels[i]);
                this.projectSet.Add(ptm);
            }
            this.meanMatrix = pca.getMeanMatrix();
        }
Esempio n. 4
0
        static void train(int metricType, int recognizer, int noOfComponents, int trainNums, int knn_k)
        {
            //set trainSet and testSet
            Dictionary <String, List <int> > trainMap = new Dictionary <string, List <int> >();
            Dictionary <String, List <int> > testMap  = new Dictionary <string, List <int> >();

            for (int i = 1; i <= 5; i++)
            {
                String     label = "s" + i;
                List <int> train = generateTrainNums(trainNums);
                List <int> test  = generateTestNums(train);
                trainMap[label] = train;
                testMap[label]  = test;
            }
            //trainingSet & respective labels
            List <Matrix> trainingSet = new List <Matrix>();
            List <String> labels      = new List <String>();

            HashSet <String> labelSet = new HashSet <string>(trainMap.Keys);

            foreach (String label in labelSet)
            {
                List <int> cases = trainMap[label];
                for (int i = 0; i < cases.Count; i++)
                {
                    String filePath = "E:/faces/" + label + "/" + cases[i] + ".bmp";
                    Matrix temp;
                    try
                    {
                        temp = ImageGrapper.GetMatrixGivenPath(filePath);
                        if (recognizer != 0)
                        {
                            temp = convertTo1D(temp);
                        }
                        trainingSet.Add(temp);
                        labels.Add(label);
                    } catch (Exception e) {}
                }
            }


            FaceRecognizer fr = null;

            if (recognizer == 0)
            {
                fr = new ModularFaceRecognitionAlgorithms(trainingSet, labels, 200, 16, trainNums);
            }
            else if (recognizer == 1)
            {
                fr = new EigenFaceRecognizer(trainingSet, labels, 40);
            }
            else if (recognizer == 2)
            {
                fr = new LinearDescriminantAnalysis(trainingSet, labels);
            }
            else if (recognizer == 3)
            {
                fr = new LaplacianFaceRecognizer(trainingSet, labels, 20);
            }
            fr.saveTrainingData(@"E:\trainLPP.txt");
        }
        public LinearDescriminantAnalysis(List <Matrix> trainingSet, List <String> labels)
        {
            this.trainingSet = trainingSet;
            this.imgRows     = trainingSet[0].RowDimension;
            int n = trainingSet.Count();  // sample size
            HashSet <string> uniqueLabels = new HashSet <string>(labels);
            int c = uniqueLabels.Count(); // class size

            this.numOfComponents = c - 1;

            EigenFaceRecognizer pca = new EigenFaceRecognizer(trainingSet, labels, n - c);

            Matrix meanTotal = new Matrix(n - c, 1);

            Dictionary <string, List <Matrix> > dic = new Dictionary <string, List <Matrix> >();
            List <ProjectMatrix> pcaWeights         = pca.getWeights();

            for (int i = 0; i < pcaWeights.Count(); i++)
            {
                string key = pcaWeights[i].getLabel();
                meanTotal.AddEquals(pcaWeights[i].getImgMat());

                if (!dic.ContainsKey(key))
                {
                    List <Matrix> temp = new List <Matrix>();
                    temp.Add(pcaWeights[i].getImgMat());
                    dic.Add(key, temp);
                }
                else
                {
                    List <Matrix> temp = dic[key];
                    temp.Add(pcaWeights[i].getImgMat());
                    dic[key] = temp;
                }
            }
            meanTotal.Multiply((double)1 / n);

            // calculate Sw, Sb
            Matrix Sw = new Matrix(n - c, n - c);
            Matrix Sb = new Matrix(n - c, n - c);

            uniqueLabels = new HashSet <string>(dic.Keys);
            foreach (string s in uniqueLabels)
            {
                List <Matrix> matrixWithinThatClass = dic[s];
                Matrix        meanOfCurrentClass    = getMean(matrixWithinThatClass);

                for (int i = 0; i < matrixWithinThatClass.Count(); i++)
                {
                    Matrix mat = matrixWithinThatClass[i].Subtract(meanOfCurrentClass);
                    mat = mat.Multiply(mat.Transpose());
                    Sw.AddEquals(mat);
                }
                Matrix temp = meanOfCurrentClass.Subtract(meanTotal);
                temp = temp.Multiply(temp.Transpose()).Multiply(matrixWithinThatClass.Count());
                Sb.AddEquals(temp);
            }

            // calculate the eigenvalues and vectors of Sw^-1 * Sb
            Matrix targetForEigen           = Sw.Inverse().Multiply(Sb);
            EigenvalueDecomposition feature = targetForEigen.Eigen();

            double[] eigenValues = feature.RealEigenvalues;

            int[]  indexOfChosenEigenValues = getIndOfHigherEV(eigenValues, c - 1);
            Matrix eigenVectors             = feature.GetV();
            Matrix selectedEigenVectors     = eigenVectors.GetMatrix(0, eigenVectors.RowDimension - 1, indexOfChosenEigenValues);

            this.weightMatrix = pca.getWeightMatrix().Multiply(selectedEigenVectors);

            // Construct weights
            this.weights = new List <ProjectMatrix>();
            for (int i = 0; i < trainingSet.Count(); i++)
            {
                ProjectMatrix ptm = new ProjectMatrix(this.weightMatrix.Transpose().Multiply(trainingSet[i].Subtract(pca.getMeanMatrix()))
                                                      , labels[i]);
                this.weights.Add(ptm);
            }
            this.meanMatrix = pca.getMeanMatrix();
        }
Esempio n. 6
0
        /*metricType:
         *  0: CosineDissimilarity
         *  1: L1Distance
         *  2: EuclideanDistance
         *
         * energyPercentage:
         *  PCA: components = samples * energyPercentage
         *  LDA: components = (c-1) *energyPercentage
         *  LLP: components = (c-1) *energyPercentage
         *
         * featureExtractionMode
         *  0: PCA
         *	1: LDA
         *  2: LLP
         *
         * trainNums: how many numbers in 1..10 are assigned to be training faces
         * for each class, randomly generate the set
         *
         * knn_k: number of K for KNN algorithm
         *
         * */
        static double test(int metricType, int noOfComponents, int faceRecognizerFlag, int trainNums, int knn_k)
        {
            //determine which metric is used
            //metric
            Metric metric = null;

            if (metricType == 0)
            {
                metric = new CosineDissimilarity();
            }
            else if (metricType == 1)
            {
                metric = new L1Distance();
            }
            else if (metricType == 2)
            {
                metric = new EuclideanDistance();
            }

            //////////assert metric != null : "metricType is wrong!";////////

            //set expectedComponents according to energyPercentage
            //componentsRetained
            //		int trainingSize = trainNums * 10;
            //		int componentsRetained = 0;
            //		if(featureExtractionMode == 0)
            //			componentsRetained = (int) (trainingSize * energyPercentage);
            //		else if(featureExtractionMode == 1)
            //			componentsRetained = (int) ((10 -1) * energyPercentage);
            //		else if(featureExtractionMode == 2)
            //			componentsRetained = (int) ((10 -1) * energyPercentage);

            //set trainSet and testSet
            Dictionary <String, List <int> > trainMap = new Dictionary <string, List <int> >();
            Dictionary <String, List <int> > testMap  = new Dictionary <string, List <int> >();

            for (int i = 1; i <= 40; i++)
            {
                String     label = "s" + i;
                List <int> train = generateTrainNums(trainNums);
                List <int> test  = generateTestNums(train);
                trainMap[label] = train;
                testMap[label]  = test;
            }

            //trainingSet & respective labels
            List <Matrix> trainingSet = new List <Matrix>();
            List <String> labels      = new List <String>();

            HashSet <String> labelSet = new HashSet <string>(trainMap.Keys);

            foreach (String label in labelSet)
            {
                List <int> cases = trainMap[label];
                for (int i = 0; i < cases.Count; i++)
                {
                    String filePath = "E:/faces/" + label + "/" + cases[i] + ".bmp";
                    Matrix temp;
                    try {
                        temp = FileManager.GetBitMapColorMatrix(filePath);
                        if (faceRecognizerFlag == 3)
                        {
                            trainingSet.Add(temp);
                        }
                        else
                        {
                            trainingSet.Add(convertTo1D(temp));
                        }
                        labels.Add(label);
                    } catch (Exception e) {
                    }
                }
            }

            //testingSet & respective true labels
            List <Matrix> testingSet = new List <Matrix>();
            List <String> trueLabels = new List <String>();

            labelSet = new HashSet <string>(trainMap.Keys);

            foreach (string label in labelSet)
            {
                List <int> cases = testMap[label];
                for (int i = 0; i < cases.Count(); i++)
                {
                    String filePath = "E:/faces/" + label + "/" + cases[i] + ".bmp";
                    Matrix temp;
                    try
                    {
                        temp = FileManager.GetBitMapColorMatrix(filePath);
                        testingSet.Add(convertTo1D(temp));
                        trueLabels.Add(label);
                    }
                    catch (Exception e) { }
                }
            }

            //set featureExtraction

            FaceRecognizer fe = null;

            if (faceRecognizerFlag == 0)
            {
                fe = new EigenFaceRecognizer(trainingSet, labels, noOfComponents);
            }
            else if (faceRecognizerFlag == 1)
            {
                fe = new LDA(trainingSet, labels, noOfComponents);
            }
            else if (faceRecognizerFlag == 2)
            {
                fe = new FisherFaceRecognizer(trainingSet, labels, noOfComponents);
            }
            else if (faceRecognizerFlag == 3)
            {
                fe = new ModularFaceRecognitionAlgorithms(trainingSet, labels, 200, 16, trainNums);
            }


            FileManager.convertMatricetoImage(fe.getWeightMatrix(), faceRecognizerFlag);

            //use test cases to validate
            //testingSet   trueLables
            List <ProjectMatrix> projectSet = fe.getProjectSet();
            int accurateNum = 0;

            for (int i = 0; i < testingSet.Count; i++)
            {
                Matrix testCase = fe.getWeightMatrix().Transpose().Multiply(testingSet[i].Subtract(fe.getMeanMatrix()));
                String result   = KNN.assignLabel(projectSet.ToArray(), testCase, knn_k, metric);

                if (result == trueLabels[i])
                {
                    accurateNum++;
                }
            }
            double accuracy = accurateNum / (double)testingSet.Count;

            Console.WriteLine("The accuracy is " + accuracy);
            return(accuracy);
        }