public static LDA loadTraining( int TRAINING_SIZE, int TEST_SIZE, string SKETCH_PATH, string SKETCH_EXTENSION, string LDA_FILE_NAME, out List <string> trainingSetSketchesPath, out List <string> testSetSketchesPath ) { LDA lda; List <FileInfo> files = new List <FileInfo>(); DirectoryInfo dinfo = new DirectoryInfo(SKETCH_PATH); files.AddRange(dinfo.GetFiles(SKETCH_EXTENSION)); Random rnd = new Random(); if (File.Exists(LDA_FILE_NAME)) { Stream openFileStream = File.OpenRead(LDA_FILE_NAME); BinaryFormatter deserializer = new BinaryFormatter(); lda = (LDA)deserializer.Deserialize(openFileStream); openFileStream.Close(); trainingSetSketchesPath = new List <string>(); testSetSketchesPath = new List <string>(); foreach (FileInfo file in files) { foreach (string train in lda.trainingSet) { if (!file.Name.StartsWith(train)) { testSetSketchesPath.Add(file.FullName); break; } } } testSetSketchesPath = testSetSketchesPath.OrderBy(x => rnd.Next()).ToList(); testSetSketchesPath = testSetSketchesPath.Take(TEST_SIZE).ToList(); } else { lda = new LDA(); files = files.OrderBy(x => rnd.Next()).ToList(); List <FileInfo> trainingSet = files.Take(TRAINING_SIZE).ToList(); List <FileInfo> testSet = files.Skip(TRAINING_SIZE).Take(TEST_SIZE).ToList(); trainingSetSketchesPath = trainingSet.Select(file => file.FullName).ToList(); testSetSketchesPath = testSet.Select(file => file.FullName).ToList(); lda.trainingSet = trainingSetSketchesPath.Select(file => file.Substring(file.LastIndexOf('\\') + 1, 5)).ToList(); } return(lda); }
//test on all images public void accuracyTest(BackgroundWorker worker, out double rank1, out double rank10, out double rank50, out double rank100, out double rank200) { rank1 = 0; rank10 = 0; rank50 = 0; rank100 = 0; rank200 = 0; if (lda == null) { lda = LDA.loadTraining( TRAINING_SIZE, TEST_SIZE, SKETCH_PATH, SKETCH_EXTENSION, LDA_FILE_NAME, out trainingSetSketchesPath, out testSetSketchesPath ); } for (int i = 0; i < testSetSketchesPath.Count; i++) { search(testSetSketchesPath[i], worker, true); var index = getSubjectIndex(); if (index == 1) { rank1++; } if (index <= 10) { rank10++; } if (index <= 50) { rank50++; } if (index <= 100) { rank100++; } if (index <= 200) { rank200++; } worker.ReportProgress(i * 100 / testSetSketchesPath.Count); } rank1 /= testSetSketchesPath.Count; rank10 /= testSetSketchesPath.Count; rank50 /= testSetSketchesPath.Count; rank100 /= testSetSketchesPath.Count; rank200 /= testSetSketchesPath.Count; worker.ReportProgress(100); }
// main query method public void search(String sketchPath, BackgroundWorker worker = null, bool progressOnlyDescriptor = false) { bool progress = (worker != null); if (sketchPath.Equals("")) { return; } if (lda == null) { lda = LDA.loadTraining( TRAINING_SIZE, TEST_SIZE, SKETCH_PATH, SKETCH_EXTENSION, LDA_FILE_NAME, out trainingSetSketchesPath, out testSetSketchesPath ); } if (descriptors == null) { if (File.Exists(DESCRIPTOR_FILE_NAME)) { Stream openFileStream = File.OpenRead(DESCRIPTOR_FILE_NAME); BinaryFormatter deserializer = new BinaryFormatter(); descriptors = (List <FaceDescriptor>)deserializer.Deserialize(openFileStream); openFileStream.Close(); } else { descriptors = new List <FaceDescriptor>(); List <FileInfo> files = new List <FileInfo>(); DirectoryInfo dinfo = new DirectoryInfo(PHOTO_PATH); files.AddRange(dinfo.GetFiles(PHOTO_EXTENSION)); dinfo = new DirectoryInfo(OTHER_PHOTO_PATH); files.AddRange(dinfo.GetFiles(PHOTO_EXTENSION)); files.AddRange(dinfo.GetFiles(OTHER_PHOTO_EXTENSION)); for (int i = 0; i < files.Count; i++) { FaceDescriptor face = processImage(files[i].FullName, files[i].Name, true); if (face != null) { descriptors.Add(face); } if (progress) { worker.ReportProgress(i * 100 / files.Count); } } Stream SaveFileStream = File.Create(DESCRIPTOR_FILE_NAME); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(SaveFileStream, descriptors); SaveFileStream.Close(); } LDA.training(lda, descriptors, trainingSetSketchesPath, processImage, TRAINING_SIZE, LDA_FILE_NAME, worker); for (int i = 0; i < descriptors.Count; i++) { for (int k = 0; k < descriptors[i].DescriptorHog.Count(); k++) { descriptors[i].DescriptorHog[k] *= (float)lda.projectingVectorHOG[k]; } for (int k = 0; k < descriptors[i].DescriptorSift.Count(); k++) { descriptors[i].DescriptorSift[k] *= (float)lda.projectingVectorSIFT[k]; } } } if (progress && !progressOnlyDescriptor) { worker.ReportProgress(0); } sketchName = sketchPath.Substring(sketchPath.LastIndexOf('\\') + 1, 5); FaceDescriptor sketchFace = processImage(sketchPath, sketchName, false); resultHog = new SortedDictionary <double, string>(); resultSift = new SortedDictionary <double, string>(); resultBordaCount = new SortedDictionary <double, string>(); for (int i = 0; i < sketchFace.DescriptorHog.Count(); i++) { sketchFace.DescriptorHog[i] *= (float)lda.projectingVectorHOG[i]; } for (int i = 0; i < sketchFace.DescriptorSift.Count(); i++) { sketchFace.DescriptorSift[i] *= (float)lda.projectingVectorSIFT[i]; } int hogSize = 540; int siftSize = 512; for (int i = 0; i < descriptors.Count; i++) { addDictionaryUnique( euclideanDistance( sketchFace.DescriptorHog, descriptors[i].DescriptorHog, hogSize ), descriptors[i].Name, resultHog); addDictionaryUnique( euclideanDistance( sketchFace.DescriptorSift, descriptors[i].DescriptorSift, siftSize ), descriptors[i].Name, resultSift); if (progress && !progressOnlyDescriptor) { worker.ReportProgress(i * 100 / descriptors.Count); } } Dictionary <string, int> dictionary = new Dictionary <string, int>(); int count = 0; foreach (var r in resultHog) { dictionary.Add(r.Value, (resultHog.Count - count) * 2);//weight count++; } count = 0; foreach (var r in resultSift) { dictionary[r.Value] += resultSift.Count - count; count++; } foreach (var r in dictionary) { double value = r.Value; //negative int because sorted dictionary work in ascending mode only addDictionaryUnique(-r.Value, r.Key, resultBordaCount); } if (progress && !progressOnlyDescriptor) { worker.ReportProgress(100); } }
public static void training( LDA lda, List <FaceDescriptor> descriptors, List <string> trainingSetSketchesPath, Func <string, string, bool, FaceDescriptor> processImage, int TRAINING_SIZE, string LDA_FILE_NAME, BackgroundWorker worker = null ) { bool progress = (worker != null); if (lda.projectingVectorHOG == null) { List <FaceDescriptor> trainingDescriptors = new List <FaceDescriptor>(); for (int i = 0; i < trainingSetSketchesPath.Count; i++) { FaceDescriptor face = processImage(trainingSetSketchesPath[i], lda.trainingSet[i], true); if (face != null) { trainingDescriptors.Add(face); } if (progress) { worker.ReportProgress(i * 100 / trainingSetSketchesPath.Count); } } if (progress) { worker.ReportProgress(0); } Dictionary <string, FaceDescriptor> descriptorsWithName = new Dictionary <string, FaceDescriptor>(); foreach (var descriptor in descriptors) { string name = descriptor.Name.Substring(0, 5); if (!descriptorsWithName.ContainsKey(name)) { descriptorsWithName.Add(name, descriptor); } } int NPoints = TRAINING_SIZE * 2; int NClasses = TRAINING_SIZE; int NVarsHOG = trainingDescriptors[0].DescriptorHog.Count(); int NVarsSIFT = trainingDescriptors[0].DescriptorSift.Count(); double[,] xyHOG = new double[NPoints, NVarsHOG + 1]; double[,] xySIFT = new double[NPoints, NVarsSIFT + 1]; for (int i = 0; i < TRAINING_SIZE; i++) { FaceDescriptor photoDescriptor = descriptorsWithName[trainingDescriptors[i].Name]; for (int k = 0; k < NVarsHOG; k++) { xyHOG[i, k] = trainingDescriptors[i].DescriptorHog[k]; xyHOG[i + TRAINING_SIZE, k] = photoDescriptor.DescriptorHog[k]; } xyHOG[i, NVarsHOG] = i; xyHOG[i + TRAINING_SIZE, NVarsHOG] = i; for (int k = 0; k < NVarsSIFT; k++) { xySIFT[i, k] = trainingDescriptors[i].DescriptorSift[k]; xySIFT[i + TRAINING_SIZE, k] = photoDescriptor.DescriptorSift[k]; } xySIFT[i, NVarsSIFT] = i; xySIFT[i + TRAINING_SIZE, NVarsSIFT] = i; if (progress) { worker.ReportProgress(i * 100 / TRAINING_SIZE); } } int info = 0; double[] wHOG = new double[0]; double[] wSIFT = new double[0]; alglib.lda.fisherlda(xyHOG, NPoints, NVarsHOG, NClasses, ref info, ref wHOG, null); alglib.lda.fisherlda(xySIFT, NPoints, NVarsSIFT, NClasses, ref info, ref wSIFT, null); lda.projectingVectorHOG = wHOG; lda.projectingVectorSIFT = wSIFT; Stream SaveFileStream = File.Create(LDA_FILE_NAME); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(SaveFileStream, lda); SaveFileStream.Close(); } }