Пример #1
1
        public SVM_Matlab(string path, double sigma, double gamma, Kernel kernel, ImageVector[] goodImages, ImageVector[] badImages)
        {
            type = "SVM";

            this.goodImages = goodImages;
            this.badImages = badImages;

            learnedTrue = new bool[goodImages.Length];
            learnedFalse = new bool[badImages.Length];

            confidenceTrue = new double[goodImages.Length];
            confidenceFalse = new double[badImages.Length];

            restartTest();

            userPath = path + "\\SVM_" + getKernel(kernel) + "_" + sigma;
            resultPath = userPath + "\\" + sigma + "_" + gamma;
            learn = GetLearnCommand(userPath, sigma, gamma, kernel);
            learn_Test = GetLearnCommand_Test(userPath, sigma, gamma, kernel,goodImages[0].getNumOfParameters());
            decide = GetDecideCommand(userPath, kernel);
            decide_Test = GetDecideCommand_Test(userPath, kernel, goodImages[0].getNumOfParameters());

            matlab = new MLApp.MLApp();
            cd = "cd " + smartAlbum.getMatlabDirectory();
            matlab.Execute(cd);
        }
Пример #2
0
        public KNN_SVM(string path, double sigma, double gamma, Kernel kernel, int k, ImageVector[] goodImages, ImageVector[] badImages)
        {
            type = "KNN & SVM";
            svm = new SVM_Matlab(path, sigma, gamma, (SVM_Matlab.Kernel)kernel, goodImages, badImages);
            knn = new KNN_Matlab(path, k, goodImages, badImages);

            totalImages = 0;

            this.goodImages = goodImages;
            this.badImages = badImages;

            learnedTrue = new bool[goodImages.Length];
            learnedFalse = new bool[badImages.Length];
            restartTest();

            userPath = path + "\\KNN_SVM";
            resultPath = userPath + "\\sigma_" + sigma + "_gamma_" + gamma;

            learn = GetLearnCommand(userPath, sigma, gamma, kernel);
            decideSVM = GetDecideCommandSVM(userPath, kernel);
            decideKNN = GetDecideCommandKNN(userPath, k);

            matlab = new MLApp.MLApp();
            cd = "cd " + smartAlbum.getMatlabDirectory();
            matlab.Execute(cd);
        }
Пример #3
0
        private string _path; // The path of the picture represented by this vector

        #endregion Fields

        #region Constructors

        /* Copy constructor for image vector */
        public ImageVector(ImageVector other)
        {
            Path = other.Path;
            _DicParameter = other._DicParameter;
            _parameterVector = new double[NUMBER_OF_PARAMETERS];
            for (int i = 0; i < _parameterVector.Length; i++)
                _parameterVector[i] = other._parameterVector[i];
        }
Пример #4
0
        public static void convertDataVectorsToFile(string path, ImageVector[] images, int[] subset, bool[] isImageGood)
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            string csvPathLearn = path + "\\Learn.txt";
            System.IO.StreamWriter fileLearn = new System.IO.StreamWriter(@csvPathLearn, false);

            int length = subset.Length;

            for (int index = 0; index < length; index++)
            {
                if (isImageGood[index])
                    fileLearn.WriteLine(images[subset[index]].getAllParameters(true));
                else
                    fileLearn.WriteLine(images[subset[index]].getAllParameters(false));
            }
        }
Пример #5
0
        /* Calssifies one given image vector */
        public static string ClassifyVector(ImageVector vector, ref double[] how_much_pictures, ref double[] how_much_good, bool how_is_picture)
        {
            bool paramExist;

            string result = String.Empty;

            for (int j = 0; j < ImageVector.NUMBER_OF_PARAMETERS; j++)
            {
                vector.DicParameter.TryGetValue(ImageVector.getParameterNameByIndex(j) , out paramExist);
                if (paramExist)
                {
                    result += getParameterClassification(ImageVector.getParameterNameByIndex(j), vector.getParameterByIndex(j));
                    Preparation_remainder(j, ImageVector.getParameterNameByIndex(j), how_is_picture, vector.getParameterByIndex(j), ref  how_much_pictures, ref how_much_good);
                }
            }

            //System.Windows.Forms.MessageBox.Show("classify " + result);
            return result;
        }
Пример #6
0
        public SVM_Matlab(string path, double sigma, double gamma, Kernel kernel, ImageVector[] allImages)
        {
            type = "SVM";

            this.allImages = allImages;

            filesTrueResults = new Algorithm[allImages.Length];

            for (int i = 0; i < filesTrueResults.Length; i++)
                filesTrueResults[i] = LearningAlgorithmML.Algorithm.BAD;

            userPath = path + "\\SVM_" + getKernel(kernel) + "_" + sigma;
            resultPath = userPath + "\\" + sigma + "_" + gamma;
            learn = GetLearnCommand(userPath, sigma, gamma, kernel);
            decide = GetDecideCommand(userPath, kernel);

            matlab = new MLApp.MLApp();
            cd = "cd " + smartAlbum.getMatlabDirectory();
            matlab.Execute(cd);
        }
Пример #7
0
        //private double runConf;
        public KNN_Matlab(string path, int k, ImageVector[] goodImages, ImageVector[] badImages)
        {
            type = "KNN";

            this.goodImages = goodImages;
            this.badImages = badImages;

            learnedTrue = new bool[goodImages.Length];
            learnedFalse = new bool[badImages.Length];

            restartTest();

            userPath = path + "\\KNN";
            resultPath = userPath + "\\" + k;

            decide = GetCommand(userPath, k);
            decide_Test = GetCommand_Test(userPath, k, goodImages[0].getNumOfParameters());
            matlab = new MLApp.MLApp();
            cd = "cd " + smartAlbum.getMatlabDirectory();
            matlab.Execute(cd);
        }
Пример #8
0
        public KnnML(string path, int k, ImageVector[] goodImages, ImageVector[] badImages)
        {
            this.k = k;

            type = "KNN";

            knn = new KNearest();

            this.goodImages = goodImages;
            this.badImages = badImages;

            learnedTrue = new bool[goodImages.Length];
            learnedFalse = new bool[badImages.Length];

            confidenceTrue = new double[goodImages.Length];
            confidenceFalse = new double[badImages.Length];

            restartTest();

            userPath = path + "\\KNN";
            resultPath = userPath + "\\" + k;
        }
Пример #9
0
        public bool Decide()
        {
            // Get list of files in folders
            String[] files = Testing.GetFilesFromDirectory(decidePath).ToArray();
            string csvPath = userPath + "\\Decide.txt";
            string resultPath = userPath + "\\Result.txt";

            for (int i = 0; i < files.Length; i++)
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(@csvPath, false);
                ImageVector vec = new ImageVector(files[i], null);
                file.WriteLine(vec.getAllParameters(false));
                file.Close();

                MLApp.MLApp matlab = new MLApp.MLApp();

                string cd = "cd C:\\Users\\anoach\\Desktop\\papa_matlab";
                //string exe = "albumSvm('" + userPath + "');";
                string exe = "albumKnn('" + userPath + "', " + 1 + ");"; //K = 1

                matlab.Execute(cd);
                matlab.Execute(exe);

                string[] readText = File.ReadAllLines(resultPath);
                if (readText[0] == "1")
                {
                    Console.WriteLine("Good Image");
                }
                else
                {
                    Console.WriteLine("Bad Image. Delete it");
                    File.Delete(files[i]);
                }

            }

               return true;
        }
Пример #10
0
        public static void convertDataVectorsToFile_Test(string path, ImageVector[] goodImages, ImageVector[] badImages, int[] subsetFilesTrue, int[] subsetFilesFalse, bool subset)
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            string csvPathLearn = path + "\\Learn.txt";
            System.IO.StreamWriter fileLearn = new System.IO.StreamWriter(@csvPathLearn, false);

            int length;
            if (subset)
                length = subsetFilesTrue.Length;
            else
                length = goodImages.Length;

            for (int index = 0; index < length; index++)
            {
                if (subset)
                    fileLearn.WriteLine(goodImages[subsetFilesTrue[index]].getAllParameters_Test(true,goodImages[index].getNumOfParameters()));
                else
                    fileLearn.WriteLine(goodImages[index].getAllParameters_Test(true, goodImages[index].getNumOfParameters()));
            }

            if (subset)
                length = subsetFilesFalse.Length;
            else
                length = badImages.Length;

            for (int index = 0; index < length; index++)
            {
                if (subset)
                    fileLearn.WriteLine(badImages[subsetFilesFalse[index]].getAllParameters_Test(false,badImages[index].getNumOfParameters()));
                else
                    fileLearn.WriteLine(badImages[index].getAllParameters_Test(false, badImages[index].getNumOfParameters()));
            }

            fileLearn.Close();
        }
Пример #11
0
        public void Tests(out ImageVector[] goodImages, out ImageVector[] badImages)
        {
            goodImages = null;
            badImages = null;

            // Get list of files in folders
            string[] filesAll = Testing.GetFilesFromDirectory(allPath).ToArray();
            string[] filesTrue = Testing.GetFilesFromDirectory(truePath).ToArray();
            if (filesAll.Length <= 0 || filesTrue.Length <= 0)
                return;

            string[] filesFalse = DataConverter.Internal_ExtractFalseFiles(filesAll, filesTrue);

            //contains the parameters for each picture and the picture path

            extractAlbum(out goodImages, out badImages, filesTrue, filesFalse, userPath, false);

            // remove comment if you want to optimize one of the algorithms

            // optimize svm
            // Internal_SVM(goodImages, badImages);
            // optimize knn
            // Internal_KNN(goodImages, badImages);
            // optimize knn and svm`
            // Internal_KNN_SVM(goodImages, badImages, true); //regular intersection
            //testIntersection(goodImages, badImages); //smart intersection
            int k = 2;
            for (double i = 0; i <= 5; i += 1)
            {
                for (double j = 0; j <= 0; j += 1)
                {
                    double sigma = Math.Pow(2, i);
                    double gamma = Math.Pow(2, j);
                    learningAlgo = new KNN_SVM(userPath, sigma, gamma, KNN_SVM.Kernel.SVM_RBF, k, goodImages, badImages);
                }
            }
            // test using cross validation
            //learningAlgo = new SVM_Matlab(userPath, -5, -5, SVM_Matlab.Kernel.SVM_RBF);
            //svm = new SVM(SVM.Kernel.SVM_RBF, 1, 1);
            //crossValidation(goodImages, badImages);
            // test on different albums
            //differentAlbums(goodImages, badImages);

            //  copyGoodImages(filesTrue, filesFalse);

            learningAlgo.Quit();
        }
Пример #12
0
 public void testIntersection(ImageVector[] goodImages, ImageVector[] badImages)
 {
     for (double conf = 0.5; conf <= 0.5; conf += 0.1)
     {
         SVM_Matlab.CONFIDENCE_BAR = conf;
         learningAlgo = new smartIntersection(userPath, goodImages, badImages);
         crossValidation(goodImages, badImages);
     }
 }
Пример #13
0
        //, Boolean isTest)
        /*
         * this test is for finding the best optimization for SVM algorithm
         * trying sigma and gamma for the rbf kernel for best accuracy.
         */
        public void Internal_KNN_SVM(ImageVector[] goodImages, ImageVector[] badImages)
        {
            int k = 2;
            for (double i = 0; i <= 5; i += 1)
            {
                for (double j = 0; j <= 0; j += 1)
                {
                    double sigma = Math.Pow(2, i);
                    double gamma = Math.Pow(2, j);
                    learningAlgo = new KNN_SVM(userPath, sigma, gamma, KNN_SVM.Kernel.SVM_RBF, k, goodImages, badImages);
                    if (!isTest)
                    {
                            crossValidation(goodImages, badImages);

                    }
                    else
                        crossValidation_second(goodImages, badImages);
                }
            }
        }
Пример #14
0
 /*
  * this test is for finding the best optimization for KNN algorithm
  * finding the best k for this classification
  */
 public void Internal_KNN(ImageVector[] goodImages, ImageVector[] badImages)
 {
     for (int k = 2; k <= 2; k++)
     {
         learningAlgo = new KNN_Matlab(userPath, k, goodImages, badImages);
         crossValidation(goodImages, badImages);
     }
 }
Пример #15
0
        public void crossValidation_Test(ImageVector[] goodImages, ImageVector[] badImages)
        {
            double totalImages = goodImages.Length + badImages.Length;
            double lowest, goodPercent = goodImages.Length / totalImages;
            lowest = 1 - goodPercent;
            lowest -= 0.10;
            bool quit = false;
            for (double percent = 0.3; percent < 0.4; percent += 0.1)
            {
                int p = (int)(percent * 100);

                for (int i = 1; i <= 1; i++)
                {

                    Console.WriteLine("Percent = " + percent + ",Iteration =  " + i + ", Time: " + string.Format("{0:HH:mm:ss tt}", DateTime.Now));
                    Console.WriteLine("Learning from " + p + "% of the album");

                    int[] subsetFilesTrue = DataConverter.Internal_ExtractSubset(goodImages.Length, percent);
                    int[] subsetFilesFalse = DataConverter.Internal_ExtractSubset(badImages.Length, percent);

                    Stopwatch stopWatch = new Stopwatch();
                    stopWatch.Start();
                    if (!learningAlgo.Learn_Test(percent, subsetFilesTrue, subsetFilesFalse))
                    {
                        MessageBox.Show("learn failed");
                        learningAlgo.Quit();
                        return;
                    }
                    if (!learningAlgo.Decide_Test())
                    {
                        MessageBox.Show("decide failed");
                        learningAlgo.Quit();
                        return;
                    }
                    learningAlgo.checkDecision();
                    stopWatch.Stop();
                    double elapsed = (double)stopWatch.ElapsedMilliseconds / 1000;
                    Console.WriteLine("Total time " + elapsed);
                }

                string algoPath = learningAlgo.ResultPath;
                if (!Directory.Exists(algoPath))
                    Directory.CreateDirectory(algoPath);
                string DataPath = algoPath + "\\Data_" + p + "_" + SVM_Matlab.CONFIDENCE_BAR + ".txt";
                System.IO.StreamWriter file = new System.IO.StreamWriter(@DataPath, false);
                string data = learningAlgo.ToString();

                //get precision and fscore value
                string[] tempArr = data.Split(',');
                double precision = Convert.ToDouble(tempArr[7]);
                double Fscore = Convert.ToDouble(tempArr[8].Substring(0, tempArr[8].IndexOf('\n')));
                Console.WriteLine("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  pre: " + precision + " fsco: " + Fscore);

                file.WriteLine(data);
                file.Close();
                if (quit)
                    return;

                //for the search algoritem
                //  bestTest = bfs.getAtt();

                List<int> subGroup = bestTest.OfType<int>().ToList();
                attributeTest = new Attribute();
                attributeTest.setSubGroup(subGroup);
                attributeTest.setFscore_pressision(Fscore, precision);
                Console.WriteLine("pre: " + precision + " fsco: " + Fscore);
                if (!isCVT)
                    bfs.setBest(attributeTest);
                isCVT = true;
                bfs.addOpen(attributeTest);
                //bfs.calcNeighbors(attributeTest);//,goodImages,badImages);

                // starting to test new percent so reset all the data to zero
                //learningAlgo.restartTest();

            }
        }
Пример #16
0
 public void crossValidation_second(ImageVector[] goodImages, ImageVector[] badImages)
 {
     if (!isCVT)
         crossValidation_Test(goodImages, badImages);
     List<Attribute> arr = bfs.BFS_Inc_Dec();//increseNum();
         if (arr == null)
     {
         Console.WriteLine(bfs.getBest().toString());
         return;
     }
     foreach (Attribute att in arr)
     {
         bestTest = att.getSubGroub().ToArray();
         crossValidation_Test(goodImages, badImages);
     }
 }
Пример #17
0
        //the main goodImages,badImages
        private void Internal_KNN_SVM_Test(ImageVector[] goodImages, ImageVector[] badImages, int[] subGroupAttribute)
        {
            ImageVector[] subGood;
            ImageVector[] subBad;
            calcSubImage(out subGood, out subBad, goodImages, badImages, subGroupAttribute);
            isTest = true;

            Internal_KNN_SVM(subGood, subBad);//, true);
        }
Пример #18
0
        private void Internal_getImageFromFile(ref ImageVector[] goodImages, ref ImageVector[] badImages, string goodPath, string badPath, int goodSize, int badSize)
        {
            System.IO.StreamReader fileImages = new System.IO.StreamReader(goodPath);
            int size = File.ReadAllLines(goodPath).Count();
            string line;
            int goodIndex = 0;
            int badIndex = 0;
            goodImages = new ImageVector[goodSize];
            badImages = new ImageVector[badSize];

            while ((line = fileImages.ReadLine()) != null)
            {
                string[] imageParams = line.Split(',');
                double[] parameters = new double[imageParams.Length - 1];

                for (int i = 0; i < imageParams.Length - 1; i++)
                    parameters[i] = Convert.ToDouble(imageParams[i]);

                if (imageParams[imageParams.Length - 1] == "1")
                    goodImages[goodIndex++] = new ImageVector(parameters);
                else
                    badImages[badIndex++] = new ImageVector(parameters);
            }

            fileImages.Close();
            fileImages = new System.IO.StreamReader(badPath);

            while ((line = fileImages.ReadLine()) != null)
            {
                string[] imageParams = line.Split(',');
                double[] parameters = new double[imageParams.Length - 1];

                for (int i = 0; i < imageParams.Length - 1; i++)
                    parameters[i] = Convert.ToDouble(imageParams[i]);

                if (imageParams[imageParams.Length - 1] == "1")
                    goodImages[goodIndex++] = new ImageVector(parameters);
                else
                    badImages[badIndex++] = new ImageVector(parameters);
            }
            fileImages.Close();
        }
Пример #19
0
        private void extractAlbum(out ImageVector[] allImages, string[] filesAll, bool checkFiles)
        {
            allImages = null;
            string allImagesPath = userPath + "\\allImages.txt";
            if ((!File.Exists(allImagesPath) || new FileInfo(allImagesPath).Length == 0 || checkFiles))
            {
                Console.WriteLine("Process images: " + string.Format("{0:HH:mm:ss tt}", DateTime.Now));
                allImages = Internal_GetImages(filesAll);

                System.IO.StreamWriter fileImages = new System.IO.StreamWriter(allImagesPath, false);
                for (int index = 0; index < allImages.Length; index++)
                {
                    fileImages.WriteLine(allImages[index].getAllParameters(true));
                }

                fileImages.Close();
            }
            else
                Internal_getImageFromFile(ref allImages, allImagesPath, filesAll.Length);
        }
Пример #20
0
        //create 2 files with the info of pic with the subGroup parameter
        private void createFileForAttributePic(ImageVector[] goodImages, ImageVector[] badImages, int[] attribute)
        {
            string goodPath = userPath + "\\goodTest.txt";
            string badPath = userPath + "\\badTest.txt";
            System.IO.StreamWriter fileGood = new System.IO.StreamWriter(goodPath, true);
            System.IO.StreamWriter fileBad = new System.IO.StreamWriter(badPath, true);

            string str = "", temp = "";
            if (!File.Exists(goodPath) || !File.Exists(badPath) ||
                new FileInfo(goodPath).Length == 0 || new FileInfo(badPath).Length == 0)
            {
                for (int i = 0; i < goodImages.Length; i++)
                {
                    for (int j = 0; j < attribute.Length; j++)
                        temp += goodImages[i].getParameterByIndex(attribute[j]) + ",";
                    str = temp + "0";
                    temp = "";
                    fileGood.WriteLine(str);
                }

                for (int i = 0; i < badImages.Length; i++)
                {
                    for (int j = 0; j < attribute.Length; j++)
                        temp += badImages[i].getParameterByIndex(attribute[j]) + ",";
                    str = temp + "0";
                    temp = "";
                    fileBad.WriteLine(str);

                }

            }
        }
Пример #21
0
 /* Get classification bounds array of given parameter*/
 public static void getBoundArray(ImageVector.ImageParameters param, ref double[] array)
 {
     array = null;
     switch (param)
     {
         case ImageVector.ImageParameters.averageGrayLevel:
             array = AVERAGE_GRAY_LEVEL_BOUNDS;
             break;
         case ImageVector.ImageParameters.averageGreenLevel:
             array = AVERAGE_GREEN_LEVEL_BOUNDS;
             break;
         case ImageVector.ImageParameters.averageRedLevel:
             array = AVERAGE_RED_LEVEL_BOUNDS;
             break;
         case ImageVector.ImageParameters.averageBlueLevel:
             array = AVERAGE_BLUE_LEVEL_BOUNDS;
             break;
         case ImageVector.ImageParameters.averageHueLevel:
             array = AVERAGE_HUE_LEVEL_BOUNDS;
             break;
         case ImageVector.ImageParameters.averageSaturationLevel:
             array = AVERAGE_SATURATION_LEVEL_BOUNDS;
             break;
         case ImageVector.ImageParameters.numOfPoeple:
             array = NUM_OF_PEOPLE_BOUNDS;
             break;
         case ImageVector.ImageParameters.edges:
             array = EDGES_BOUNDS;
             break;
        case ImageVector.ImageParameters.redEye:
             array = RED_EYE_BOUNDS;
             break;
         case ImageVector.ImageParameters.distanceFromGravityCenter:
             array = DISTANCE_FROM_COG_BOUNDS;
             break;
         case ImageVector.ImageParameters.facesImageAreaRatio:
             array = IMAGE_FACES_AREA_RATIO_BOUNDS;
             break;
         case ImageVector.ImageParameters.facesCenterOfGravityX:
             array = CENTER_OF_GRAVITY_BOUNDS;
             break;
         case ImageVector.ImageParameters.facesCenterOfGravityY:
             array = CENTER_OF_GRAVITY_BOUNDS;
             break;
         case ImageVector.ImageParameters.variance:
             array = VARIANCE_BOUNDS;
             break;
         case ImageVector.ImageParameters.imageInformation:
             array = IMAGE_INFORMATION_BOUNDS;
             break;
         case ImageVector.ImageParameters.stdBlur:
             array = STD_BLUR_BOUNDS;
             break;
         case ImageVector.ImageParameters.faceBlur: //!!!!!!!
             array = FACE_BLUR_BOUNDS;
             break;
         case ImageVector.ImageParameters.closedEye: //!!!!!!!
             array = FACE_BLUR_BOUNDS;
             break;
         case ImageVector.ImageParameters.numOfSmiles: //!!!!!!!
             array = NUM_OF_SMILES_BOUNDS;
             break;
         default:
             throw (new Exception("Classification for " + param.ToString() + " is not implemented"));
     }
 }
Пример #22
0
        // extract specific lines by bound and attributes from goodImages and badImages files
        private void getIndexFromFile(ref ImageVector[] subGoodImages, ref ImageVector[] subBadImages, string goodPath, string badPath, string attributeFilePath)
        {
            string starLine = "";
            CurrentAttrubuteList(attributeFilePath);

            int sizeAttributeFile = File.ReadAllLines(attributeFilePath).Count();
            int lineNum = 0;
            string line,tempLine = "";

            System.IO.StreamReader fileAttribute = new System.IO.StreamReader(attributeFilePath);

            List<String> lineNunToDelete = new List<String>();

            List <ImageVector> goodList = new List<ImageVector>();
            List<ImageVector> badList = new List<ImageVector>();
            int attributeNum = -1, boundNum = -1;
            int tempCount = 0;
            string tmpLine = "";
            int prevStarLine = 0;
            int attributeCurrNum=-1;
            if (!writeFlag)//we didnt write
            {

               prevStarLine = readFromTheEnd(attributeFilePath);
                if (prevStarLine != -1 && prevStarLine != 0)
                {

                    tempCount = starList.IndexOf(prevStarLine + 1);

                    starLine = File.ReadAllLines(attributeFilePath).Skip(prevStarLine).Take(1).First();//go to the last * line
                    do
                    {

                        while ((line = fileAttribute.ReadLine()) != starLine) { tempCount++; }

                    } while (tempCount != prevStarLine);
                    lineNum = tempCount + 1;

                }

                if (prevStarLine != -1 && currentAttributeList.Count != 0)
                {
                    while (attributeList[attributeList.Count - 1] != currentAttributeList[currentAttributeList.Count - 1])
                        attributeList.RemoveAt(attributeList.Count - 1);
                }

            }

            if (starList.Count > 1 && writeFlag)
            {
                tempCount = 0;
                starLineCount++;
                starLine = File.ReadAllLines(attributeFilePath).Skip(starList[starList.Count - 1]-1).Take(1).First();//go to the last * line
                do
                {
                    while ((line = fileAttribute.ReadLine()) != starLine) ;
                    tempCount++;
                } while (tempCount != starLineCount);

            }

            while ((line = fileAttribute.ReadLine()) != null )
            {

                lineNum++;
                Console.WriteLine("getIndexFromFile: line " + line + " numLine: " + lineNum);

                if (line.Contains("*"))
                {
                  /*  if(starList.Count >= 1)
                        starList.Add(lineNum + starList[starList.Count - 1]);
                    else
                        starList.Add(lineNum);*/
                    break;

                }
                if (line.Contains("deleted"))
                {
                    deleteFlag = true;
                    continue;
                }
                deleteFlag = false;
                //read line from attribute file split by ',' and convert to int array
                string[] temp = line.Split(',');
                int[] attributeInfo = new int[temp.Length];

                for (int i = 0; i < temp.Length ; i++)
                    attributeInfo[i] = Int32.Parse(temp[i]);

                if (attributeNum == -1 && boundNum == -1)
                {
                    attributeNum = attributeInfo[1];
                    boundNum = attributeInfo[2];
                }
                if (attributeNum == attributeInfo[1] && boundNum == attributeInfo[2])
                {

                    if (attributeInfo[attributeInfo.Length - 1] == 1)//good img
                        tempLine = File.ReadAllLines(goodPath).Skip(attributeInfo[0]).Take(1).First();//read one specific line
                    else if (attributeInfo[attributeInfo.Length - 1] == 0)//bad img
                        tempLine = File.ReadAllLines(badPath).Skip(attributeInfo[0]).Take(1).First();//read one specific line

                    lineNunToDelete.Add(line);

                    //split tempLine
                    string[] imageParams = tempLine.Split(',');
                    double[] parameters = new double[imageParams.Length - 1];
                    for (int i = 0; i < imageParams.Length - 1; i++)//imageParam-1 the last num is for indicating pic is good or bad
                    {
                        parameters[i] = Convert.ToDouble(imageParams[i]);
                       // Console.WriteLine("getIndexFromFile: parameters[i]" + parameters[i]);
                    }

                    if (imageParams[imageParams.Length - 1] == "1")
                    {
                       // subGoodImages[goodIndex++] = new ImageVector(parameters);
                        goodList.Add(new ImageVector(parameters));
                      //  Console.WriteLine("getIndexFromFile: goodIndex " + goodIndex + ",goodList " + goodList[goodIndex]);
                    }
                    else
                    {
                        badList.Add(new ImageVector(parameters));

                      //  subBadImages[badIndex++] = new ImageVector(parameters);
                    }

                }

            }

            subGoodImages = goodList.ToArray();
            subBadImages = badList.ToArray();
            goodList.Clear();
            badList.Clear();

            fileAttribute.Close();

            deleteLinesFromFile(attributeFilePath,ref lineNunToDelete);
        }
Пример #23
0
        /*
         * this function is for testing a single album using cross validation technique
         * learn from 10% - 60% of the album randomly and the decide on the rest
         * each percent is tested 10 times and the results are averaged
         */
        private void crossValidation(ImageVector[] goodImages, ImageVector[] badImages)
        {
            double totalImages = goodImages.Length + badImages.Length;
            double lowest, goodPercent = goodImages.Length / totalImages;
            lowest = 1 - goodPercent;
            lowest -= 0.10;
            bool quit = false;
            for (double percent = 0.1; percent < 0.2; percent += 0.1)
            {
                int p = (int)(percent * 100);

                for (int i = 1; i <= 1; i++)
                {

                    Console.WriteLine("Percent = " + percent + ",Iteration =  " + i + ", Time: " + string.Format("{0:HH:mm:ss tt}", DateTime.Now));
                    Console.WriteLine("Learning from " + p + "% of the album");

                    int[] subsetFilesTrue = DataConverter.Internal_ExtractSubset(goodImages.Length, percent);
                    int[] subsetFilesFalse = DataConverter.Internal_ExtractSubset(badImages.Length, percent);

                    Stopwatch stopWatch = new Stopwatch();
                    stopWatch.Start();
                    if (!learningAlgo.Learn(percent, subsetFilesTrue, subsetFilesFalse))
                    {
                        MessageBox.Show("learn failed");
                        learningAlgo.Quit();
                        return;
                    }
                    if (!learningAlgo.Decide())
                    {
                        MessageBox.Show("decide failed");
                        learningAlgo.Quit();
                        return;
                    }
                    learningAlgo.checkDecision();
                    stopWatch.Stop();
                    double elapsed = (double)stopWatch.ElapsedMilliseconds / 1000;
                    Console.WriteLine("Total time " + elapsed);
                }

                string algoPath = learningAlgo.ResultPath;
                if (!Directory.Exists(algoPath))
                    Directory.CreateDirectory(algoPath);
                string DataPath = algoPath + "\\Data_" + p + "_" + SVM_Matlab.CONFIDENCE_BAR + ".txt";
                System.IO.StreamWriter file = new System.IO.StreamWriter(@DataPath, false);
                string data = learningAlgo.ToString();
                file.WriteLine(data);
                file.Close();
                if (quit)
                    return;

                //**********calculate the most importent attributes***********//

                bestTest = calcBestAttribute(goodImages, badImages);//
                //  createFileForAttributePic(goodImages, badImages, bestTest);
                Internal_KNN_SVM_Test(goodImages, badImages, bestTest);

                //********************************//

                // starting to test new percent so reset all the data to zero
                //learningAlgo.restartTest();

            }
        }
Пример #24
0
        /// <summary>
        /// predicts for all images in folder if true or false (For testing, using given repository to retrive already scanned images)
        /// </summary>
        /// <param name="folder">path to folder with images to predict</param>
        /// <param name="res">keeps for each image if true or falde</param>
        /// <returns>true if decision was successful. else false.</returns>
        public bool DecideForTesting(List<string> allFiles, VectorRepository rep, out double[] res)
        {
            res = null;
            // Get list of files in folders
            String[] files = allFiles.ToArray();
            DecisionListcount = 0;
            String[] cVectors = new string[files.Length];
            double[] results;
            ImageVector vector;
            List<ImageVector> vectorList = new List<ImageVector>();

            for (int i = 0; i < files.Length; i++)
            {
                //get imageVectors
                vector = rep.getVectorByPath(files[i]);
                if (vector == null)
                    vector = new ImageVector(files[i], ParameterList);

                vectorList.Add(vector);
                DecisionListcount++;
            }

            //load algorithm data and predict
            //if (!_algorithm.LoadData())
            //{
            //    return false;
            //}

            if (!Algorithm.Predict(vectorList, out results))
                return false;

            res = results;
            return true;
        }
Пример #25
0
        private void extractAlbum(out ImageVector[] goodImages, out ImageVector[] badImages, string[] filesTrue,
            string[] filesFalse, string path, bool checkFiles)
        {
            goodImages = null;
            badImages = null;
            string goodImagesPath = path + "\\imagesGood.txt";
            string badImagesPath = path + "\\imagesBad.txt";
            if ((!File.Exists(goodImagesPath) || !File.Exists(badImagesPath) ||
                new FileInfo(goodImagesPath).Length == 0 || new FileInfo(badImagesPath).Length == 0) || checkFiles)
            {
                Console.WriteLine("Process good images: " + string.Format("{0:HH:mm:ss tt}", DateTime.Now));
                goodImages = Internal_GetImages(filesTrue);
                Console.WriteLine("Process bad images: " + string.Format("{0:HH:mm:ss tt}", DateTime.Now));
                badImages = Internal_GetImages(filesFalse);

                System.IO.StreamWriter fileGoodImages = new System.IO.StreamWriter(goodImagesPath, false);
                System.IO.StreamWriter fileBadImages = new System.IO.StreamWriter(badImagesPath, false);
                for (int index = 0; index < goodImages.Length; index++)
                {
                    fileGoodImages.WriteLine(goodImages[index].getAllParameters(true));

                }
                for (int index = 0; index < badImages.Length; index++)
                {
                    fileBadImages.WriteLine(badImages[index].getAllParameters(false));
                }

                fileGoodImages.Close();
                fileBadImages.Close();
            }
            else
                Internal_getImageFromFile(ref goodImages, ref badImages, goodImagesPath, badImagesPath, filesTrue.Length, filesFalse.Length);
        }
Пример #26
0
        /// <summary>
        /// predicts for all images in folder if true or false
        /// </summary>
        /// <param name="folder">path to folder with images to predict</param>
        /// <param name="res">keeps for each image if true or falde</param>
        /// <returns>true if decision was successful. else false.</returns>
        public bool Decide(string folder, out double [] res)
        {
            res = null;
            // Get list of files in folders
            String[] files;
            try
            {
                files = Testing.GetFilesFromDirectory(folder).ToArray();//Directory.GetFiles(folder);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
                return false;
            }

            DecisionListcount = 0;
            String[] cVectors= new string[files.Length];
            List<ImageVector> vectors = new List<ImageVector>();
            for(int i = 0; i < files.Length; i++)
            {
                string path = "C:\\Users\\anoach\\Desktop\\PAPA\\csv"+i+".txt";
                System.IO.StreamWriter file = new System.IO.StreamWriter(@path, true);
                //get imageVectors
                ImageVector vec = new ImageVector(files[i], ParameterList);

                vectors.Add(new ImageVector(vec));

                //Convert to clasiffied vector
                DecisionListcount++;
                file.WriteLine(vec.getAllParameters(false));
                file.Close();
            }

            double[] results;
            //load algorithm data and predict
            //if (!_algorithm.LoadData())
            //{
            //    return false;
            //}

            if (!Algorithm.Predict(vectors, out results))
                return false;

            res = results;
            return true;
        }
Пример #27
0
 private ImageVector[] Internal_GetImages(string[] imagesStr)
 {
     ImageVector[] images = new ImageVector[imagesStr.Length];
     int index = 0;
     foreach (string path in imagesStr)
     {
         int i = index + 1;
         Console.WriteLine("Processing image parameters #" + i + " out of " + imagesStr.Length);
         images[index] = new ImageVector(path, null);
         index++;
     }
     return images;
 }
Пример #28
0
        public int[] calcBestAttribute_Test(ImageVector[] goodImages, ImageVector[] badImages)
        {
            double[][] pk = new double[ImageVector.NUMBER_OF_PARAMETERS][];
            double[][] nk = new double[ImageVector.NUMBER_OF_PARAMETERS][];
            double[] remainder = new double[ImageVector.NUMBER_OF_PARAMETERS];
            double[] gain = new double[ImageVector.NUMBER_OF_PARAMETERS];

            ClassifierNew cn;
            cn = new ClassifierNew();

            cn.initArray(remainder);
            cn.initArray(gain);

            for (int k = 0; k < ImageVector.NUMBER_OF_PARAMETERS; k++)
            {
                //positive negative example
                pk[k] = new double[cn.sizeRange(ImageVector.getParameterNameByIndex(k)) + 1];
                nk[k] = new double[cn.sizeRange(ImageVector.getParameterNameByIndex(k)) + 1];
            }

            for (int i = 0; i < goodImages.Length; i++)
            {
                // Console.WriteLine("pk["+i+"]");
                for (int k = 0; k < goodImages[i].getNumOfParameters(); k++)
                {
                    cn.calacPositiveNegative(ref pk[k], goodImages[i].getParameterByIndex(k), ImageVector.getParameterNameByIndex(k));
                }
            }

            for (int i = 0; i < badImages.Length; i++)
            {
                //   Console.WriteLine("nk[" + i + "]");
                for (int k = 0; k < badImages[i].getNumOfParameters(); k++)
                {
                    cn.calacPositiveNegative(ref nk[k], badImages[i].getParameterByIndex(k), ImageVector.getParameterNameByIndex(k));
                }

            }

            int size = goodImages.Length + badImages.Length;
            double max = 1;
            int bestIndex = -1;

            for (int i = 0; i < goodImages[0].getNumOfParameters(); i++)
            {
                cn.calcRemainder(ref remainder[i], pk[i], nk[i], size);
                //   Console.WriteLine("remainder["+i+"] " +remainder[i]);
                cn.calcGain(ref gain[i], remainder[i], goodImages.Length, badImages.Length);
                //   Console.WriteLine("gain[" + i + "] " + gain[i]);
                if (max > remainder[i])
                {
                    max = remainder[i];
                    bestIndex = i;
                }
            }
            string AttributeImgPath = userPath + "\\decisionTreeBestAttributes.txt";
            string goodPath = userPath + "\\imagesGood.txt";
            string badPath = userPath + "\\imagesBad.txt";
            List<int[]> bestAt = new List<int[]>();
            if (!File.Exists(AttributeImgPath) || new FileInfo(AttributeImgPath).Length == 0)
            {

                for (int i = 0; i < ImageVector.NUMBER_OF_PARAMETERS; i++)
                {
                    bestAt = cn.calcAttributes(goodImages, badImages, pk[i], nk[i], bestIndex, goodPath, badPath, AttributeImgPath);
                }

                //fileBestAttributeImages.Close();

            }
            File.Delete(AttributeImgPath);

            int[] best = cn.bestAtributeByOrder(bestAt);
            for (int i = 0; i < best.Length; i++)
                Console.WriteLine(best[i]);

            return best;
        }
Пример #29
0
 /*
  * this test is for finding the best optimization for SVM algorithm
  * trying sigma and gamma for the rbf kernel for best accuracy.
  */
 private void Internal_SVM(ImageVector[] goodImages, ImageVector[] badImages)
 {
     for (double i = 0; i <= 5; i += 1)
     {
         for (double j = 0; j <= 0; j += 1)
         {
             double sigma = Math.Pow(2, i);
             double gamma = Math.Pow(2, j);
             //learningAlgo = new SVM(userPath, i, 1, SVM.Kernel.SVM_POLY, goodImages, badImages);
             learningAlgo = new SVM_Matlab(userPath, sigma, 1, SVM_Matlab.Kernel.SVM_RBF, goodImages, badImages);
             crossValidation(goodImages, badImages);
         }
     }
 }
Пример #30
0
        public void calcSubImage(out ImageVector[] subGood, out ImageVector[] subBad, ImageVector[] goodImages, ImageVector[] badImages, int[] subGroupAttribute)
        {
            int numOfParam = subGroupAttribute.Length;

            subGood = new ImageVector[goodImages.Length];
            subBad = new ImageVector[badImages.Length];
            double[] temp = new double[numOfParam];

            for (int i = 0; i < goodImages.Length; i++)
            {
                for (int j = 0; j < numOfParam; j++)
                    temp[j] = goodImages[i].getParameterByIndex(subGroupAttribute[j]);
                subGood[i] = new ImageVector(numOfParam, subGroupAttribute, temp);
            }
            for (int i = 0; i < badImages.Length; i++)
            {
                for (int j = 0; j < numOfParam; j++)
                    temp[j] = badImages[i].getParameterByIndex(subGroupAttribute[j]);
                subBad[i] = new ImageVector(numOfParam, subGroupAttribute, temp);
            }
        }