public static double[] Predict(this SVMProblem problem, SVMModel model) { IntPtr ptr_model = SVMModel.Allocate(model); double[] target = problem.X.Select(x => x.Predict(ptr_model)).ToArray(); SVMModel.Free(ptr_model); return target; }
public void SetModel(SVMModel model) { if (model != null) { Model = model.Clone(); _ptr_model = SVMModel.Allocate(Model); } }
public void ClassifiGesture(string pathModel, string pathTest, string pathResult) { testProblem = SVMProblemHelper.Load(pathTest); model = SVM.LoadModel(pathModel); double[] testResults = testProblem.Predict(model); using (StreamWriter file = new StreamWriter(pathResult, true)) { foreach (double element in testResults) { file.Write(element.ToString()+"\n"); file.Write(Environment.NewLine); } } }//end ClassifyGesture
public static double[] PredictValues(this SVMProblem problem, SVMModel model, out List<double[]> valuesList) { IntPtr ptr_model = SVMModel.Allocate(model); List<double[]> temp = new List<double[]>(); double[] target = problem.X.Select(x => { double[] estimations; double y = x.PredictProbability(ptr_model, out estimations); temp.Add(estimations); return y; }).ToArray(); SVMModel.Free(ptr_model); valuesList = temp; return target; }
public static double[] PredictValues(this SVMProblem problem, SVMModel model, out List <double[]> valuesList) { IntPtr ptr_model = SVMModel.Allocate(model); List <double[]> temp = new List <double[]>(); double[] target = problem.X.Select(x => { double[] estimations; double y = x.PredictProbability(ptr_model, out estimations); temp.Add(estimations); return(y); }).ToArray(); SVMModel.Free(ptr_model); valuesList = temp; return(target); }
public static void Train() { SVMProblem svmproblem = new SVMProblem(); List <Tuple <Sentence, string> > sentences = new List <Tuple <Sentence, string> >(); StreamReader sr = new StreamReader("training_data.txt"); string line; while ((line = sr.ReadLine()) != null) { string[] lines = line.Split(','); lines[0] = lines[0].Substring(1, lines[0].Length - 2); string sentence = lines[0]; sentences.Add(Tuple.Create(Sentence.ParseIntoSentence(sentence, 0, false), lines[1])); } List <SVMNode[]> modelNodes = new List <SVMNode[]>(); for (int i = 0; i < sentences.Count; i++) { List <SVMNode> nodes = new List <SVMNode>(); for (int j = 0; j < Corpus.count; j++) { SVMNode sentenceNode = new SVMNode(j, 0); nodes.Add(sentenceNode); } for (int j = 0; j < sentences[i].Item1.SentenceWords.Count; j++) { CorpusWord cw = Corpus.CorpusList.Find(c => c.Word.Equals(sentences[i].Item1.SentenceWords[j].Stem.Word) && c.Attribute.Equals(sentences[i].Item1.SentenceWords[j].Stem.Attribute)); SVMNode sentenceNode = new SVMNode(Corpus.CorpusList.IndexOf(cw), 1); SVMNode sentenceNodeToRemove = new SVMNode(Corpus.CorpusList.IndexOf(cw), 0); nodes.Remove(sentenceNodeToRemove); nodes.Add(sentenceNode); } SVMNode[] sentenceNodes = nodes.ToArray(); sentenceNodes = sentenceNodes.OrderBy(x => x.Index).ToArray(); svmproblem.Add(sentenceNodes, Corpus.CorpusList.IndexOf(Corpus.CorpusList.Find(c => c.Word.Equals(sentences[i].Item2)))); } model = SVM.Train(svmproblem, parameter); sr.Close(); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { SVMModel dmodel = serializer.Deserialize <SVMModel>(reader); svm_model model = new svm_model() { SV = dmodel.SV }; model.SV = dmodel.SV; DataHelpers.SetFieldValue <int>(model, "l", dmodel.l); DataHelpers.SetFieldValue <int[]>(model, "label", dmodel.label); DataHelpers.SetFieldValue <int>(model, "nr_class", dmodel.nr_class); DataHelpers.SetFieldValue <int[]>(model, "nSV", dmodel.nSV); DataHelpers.SetFieldValue <double[]>(model, "probA", dmodel.probA); DataHelpers.SetFieldValue <double[]>(model, "probB", dmodel.probB); DataHelpers.SetFieldValue <double[]>(model, "rho", dmodel.rho); DataHelpers.SetFieldValue <double[][]>(model, "sv_coef", dmodel.sv_coef); DataHelpers.SetFieldValue <svm_parameter>(model, "param", dmodel.param); return(model); }
static void Main(string[] args) { SVMProblem problem = SVMProblemHelper.Load(@"Datasets\wine.txt"); problem = SVMProblemHelper.Normalize(problem, SVMNormType.L2); // Optional SVMParameter parameter = new SVMParameter(); parameter.Type = SVMType.C_SVC; parameter.Kernel = SVMKernelType.RBF; parameter.C = 1; parameter.Gamma = 1; // Do 10-fold cross validation double[] target; SVM.CrossValidation(problem, parameter, 10, out target); double crossValidationAccuracy = SVMHelper.EvaluateClassificationProblem(problem, target); // Train the model SVMModel model = SVM.Train(problem, parameter); double correct = 0; for (int i = 0; i < problem.Length; i++) { double y = SVM.Predict(model, problem.X[i]); if (y == problem.Y[i]) { correct++; } } double trainingAccuracy = correct / (double)problem.Length; Console.WriteLine("\nCross validation accuracy: " + crossValidationAccuracy); Console.WriteLine("\nTraining accuracy: " + trainingAccuracy); Console.ReadLine(); }
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { int selIndex = 0; if (radioButton1.Checked) { selIndex = 0; } if (radioButton2.Checked) { selIndex = 1; } if (radioButton3.Checked) { selIndex = 2; } if (radioButton4.Checked) { selIndex = 3; } mList.Add(new DataInfo(selIndex, e.X, e.Y)); Draw(); } else if (e.Button == MouseButtons.Right) { SVMNode[] node = new SVMNode[2]; node[0] = new SVMNode(1, (double)e.X / (double)mWidth); node[1] = new SVMNode(2, (double)e.Y / (double)mHeight); SVMModel model = SVM.LoadModel(FILE_MODEL); double result = SVM.Predict(model, node); Console.WriteLine("result=" + result); } }
//학습모델 생성 public static SVMModel SVM_GenModel(String dataset) { SVMProblem trainingSet = SVMProblemHelper.Load(dataset); //학습데이터셋 열기 trainingSet = trainingSet.Normalize(SVMNormType.L2); SVMParameter parameter = new SVMParameter(); parameter.Type = SVMType.C_SVC; parameter.Kernel = SVMKernelType.RBF; parameter.C = 1; parameter.Gamma = 1; double[] crossValidationResults; int nFold = 5; trainingSet.CrossValidation(parameter, nFold, out crossValidationResults); double crossValidationAccuracy = trainingSet.EvaluateClassificationProblem(crossValidationResults); SVMModel model = trainingSet.Train(parameter); // 학습된 모델 생성 SVM.SaveModel(model, "model_" + dataset); // 모델 저장 return(model); }
public static double PredictValues(this SVMModel model, SVMNode[] x, out double[] values) { return(SVM.PredictValues(model, x, out values)); }
public static double PredictProbability(this SVMModel model, SVMNode[] x, out double[] estimations) { return(SVM.PredictProbability(model, x, out estimations)); }
public static bool trainProblem() { if (checkExistingDataset()) { SVMProblem problem = SVMProblemHelper.Load(Constants.DATA_PATH); SVMProblem randdata = SVMProblemHelper.Load(Constants.RAND_PATH); List <string> resultsstring = new List <string>(); List <SVMClass.SVMResult> ResultsList = new List <SVMClass.SVMResult>(); double C, gammasq; double Cmin = 1, Cmax = 10000, Cstep = 10; double gmin = 0.0001, gmax = 1000, gstep = 10; bool satisfied = false; while (!satisfied) { for (C = Cmin; C <= Cmax; C = C * Cstep) { for (gammasq = gmin; gammasq <= gmax; gammasq = gammasq * gstep) { SVMParameter tempparameter = new SVMParameter(); tempparameter.Type = SVMType.C_SVC; tempparameter.Kernel = SVMKernelType.RBF; tempparameter.C = C; tempparameter.Gamma = gammasq; SVMModel tempmodel = SVM.Train(problem, tempparameter); SVMProblem testData = SVMProblemHelper.Load(Constants.RAND_PATH); double[] results = testData.Predict(tempmodel); int[,] confusionMatrix; double testAccuracy = testData.EvaluateClassificationProblem(results, tempmodel.Labels, out confusionMatrix); // Do cross validation to check this parameter set is correct for the dataset or not double[] crossValidationResults; // output labels int nFold = 10; problem.CrossValidation(tempparameter, nFold, out crossValidationResults); // Evaluate the cross validation result // If it is not good enough, select the parameter set again double crossValidationAccuracy = problem.EvaluateClassificationProblem(crossValidationResults); SVMClass.SVMResult compiled = new SVMClass.SVMResult(); compiled.C = C; compiled.gamma = gammasq; compiled.testAcc = testAccuracy; compiled.crossValidAcc = crossValidationAccuracy; ResultsList.Add(compiled); } } // Evaluate the test results double maxTestAcc = ResultsList.Max(resultdata => resultdata.testAcc); //int maxTestAccIndex = ResultsList.FindIndex(resultdata => resultdata.testAcc.Equals(maxTestAcc)); double maxValidAcc = ResultsList.Max(resultdata => resultdata.crossValidAcc); //int maxValidAccIndex = ResultsList.FindIndex(resultdata => resultdata.crossValidAcc.Equals(maxValidAcc)); if (maxTestAcc < 95 || maxValidAcc < 95) { satisfied = false; Cstep--; gstep--; } else { satisfied = true; List <SVMClass.SVMResult> topResults = ResultsList.FindAll(resultdata => resultdata.testAcc.Equals(maxTestAcc)); List <SVMClass.SVMResult> topValid = ResultsList.FindAll(resultdata => resultdata.crossValidAcc.Equals(maxValidAcc)); while (topResults.Count > topValid.Count) { topResults.RemoveAt(ResultsList.FindIndex(resultsdata => resultsdata.crossValidAcc.Equals(ResultsList.Min(resultdata => resultdata.crossValidAcc)))); } double maxC = topResults.Max(resultdata => resultdata.C); int maxCIndex = topResults.FindIndex(resultdata => resultdata.C.Equals(maxC)); double bestgamma = topResults[maxCIndex].gamma; // maxC or not??? //double bestC = topResults[topResults.Count - 2].C; //topResults[maxCIndex].C; //double bestgamma = topResults[topResults.Count - 2].gamma;//topResults[maxCIndex].gamma; Console.WriteLine("Best C: " + maxC + " Best gammasq: " + bestgamma); Constants.C = maxC; Constants.gammasq = bestgamma; foreach (SVMClass.SVMResult resultdata in topResults) { Console.WriteLine(resultdata.C.ToString() + " " + resultdata.gamma.ToString()); } } } SVMParameter parameter = new SVMParameter(); parameter.Type = SVMType.C_SVC; parameter.Kernel = SVMKernelType.RBF; parameter.C = Constants.C; parameter.Gamma = Constants.gammasq; Variables.model = SVM.Train(problem, parameter); //File.WriteAllText(Constants.MODEL_PATH, String.Empty); //SVM.SaveModel(Variables.model, Constants.MODEL_PATH); Console.WriteLine("Trained and saved model.\n"); //return Variables.model; return(true); } else { MessageBox.Show("Invalid training data!"); return(false); } }
private void button2_Click(object sender, EventArgs e) { SVMProblem trainingSet = new SVMProblem(); SVMProblem testSet = trainingSet; foreach (DataInfo info in mList) { SVMNode[] node = new SVMNode[2]; node[0] = new SVMNode(1, info.X / mWidth); node[1] = new SVMNode(2, info.Y / mHeight); trainingSet.Add(node, info.Group); } // Normalize the datasets if you want: L2 Norm => x / ||x|| //trainingSet = trainingSet.Normalize(SVMNormType.L2); // Select the parameter set SVMParameter parameter = new SVMParameter(); parameter.Type = SVMType.C_SVC; parameter.Kernel = SVMKernelType.RBF; parameter.C = 1; parameter.Gamma = 4; parameter.Coef0 = hScrollBar1.Value; parameter.Degree = 3; // Do cross validation to check this parameter set is correct for the dataset or not double[] crossValidationResults; // output labels int nFold = 5; trainingSet.CrossValidation(parameter, nFold, out crossValidationResults); // Evaluate the cross validation result // If it is not good enough, select the parameter set again double crossValidationAccuracy = trainingSet.EvaluateClassificationProblem(crossValidationResults); // Train the model, If your parameter set gives good result on cross validation SVMModel model = trainingSet.Train(parameter); // Save the model SVM.SaveModel(model, FILE_MODEL); // Predict the instances in the test set double[] testResults = testSet.Predict(model); // Evaluate the test results int[,] confusionMatrix; double testAccuracy = testSet.EvaluateClassificationProblem(testResults, model.Labels, out confusionMatrix); // Print the resutls Console.WriteLine("\n\nCross validation accuracy: " + crossValidationAccuracy); Console.WriteLine("\nTest accuracy: " + testAccuracy); Console.WriteLine("\nConfusion matrix:\n"); // Print formatted confusion matrix Console.Write(String.Format("{0,6}", "")); for (int i = 0; i < model.Labels.Length; i++) { Console.Write(String.Format("{0,5}", "(" + model.Labels[i] + ")")); } Console.WriteLine(); for (int i = 0; i < confusionMatrix.GetLength(0); i++) { Console.Write(String.Format("{0,5}", "(" + model.Labels[i] + ")")); for (int j = 0; j < confusionMatrix.GetLength(1); j++) { Console.Write(String.Format("{0,5}", confusionMatrix[i, j])); } Console.WriteLine(); } Pen[] pen = new Pen[4]; pen[0] = new Pen(Color.Black, 1); pen[1] = new Pen(Color.Red, 1); pen[2] = new Pen(Color.LightGreen, 1); pen[3] = new Pen(Color.Blue, 1); Pen[] pen2 = new Pen[4]; pen2[0] = new Pen(Color.LightGray, 1); pen2[1] = new Pen(Color.DarkRed, 1); pen2[2] = new Pen(Color.DarkGreen, 1); pen2[3] = new Pen(Color.DarkBlue, 1); Bitmap canvas = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height); using (Graphics g = Graphics.FromImage(canvas)) { for (int i = 0; i < pictureBox1.ClientSize.Width; i++) { for (int j = 0; j < pictureBox1.ClientSize.Height; j++) { SVMNode[] node = new SVMNode[2]; node[0] = new SVMNode(1, (double)i / (double)mWidth); node[1] = new SVMNode(2, (double)j / (double)mHeight); double result = SVM.Predict(model, node); g.DrawRectangle(pen2[(int)result], i, j, 1, 1); } } foreach (DataInfo info in mList) { g.DrawEllipse(pen[(int)info.Group], (float)info.X - 5, (float)info.Y - 5, 5, 5); } } Bitmap image = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height); pictureBox1.BackgroundImage = canvas; // 設置為背景層 pictureBox1.Refresh(); pictureBox1.CreateGraphics().DrawImage(canvas, 0, 0); }
// label: 1.0 : -1.0 public AccuracyMetric Fit(List <KeyValuePair <double[], bool> > train_data, List <KeyValuePair <double[], bool> > test_data) { if (this.quiet) { libsvm.SVM.svm_set_print_string_function(new svm_print_null()); } else { libsvm.SVM.svm_set_print_string_function(null); } List <double> vy = new List <double>(); List <SVMNode[]> vx = new List <SVMNode[]>(); int max_index = 0; int m = train_data.Count; for (int i = 0; i < m; ++i) { double[] x0 = train_data[i].Key; int n = x0.Length; vy.Add(train_data[i].Value ? 1 : -1); SVMNode[] x = new SVMNode[n]; for (int j = 0; j < n; j++) { x[j] = new SVMNode(); x[j].index = j + 1; x[j].value = x0[j]; } if (n > 0) { max_index = Math.Max(max_index, x[n - 1].index); } vx.Add(x); } SVMProblem prob = new SVMProblem(); prob.ProblemSize = m; prob.x = new SVMNode[m][]; for (int i = 0; i < m; i++) { prob.x[i] = vx[i]; } prob.y = new double[m]; for (int i = 0; i < m; i++) { prob.y[i] = vy[i]; } if (_param.Gamma == 0 && max_index > 0) { _param.Gamma = 1.0 / max_index; } model = libsvm.SVM.svm_train(prob, _param, (state) => { return(false); }); AccuracyMetric metric = new AccuracyMetric(); metric.TrainAccuracy = GetAccuracy(train_data); metric.TestAccuracy = GetAccuracy(test_data); return(metric); }
static void Main(string[] args) { // Load the datasets: In this example I use the same datasets for training and testing which is not suggested SVMProblem trainingSet = SVMProblemHelper.Load(@"C:\Users\temp\Desktop\ADLfall_train.txt"); // SVMProblem testSet = SVMProblemHelper.Load(@"C:\Users\temp\Desktop\ADLfall_test.txt"); SVMProblem testSet1 = SVMProblemHelper.Load(@"C:\Users\temp\Desktop\ADLfall_test1.txt"); // SVMProblem testSet1 = SVMProblemHelper.Load(@"C:\Users\temp\Desktop\result.txt"); // Normalize the datasets if you want: L2 Norm => x / ||x|| trainingSet = trainingSet.Normalize(SVMNormType.L2); // testSet = testSet.Normalize(SVMNormType.L2); testSet1 = testSet1.Normalize(SVMNormType.L2); // Select the parameter set SVMParameter parameter = new SVMParameter(); parameter.Type = SVMType.C_SVC; parameter.Kernel = SVMKernelType.RBF; parameter.C = 32768.0; parameter.Gamma = 8.0; // Do cross validation to check this parameter set is correct for the dataset or not double[] crossValidationResults; // output labels int nFold = 5; // trainingSet1.CrossValidation(parameter, nFold, out crossValidationResults); // Evaluate the cross validation result // If it is not good enough, select the parameter set again // double crossValidationAccuracy = trainingSet.EvaluateClassificationProblem(crossValidationResults); // Train the model, If your parameter set gives good result on cross validation // SVMModel model = trainingSet.Train(parameter); // Save the model // SVM.SaveModel(model, @"Model\activity_recognition.txt"); SVMModel model = SVM.LoadModel(@"Model\activity_recognition.txt"); int p, q, w, e, r, ok = 0; double sum; q = 0; w = 0; e = 0; r = 0; // Predict the instances in the test set double[] testResults = testSet1.Predict(model); while (ok < testSet1.Length) { var resut = model.Predict(testSet1.X[ok]); // Console.WriteLine("resut111:" + resut); p = Convert.ToInt16(resut); switch (p) { case 1: q++; break; case 2: w++; break; case 3: e++; break; case 4: r++; break; } ok++; } sum = q + w + e + r; Console.WriteLine("result:" + Math.Round(q / sum, 2) + "," + Math.Round(w / sum, 2) + "," + Math.Round(e / sum, 2) + "," + Math.Round(r / sum, 2)); // Evaluate the test results int[,] confusionMatrix; double testAccuracy = testSet1.EvaluateClassificationProblem(testResults, model.Labels, out confusionMatrix); // Print the resutls // Console.WriteLine("\n\nCross validation accuracy: " + crossValidationAccuracy); Console.WriteLine("\nTest accuracy: " + testAccuracy); Console.WriteLine("\nConfusion matrix:\n"); // Print formatted confusion matrix Console.Write(String.Format("{0,6}", "")); for (int i = 0; i < model.Labels.Length; i++) { Console.Write(String.Format("{0,5}", "(" + model.Labels[i] + ")")); } Console.WriteLine(); for (int i = 0; i < confusionMatrix.GetLength(0); i++) { Console.Write(String.Format("{0,5}", "(" + model.Labels[i] + ")")); for (int j = 0; j < confusionMatrix.GetLength(1); j++) { Console.Write(String.Format("{0,5}", confusionMatrix[i, j])); } Console.WriteLine(); } Console.WriteLine("\n\nPress any key to quit..."); Console.ReadLine(); }
static void Main(string[] args) { SVMProblem testSet = SVMProblemHelper.Load(@"Dataset\wine.txt"); // Same as the training set SVMModel model = SVM.LoadModel(@"Model\wine_model.txt"); Console.WriteLine("Feature count in one instance: " + model.SV[0].Length + "\n\n"); // Test 1: Predict instances with SVMProblem's Predict extension method. sw.Start(); double[] target = testSet.Predict(model); sw.Stop(); double elapsedTimeInTest1 = (double)sw.ElapsedMilliseconds / (double)testSet.Length; Console.WriteLine("> Test 1: \nPredict instances with SVMProblem's Predict extension method.\n"); Console.WriteLine("\tAverage elapsed time of one prediction: " + elapsedTimeInTest1 + " ms\n"); // Test 2: Predict instances with RapidPreditor class which is an explicit implementation of the method used in Test 1. using (RapidPredictor predictor = new RapidPredictor(model)) // It needs to be Disposed { sw.Start(); target = new double[testSet.Length]; for (int i = 0; i < testSet.Length; i++) { target[i] = predictor.Predict(testSet.X[i]); } sw.Stop(); } double elapsedTimeInTest2 = (double)sw.ElapsedMilliseconds / (double)testSet.Length; Console.WriteLine("> Test 2: \nPredict instances with RapidPreditor class which is an explicit implementation of the method used in Test 1.\n"); Console.WriteLine("\tAverage elapsed time of one prediction: " + elapsedTimeInTest2 + " ms\n"); // Test 3: Predict instances with standard SVM.Predict method or SVMNode[]'s predict extension method. sw.Start(); target = new double[testSet.Length]; for (int i = 0; i < testSet.Length; i++) { target[i] = SVM.Predict(model, testSet.X[i]); } sw.Stop(); double elapsedTimeInTest3 = (double)sw.ElapsedMilliseconds / (double)testSet.Length; Console.WriteLine("> Test 3: \nPredict instances with standard SVM.Predict method or SVMNode[]'s Predict extension method.\n"); Console.WriteLine("\tAverage elapsed time of one prediction: " + elapsedTimeInTest3 + " ms\n"); // Print the results Console.WriteLine("\nExplanation:\n"); Console.WriteLine( "In standard SVM.Predict method, the SVMModel object is allocated and deallocated every time when the method called. " + "Also the SVMNode[]'s Predict extension methods directly calls the SVM.Predict. " + "However, the model is allocated once and is used to predict whole instances with its pointer in SVMProblem's " + "Predict extension method as implemented in the RapidPredictor class. You can take or modify this class in order " + "to use in your applications, if you have performance considerations. " + "I am not suggesting that SVMProblem's Predict extension method is used in real-time, because the model is allocated" + "in every method call."); Console.WriteLine("\n\nPress any key to quit..."); Console.ReadLine(); }
//学習ファイルの作成 public void TrainingExec(List <FaceFeature.FeatureValue> FeatureList) { //特徴量をMatに移し替える 2個で一つ //2個のfloat * LISTの大きさの配列 double[] feature_array = new double[2 * FeatureList.Count]; //特徴量をSVMで扱えるように配列に置き換える SetFeatureListToArray(FeatureList, ref feature_array); CvPoint2D32f[] feature_points = new CvPoint2D32f[feature_array.Length / 2]; int id = 0; for (int i = 0; i < feature_array.Length / 2; i++) { feature_points[id].X = (float)feature_array[i * 2]; feature_points[id].Y = (float)feature_array[i * 2 + 1]; id++; } CvMat dataMat = new CvMat(feature_points.Length, 2, MatrixType.F32C1, feature_points, true); //これがラベル番号 int[] id_array = new int[FeatureList.Count]; for (int i = 0; i < id_array.Length; i++) { id_array[i] = FeatureList[i].ID; } CvMat resMat = new CvMat(id_array.Length, 1, MatrixType.S32C1, id_array, true); // dataとresponsesの様子を描画 CvPoint2D32f[] points = new CvPoint2D32f[id_array.Length]; int idx = 0; for (int i = 0; i < id_array.Length; i++) { points[idx].X = (float)feature_array[i * 2]; points[idx].Y = (float)feature_array[i * 2 + 1]; idx++; } //学習データを図にする Debug_DrawInputFeature(points, id_array); //デバッグ用 学習させる特徴量を出力する OutPut_FeatureAndID(points, id_array); //LibSVMのテスト //学習用のデータの読み込み SVMProblem problem = SVMProblemHelper.Load(@"wine.txt"); SVMProblem testProblem = SVMProblemHelper.Load(@"wine.txt"); SVMParameter parameter = new SVMParameter(); parameter.Type = LibSVMsharp.SVMType.C_SVC; parameter.Kernel = LibSVMsharp.SVMKernelType.RBF; parameter.C = 1; parameter.Gamma = 1; SVMModel model = SVM.Train(problem, parameter); double[] target = new double[testProblem.Length]; for (int i = 0; i < testProblem.Length; i++) { target[i] = SVM.Predict(model, testProblem.X[i]); } double accuracy = SVMHelper.EvaluateClassificationProblem(testProblem, target); //SVMの用意 CvTermCriteria criteria = new CvTermCriteria(1000, 0.000001); CvSVMParams param = new CvSVMParams( OpenCvSharp.CPlusPlus.SVMType.CSvc, OpenCvSharp.CPlusPlus.SVMKernelType.Rbf, 10.0, // degree 100.0, // gamma 調整 1.0, // coeff0 10.0, // c 調整 0.5, // nu 0.1, // p null, criteria); //学習実行 svm.Train(dataMat, resMat, null, null, param); Debug_DispPredict(); }
/// <summary> /// Run crossvalidation for the feature setup for this machine /// </summary> /// <param name="feelingsmodel"></param> /// <param name="nFold"></param> /// <param name="useIAPSratings"></param> public List <PredictionResult> OldCrossValidate(SAMDataPoint.FeelingModel feelingsmodel, int nFold, bool useIAPSratings = false, Normalize normalizationType = Normalize.OneMinusOne) { List <PredictionResult> predictedResults = new List <PredictionResult>(); //Split into crossvalidation parts List <Tuple <SVMProblem, SVMProblem> > problems = GetFeatureValues(features, samData).NormalizeFeatureList <double>(normalizationType).GetCrossValidationSets <double>(samData, feelingsmodel, nFold, useIAPSratings); //Get correct results int[] answers = samData.dataPoints.Select(x => x.ToAVCoordinate(feelingsmodel, useIAPSratings)).ToArray(); int progressCounter = 0; bool postedOneClassError = false; if (answers.Distinct().Count() <= 1) { int numberOfLabels = SAMData.GetNumberOfLabels(feelingsmodel); //Calculate scoring results double[,] confus = CalculateConfusion(answers.ToList().ConvertAll(x => (double)x).ToArray(), answers, numberOfLabels); List <double> pres = CalculatePrecision(confus, numberOfLabels); List <double> recall = CalculateRecall(confus, numberOfLabels); List <double> fscore = CalculateFScore(pres, recall); PredictionResult pR = new PredictionResult(confus, recall, pres, fscore, new SVMParameter(), features, answers.ToList(), answers.ToList().ConvertAll(x => (int)x)); predictedResults.Add(pR); progressCounter++; Log.LogMessage(ONLY_ONE_CLASS); Log.LogMessage(""); return(predictedResults); } foreach (SVMParameter SVMpara in Parameters) { if (UpdateCallback != null) { UpdateCallback(progressCounter, Parameters.Count); } List <double> guesses = new List <double>(); //model and predict each nfold try { foreach (Tuple <SVMProblem, SVMProblem> tupleProblem in problems) { SVMModel trainingModel = tupleProblem.Item1.Train(SVMpara); if (trainingModel.ClassCount <= 1) { if (!postedOneClassError) { Log.LogMessage(ONLY_ONE_CLASS_IN_TRAINING); postedOneClassError = true; } guesses.AddRange(tupleProblem.Item1.Y.ToList().Take(tupleProblem.Item2.Y.Count()).ToList()); } else { double[] d = tupleProblem.Item2.Predict(trainingModel); guesses.AddRange(d); } } } catch (Exception e) { for (int i = 0; i < samData.dataPoints.Count; i++) { guesses.Add(-1); } } int numberOfLabels = SAMData.GetNumberOfLabels(feelingsmodel); //Calculate scoring results double[,] confus = CalculateConfusion(guesses.ToArray(), answers, numberOfLabels); List <double> pres = CalculatePrecision(confus, numberOfLabels); List <double> recall = CalculateRecall(confus, numberOfLabels); List <double> fscore = CalculateFScore(pres, recall); PredictionResult pR = new PredictionResult(confus, recall, pres, fscore, SVMpara, features, answers.ToList(), guesses.ConvertAll(x => (int)x)); predictedResults.Add(pR); progressCounter++; if (UpdateCallback != null) { UpdateCallback(progressCounter, Parameters.Count); } } return(predictedResults); }
static void Main(string[] args) { // Load the datasets: In this example I use the same datasets for training and testing which is not suggested SVMProblem trainingSet = SVMProblemHelper.Load(@"Dataset\wine.txt"); SVMProblem testSet = SVMProblemHelper.Load(@"Dataset\wine2.txt"); // Normalize the datasets if you want: L2 Norm => x / ||x|| trainingSet = trainingSet.Normalize(SVMNormType.L2); testSet = testSet.Normalize(SVMNormType.L2); // Select the parameter set SVMParameter parameter = new SVMParameter(); parameter.Type = SVMType.C_SVC; parameter.Kernel = SVMKernelType.RBF; parameter.C = 1; parameter.Gamma = 1; // Do cross validation to check this parameter set is correct for the dataset or not double[] crossValidationResults; // output labels int nFold = 5; trainingSet.CrossValidation(parameter, nFold, out crossValidationResults); // Evaluate the cross validation result // If it is not good enough, select the parameter set again double crossValidationAccuracy = trainingSet.EvaluateClassificationProblem(crossValidationResults); // Train the model, If your parameter set gives good result on cross validation SVMModel model = trainingSet.Train(parameter); // Save the model SVM.SaveModel(model, @"Model\wine_model.txt"); // Predict the instances in the test set double[] testResults = testSet.Predict(model); Console.WriteLine("aaa:" + testResults[0] + "\n"); /* * // Evaluate the test results * int[,] confusionMatrix; * double testAccuracy = testSet.EvaluateClassificationProblem(testResults, model.Labels, out confusionMatrix); * * * * * // Print the resutls * Console.WriteLine("\n\nCross validation accuracy: " + crossValidationAccuracy); * Console.WriteLine("\nTest accuracy: " + testAccuracy); * Console.WriteLine("\nConfusion matrix:\n"); * * // Print formatted confusion matrix * Console.Write(String.Format("{0,6}", "")); * for (int i = 0; i < model.Labels.Length; i++) * Console.Write(String.Format("{0,5}", "(" + model.Labels[i] + ")")); * Console.WriteLine(); * for (int i = 0; i < confusionMatrix.GetLength(0); i++) * { * Console.Write(String.Format("{0,5}", "(" + model.Labels[i] + ")")); * for (int j = 0; j < confusionMatrix.GetLength(1); j++) * Console.Write(String.Format("{0,5}", confusionMatrix[i,j])); * Console.WriteLine(); * } * * Console.WriteLine("\n\nPress any key to quit..."); * Console.ReadLine();*/ }
//SVM按鈕事件 private void button5_Click(object sender, EventArgs e) { StreamWriter Train_txt = new StreamWriter(@"train.txt"); StreamWriter Test_txt = new StreamWriter(@"test.txt"); int[] get; for (int i = 0; i < TrainingSet.Count; i++) { get = TrainingSet[i]; if (get[0] == 1) { Train_txt.WriteLine("1" + " 1:" + get[1] + " 2:" + get[2] + " 3:" + get[3]); } else { Train_txt.WriteLine("-1" + " 1:" + get[1] + " 2:" + get[2] + " 3:" + get[3]); } } for (int i = 0; i < height; i++) { for (int j = 0; j < wide; j++) { Test_txt.WriteLine("1" + " 1:" + Image[0, j, i] + " 2:" + Image[1, j, i] + " 3:" + Image[2, j, i]); } } Train_txt.Close(); Test_txt.Close(); SVMProblem problem = SVMProblemHelper.Load(@"train.txt"); SVMProblem testProblem = SVMProblemHelper.Load(@"test.txt"); SVMParameter parameter = new SVMParameter(); parameter.Type = SVMType.C_SVC; parameter.Kernel = SVMKernelType.RBF; parameter.C = 1; parameter.Gamma = 0.0001; SVMModel model = SVM.Train(problem, parameter); double[] target = new double[testProblem.Length]; for (int i = 0; i < testProblem.Length; i++) { target[i] = SVM.Predict(model, testProblem.X[i]); } //改圖 Rectangle recta = new Rectangle(0, 0, wide, height); BmData = copy.LockBits(recta, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); IntPtr Scan = BmData.Scan0; int Offset = BmData.Stride - wide * 3; unsafe { byte *P = (byte *)(void *)Scan; for (int y = 0; y < height; y++, P += Offset) { for (int x = 0; x < wide; x++, P += 3) { if (target[y * wide + x] > 0) { P[2] = 255; P[1] = 255; P[0] = 255; } else { P[2] = 0; P[1] = 0; P[0] = 0; } } } } copy.UnlockBits(BmData); pictureBox3.Image = copy; }
private void btnTrain_Click(object sender, EventArgs e) { System.Diagnostics.Stopwatch time = new System.Diagnostics.Stopwatch(); parameter = load_json_file(parameter_file); try { File.Create(parameter["path_model"] + textBox3.Text + ".txt").Dispose(); File.Create(parameter["result_file"]).Dispose(); if (textBox3.Text == "") { MessageBox.Show("create model name"); } //if(SHOWRESULT.ContainsKey(txtModelName.Text)) //{ // MessageBox.Show("Model name already exits"); //} else { time.Start(); #region Creat file Model // Creat param SVM SVMProblem FileTrain = SVMProblemHelper.Load(parameter["hog_train_file"]); SVMParameter param = new SVMParameter(); param.Type = SVMType.C_SVC; if (parameter["kernel_svm"] == "RBF") { param.Kernel = SVMKernelType.RBF; } if (parameter["kernel_svm"] == "Linear") { param.Kernel = SVMKernelType.LINEAR; } if (parameter["kernel_svm"] == "Poly") { param.Kernel = SVMKernelType.POLY; } if (parameter["kernel_svm"] == "Sigmoid") { param.Kernel = SVMKernelType.SIGMOID; } // param.C = Convert.ToDouble(parameter["c"]); param.C = double.Parse(parameter["c"], CultureInfo.InvariantCulture); param.P = double.Parse(parameter["p"], CultureInfo.InvariantCulture); param.Gamma = double.Parse(parameter["gamma"], CultureInfo.InvariantCulture); param.Degree = Convert.ToInt16(parameter["degree"]); param.Nu = double.Parse(parameter["nu"], CultureInfo.InvariantCulture); param.Coef0 = double.Parse(parameter["coef0"], CultureInfo.InvariantCulture); param.Eps = double.Parse(parameter["eps"], CultureInfo.InvariantCulture); //Train model model = LibSVMsharp.SVM.Train(FileTrain, param); LibSVMsharp.SVM.SaveModel(model, parameter["path_model"] + textBox3.Text + ".txt"); time.Stop(); double train_time = time.ElapsedMilliseconds; #endregion #region Validation data SVMProblem Validation = SVMProblemHelper.Load(parameter["hog_val_file"]); double[] Target_validation = Validation.Predict(model); StreamWriter sw = new StreamWriter(parameter["result_file"], true, Encoding.UTF8); for (int i = 0; i < Target_validation.Length; i++) { string lines = Target_validation[i].ToString(); sw.WriteLine(lines); } sw.Close(); Accuracy = SVMHelper.EvaluateClassificationProblem(Validation, Target_validation); Accuracy = Math.Round(Accuracy, 3); // show result training textBox4.Text = (train_time / 1000).ToString(); textBox5.Text = Accuracy.ToString(); MessageBox.Show("Trainning sucessful"); #endregion } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void testSVM() { if (!holdCommandListener) { holdCommandListener = true; } string parentpath = System.AppDomain.CurrentDomain.BaseDirectory; string DATA_PATH = parentpath + "Datasets\\dataset - Copy (2).txt"; string MODEL_PATH = parentpath + "Model\\testmodel.txt"; string NEWDATA_PATH = parentpath + "Datasets\\testdata.txt"; string RESULTS_PATH = parentpath + "Datasets\\results.txt"; List <string> resultsstring = new List <string>(); SVMProblem testSet = SVMProblemHelper.Load(NEWDATA_PATH); SVMParameter testparameter = new SVMParameter(); testparameter.Type = SVMType.C_SVC; testparameter.Kernel = SVMKernelType.RBF; testparameter.C = 0.1; //Constants.C; testparameter.Gamma = 0.001; // Constants.gammasq; List <SVMClass.SVMResult> ResultsList = new List <SVMClass.SVMResult>(); SVMProblem problem = SVMProblemHelper.Load(DATA_PATH); double C = 0.001; double gammasq = 0.001; for (C = 1; C <= 1000; C = C * 10) { for (gammasq = 0.001; gammasq <= 1000; gammasq = gammasq * 10) { SVMParameter parameter = new SVMParameter(); parameter.Type = SVMType.C_SVC; parameter.Kernel = SVMKernelType.RBF; parameter.C = C; parameter.Gamma = gammasq; SVMModel model = SVM.Train(problem, parameter); //File.WriteAllText(MODEL_PATH, String.Empty); //SVM.SaveModel(model, MODEL_PATH); //Console.WriteLine("Trained and saved model.\n"); //model = SVM.LoadModel(MODEL_PATH); SVMProblem newData = SVMProblemHelper.Load(NEWDATA_PATH); //Console.Write("Predicted Result:\n"); double[] results = newData.Predict(model); //Console.Write(results[0]); int[,] confusionMatrix; double testAccuracy = newData.EvaluateClassificationProblem(results, model.Labels, out confusionMatrix); // Do cross validation to check this parameter set is correct for the dataset or not double[] crossValidationResults; // output labels int nFold = 10; problem.CrossValidation(parameter, nFold, out crossValidationResults); // Evaluate the cross validation result // If it is not good enough, select the parameter set again double crossValidationAccuracy = problem.EvaluateClassificationProblem(crossValidationResults); //Console.WriteLine("\n\nCross validation accuracy: " + crossValidationAccuracy); string temp = ""; string resultstring = "Predict accuracy: " + testAccuracy + " C: " + C + " gamma: " + gammasq + " Cross validation accuracy: " + crossValidationAccuracy; resultsstring.Add(resultstring); if (parameter.C == testparameter.C && parameter.Gamma == testparameter.Gamma) { resultsstring.Add("This one is same as separate test."); } foreach (double res in results) { temp += res.ToString() + " "; } resultsstring.Add(temp); SVMClass.SVMResult compiled = new SVMClass.SVMResult(); compiled.C = C; compiled.gamma = gammasq; compiled.testAcc = testAccuracy; compiled.crossValidAcc = crossValidationAccuracy; ResultsList.Add(compiled); } } File.WriteAllLines(RESULTS_PATH, resultsstring); SVMModel testmodel = SVM.Train(problem, testparameter); // Predict the instances in the test set double[] testResults = testSet.Predict(testmodel); foreach (double result in testResults) { Console.WriteLine(result); } // Evaluate the test results double maxTestAcc = ResultsList.Max(resultdata => resultdata.testAcc); int maxTestAccIndex = ResultsList.FindIndex(resultdata => resultdata.testAcc.Equals(maxTestAcc)); //double maxValidAcc = ResultsList.Max(resultdata => resultdata.crossValidAcc); //int maxValidAccIndex = ResultsList.FindIndex(resultdata => resultdata.crossValidAcc.Equals(maxValidAcc)); List <SVMClass.SVMResult> topResults = ResultsList.FindAll(resultdata => resultdata.testAcc.Equals(maxTestAcc)); double maxC = topResults.Max(resultdata => resultdata.C); int maxCIndex = topResults.FindIndex(resultdata => resultdata.C.Equals(maxC)); double bestC = topResults[topResults.Count - 2].C; //topResults[maxCIndex].C; double bestgamma = topResults[topResults.Count - 2].gamma; //topResults[maxCIndex].gamma; Console.WriteLine("Best C: " + bestC + " Best gammasq: " + bestgamma); foreach (SVMClass.SVMResult resultdata in topResults) { Console.WriteLine(resultdata.C.ToString() + " " + resultdata.gamma.ToString()); } //int[,] confusionMatrix; //double testAccuracy = testSet.EvaluateClassificationProblem(testResults, testmodel.Labels, out confusionMatrix); //Console.WriteLine("\n\nTest accuracy: " + testAccuracy); }
public void SetModel(SVMModel model) { this.model = model; }
public static SvmResult TrainAndTestSvm(SVMProblem trainingSet, SVMProblem testSet) { // find the ratio of malignant:benign cases: double mbTrainRatio = trainingSet.Y.Where(x => x == 0).ToArray().Length *1F / trainingSet.Y.Count; Console.WriteLine($"MB TRAIN RATIO: {mbTrainRatio}"); double mbTestRatio = testSet.Y.Where(x => x == 0).ToArray().Length * 1F / testSet.Y.Count; Console.WriteLine($"MB TEST RATIO: {mbTestRatio}"); SVMParameter parameter = new SVMParameter { Type = SVMType.C_SVC, Kernel = SVMKernelType.RBF, C = double.Parse(Configuration.Get("C")), Gamma = double.Parse(Configuration.Get("Gamma")), Probability = true, WeightLabels = new[] { 0, 1 }, Weights = new[] { (1 - mbTrainRatio) / mbTrainRatio, 1 } }; //parameter = TrainingHelper.FindBestHyperparameters(trainingSet, parameter); Console.WriteLine($"Found best parameters: c={parameter.C},gamma={parameter.Gamma}"); SVMModel model = trainingSet.Train(parameter); SVM.SaveModel(model, Configuration.Get("ModelLocation")); // The following evaluation has code from: // https://csharp.hotexamples.com/examples/LibSVMsharp/SVMParameter/-/php-svmparameter-class-examples.html // Predict the instances in the test set double[] testResults = testSet.Predict(model); // Evaluate the test results double testAccuracy = testSet.EvaluateClassificationProblem(testResults, model.Labels, out var confusionMatrix); // Print the resutls Console.WriteLine("\nTest accuracy: " + testAccuracy); Console.WriteLine("\nConfusion matrix:\n"); // Print formatted confusion matrix Console.Write($"{"",6}"); for (int i = 0; i < model.Labels.Length; i++) { Console.Write($"{"(" + model.Labels[i] + ")",5}"); } Console.WriteLine(); for (int i = 0; i < confusionMatrix.GetLength(0); i++) { Console.Write($"{"(" + model.Labels[i] + ")",5}"); for (int j = 0; j < confusionMatrix.GetLength(1); j++) { Console.Write($"{confusionMatrix[i, j],5}"); } Console.WriteLine(); } double sensitivity = confusionMatrix[0, 0] * 1.0 / (confusionMatrix[0, 1] + confusionMatrix[0, 0]); double specificity = confusionMatrix[1, 1] * 1.0 / (confusionMatrix[1, 1] + confusionMatrix[1, 0]); double[] results = testSet.PredictProbability(model, out var probabilities); for (int i = 0; i < probabilities.Count; i++) { // ReSharper disable once CompareOfFloatsByEqualityOperator String x = results[i] != testSet.Y[i] ? "MISPREDICTION" :""; Console.WriteLine($"{results[i]} | {probabilities[i][0]} | {probabilities[i][1]} | {testSet.Y[i]} | {x}"); } return(new SvmResult() { C = parameter.C, Gamma = parameter.Gamma, TestAccuracy = testAccuracy, Sensitivity = sensitivity, Specificity = specificity }); }
/// <summary> /// Function which will run forever, continously classifying histogram batches into key events. /// </summary> public void run() { List <Histogram> temp = null; List <Histogram> hb; SVMProblem problem = SVMProblemHelper.Load(PipelineConstants.SVMFeaturesFile); SVMParameter parameter = new SVMParameter(); parameter.Type = SVMType.C_SVC; parameter.Kernel = SVMKernelType.RBF; parameter.C = 13.9; parameter.Gamma = .029; SVMModel model = SVM.Train(problem, parameter); string[] eventTrigger = { "standing", "leftShoulder", "rightShoulder", "leftHip", "rightHip" }; ScanCodeShort[] keyEvents = { ScanCodeShort.KEY_S, ScanCodeShort.KEY_I, ScanCodeShort.KEY_O, ScanCodeShort.KEY_K, ScanCodeShort.KEY_L }; // Continuously scan the histogram share point for new histogram data while (true) { hb = hsp.histBatch; // Compare references -- if the share point has a different reference, we're out of date if (temp != hb && hb != null) { temp = hb; int count = 1; // Convert histogram bins into SVM feature vectors List <SVMNode> nodes = new List <SVMNode>(); for (int i = 0; i < temp.Count; i++) { Histogram histObject = temp[i]; for (int j = 0; j < histObject.BucketCount; j++) { SVMNode node = new SVMNode(); node.Index = count++; node.Value = histObject[j].Count / SkeletonFrameWindowProcessor.WindowSize; nodes.Add(node); } } // Get a prediction double y = SVM.Predict(model, nodes.ToArray()); // Use a sliding of votes to filter out brief moments of misclassification votingWindow.Add(y); while (votingWindow.Count > VotingWindowSize) { votingWindow.RemoveAt(0); } // Neat one-liner taken from http://stackoverflow.com/a/8260598 // Group the votes, sorty by group size, select the largest, select the associated vote value double vote = votingWindow.GroupBy(v => v).OrderByDescending(g => g.Count()).First().Key; // Change the console title to make it clear what the classifier is seeing System.Console.Title = eventTrigger[(int)vote]; // Only trigger a keypress when the voted value changes // This has the result of holding a pose being equivalent to quickly dropping it // i.e., the gesture is invariant to duration if (vote != 0 && vote != previousVote) { SendInputWithAPI(keyEvents[(int)vote]); } previousVote = vote; } } }
public RapidPredictor(SVMModel model) { SetModel(model); }
private static void Main(string[] args) { var report = new MakeReport(); List <string> consoleOutput = new List <string>(); consoleOutput.Add($"{System.DateTime.Now} - Loading"); var train = ConvertHelper.CSVToDataConstrutor(File.ReadAllLines("train.csv"), ','); var test = ConvertHelper.CSVToDataConstrutor(File.ReadAllLines("test.csv"), ','); var fs = new DataProcessing(train, test, "SalePrice", "Id"); fs.GetFeature("SalePrice").Transform((value) => Math.Log(1 + value)); fs.SetInactive(new List <string> { "Id", "PoolArea", "LandContour", "PoolQC", "LotConfig", "Utilities", "Alley", "Street", "BsmtHalfBath", "LowQualFinSF", "3SsnPorch", "LandSlope", "YrSold", "Condition1", "BsmtFinType2", "RoofMatl", "MiscVal", "MiscFeature", "BsmtFinSF2", "Condition2", "BldgType", "ScreenPorch", "MoSold", "Functional" }); fs.SetInactive(new List <string> { "BsmtCond", "BsmtUnfSF", "GarageCars", "PavedDrive", "SaleType", "SaleCondition", "BsmtExposure", "GarageCond", "Fence", "Heating", "BsmtQual", }); //fs.SetInactive(new List<string> { "EnclosedPorch" }); fs.SetTrainRowsInactiveByIds(new List <string> { "1299", "186", "198", "636", "1032", "1183", "1153", "1174" }); //fs.SetTrainRowsInactiveByIds(new List<string> { "130", "188", "199", "268", "305", "497", "524", "530", "692", "770", "884", "1025", "1231", "1371", "1387", "1424", "1441" }); consoleOutput.Add($"{System.DateTime.Now} - Transforming model"); fs.GetFeature("LotFrontage").ReplaceValues("NA", "0"); fs.GetFeature("MasVnrArea").ReplaceValues("NA", "-1"); fs.GetFeature("GarageYrBlt").ReplaceValues("NA", "-1"); fs.Features.Where(f => !f.IsNumeric() && !f.IsClass).All(n => { n.TransformEnumToInt(); return(true); }); //fs.Features.Select(f => new OutlierLine { FeatureName = f.Name, Outliers = string.Join(", ", f.GetOutliers()) }).ToList().ForEach(o => consoleOutput.Add($"{o.FeatureName} => {o.Outliers}")); //TODO: To jakoś nie polepszyło, a nawet pogorszyło, trzeba by dodać taką funkcję zamiast wyliczać to tutaj i ująć zmienną GarageYrBlt //var oldestYearBuild = fs.GetFeature("YearBuilt").Values.Min(v => double.Parse(v.NewValue)); //fs.GetFeature("YearBuilt").Transform((value) => (double.Parse(value) - oldestYearBuild).ToString()); //fs.Features.ToList().ForEach(f => report.AddScatterPlot(f.Name, f.Values.Where(v => !v.IsTest).Select(v => new Point() { X = double.Parse(v.NewValue), Y = double.Parse(fs.GetClassForTrainId(v.RowId)) }).ToList())); //report.CreatePDF("Report.pdf"); //var oldestYearRemodAdd = fs.GetFeature("YearRemodAdd").Values.Min(v => double.Parse(v.NewValue)); //fs.GetFeature("YearRemodAdd").Transform((value) => (double.Parse(value) - oldestYearRemodAdd).ToString()); //OUTLIERS //foreach (var feature in fs.Features.Where(f => f.IsActive && !f.IsClass && !f.IsId && new List<string> { "EnclosedPorch", "BsmtFinSF2", "GarageYrBlt", "OpenPorchSF", "ScreenPorch", "MasVnrArea", "LotArea", "Condition1", "MSSubClass", "MiscVal" }.IndexOf(f.Name) < 0)) //{ // feature.MarkOutliers(); //} //fs.PrintOutliersAmount(); consoleOutput.Add($"{System.DateTime.Now} - Gathering data"); var dataModel = fs.GetDataModel(); File.WriteAllLines("output-train.csv", ConvertHelper.DataSetToCSV(dataModel.HeadersWithClass, dataModel.OutputTrain, ",")); File.WriteAllLines("output-test.csv", ConvertHelper.DataSetToCSV(dataModel.HeadersWithoutClass, dataModel.OutputTest, ",")); consoleOutput.Add($"{System.DateTime.Now} - Preparing SVM problem"); //if (false) { //SVM.SetPrintStringFunction(null); SVMProblem testSet = SVMLoadHelper.Load(dataModel.OutputTest, isWithClass: false); SVMProblem trainingSet = SVMLoadHelper.Load(dataModel.OutputTrain, isWithClass: true); SVMParameter parameter = new SVMParameter() { Type = SVMType.EPSILON_SVR, Kernel = SVMKernelType.RBF, //C = 10, Gamma = 0.01, CacheSize = 2000, Eps = 0.1,// * Math.Pow(10, i); //parameter.Probability = true; }; //List<Tuple<string, double>> rmseAfterRemoveFeature = new List<Tuple<string, double>>(); //var activeFeatures = fs.Features.Where(f => f.IsActive || !f.IsClass); //foreach (var feature in activeFeatures) //{ // feature.IsActive = false; // var outputTrain = fs.GetTransfomedTrain(); // SVMProblem trainingSet = SVMLoadHelper.Load(outputTrain, isWithClass: true); // consoleOutput.Add("====================="); // //SVMModel model = trainingSet.Train(parameter); // //double[] trainingResults = trainingSet.Predict(model); // double[] crossvalidationResults; // trainingSet.CrossValidation(parameter, 3, out crossvalidationResults); // //consoleOutput.Add(parameter.GetOutput()); // var rmselog = EvaulationHelper.RMSELog(trainingSet.Y.ToArray(), crossvalidationResults); // rmseAfterRemoveFeature.Add(new Tuple<string, double>(feature.Name, rmselog)); // consoleOutput.Add($"{System.DateTime.Now} - {feature.Name} - {rmselog}"); // feature.IsActive = true; //} //consoleOutput.Add($"{System.DateTime.Now} - {bestToRemove.Item1} - {bestToRemove.Item2}"); consoleOutput.Add("====================="); consoleOutput.Add("Ordered"); //consoleOutput.AddRange(rmseAfterRemoveFeature.OrderBy(t => t.Item2).Select(t => $"{t.Item1} - {t.Item2}")); SVMModel model = trainingSet.Train(parameter); double[] trainResults = trainingSet.Predict(model); double[] testResults = testSet.Predict(model); //double meanSquaredErr = testSet.EvaluateRegressionProblem(testResults, out correlationCoef); trainingSet.Y .Select((y, index) => Math.Abs(y - trainResults[index])) .Select((v, index) => new { index, v }) .OrderByDescending(v => v.v) .Take(15) .ToList() .ForEach(e => consoleOutput.Add($"{e.index}:{e.v}")); var rmselog = EvaulationHelper.RMSELog(trainingSet.Y.ToArray(), trainResults); consoleOutput.Add($"{System.DateTime.Now} - {rmselog}"); File.WriteAllLines("submission_fs.csv", ConvertHelper.ResultToCSV("Id,SalePrice", testResults.Select(v => Math.Exp(v) - 1).ToArray(), dataModel.TestIds)); consoleOutput.Add($"{System.DateTime.Now} -I had finish"); SVM.SaveModel(model, "model.txt"); } //report.CreatePDF("Report.pdf"); consoleOutput.ForEach(s => System.Console.WriteLine(s)); File.WriteAllLines("consoleOutput.txt", consoleOutput); System.Console.ReadLine(); }
public void Dispose() { SVMModel.Free(_ptr_model); _ptr_model = IntPtr.Zero; Model = null; }
public static bool CheckProbabilityModel(this SVMModel model) { return(SVM.CheckProbabilityModel(model)); }
public SVMModel readModelFile(string modelFile) { SVMModel mm = new SVMModel(); string posClassName; string negClassName; double[] weight; double b; string[] allLines = File.ReadAllLines(modelFile); string[] posClassLineParts = allLines[0].Split(' '); posClassName = posClassLineParts[1]; string[] negClassLineParts = allLines[1].Split(' '); negClassName = negClassLineParts[1]; string[] wtstr = allLines[2].Split(' '); weight = new double[wtstr.Count()]; for (int w = 0; w < wtstr.Count(); w++) { if (!string.IsNullOrWhiteSpace(wtstr[w])) { weight[w] = double.Parse(wtstr[w]); } } string bstr = allLines[3]; b = 0.0; if (!string.IsNullOrWhiteSpace(bstr)) { b = double.Parse(bstr); } mm.b = b; mm.weight = weight; mm.posClassName = posClassName; mm.negClassName = negClassName; return mm; }
public static double Predict(this SVMModel model, SVMNode[] x) { return(SVM.Predict(model, x)); }
public void loadModel(string key) { svmModel = SVM.LoadModel(modelPathMap[key]); }
public static bool SaveModel(this SVMModel model, string filename) { return(SVM.SaveModel(model, filename)); }
public static double PredictProbability(this SVMNode[] x, SVMModel model, out double[] estimations) { return SVM.PredictProbability(model, x, out estimations); }
public void Fit(List <double[]> batch) { if (this.quiet) { libsvm.SVM.svm_set_print_string_function(new svm_print_null()); } else { libsvm.SVM.svm_set_print_string_function(null); } List <SVMNode[]> vx = new List <SVMNode[]>(); int max_index = 0; int m = batch.Count; for (int i = 0; i < m; ++i) { double[] x0 = batch[i]; int n = x0.Length; SVMNode[] x = new SVMNode[n]; for (int j = 0; j < n; j++) { x[j] = new SVMNode(); x[j].index = j + 1; x[j].value = x0[j]; } if (n > 0) { max_index = Math.Max(max_index, x[n - 1].index); } vx.Add(x); } SVMProblem prob = new SVMProblem(); prob.ProblemSize = m; prob.x = new SVMNode[m][]; for (int i = 0; i < prob.ProblemSize; i++) { prob.x[i] = vx[i]; } prob.y = new double[m]; for (int i = 0; i < prob.ProblemSize; i++) { prob.y[i] = 0; } if (param.Gamma == 0 && max_index > 0) { param.Gamma = 1.0 / max_index; } model = libsvm.SVM.svm_train(prob, param, (iteration) => { return(false); }); }
public static double PredictValues(this SVMNode[] x, SVMModel model, out double[] values) { return SVM.PredictValues(model, x, out values); }
public static double Predict(this SVMNode[] x, SVMModel model) { return SVM.Predict(model, x); }
private void btnSVM_Click(object sender, EventArgs e) { SVMProblem problem = SVMProblemHelper.Load(@"train.txt"); //訓練樣本 SVMProblem testProblem = SVMProblemHelper.Load(@"train.txt"); //測試樣本 Image <Bgr, Byte> img = new Image <Bgr, Byte>(testProblem.Length, 128); //建立圖片 img.SetValue(255); //設為全白 double res = 0.0; //SVM的模板 SVMParameter parameter = new SVMParameter(); parameter.Type = SVMType.C_SVC; parameter.Kernel = SVMKernelType.LINEAR; //SVM模型 SVMModel model = SVM.Train(problem, parameter); double[] target = new double[testProblem.Length]; //上色 for (int i = img.Height - 1, k = 0; i > 0; --i) { for (int j = 0; j < img.Width; ++j) { if (k >= testProblem.Length) { break; } //SVM預測 res = SVM.Predict(model, testProblem.X[k]); target[k] = res; k++; if (res == 1) { for (int count = 100; count > 0; count--) { img[i - count, j] = new Bgr(255, 0, 0); } } else if (res == 2) { for (int count = 100; count > 0; count--) { img[i - count, j] = new Bgr(0, 255, 0); } } else if (res == 3) { for (int count = 100; count > 0; count--) { img[i - count, j] = new Bgr(0, 0, 255); } } else if (res == 4) { for (int count = 100; count > 0; count--) { img[i - count, j] = new Bgr(0, 0, 0); } } else if (res == 0) { for (int count = 100; count > 0; count--) { img[i - count, j] = new Bgr(127, 127, 127); } } } } //輸出圖片 ImageViewer output = new ImageViewer(img, "123"); output.Show(); //算出準確率 double accuracy = SVMHelper.EvaluateClassificationProblem(testProblem, target); lblAccuracy.Text = "準確率: " + accuracy; }
public string classifyUsingModel(List<feature> featurevalues, SVMModel sm) { double svmOut = weightDotProduct(sm.weight, featurevalues); return ((svmOut - sm.b) > Double.Epsilon ? sm.posClassName : sm.negClassName); }
private async Task PerformAnalysis(String path, Rectangle rectangle) { UShortArrayAsImage image = null; double[] pcaComponents = null; int tasksComplete = 0; UpdateStatus(path, startingImageStatusStr); List <Task> tasks = new List <Task>() { new Task(() => { var file = db.FileStorage.FindById($"images/{path}"); var ms = new MemoryStream(); file.CopyTo(ms); ms.Seek(0, 0); image = DicomFile.Open(ms).GetUshortImageInfo(); UpdateStatus(path, loadedImageStatusStr); }), new Task(() => { image = Normalization.GetNormalizedImage(image, rectangle, int.Parse(Configuration.Get("sizeImageToAnalyze"))); db.FileStorage.Upload($"images/{path}-cropped", $"{path}-cropped", image.GetPngAsMemoryStream()); UpdateStatus(path, croppedImageStatusStr); }), new Task(() => { image = Contrast.ApplyHistogramEqualization(image); db.FileStorage.Upload($"images/{path}-croppedContrast", $"{path}-croppedContrast", image.GetPngAsMemoryStream()); UpdateStatus(path, contrastImageStatusStr); }), new Task(() => { //PCA PCA pca = PCA.LoadModelFromFile(Configuration.Get("PcaModelLocation")); if (!int.TryParse(Configuration.Get("componentsToUse"), out int components)) { components = pca.Eigenvalues.Length; } pcaComponents = pca.GetComponentsFromImage(image, components); UpdateStatus(path, pcaImageStatusStr); }), new Task(() => { //SVM SVMProblem svmProblem = new SVMProblem(); // add all the components to an SVMNode[] SVMNode[] nodes = new SVMNode[pcaComponents.Length]; for (int i = 0; i < pcaComponents.Length; i++) { nodes[i] = new SVMNode(i + 1, pcaComponents[i]); } svmProblem.Add(nodes, 0); svmProblem = svmProblem.Normalize(SVMNormType.L2); SVMModel svmModel = SVM.LoadModel(Configuration.Get("ModelLocation")); double[] results = svmProblem.PredictProbability(svmModel, out var probabilities); var analysis = db.GetCollection <Analysis>("analysis"); Analysis currentAnalysis = analysis.FindOne(x => x.Id.ToString().Equals(path)); currentAnalysis.Certainty = results[0] == 0 ? probabilities[0][1] * 100 : probabilities[0][0] * 100; currentAnalysis.Diagnosis = results[0] == 0 ? DdsmImage.Pathologies.Benign : DdsmImage.Pathologies .Malignant; analysis.Update(currentAnalysis); UpdateStatus(path, svmImageStatusStr); }) }; foreach (Task task in tasks) { task.Start(); await task; // lets set percentage done: var analysis = db.GetCollection <Analysis>("analysis"); Analysis currentAnalysis = analysis.FindOne(x => x.Id.ToString().Equals(path)); currentAnalysis.PercentageDone = (++tasksComplete * 100) / tasks.Count; analysis.Update(currentAnalysis); } UpdateStatus(path, doneStatusStr); }