Exemplo n.º 1
0
 public KNN(int wantedK, VectorRepository rep)
 {
     //k is usually 10
     _K = wantedK;
     _knn = new KNearest();
     _repository = rep;
 }
Exemplo n.º 2
0
        public DataLearning(string name, LearningAlgorithm.Algorithm algorithm, Dictionary<ImageVector.ImageParameters, bool> parameterList, int K)
        {
            // Set name
            _name = name;

            // Create repository and try to load vectors (if exists)
            _repository = new VectorRepository(_name);
            //   _repository.loadList();

            // Create new algorithm instance
            /*  if (algorithm == LearningAlgorithm.Algorithm.DecisionTree)
                  _algorithm = new DecisionTree();
                  _algorithm = new DecisionTreeNumerical();
              if (algorithm == LearningAlgorithm.Algorithm.DecisionTreeNumerical)
                  if (algorithm == LearningAlgorithm.Algorithm.KNN)
                  _algorithm = new KNN(K,_repository);
                         */

            // Set parameter list dictionary
            _parameterList = new Dictionary<ImageVector.ImageParameters, bool>(parameterList);
        }
Exemplo n.º 3
0
        private void button2_Click_1(object sender, EventArgs e)
        {
            const string USERNAME = "******";
            VectorRepository repository = new VectorRepository(USERNAME);
            // Load vectors to repository
            repository.loadList();

            //use vectors already in repository
            // Scan pictures
            //Testing.scanPicturesIntoReporistory(txtFolderAllTesting3.Text, txtFolderTrueTesting3.Text, learning.Repository, ParamDictionary3);
            // int paramsCount = 0;
            foreach (ImageVector.ImageParameters parameter in ParamDictionary.Keys)
            {
                if (_paramDictionary[parameter])
                {
                    //paramsCount++;
                }
            }

            //array of lists. each list is for 1 parameter, inside are the values of all vectors for this parameter
            List<char>[] trueDist = new List<char>[ImageVector.NUMBER_OF_PARAMETERS];
            List<char>[] falseDist = new List<char>[ImageVector.NUMBER_OF_PARAMETERS];

            //scan lists and check value of each parameter in dictionary
            foreach (ImageVector.ImageParameters parameter in ParamDictionary.Keys)
            {
                if (_paramDictionary[parameter])
                {
                    trueDist[(int)parameter] = new List<char>();
                    foreach (ImageVector vec in repository.VectorListTrue)
                    {
                        char val = Classifier.getParameterClassification(parameter, vec.getParameter(parameter));
                        trueDist[(int)parameter].Add(val);
                    }
                    falseDist[(int)parameter] = new List<char>();
                    foreach (ImageVector vec in repository.VectorListFalse)
                    {
                        char val = Classifier.getParameterClassification(parameter, vec.getParameter(parameter));
                        falseDist[(int)parameter].Add(val);
                    }
                }
            }
            List<char>[] allDist = new List<char>[ImageVector.NUMBER_OF_PARAMETERS];

            //make order in arrays and count values
            for (int i = 0; i < trueDist.Length; i++)
            {
                if (trueDist[i] != null)
                {
                    allDist[i] = new List<char>(trueDist[i]);
                    allDist[i].AddRange(falseDist[i]);
                    allDist[i].Sort();
                    trueDist[i].Sort();
                    falseDist[i].Sort();
                }
            }
            countValsAndWriteXML(allDist, "all");
            countValsAndWriteXML(trueDist, "true");
            countValsAndWriteXML(falseDist, "false");
        }
Exemplo n.º 4
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;
        }
Exemplo n.º 5
0
        //private static string[] GetfilesFromDirectory(string folder)
        //{
        //    List<string> files = Directory.GetFiles(folder).ToList();
        //    List<String> removeList = new List<string>();
        //    for (int i = 0; i < files.Count; i++)
        //    {
        //        if(files[i].EndsWith("ini"))
        //            removeList.Add(files[i]);
        //    }
        //    foreach (string path in removeList)
        //    {
        //        files.Remove(path);
        //    }
        //    return files.ToArray();
        //}
        private static int HandleIdenticalVectorWithDifferentChoise(ref List<string> aFiles, ref List<string> tFiles, ref List<string> fFiles, VectorRepository rep, Testing.HandleIdenticalMethod identical)
        {
            if (identical == Testing.HandleIdenticalMethod.Ignore)
                return -1;

            ImageVector tVector;
            ImageVector fVector;
            string tVectorC;
            string fVectorC;
            bool same;
            int count = 0;

            for (int t = 0; t < tFiles.Count; t++)
                for (int f = 0; f < fFiles.Count; f++)
                {
                    if (tFiles[t] == "" || fFiles[f] == "")
                        continue;

                    tVector = null;
                    fVector = null;
                    same = true;

                    // Load Images as vectors
                    tVector = rep.getVectorByPath(tFiles[t]);
                    fVector = rep.getVectorByPath(fFiles[f]);
                    if (tVector == null || fVector == null)
                        throw (new Exception("Unexpected Error: a picture was not found in repository"));

                    // Classify
                    tVectorC = Classifier.ClassifyVector(tVector);
                    fVectorC = Classifier.ClassifyVector(fVector);

                    // Check similarity
                    for (int c=0; c < ImageVector.NUMBER_OF_PARAMETERS; c++)
                        if (tVectorC[c] != fVectorC[c])
                        {
                            same = false;
                            break;
                        }

                    // Handle identical vectors (Mark as empty for removel)
                    if (same)
                    {
                        count++;
                        switch (identical)
                        {
                            case Testing.HandleIdenticalMethod.Remove:
                                aFiles.Remove(tFiles[t]);
                                aFiles.Remove(fFiles[f]);
                                tFiles[t] = "";
                                fFiles[f] = "";
                                break;
                        }
                    }
                }

            // Remove marked
            while (tFiles.Remove(""));
            while (fFiles.Remove(""));

            return count;
        }
Exemplo n.º 6
0
        // Scan image to reporistory
        public static bool scanPicturesIntoReporistory(string folderAll, string folderTrue, VectorRepository rep, Dictionary<ImageVector.ImageParameters,bool> parameterList)
        {
            // Get list of files in folders
            ProgressString = "Starting picture scan..";

            List<string> allFiles, falseFiles, trueFiles;
            try
            {
                allFiles = LoadImages(folderAll).ToList();
                trueFiles = LoadImages(folderTrue).ToList();

                falseFiles = SubstractListsByFileName(allFiles, trueFiles);
                trueFiles = SubstractListsByFileName(allFiles, falseFiles);  // In order for the path to be via 'allFiles' folder
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
                return false;
            }

            int numOfFiles = allFiles.Count();
            int completed = 0;
            _progress = 0;

            // Scan files
            ImageVector existing;
            List<ImageVector> newTrue = new List<ImageVector>();
            List<ImageVector> newFalse = new List<ImageVector>();
            foreach (string path in trueFiles)
            {
                ProgressString = "Checking if " + path + "is already scanned..";
                existing = rep.getVectorByPath(path);
                if (existing == null && File.Exists(path))
                {
                    ProgressString = "Scanning " + path;
                    ImageVector vec = new ImageVector(path, parameterList);
                    newTrue.Add(vec);
                }
                completed++;
                _progress = (int)((completed * 100) / numOfFiles);
            }
            foreach (string path in falseFiles)
            {
                ProgressString = "Checking if " + path + "is already scanned..";
                existing = rep.getVectorByPath(path);
                if (existing == null && File.Exists(path))
                {
                    ProgressString = "Scanning " + path;
                    ImageVector vec = new ImageVector(path, parameterList);
                    newFalse.Add(vec);
                }
                completed++;
                _progress = (int)((completed * 100) / numOfFiles);
            }
            ProgressString = "Scan done. Saving scanned pictures.";

            // Add to repository
            rep.AddToList(newTrue, newFalse);

            ProgressString = "Done scanning";
            return true;
        }
Exemplo n.º 7
0
        /// <summary>
        /// trains algorithm
        /// </summary>
        /// <param name="cVectorsTrue"></param>
        /// <param name="cVectorsFalse"></param>
        /// <returns>true always</returns>
        public override bool Train(List<ImageVector> vectorsTrue, List<ImageVector> vectorsFalse)
        {
            //delete the test repository if it exists
            if(Directory.Exists("knnTest'sVectorData"))
                Directory.Delete("knnTest'sVectorData", true);

             //create repository only for knn learning
            _testingRepository = new VectorRepository("knnTest");
            _testingRepository.delete();
            _testingRepository.AddToList(vectorsTrue, vectorsFalse);

            return true;
        }