/*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); }