public ModularPCA(List <Matrix> trainingSet, List <String> labels, int numOfComponents, int N) { if (numOfComponents >= trainingSet.Count) { throw new Exception("the expected dimensions could not be achieved!" + numOfComponents + " " + trainingSet.Count); } this.trainingSet = trainingSet; this.M = trainingSet.Count; this.N = N; this.labels = labels; this.numOfComponents = numOfComponents; this.meanMatrix = getMean(this.trainingSet); Matrix covariance = this.calculateCovarianceMatrix(this.normalizeSubImages(this.trainingSet, this.meanMatrix)); this.weightMatrix = getFeature(covariance, this.numOfComponents); // Construct projectedTrainingGeneralMatrix this.projectSet = new List <ProjectMatrix>(); for (int i = 0; i < trainingSet.Count; i++) { ProjectMatrix ptm = new ProjectMatrix(this.weightMatrix .Transpose().Multiply(trainingSet[i].Subtract(meanMatrix)), labels[i]); this.projectSet.Add(ptm); } }
public ModularFaceRecognitionAlgorithms(List <Matrix> trainSet, List <String> trainLables, int numOfComponents, int N, int noOftrainingPerInd) { this.weights = new List <ProjectMatrix>(); this.noOftrainingPerInd = noOftrainingPerInd; this.N = N; this.trainingSet = new List <Matrix>(); this.labels = new List <string>(); devideTheTrainingSetToN(trainSet, trainLables, N); this.numOfComponents = numOfComponents; this.meanMatrix = getMean(this.trainingSet); //the average matrix of all images this.weightMatrix = getFeature(this.trainingSet, this.numOfComponents); //extract the features // Construct projectedTrainingGeneralMatrix this.weights = new List <ProjectMatrix>(); for (int i = 0; i < trainingSet.Count; i++) { ProjectMatrix ptm = new ProjectMatrix (this.weightMatrix.Transpose().Multiply(trainingSet[i].Subtract(meanMatrix)), labels[i]); this.weights.Add(ptm); if (!firstIndexMap.ContainsKey(labels[i])) { firstIndexMap.Add(labels[i], i); } } formSubMatrixWeights(); }
public override void loadTrainingData(string path) { int lineCounter = 0; string[] lines = System.IO.File.ReadAllLines(path); //read the header string[] splitLine = lines[lineCounter].Trim().Split(' '); N = Int32.Parse(splitLine[0]); //N, noOfComp , count numOfComponents = Int32.Parse(splitLine[1]); noOftrainingPerInd = Int32.Parse(splitLine[2]); //read the average matrix meanMatrix = new Matrix(N * N, 1); lineCounter++; splitLine = lines[lineCounter].Trim().Split(' '); for (int i = 0; i < N * N; i++) { double value = Double.Parse(splitLine[i]); meanMatrix.SetElement(i, 0, value); } //read the eigenvectors lineCounter++; splitLine = lines[lineCounter].Trim().Split(' '); int file1DCounter = 0; weightMatrix = new Matrix(N * N, numOfComponents); for (int i = 0; i < N * N; i++) { for (int j = 0; j < numOfComponents; j++) { double value = Double.Parse(splitLine[file1DCounter]); weightMatrix.SetElement(i, j, value); file1DCounter++; } } lineCounter++; splitLine = lines[lineCounter].Trim().Split(' '); int noOfProjectSet = Int32.Parse(splitLine[0]); //read the project set weights = new List <ProjectMatrix>(); for (int i = 0; i < noOfProjectSet; i++) { lineCounter++; splitLine = lines[lineCounter].Trim().Split(' '); String label = splitLine[0]; lineCounter++; splitLine = lines[lineCounter].Trim().Split(' '); Matrix mat = new Matrix(numOfComponents, 1); for (int j = 0; j < numOfComponents; j++) { double value = Double.Parse(splitLine[j]); mat.SetElement(j, 0, value); } ProjectMatrix pm = new ProjectMatrix(mat, label); weights.Add(pm); } }
public EigenFaceRecognizer(List <Matrix> trainingSet, List <String> labels, int numOfComponents) { this.trainingSet = trainingSet; this.labels = labels; this.numOfComponents = numOfComponents; this.imgRows = trainingSet[0].RowDimension; this.meanMatrix = getMean(this.trainingSet); //the average matrix of all images this.weightMatrix = getFeature(this.trainingSet, this.numOfComponents); //extract the features // Construct projectedTrainingGeneralMatrix this.weights = new List <ProjectMatrix>(); for (int i = 0; i < trainingSet.Count; i++) { ProjectMatrix ptm = new ProjectMatrix (this.weightMatrix.Transpose().Multiply(trainingSet[i].Subtract(meanMatrix)), labels[i]); this.weights.Add(ptm); } }
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 void formSubMatrixWeights() { foreach (KeyValuePair <string, int> firstIndex in firstIndexMap) { for (int n = 0; n < N; n++) { int r = weights[0].getImgMat().RowDimension; int c = weights[0].getImgMat().ColumnDimension; ProjectMatrix sumMat = new ProjectMatrix(new Matrix(r, c), firstIndex.Key); for (int t = 0; t < noOftrainingPerInd; t++) { int projectedIndex = n + firstIndex.Value + N * t; sumMat.getImgMat().AddEquals(weights[projectedIndex].getImgMat()); } sumMat.getImgMat().MultiplyEquals((double)1 / noOftrainingPerInd); weights.Add(sumMat); } } }
// testFace has been projected to the subspace static public ProjectMatrix[] findKNN(ProjectMatrix[] trainingSet, Matrix testFace, int K, Metric metric) { int NumOfTrainingSet = trainingSet.Length; //assert K <= NumOfTrainingSet : "K is lager than the length of trainingSet!"; Debug.Assert(K <= NumOfTrainingSet, "K is lager than the length of trainingSet!"); // initialization ProjectMatrix[] neighbors = new ProjectMatrix[K]; int i; for (i = 0; i < K; i++) { trainingSet[i].setDistance(metric.getDistance(trainingSet[i].getImgMat(), testFace)); // System.out.println("index: " + i + " distance: " // + trainingSet[i].distance); neighbors[i] = trainingSet[i]; } // go through the remaining records in the trainingSet to find K nearest // neighbors for (i = K; i < NumOfTrainingSet; i++) { trainingSet[i].setDistance(metric.getDistance(trainingSet[i].getImgMat(), testFace)); // System.out.println("index: " + i + " distance: " // + trainingSet[i].distance); int maxIndex = 0; for (int j = 0; j < K; j++) { if (neighbors[j].getDistance() > neighbors[maxIndex].getDistance()) { maxIndex = j; } } if (neighbors[maxIndex].getDistance() > trainingSet[i].getDistance()) { neighbors[maxIndex] = trainingSet[i]; } } return(neighbors); }
public ModularFaceRecognitionAlgorithms(List <Matrix> trainSet, List <String> trainLables, int numOfComponents, int N, int noOftrainingPerInd) { this.noOftrainingPerInd = noOftrainingPerInd; this.N = N; this.trainingSet = new List <Matrix>(); this.labels = new List <string>(); devideTheTrainingSetToN(trainSet, trainLables, N); this.numOfComponents = numOfComponents; this.meanMatrix = getMean(this.trainingSet); //the average matrix of all images this.weightMatrix = getFeature(this.trainingSet, this.numOfComponents); //extract the features // Construct projectedTrainingGeneralMatrix this.projectSet = new List <ProjectMatrix>(); for (int i = 0; i < trainingSet.Count; i++) { ProjectMatrix ptm = new ProjectMatrix (this.weightMatrix.Transpose().Multiply(trainingSet[i].Subtract(meanMatrix)), labels[i]); this.projectSet.Add(ptm); if (!firstIndexMap.ContainsKey(labels[i])) { firstIndexMap.Add(labels[i], i); } } formSubMatrixWeights(); int count = 0; for (int u = 1; u <= 40; u++) { String filePath = "C:/img/s" + u + "/10.bmp"; Matrix temp; temp = FileManager.GetBitMapColorMatrix(filePath); string x = this.test(temp); if (x.Equals("s" + u.ToString())) { count++; } } int m = count; }
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(); }
// get the class label by using neighbors static String classify(ProjectMatrix[] neighbors) { Dictionary <String, Double> map = new Dictionary <String, Double>(); int num = neighbors.Length; for (int index = 0; index < num; index++) { ProjectMatrix temp = neighbors[index]; String key = temp.getLabel(); if (!map.ContainsKey(key)) { map.Add(key, 1 / temp.getDistance()); } else { double value = map[key]; // to get value from key value += 1 / temp.getDistance(); map[key] = value; // to put into dictionary } } // Find the most likely label double maxSimilarity = 0; String returnLabel = ""; //Set<String> labelSet = map.keySet(); HashSet <string> labelSet = new HashSet <string>(map.Keys); foreach (String label in labelSet) { double value = map[label]; if (value > maxSimilarity) { maxSimilarity = value; returnLabel = label; } } return(returnLabel); }
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(); }
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(); }