/// <summary> /// creates learning curve after learning+deciding about given precentage of pictures according to percent[] /// </summary> /// <param name="folderAll">folder of all images</param> /// <param name="folderTrue">folder of true images</param> /// <param name="learn">data learning object for learning</param> /// <param name="decide">decision making object for deciding</param> /// <param name="percent">percentages to use for learning (percent.length is number of points in learning curve)</param> /// <returns>true if succeeded creating the curve</returns> public static CurvePoint[] LearningCurve(string folderAll, string folderTrue, DataLearning learning, DecisionMaking deciding, int[] percent, out double[] simpleAlgo, Testing.HandleIdenticalMethod identical, bool excludeTrainingSet) { _progress = 0; int itrCount=0; simpleAlgo = new double[3]; //randomLearningCurve = null; const int ITERATIONS_PER_POINT = 5; // Make All, True & False files lists 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 null; } // Handle duplicates ProgressString = "Checking for identicals with different choise"; HandleIdenticalVectorWithDifferentChoise(ref allFiles, ref trueFiles, ref falseFiles, learning.Repository, identical); //create curve of percent.length number of points ProgressString = "Calculating Curves"; CurvePoint[] learningCurve = new CurvePoint[percent.Length]; //for main graph //randomLearningCurve = new CurvePoint[percent.Length]; //for random algorithm graph for (int k = 0; k < percent.Length; k++) { learningCurve[k] = new CurvePoint(percent[k]); // randomLearningCurve[k] = new CurvePoint(percent[k]); for (int itr = 0; itr < ITERATIONS_PER_POINT; itr++) { //get wanted amount of randome images List<string> chosenFiles; do chosenFiles = getRandomImages(allFiles, percent[k]); while (SubstractListsByFileName(chosenFiles, trueFiles).Count == 0 || SubstractListsByFileName(chosenFiles, falseFiles).Count == 0); // incase only true or only false was chosen //get new lists of true and false files (Used for learning in current curve point) List<string> subsetTrueFiles = SubstractListsByFileName(chosenFiles, falseFiles); List<string> subsetFalseFiles = SubstractListsByFileName(chosenFiles, trueFiles); //------------------------------------- //learn from subsets learning.LearnForTesting(chosenFiles, subsetTrueFiles); //------------------------------------- //decide for all files double[] results; deciding.DecideForTesting(allFiles, learning.Repository, out results); //------------------------------------- //make true and false lists according to results List<string> resultsTrue = new List<string>(); List<string> resultsFalse = new List<string>(); for (int i = 0; i < results.Length; i++) if (results[i]>0) //*******here deside for testing*******/ resultsTrue.Add(allFiles[i]); else resultsFalse.Add(allFiles[i]); //------------------------------------- //calculate success precentage CurvePoint.SingleCurvePointCalc currentIteration = new CurvePoint.SingleCurvePointCalc(trueFiles, falseFiles, subsetTrueFiles, subsetFalseFiles, resultsTrue, resultsFalse, excludeTrainingSet); learningCurve[k].AddSingleCurvePointCalc(currentIteration); //------------------------------------- //get simple algorithms calculations double simpleFalseNegative, simpleFalsePositive, simpleSuccess; Testing.SimpleAlgorithm.CalcStatisticalAlgorithm(allFiles.Count, trueFiles.Count, out simpleSuccess, out simpleFalseNegative, out simpleFalsePositive); simpleAlgo[0] = simpleSuccess*100; simpleAlgo[1] = simpleFalseNegative * 100; simpleAlgo[2] = simpleFalsePositive * 100; // Update progress indicators itrCount++; _progress = (int)((itrCount * 100)/(ITERATIONS_PER_POINT * percent.Length)); } } ProgressString = "Done Calculating Curves"; return learningCurve; }
private void threadCalcLearningCurve() { // Make Learning & Deciding objects const string USERNAME = "******"; DataLearning learning; if (_rdoKNN.Checked) { //K = int.Parse(_txtK.Text); K = 1; learning = new DataLearning(USERNAME, LearningAlgorithm.Algorithm.KNN, ParamDictionary, K); } else if (_rdoTree.Checked) { K = 0; learning = new DataLearning(USERNAME, LearningAlgorithm.Algorithm.DecisionTree, ParamDictionary, K); } else // if (_rdoTreeNum.Checked) { K = 0; learning = new DataLearning(USERNAME, LearningAlgorithm.Algorithm.DecisionTreeNumerical, ParamDictionary, K); } DecisionMaking deciding = new DecisionMaking(USERNAME, learning.Algorithm, ParamDictionary); // Load vectors to repository learning.Repository.loadList(); // Scan pictures Testing.scanPicturesIntoReporistory(txtFolderAllTesting3.Text, txtFolderTrueTesting3.Text, learning.Repository, ParamDictionary); // Set X-Axis points for learning curve int nPoints = Convert.ToInt16(txtNumOfPointsTesting3.Text); int[] percent = new int[nPoints]; for (int i = 0; i < nPoints; i++) if (!chkExclude.Checked) percent[i] = (int)(((i + 1) * 100) / (nPoints)); else percent[i] = (int)(((i + 1) * 100) / (nPoints+1)); // Set method for handeling identical vectors with different choice double[] simpleAlgorithm = new double[3]; // Set method for handeling identical vectors with different choice Testing.HandleIdenticalMethod identicalMethod = Testing.HandleIdenticalMethod.Ignore; if (rdoSameRemove.Checked) identicalMethod = Testing.HandleIdenticalMethod.Remove; // Set if training set should be excluded from testing set _excludeTrainingSet = chkExclude.Checked; // Calc learning curves _learningCurve = Testing.LearningCurve(txtFolderAllTesting3.Text, txtFolderTrueTesting3.Text, learning, deciding, percent, out simpleAlgorithm, identicalMethod, _excludeTrainingSet); _statisticAlgoCurve = simpleAlgorithm; _trdTest.Abort(); }
private void _btnUser_Click(object sender, EventArgs e) { if (User.Equals("") || _lstParameters.CheckedItems.Count == 0) MessageBox.Show("Fill User, Learning Algorithm and Parameter fields"); else { //check if user already exists Boolean isNewUser = true; if (Directory.Exists(User + "'sVectorData")) { String[] files = Directory.GetFiles(User + "'sVectorData"); //if empty directory if (files.Length != 0) isNewUser = false; else { //check if files (only 2 vector files) have data in them foreach (string file in files) { Stream s = File.Open(file, FileMode.Open); if (s.Length == 0) { isNewUser = true; s.Close(); break; } s.Close(); } } } K = 1; _data = new DataLearning(User, SelectedAlgorithm, ParametersDictionary, K); if (SelectedAlgorithm == LearningAlgorithm.Algorithm.KNN) { _knn = new KNN(K, _data.Repository); _btnBrowsePredict.Enabled = true; //_btnPredict.Enabled = true; } /*if (_rdoKNNweight.Checked) _btnPredictUser.Enabled = true;*/ //else //{ // _tree = new DecisionTree(); //} //if user exists: loads repository and algorithm. and enables to train or predict. if (!isNewUser) { if (!(SelectedAlgorithm == LearningAlgorithm.Algorithm.KNN)) { _data.Algorithm.LoadData(User); } _data.Repository.loadList(); _btnBrowsePredict.Enabled = true; //_btnPredict.Enabled = true; } //if doesn't exist: enables only to train. files will be created _btnBrowseAll.Enabled = true; _btnBrowseTrue.Enabled = true; //_btnTrain.Enabled = true; } }