Exemplo n.º 1
0
        //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);
        }
Exemplo n.º 2
0
        // 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);
            }
        }