public void Training(IResults results, int start, int end) { for (int scanNum = start; scanNum <= end; scanNum++) { if (results.Contains(scanNum)) { List <IScore> scores = results.GetResult(scanNum); foreach (IScore score in scores) { double y = (score as FDRScoreProxy).IsDecoy() ? 0 : 1; List <SVMNode> X = new List <SVMNode>(); // store score value in X int idx = 0; foreach (MassType type in types) { SVMNode node = new SVMNode(); node.Index = idx; node.Value = score.GetScore(type); X.Add(node); idx++; } problem.Add(X.ToArray(), y); } } } // training SVMParameter parameter = new SVMParameter(); parameter.Probability = true; model = problem.Train(parameter); }
//作成した辞書を図でみる public void Debug_DispPredict() { return; //辞書ファイルのロード this.libSVM_model = SVM.LoadModel(@"libsvm_model.xml"); using (IplImage retPlot = new IplImage(300, 300, BitDepth.U8, 3)) { for (int x = 0; x < 300; x++) { for (int y = 0; y < 300; y++) { float[] sample = { x / 300f, y / 300f }; //問題を作成 SVMNode[] node_array = new SVMNode[2]; node_array[0] = new SVMNode(1, sample[0]); node_array[1] = new SVMNode(2, sample[1]); int ret_double = (int)SVM.Predict(libSVM_model, node_array); int ret_i = (int)ret_double; CvRect plotRect = new CvRect(x, 300 - y, 1, 1); if (ret_i == 1) { retPlot.Rectangle(plotRect, CvColor.Red); } else if (ret_i == 2) { retPlot.Rectangle(plotRect, CvColor.GreenYellow); } } } CvWindow.ShowImages(retPlot); } }
public EmotionModel() { modelPathMap = new Dictionary <string, string>(); // 模型路径信息 modelPathMap.Add("混合模型(5秒)", Environment.CurrentDirectory + "\\models\\data_after_boardline_smote.scale.model"); modelPathMap.Add("混合模型(10秒)", Environment.CurrentDirectory + "\\models\\data_after_boardline_smote_and_pca_with_49.scale.model"); modelPathMap.Add("交互模型(5秒)", Environment.CurrentDirectory + "\\models\\data_after_undersampling.scale.model"); modelPathMap.Add("交互模型(10秒)", Environment.CurrentDirectory + "\\models\\data_after_undersampling_and_pca_with_49.scale.model"); modelPathMap.Add("人像模型", Environment.CurrentDirectory + "\\models\\data_after_undersampling_and_pca_with_49.scale.model"); svmModel = SVM.LoadModel(modelPathMap["混合模型(5秒)"]); // 加载默认模型 // 初始化特征向量 for (int i = 0; i < featureNum; i++) { svmFeature[i] = new SVMNode(i, 0); } //indexEmotionMap.Add(1, 1); //indexEmotionMap.Add(4, 2); //indexEmotionMap.Add(5, 3); //indexEmotionMap.Add(6, 4); //indexEmotionMap.Add(7, 5); //indexEmotionMap.Add(8, 6); //indexEmotionMap.Add(9, 7); //indexEmotionMap.Add(10, 8); //indexEmotionMap.Add(11, 9); }
public List <IProbScoreProxy> Predicting(IResults results, int scanNum) { List <IScore> scores = results.GetResult(scanNum); ISpectrum spectrum = results.GetSpectrum(scanNum); List <double[]> estimationList = new List <double[]>(); List <IProbScoreProxy> probabilities = new List <IProbScoreProxy>(); foreach (IScore score in scores) { List <SVMNode> X = new List <SVMNode>(); // store score value in X int idx = 0; foreach (MassType type in types) { SVMNode node = new SVMNode(); node.Index = idx; node.Value = score.GetScore(type); X.Add(node); idx++; } testingProblem.X.Add(X.ToArray()); } // prediction double[] target = testingProblem.PredictProbability(model, out estimationList); testingProblem.X.Clear(); // create new scores for (int i = 0; i < scores.Count; i++) { probabilities.Add(new ProbScoreProxy(scores[i], spectrum, estimationList[i][1])); } return(probabilities); }
/// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="type"></param> /// <returns></returns> public static SVMNode[] Normalize(SVMNode[] x, SVMNormType type) { double norm_l = (double)(int)type; double norm = x.Sum(a => Math.Pow(a.Value, norm_l)); norm = Math.Pow(norm, 1 / norm_l); SVMNode[] y = x.Select(a => new SVMNode(a.Index, a.Value / norm)).ToArray(); return y; }
public void Add(SVMNode[] x, double y) { if (x.Length > 0) { SVMNode[] nodes = x.OrderBy(a => a.Index).ToArray(); X.Add(nodes); Y.Add(y); } }
public void Insert(int index, SVMNode[] x, double y) { if (x.Length > 0) { SVMNode[] nodes = x.OrderBy(a => a.Index).ToArray(); X.Insert(index, x); Y.Insert(index, y); } }
protected SVMNode CreateNode(KeyValuePair <string, double> xValue) { if (!Features.ContainsKey(xValue.Key)) { Features.Add(xValue.Key, Features.Count + 1); } var node = new SVMNode(Features[xValue.Key], xValue.Value); return(node); }
//SVM判定 public int SVMPredict(FaceFeature.FeatureValue feature) { //学習ファイルを読み込んでいなかったらロード if (this.LoadFlag == false) { this.libSVM_model_1 = SVM.LoadModel(@"model_Feature_L_EYE_NOSE.xml"); this.libSVM_model_2 = SVM.LoadModel(@"model_Feature_L_EYE_MOUTH.xml"); this.libSVM_model_3 = SVM.LoadModel(@"model_Feature_R_EYE_MOUTH.xml"); this.LoadFlag = true; } double[] feature_array = new double[2]; int[] answer = new int[3]; { SetFeatureToArray(feature, ref feature_array, LEARNING_TYPE.L_EYE_NOSE); //問題を作成 SVMNode[] node_array = new SVMNode[2]; node_array[0] = new SVMNode(1, feature_array[0]); node_array[1] = new SVMNode(2, feature_array[1]); answer[0] = (int)SVM.Predict(libSVM_model_1, node_array); return answer[0]; } /* { SetFeatureToArray(feature, ref feature_array, LEARNING_TYPE.L_EYE_MOUTH); //問題を作成 SVMNode[] node_array = new SVMNode[2]; node_array[0] = new SVMNode(1, feature_array[0]); node_array[1] = new SVMNode(2, feature_array[1]); answer[1] = (int)SVM.Predict(libSVM_model_2, node_array); } { SetFeatureToArray(feature, ref feature_array, LEARNING_TYPE.R_EYE_MOUTH); //問題を作成 SVMNode[] node_array = new SVMNode[2]; node_array[0] = new SVMNode(1, feature_array[0]); node_array[1] = new SVMNode(2, feature_array[1]); answer[2] = (int)SVM.Predict(libSVM_model_2, node_array); } */ int final = answer[0] + answer[1] + answer[2]; if(final < 5) { final = 1; } else { final = 2; } return final; }
/// <summary> /// /// </summary> /// <param name="x1"></param> /// <param name="y1"></param> /// <param name="x2"></param> /// <param name="y2"></param> /// <returns></returns> public static bool IsEqual(SVMNode[] x1, double y1, SVMNode[] x2, double y2) { bool same = false; if (y1 == y2 && x1.Length == x2.Length) { same = true; for (int i = 0; i < x1.Length; i++) { same &= x1[i].Equals(x2[i]); } } return same; }
public double evaluate(double[] x0) { 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]; } double v = libsvm.SVM.svm_predict(model, x); return(v); }
public SVMClassifier(int folds = 5) { Folds = folds; DataFile = @"Data\data.csv"; Data = new DataLoader(Folds, DataFile); TrainProblems = new SVMProblem[folds]; TestProblems = new SVMProblem[folds]; for (int i = 0; i < folds; i++) { TrainProblems[i] = new SVMProblem(); TestProblems[i] = new SVMProblem(); } for (int i = 0; i < folds; i++) { for (int j = 0; j < folds; j++) { for (int k = 0; k < Data.InstancesPerFold; k++) { var nodes = new SVMNode[Data.FeatureCount]; var label = (double)Data.Labels[j, k]; for (int x = 0; x < Data.FeatureCount; x++) { nodes[x] = new SVMNode(x + 1, Data.Data[j, k, x]); } if (i != j) { TrainProblems[i].Add(nodes, label); } else { TestProblems[i].Add(nodes, label); } } } } Parameters = new SVMParameter(); Parameters.Type = SVMType.C_SVC; Parameters.Kernel = SVMKernelType.RBF; Parameters.C = 1000000; Parameters.Degree = 3; Parameters.Coef0 = 0; Parameters.Gamma = 0.001; }
public static SVMProblem CreateCompleteProblemOneClass(this IEnumerable <List <double> > original) { SVMProblem completeProblem = new SVMProblem(); for (int i = 0; i < original.Count(); i++) { SVMNode[] nodeSet = new SVMNode[original.ElementAt(i).Count]; for (int j = 0; j < original.ElementAt(i).Count; j++) { SVMNode currentNode = new SVMNode(); currentNode.Index = j + 1; currentNode.Value = original.ElementAt(i)[j]; nodeSet[j] = currentNode; } completeProblem.Add(nodeSet, 1); } return(completeProblem); }
public static List <SVMNode[]> CreateNodesFromData(this IEnumerable <List <double> > original) { List <SVMNode[]> svmNodeList = new List <SVMNode[]>(); for (int i = 0; i < original.Count(); i++) { SVMNode[] nodeSet = new SVMNode[original.ElementAt(i).Count]; for (int j = 0; j < original.ElementAt(i).Count; j++) { SVMNode currentNode = new SVMNode(); currentNode.Index = j + 1; currentNode.Value = original.ElementAt(i)[j]; nodeSet[j] = currentNode; } svmNodeList.Add(nodeSet); } return(svmNodeList); }
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 static SVMProblem CreateCompleteProblem(this IEnumerable <List <double> > original, SAMData sam, SAMDataPoint.FeelingModel feelingModel) { SVMProblem completeProblem = new SVMProblem(); for (int i = 0; i < original.Count(); i++) { SVMNode[] nodeSet = new SVMNode[original.ElementAt(i).Count]; for (int j = 0; j < original.ElementAt(i).Count; j++) { SVMNode currentNode = new SVMNode(); currentNode.Index = j + 1; currentNode.Value = original.ElementAt(i)[j]; nodeSet[j] = currentNode; } completeProblem.Add(nodeSet, sam.dataPoints[i].ToAVCoordinate(feelingModel)); } return(completeProblem); }
/// <summary> /// /// </summary> /// <param name="filename"></param> /// <returns></returns> public static SVMProblem Load(string filename) { if (String.IsNullOrWhiteSpace(filename) || !File.Exists(filename)) { return(null); } NumberFormatInfo provider = new NumberFormatInfo(); provider.NumberDecimalSeparator = "."; SVMProblem problem = new SVMProblem(); using (StreamReader sr = new StreamReader(filename)) { while (true) { string line = sr.ReadLine(); if (line == null) { break; } string[] list = line.Trim().Split(' '); double y = Convert.ToDouble(list[0].Trim()); List <SVMNode> nodes = new List <SVMNode>(); for (int i = 1; i < list.Length; i++) { string[] temp = list[i].Split(':'); SVMNode node = new SVMNode(); node.Index = Convert.ToInt32(temp[0].Trim()); node.Value = Convert.ToDouble(temp[1].Trim(), provider); nodes.Add(node); } problem.Add(nodes.ToArray(), y); } } return(problem); }
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); } }
//SVM判定 public int SVMPredict(FaceFeature.FeatureValue feature) { //学習ファイルを読み込んでいなかったらロード if (this.LoadFlag == false) { this.libSVM_model = SVM.LoadModel(@"model_FaceFeature.xml"); this.LoadFlag = true; } //スケーリングファイルを読み込む あれば if (this.LoadScaleFlag == false && JudgeGUII.APPSetting.NORMALIZE_USE) { this.LoadScaleFlag = ReadScaleFile(@"out/normalize_scale.csv"); } double[] feature_array = new double[FEATURE_COUNT]; int answer = 0; { SetFeatureToArray(feature, ref feature_array); //ここでスケーリングのデータを読み込んでいたら使う if (this.LoadScaleFlag == true && JudgeGUII.APPSetting.NORMALIZE_USE) { execNormalize(ref feature_array); } //問題を作成 SVMNode[] node_array = new SVMNode[FEATURE_COUNT]; for (int i = 0; i < FEATURE_COUNT; i++) { node_array[i] = new SVMNode(i + 1, feature_array[i]); } answer = (int)SVM.Predict(libSVM_model, node_array); return(answer); } }
public Tuple <List <double[]>, double[]> Predicting(List <IScore> scores) { List <double[]> estimationList = new List <double[]>(); foreach (IScore score in scores) { List <SVMNode> X = new List <SVMNode>(); // store score value in X int idx = 0; foreach (MassType type in types) { SVMNode node = new SVMNode(); node.Index = idx; node.Value = score.GetScore(type); X.Add(node); idx++; } testingProblem.X.Add(X.ToArray()); } double [] target = testingProblem.PredictProbability(model, out estimationList); testingProblem.X.Clear(); return(Tuple.Create(estimationList, target)); }
public static double Predict(this SVMModel model, SVMNode[] x) { return SVM.Predict(model, x); }
private void CreateHRFeatures(List<HRDataReading> data) { for (int time = 0; time < data.Last().timestamp - data.First().timestamp - (HR_DELAY + HR_DURATION); time += STEP_SIZE) { SVMNode[] featureVector = new SVMNode[3]; List<HRDataReading> d = data.SkipWhile(x => (x.timestamp - data.First().timestamp) < time + HR_DELAY).TakeWhile(x => time + HR_DURATION + HR_DELAY > (x.timestamp - data.First().timestamp)).Where(x => x.isBeat).ToList(); if (d.Count == 0) { continue; } featureVector[0] = new SVMNode(1, d.Select(x => (double)x.IBI).Average()); double sd = Math.Sqrt(d.Average(x => Math.Pow((double)x.IBI - featureVector[0].Value, 2))); featureVector[1] = new SVMNode(2, sd); featureVector[2] = new SVMNode(3, FeatureCreator.HRVRMSSD(d.ToList<DataReading>())); featureVectors[SENSOR.HR].Add(new OneClassFV(featureVector, time)); } Log.LogMessage($"Calculation HR Feature Done: {sw.Elapsed}"); }
/// <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 static double PredictValues(this SVMModel model, SVMNode[] x, out double[] values) { return PredictValues(model, x, out values); }
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 double PredictValues(SVMNode[] x, out double[] values) { return x.PredictValues(_ptr_model, out values); }
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); }
/// <summary> /// /// </summary> /// <param name="model"></param> /// <param name="x"></param> /// <param name="values"></param> /// <returns></returns> public static double PredictValues(SVMModel model, SVMNode[] x, out double[] values) { IntPtr ptr_model = SVMModel.Allocate(model); double result = PredictValues(ptr_model, x, out values); SVMModel.Free(ptr_model); return result; }
/// <summary> /// Item1 is the training set, Item2 is the prediction set. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="original"></param> /// <param name="nFold"></param> /// <returns>returns null if the collection can't be nfolded</returns> public static List <Tuple <SVMProblem, SVMProblem> > GetCrossValidationSets <T>(this IEnumerable <List <double> > original, SAMData samData, SAMDataPoint.FeelingModel feelingsmodel, int nFold, bool UseIAPSRatings = false) { //TODO: Needs to be tested, can't test before data can be loaded into the program List <Tuple <SVMProblem, SVMProblem> > allSets = new List <Tuple <SVMProblem, SVMProblem> >(); if (original.Count() % nFold != 0) { return(null); } //Full List of indicies List <int> counter = new List <int>(); for (int k = 0; k < original.Count(); k++) { counter.Add(k); } //Divide indicies into correct nfold List <List <int> > trainIndicies = new List <List <int> >(); List <List <int> > predictIndicies = new List <List <int> >(); for (int i = 0; i < original.Count(); i += nFold) { var temp = counter.Skip(i).Take(nFold).ToList(); predictIndicies.Add(temp); trainIndicies.Add(counter.Except(temp).ToList()); } for (int j = 0; j < original.Count(); j++) { //Create training problem SVMProblem trainSVMProblem = new SVMProblem(); //Foreach training index, add features to the problem foreach (int trainIndex in trainIndicies[j]) { SVMNode[] featureVector = new SVMNode[original.ElementAt(trainIndex).Count]; for (int w = 0; w < original.ElementAt(trainIndex).Count; w++) { featureVector[w] = new SVMNode(w + 1, original.ElementAt(trainIndex)[w]); } trainSVMProblem.Add(featureVector, samData.dataPoints[trainIndex].ToAVCoordinate(feelingsmodel, UseIAPSRatings)); } //Create predict problem SVMProblem predictSVMProblem = new SVMProblem(); //Foreach predict index, add features to the problem foreach (int predictIndex in predictIndicies[j]) { SVMNode[] featureVector = new SVMNode[original.ElementAt(predictIndex).Count]; for (int w = 0; w < original.ElementAt(predictIndex).Count; w++) { featureVector[w] = new SVMNode(w + 1, original.ElementAt(predictIndex)[w]); } predictSVMProblem.Add(featureVector, samData.dataPoints[predictIndex].ToAVCoordinate(feelingsmodel, UseIAPSRatings)); } allSets.Add(new Tuple <SVMProblem, SVMProblem>(trainSVMProblem, predictSVMProblem)); } return(allSets); }
/// <summary> /// This function does classification or regression on a test vector x given a model. /// </summary> /// <param name="model">SVM model.</param> /// <param name="x">Test vector.</param> /// <returns>For a classification model, the predicted class for x is returned. /// For a regression model, the function value of x calculated using the model is returned. /// For an one-class model, +1 or -1 is returned.</returns> public static double Predict(SVMModel model, SVMNode[] x) { if (model == null) { throw new ArgumentNullException("model"); } if (x == null) { throw new ArgumentNullException("x"); } IntPtr ptr_model = SVMModel.Allocate(model); double result = Predict(ptr_model, x); SVMModel.Free(ptr_model); return result; }
private void CreateFACEFeatures(List<DataReading> data) { List<int> leftSide = new List<int>() { 5, 13, 15 }; for (int time = 0; time < data.Last().timestamp - data.First().timestamp - (FACE_DELAY + FACE_DURATION); time += STEP_SIZE) { List<DataReading> dataSlice = data.SkipWhile(x => (x.timestamp - data.First().timestamp) < FACE_DELAY + time).TakeWhile(x => time + FACE_DELAY + FACE_DURATION > (x.timestamp - data.First().timestamp)).ToList(); if (dataSlice.Count == 0) { continue; } SVMNode[] featureVector = new SVMNode[4]; double average12 = dataSlice.Average(d => ((FaceDataReading)d).data[FaceShapeAnimations.RighteyeClosed]); double average11 = dataSlice.Average(d => ((FaceDataReading)d).data[FaceShapeAnimations.LefteyeClosed]); double sd12 = Math.Sqrt(dataSlice.Average(x => Math.Pow(((FaceDataReading)x).data[FaceShapeAnimations.RighteyeClosed] - average12, 2))); double sd11 = Math.Sqrt(dataSlice.Average(x => Math.Pow(((FaceDataReading)x).data[FaceShapeAnimations.LefteyeClosed] - average11, 2))); featureVector[0] = new SVMNode(1, average11); featureVector[1] = new SVMNode(2, average12); featureVector[2] = new SVMNode(3, sd11); featureVector[3] = new SVMNode(4, sd12); featureVectors[SENSOR.FACE].Add(new OneClassFV(featureVector, time)); } Log.LogMessage($"Calculation FACE Feature: {sw.Elapsed}"); }
/// <summary> /// /// </summary> /// <param name="model"></param> /// <param name="x"></param> /// <param name="estimations"></param> /// <returns></returns> public static double PredictProbability(SVMModel model, SVMNode[] x, out double[] estimations) { IntPtr ptr_model = SVMModel.Allocate(model); double result = PredictProbability(ptr_model, x, out estimations); SVMModel.Free(ptr_model); return result; }
public double Predict(SVMNode[] x) { return x.Predict(_ptr_model); }
private void button1_Click(object sender, EventArgs e) { if (field1.Text != "" && field2.Text != "") { sentence1 = Sentence.ParseIntoSentence(field1.Text, 1, true); sentence2 = Sentence.ParseIntoSentence(field2.Text, 2, true); if (sentence1 != null && sentence2 != null) { 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 < sentence1.SentenceWords.Count; j++) { CorpusWord cw = Corpus.CorpusList.Find(c => c.Word.Equals(sentence1.SentenceWords[j].Stem.Word) && c.Attribute.Equals(sentence1.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(); double s1_prediction = SVM.Predict(SVMInterface.model, sentenceNodes); 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 < sentence2.SentenceWords.Count; j++) { CorpusWord cw = Corpus.CorpusList.Find(c => c.Word.Equals(sentence2.SentenceWords[j].Stem.Word) && c.Attribute.Equals(sentence2.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); } sentenceNodes = nodes.ToArray(); sentenceNodes = sentenceNodes.OrderBy(x => x.Index).ToArray(); double s2_prediction = SVM.Predict(SVMInterface.model, sentenceNodes); Console.WriteLine(s1_prediction + " " + s2_prediction); if (s1_prediction != s2_prediction) { result.Text = "UNRELATED"; } else if (sentence1 != null && sentence2 != null) { if (sentence1.Polarity == sentence2.Polarity) { result.Text = "NO CONTRADICTION"; } if (sentence1.Polarity != sentence2.Polarity) { result.Text = "CONTRADICTION"; } } } } }
/// <summary> /// This function does classification or regression on a test vector x given a model. /// </summary> /// <param name="model">SVM model.</param> /// <param name="x">Test vector.</param> /// <returns>For a classification model, the predicted class for x is returned. /// For a regression model, the function value of x calculated using the model is returned. /// For an one-class model, +1 or -1 is returned.</returns> public static double Predict(SVMModel model, SVMNode[] x) { IntPtr ptr_model = SVMModel.Allocate(model); double result = Predict(ptr_model, x); SVMModel.Free(ptr_model); return result; }
/// <summary> /// /// </summary> /// <param name="filename"></param> /// <returns></returns> public static SVMProblem Load(string filename) { if (String.IsNullOrWhiteSpace(filename) || !File.Exists(filename)) { return null; } NumberFormatInfo provider = new NumberFormatInfo(); provider.NumberDecimalSeparator = "."; SVMProblem problem = new SVMProblem(); using (StreamReader sr = new StreamReader(filename)) { while (true) { string line = sr.ReadLine(); if (line == null) break; string[] list = line.Trim().Split(' '); double y = Convert.ToDouble(list[0].Trim(), provider); List<SVMNode> nodes = new List<SVMNode>(); for (int i = 1; i < list.Length; i++) { string[] temp = list[i].Split(':'); SVMNode node = new SVMNode(); node.Index = Convert.ToInt32(temp[0].Trim()); node.Value = Convert.ToDouble(temp[1].Trim(), provider); nodes.Add(node); } problem.Add(nodes.ToArray(), y); } } return problem; }
public static double PredictProbability(this SVMModel model, SVMNode[] x, out double[] estimations) { return SVM.PredictProbability(model, x, out estimations); }
/// <summary> /// /// </summary> /// <param name="model"></param> /// <param name="x"></param> /// <param name="estimations"></param> /// <returns></returns> public static double PredictProbability(SVMModel model, SVMNode[] x, out double[] estimations) { if (model == null) { throw new ArgumentNullException("model"); } if (x == null) { throw new ArgumentNullException("x"); } IntPtr ptr_model = SVMModel.Allocate(model); double result = PredictProbability(ptr_model, x, out estimations); SVMModel.Free(ptr_model); return result; }
public double PredictProbability(SVMNode[] x, out double[] estimations) { return x.PredictProbability(_ptr_model, out estimations); }
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); }
// 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); }
/// <summary> /// /// </summary> /// <param name="ptr_model"></param> /// <param name="x"></param> /// <param name="values"></param> /// <returns></returns> public static double PredictValues(IntPtr ptr_model, SVMNode[] x, out double[] values) { if (ptr_model == IntPtr.Zero) throw new ArgumentNullException("ptr_model"); int classCount = libsvm.svm_get_nr_class(ptr_model); int size = (int)(classCount * (classCount - 1) * 0.5); IntPtr ptr_values = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * size); List<SVMNode> nodes = x.Select(a => a.Clone()).ToList(); nodes.Add(new SVMNode(-1, 0)); IntPtr ptr_nodes = SVMNode.Allocate(nodes.ToArray()); double result = libsvm.svm_predict_values(ptr_model, ptr_nodes, ptr_values); values = new double[size]; Marshal.Copy(ptr_values, values, 0, values.Length); SVMNode.Free(ptr_nodes); Marshal.FreeHGlobal(ptr_values); ptr_values = IntPtr.Zero; return result; }
private void CreateEEGFeatures(List<DataReading> data) { for (int time = 0; time < data.Last().timestamp - data.First().timestamp - (EEG_DELAY + EEG_DURATION); time += STEP_SIZE) { SVMNode[] featureVector = new SVMNode[10]; List<DataReading> slice = data.SkipWhile(x => (x.timestamp - data.First().timestamp) < EEG_DELAY + time).TakeWhile(x => time + EEG_DELAY + EEG_DURATION > (x.timestamp - data.First().timestamp)).ToList(); List<string> names = new List<string>() { "Delta", "Theta", "Alpha", "Beta", "Gamma" }; if (slice.Count == 0) { continue; } int counter = 0; foreach (string name in names) { //Arousal featureVector[counter] = new SVMNode(counter + 1, FeatureCreator.DASM(slice, name, (x => FeatureCreator.EEGValueAccessor(x, EEGDataReading.ELECTRODE.AF3.ToString())), (x => FeatureCreator.EEGValueAccessor(x, EEGDataReading.ELECTRODE.AF4.ToString())))); counter++; featureVector[counter] = new SVMNode(counter + 1, FeatureCreator.DASM(slice, name, (x => FeatureCreator.EEGValueAccessor(x, EEGDataReading.ELECTRODE.F3.ToString())), (x => FeatureCreator.EEGValueAccessor(x, EEGDataReading.ELECTRODE.F4.ToString())))); counter++; } featureVectors[SENSOR.EEG].Add(new OneClassFV(featureVector, time)); } Log.LogMessage($"Calculation EEG Feature: {sw.Elapsed}"); }
/// <summary> /// /// </summary> /// <param name="ptr_model"></param> /// <param name="x"></param> /// <returns></returns> public static double Predict(IntPtr ptr_model, SVMNode[] x) { if (ptr_model == IntPtr.Zero) throw new ArgumentNullException("ptr_model"); List<SVMNode> nodes = x.Select(a => a.Clone()).ToList(); nodes.Add(new SVMNode(-1, 0)); IntPtr ptr_nodes = SVMNode.Allocate(nodes.ToArray()); double result = libsvm.svm_predict(ptr_model, ptr_nodes); SVMNode.Free(ptr_nodes); return result; }
private void CreateGSRFeatures(List<GSRDataReading> data) { for (int time = 0; time < data.Last().timestamp - data.First().timestamp - (GSR_DELAY + GSR_DURATION); time += STEP_SIZE) { SVMNode[] featureVector = new SVMNode[4]; List<double> slice = data.SkipWhile(x => (x.timestamp - data.First().timestamp) < GSR_DELAY + time).TakeWhile(x => time + GSR_DELAY + GSR_DURATION > (x.timestamp - data.First().timestamp)).Select(x => (double)x.resistance).ToList(); if (slice.Count == 0) { continue; } featureVector[0] = new SVMNode(1, slice.Average()); double sd = Math.Sqrt(slice.Average(x => Math.Pow(x - slice.Average(), 2))); featureVector[1] = new SVMNode(2, sd); featureVector[2] = new SVMNode(3, slice.Max()); featureVector[3] = new SVMNode(4, slice.Min()); featureVectors[SENSOR.GSR].Add(new OneClassFV(featureVector, time)); } Log.LogMessage($"Calculation GSR Feature: {sw.Elapsed}"); }
/// <summary> /// /// </summary> /// <param name="ptr_model"></param> /// <param name="x"></param> /// <param name="estimations"></param> /// <returns></returns> public static double PredictProbability(IntPtr ptr_model, SVMNode[] x, out double[] estimations) { if (ptr_model == IntPtr.Zero) throw new ArgumentNullException("ptr_model"); bool isProbabilityModel = libsvm.svm_check_probability_model(ptr_model); if (!isProbabilityModel) { SVMModel.Free(ptr_model); estimations = null; return -1; } int classCount = libsvm.svm_get_nr_class(ptr_model); IntPtr ptr_estimations = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * classCount); List<SVMNode> nodes = x.Select(a => a.Clone()).ToList(); nodes.Add(new SVMNode(-1, 0)); IntPtr ptr_nodes = SVMNode.Allocate(nodes.ToArray()); double result = libsvm.svm_predict_probability(ptr_model, ptr_nodes, ptr_estimations); estimations = new double[classCount]; Marshal.Copy(ptr_estimations, estimations, 0, estimations.Length); SVMNode.Free(ptr_nodes); Marshal.FreeHGlobal(ptr_estimations); ptr_estimations = IntPtr.Zero; return result; }
//作成した辞書を図でみる public void Debug_DispPredict() { //辞書ファイルのロード this.libSVM_model = SVM.LoadModel(@"libsvm_model.xml"); using (IplImage retPlot = new IplImage(300, 300, BitDepth.U8, 3)) { for (int x = 0; x < 300; x++) { for (int y = 0; y < 300; y++) { float[] sample = { x / 300f, y / 300f }; //問題を作成 SVMNode[] node_array = new SVMNode[2]; node_array[0] = new SVMNode(1, sample[0]); node_array[1] = new SVMNode(2, sample[1]); int ret_double = (int)SVM.Predict(libSVM_model, node_array); int ret_i = (int)ret_double; CvRect plotRect = new CvRect(x, 300 - y, 1, 1); if (ret_i == 1) retPlot.Rectangle(plotRect, CvColor.Red); else if (ret_i == 2) retPlot.Rectangle(plotRect, CvColor.GreenYellow); } } CvWindow.ShowImages(retPlot); } }