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(); }
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(); }