public static double PredictValues(this SVMModel model, SVMNode[] x, out double[] values) { return(SVM.PredictValues(model, x, out values)); }
public override void RunTest() { // Training data var points = new Point2f[500]; var responses = new int[points.Length]; var rand = new Random(); for (int i = 0; i < responses.Length; i++) { float x = rand.Next(0, 300); float y = rand.Next(0, 300); points[i] = new Point2f(x, y); responses[i] = (y > Function(x)) ? 1 : 2; } // Show training data and f(x) using (Mat pointsPlot = Mat.Zeros(300, 300, MatType.CV_8UC3)) { for (int i = 0; i < points.Length; i++) { int x = (int)points[i].X; int y = (int)(300 - points[i].Y); int res = responses[i]; Scalar color = (res == 1) ? Scalar.Red : Scalar.GreenYellow; pointsPlot.Circle(x, y, 2, color, -1); } // f(x) for (int x = 1; x < 300; x++) { int y1 = (int)(300 - Function(x - 1)); int y2 = (int)(300 - Function(x)); pointsPlot.Line(x - 1, y1, x, y2, Scalar.LightBlue, 1); } Window.ShowImages(pointsPlot); } // Train var dataMat = new Mat(points.Length, 2, MatType.CV_32FC1, points); var resMat = new Mat(responses.Length, 1, MatType.CV_32SC1, responses); using var svm = SVM.Create(); // normalize data dataMat /= 300.0; // SVM parameters svm.Type = SVM.Types.CSvc; svm.KernelType = SVM.KernelTypes.Rbf; svm.TermCriteria = TermCriteria.Both(1000, 0.000001); svm.Degree = 100.0; svm.Gamma = 100.0; svm.Coef0 = 1.0; svm.C = 1.0; svm.Nu = 0.5; svm.P = 0.1; svm.Train(dataMat, SampleTypes.RowSample, resMat); // Predict for each 300x300 pixel using Mat retPlot = Mat.Zeros(300, 300, MatType.CV_8UC3); for (int x = 0; x < 300; x++) { for (int y = 0; y < 300; y++) { float[] sample = { x / 300f, y / 300f }; var sampleMat = new Mat(1, 2, MatType.CV_32FC1, sample); int ret = (int)svm.Predict(sampleMat); var plotRect = new Rect(x, 300 - y, 1, 1); if (ret == 1) { retPlot.Rectangle(plotRect, Scalar.Red); } else if (ret == 2) { retPlot.Rectangle(plotRect, Scalar.GreenYellow); } } } Window.ShowImages(retPlot); }
public void TestSVM() { int trainSampleCount = 150; int sigma = 60; #region Generate the training data and classes Matrix <float> trainData = new Matrix <float>(trainSampleCount, 2); Matrix <float> trainClasses = new Matrix <float>(trainSampleCount, 1); Image <Bgr, Byte> img = new Image <Bgr, byte>(500, 500); Matrix <float> sample = new Matrix <float>(1, 2); Matrix <float> trainData1 = trainData.GetRows(0, trainSampleCount / 3, 1); trainData1.GetCols(0, 1).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma)); trainData1.GetCols(1, 2).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma)); Matrix <float> trainData2 = trainData.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1); trainData2.SetRandNormal(new MCvScalar(400), new MCvScalar(sigma)); Matrix <float> trainData3 = trainData.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1); trainData3.GetCols(0, 1).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma)); trainData3.GetCols(1, 2).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma)); Matrix <float> trainClasses1 = trainClasses.GetRows(0, trainSampleCount / 3, 1); trainClasses1.SetValue(1); Matrix <float> trainClasses2 = trainClasses.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1); trainClasses2.SetValue(2); Matrix <float> trainClasses3 = trainClasses.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1); trainClasses3.SetValue(3); #endregion //using (SVM.Params p = new SVM.Params(MlEnum.SvmType.CSvc, MlEnum.SvmKernelType.Linear, 0, 1, 0, 1, 0, 0, null, new MCvTermCriteria(100, 1.0e-6))) using (SVM model = new SVM()) using (Matrix <int> trainClassesInt = trainClasses.Convert <int>()) using (TrainData td = new TrainData(trainData, MlEnum.DataLayoutType.RowSample, trainClassesInt)) { model.Type = SVM.SvmType.CSvc; model.SetKernel(SVM.SvmKernelType.Inter); model.Degree = 0; model.Gamma = 1; model.Coef0 = 0; model.C = 1; model.Nu = 0; model.P = 0; model.TermCriteria = new MCvTermCriteria(100, 1.0e-6); //bool trained = model.TrainAuto(td, 5); model.Train(td); #if !NETFX_CORE String fileName = "svmModel.xml"; //String fileName = Path.Combine(Path.GetTempPath(), "svmModel.xml"); model.Save(fileName); SVM model2 = new SVM(); FileStorage fs = new FileStorage(fileName, FileStorage.Mode.Read); model2.Read(fs.GetFirstTopLevelNode()); if (File.Exists(fileName)) { File.Delete(fileName); } #endif for (int i = 0; i < img.Height; i++) { for (int j = 0; j < img.Width; j++) { sample.Data[0, 0] = j; sample.Data[0, 1] = i; float response = model.Predict(sample); img[i, j] = response == 1 ? new Bgr(90, 0, 0) : response == 2 ? new Bgr(0, 90, 0) : new Bgr(0, 0, 90); } } Mat supportVectors = model.GetSupportVectors(); //TODO: find out how to draw the support vectors Image <Gray, float> pts = supportVectors.ToImage <Gray, float>(); PointF[] vectors = new PointF[supportVectors.Rows]; GCHandle handler = GCHandle.Alloc(vectors, GCHandleType.Pinned); using ( Mat vMat = new Mat(supportVectors.Rows, supportVectors.Cols, DepthType.Cv32F, 1, handler.AddrOfPinnedObject(), supportVectors.Cols * 4)) { supportVectors.CopyTo(vMat); } handler.Free(); /* * int c = model.GetSupportVectorCount(); * for (int i = 0; i < c; i++) * { * float[] v = model.GetSupportVector(i); * PointF p1 = new PointF(v[0], v[1]); * img.Draw(new CircleF(p1, 4), new Bgr(128, 128, 128), 2); * }*/ } // display the original training samples for (int i = 0; i < (trainSampleCount / 3); i++) { PointF p1 = new PointF(trainData1[i, 0], trainData1[i, 1]); img.Draw(new CircleF(p1, 2.0f), new Bgr(255, 100, 100), -1); PointF p2 = new PointF(trainData2[i, 0], trainData2[i, 1]); img.Draw(new CircleF(p2, 2.0f), new Bgr(100, 255, 100), -1); PointF p3 = new PointF(trainData3[i, 0], trainData3[i, 1]); img.Draw(new CircleF(p3, 2.0f), new Bgr(100, 100, 255), -1); } //Emgu.CV.UI.ImageViewer.Show(img); }
public static double PredictProbability(this SVMModel model, SVMNode[] x, out double[] estimations) { return(SVM.PredictProbability(model, x, out estimations)); }
public void set_constraint_generator(SVM s) { modshogunPINVOKE.MKL_set_constraint_generator(swigCPtr, SVM.getCPtr(s)); if (modshogunPINVOKE.SWIGPendingException.Pending) throw modshogunPINVOKE.SWIGPendingException.Retrieve(); }
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); } }
public virtual void Solve(int l, QMatrix Q, double[] p_, int[] y_, double[] alpha_, double Cp, double Cn, double eps, SolutionInfo si, bool shrinking) { this.l = l; this.Q = Q; QD = Q.get_QD(); p = (double[])p_.Clone(); y = (int[])y_.Clone(); alpha = (double[])alpha_.Clone(); this.Cp = Cp; this.Cn = Cn; this.eps = eps; this.unshrink = false; // initialize alpha_status { alpha_status = new int[l]; for (int i = 0; i < l; i++) { update_alpha_status(i); } } // initialize active set (for shrinking) { active_set = new int[l]; for (int i = 0; i < l; i++) { active_set[i] = i; } active_size = l; } // initialize gradient { G = new double[l]; G_bar = new double[l]; int i; for (i = 0; i < l; i++) { G[i] = p[i]; G_bar[i] = 0; } for (i = 0; i < l; i++) { if (!is_lower_bound(i)) { float[] Q_i = Q.get_Q(i, l); double alpha_i = alpha[i]; int j; for (j = 0; j < l; j++) { G[j] += alpha_i * Q_i[j]; } if (is_upper_bound(i)) { for (j = 0; j < l; j++) { G_bar[j] += get_C(i) * Q_i[j]; } } } } } // optimization step int iter = 0; int max_iter = Math.Max(10000000, l > int.MaxValue / 100 ? int.MaxValue : 100 * l); int counter = Math.Min(l, 1000) + 1; int[] working_set = new int[2]; while (iter < max_iter) { // show progress and do shrinking if (--counter == 0) { counter = Math.Min(l, 1000); if (shrinking) { do_shrinking(); } SVM.info("."); } if (select_working_set(working_set) != 0) { // reconstruct the whole gradient reconstruct_gradient(); // reset active set size and check active_size = l; SVM.info("*"); if (select_working_set(working_set) != 0) { break; } else { counter = 1; // do shrinking next iteration } } int i = working_set[0]; int j = working_set[1]; ++iter; // update alpha[i] and alpha[j], handle bounds carefully float[] Q_i = Q.get_Q(i, active_size); float[] Q_j = Q.get_Q(j, active_size); double C_i = get_C(i); double C_j = get_C(j); double old_alpha_i = alpha[i]; double old_alpha_j = alpha[j]; if (y[i] != y[j]) { double quad_coef = QD[i] + QD[j] + 2 * Q_i[j]; if (quad_coef <= 0) { quad_coef = 1e-12; } double delta = (-G[i] - G[j]) / quad_coef; double diff = alpha[i] - alpha[j]; alpha[i] += delta; alpha[j] += delta; if (diff > 0) { if (alpha[j] < 0) { alpha[j] = 0; alpha[i] = diff; } } else { if (alpha[i] < 0) { alpha[i] = 0; alpha[j] = -diff; } } if (diff > C_i - C_j) { if (alpha[i] > C_i) { alpha[i] = C_i; alpha[j] = C_i - diff; } } else { if (alpha[j] > C_j) { alpha[j] = C_j; alpha[i] = C_j + diff; } } } else { double quad_coef = QD[i] + QD[j] - 2 * Q_i[j]; if (quad_coef <= 0) { quad_coef = 1e-12; } double delta = (G[i] - G[j]) / quad_coef; double sum = alpha[i] + alpha[j]; alpha[i] -= delta; alpha[j] += delta; if (sum > C_i) { if (alpha[i] > C_i) { alpha[i] = C_i; alpha[j] = sum - C_i; } } else { if (alpha[j] < 0) { alpha[j] = 0; alpha[i] = sum; } } if (sum > C_j) { if (alpha[j] > C_j) { alpha[j] = C_j; alpha[i] = sum - C_j; } } else { if (alpha[i] < 0) { alpha[i] = 0; alpha[j] = sum; } } } // update G double delta_alpha_i = alpha[i] - old_alpha_i; double delta_alpha_j = alpha[j] - old_alpha_j; for (int k = 0; k < active_size; k++) { G[k] += Q_i[k] * delta_alpha_i + Q_j[k] * delta_alpha_j; } // update alpha_status and G_bar { bool ui = is_upper_bound(i); bool uj = is_upper_bound(j); update_alpha_status(i); update_alpha_status(j); int k; if (ui != is_upper_bound(i)) { Q_i = Q.get_Q(i, l); if (ui) { for (k = 0; k < l; k++) { G_bar[k] -= C_i * Q_i[k]; } } else { for (k = 0; k < l; k++) { G_bar[k] += C_i * Q_i[k]; } } } if (uj != is_upper_bound(j)) { Q_j = Q.get_Q(j, l); if (uj) { for (k = 0; k < l; k++) { G_bar[k] -= C_j * Q_j[k]; } } else { for (k = 0; k < l; k++) { G_bar[k] += C_j * Q_j[k]; } } } } } if (iter >= max_iter) { if (active_size < l) { // reconstruct the whole gradient to calculate objective value reconstruct_gradient(); active_size = l; SVM.info("*"); } Console.WriteLine("\nWARNING: reaching max number of iterations\n"); } // calculate rho si.Rho = calculate_rho(); // calculate objective value { double v = 0; int i; for (i = 0; i < l; i++) { v += alpha[i] * (G[i] + p[i]); } si.Obj = v / 2; } // put back the solution { for (int i = 0; i < l; i++) { alpha_[active_set[i]] = alpha[i]; } } si.UpperBoundP = Cp; si.UpperBoundN = Cn; SVM.info("\noptimization finished, #iter = " + iter + "\n"); }
public void Start() { try { svmWriter = new StreamWriter("Complex_Hybrid_SVMOutput.txt"); arimaLogger = new StreamWriter("Complex_Hybrid_ArimaGALog.txt"); hybridWriter = new StreamWriter(OUTPUT_FILE_NAME); #region Loading the training data and classes and test data and test classes TimeSeriGenerator <float> timeSeriGenerator = new TimeSeriGenerator <float>(); int numInp = 0; this.Dispatcher.Invoke(new Action(() => numInp = Int32.Parse(NumberOfInpTextBox.Text))); timeSeriGenerator.load(numInp); Dispatcher.Invoke(new Action(() => ActivityProgressBar.IsIndeterminate = true)); Dispatcher.Invoke(new Action(() => numberOfTests = Int32.Parse(OptimumTestTextBox.Text))); Dispatcher.Invoke(new Action(() => numberOfForecastTests = Int32.Parse(ForecastTestTextBox.Text))); myCategorizedTimeSeri = timeSeriGenerator.generate(numberOfTests, numberOfForecastTests); #endregion #region creating and training the svm model double minError = 9999999; SVM_KERNEL_TYPE bestKernelType = SVM_KERNEL_TYPE.LINEAR; double bestEps = 0.1; SVMParams p; Matrix <float> trainData = new Matrix <float>(myCategorizedTimeSeri.TrainInputs); Matrix <float> trainClasses = new Matrix <float>(myCategorizedTimeSeri.TrainTargets); Matrix <float> testData = new Matrix <float>(myCategorizedTimeSeri.TestInputs); Matrix <float> testClasses = new Matrix <float>(myCategorizedTimeSeri.TestTargets); foreach (SVM_KERNEL_TYPE tp in Enum.GetValues(typeof(SVM_KERNEL_TYPE))) { for (double eps = 0.1; eps >= 0.00001; eps *= 0.1) { using (SVM model = new SVM()) { p = new SVMParams(); p.KernelType = tp; p.SVMType = SVM_TYPE.EPS_SVR; // for regression p.C = 1; p.TermCrit = new MCvTermCriteria(100, eps); p.Gamma = 1; p.Degree = 1; p.P = 1; p.Nu = 0.1; bool trained = model.TrainAuto(trainData, trainClasses, null, null, p.MCvSVMParams, 10); double error = getSumError(model, testData, testClasses); if (trained && minError > error) { minError = error; bestEps = eps; bestKernelType = tp; } } } } Matrix <float> trainDataWithGATest = new Matrix <float>(myCategorizedTimeSeri.getTrainWithTestInputs()); Matrix <float> trainClassesWithGATest = new Matrix <float>(myCategorizedTimeSeri.getTrainWithTestTargets()); svmModel = new SVM(); p = new SVMParams(); p.KernelType = bestKernelType; p.SVMType = Emgu.CV.ML.MlEnum.SVM_TYPE.EPS_SVR; // for regression p.C = 1; p.TermCrit = new MCvTermCriteria(100, bestEps); p.Gamma = 1; p.Degree = 1; p.P = 1; p.Nu = 0.1; bool _trained = svmModel.TrainAuto(trainDataWithGATest, trainClassesWithGATest, null, null, p.MCvSVMParams, 10); List <float> Et = getResidual(trainDataWithGATest, trainClassesWithGATest); svmWriter.Flush(); svmWriter.Close(); int bestD = StartArima(Et.ToArray()); List <float> Zt = new List <float>(); float mu = Et.Average(); if (bestD == 0) { for (int i = 0; i < Et.Count; i++) { Zt.Add(Et[i] - mu); } } else if (bestD == 1) { Zt.Add(0); for (int i = 1; i < Et.Count; i++) { Zt.Add(Et[i] - Et[i - 1] - mu); } } else //else if (bestD == 2) << CHECK HERE >> { Zt.Add(0); Zt.Add(0); for (int i = 2; i < Et.Count; i++) { Zt.Add(Et[i] - 2 * Et[i - 1] + Et[i - 2] - mu); } } Pair <int> bestAB = CreateComplexHybridModel(Et.ToArray(), Zt.ToArray()); MessageBox.Show(bestAB.First + " , " + bestAB.Second, "INJAAAAAAAAAAAAAAAAA", MessageBoxButton.OK, MessageBoxImage.Asterisk); // now our complex hybrid model is created double minErr = SVMComplexModelForBestModel(bestAB.First, bestAB.Second, Et.ToArray(), Zt.ToArray()); MessageBox.Show("MinError In Training => " + minErr); double mse = 0; double errorPercent = 0; double sumTargets = 0; List <float> results = new List <float>(); Matrix <float> testIn = new Matrix <float>(myCategorizedTimeSeri.ForecastTestInputs); Queue <float> EtQueue = new Queue <float>(); Queue <float> ZtQueue = new Queue <float>(); for (int i = 0; i < bestAB.First; i++) { EtQueue.Enqueue(Et[Et.Count - bestAB.First + i]); } for (int i = 0; i < bestAB.Second; i++) { ZtQueue.Enqueue(Zt[Zt.Count - bestAB.Second + i]); } for (int i = 0; i < numberOfForecastTests; i++) { float Lt = svmModel.Predict(testIn.GetRow(i)); float[] inpTest = new float[bestAB.First + bestAB.Second + 1]; float[] EQArray = EtQueue.ToArray(); float[] ZQArray = ZtQueue.ToArray(); int l = 0; for (int j = 0; j < bestAB.First; j++, l++) { inpTest[l] = EQArray[j]; } inpTest[l++] = Lt; for (int j = 0; j < bestAB.Second; j++, l++) { inpTest[l] = ZQArray[j]; } float result = svmModelHybrid.Predict(new Matrix <float>(inpTest)); results.Add(result); hybridWriter.WriteLine(result); float target = myCategorizedTimeSeri.TestTargets[i]; //mse += Math.Pow(target - result, 2); //errorPercent += Math.Abs(target - result); //sumTargets += Math.Abs(target); // preparing for next use in this for loop float resi = target - Lt; // float resi = target - result; << CHECK HERE IMPORTANT >> Et.Add(resi); EtQueue.Dequeue(); EtQueue.Enqueue(resi); ZtQueue.Dequeue(); mu = Et.Average(); if (bestD == 0) { ZtQueue.Enqueue(EQArray[EQArray.Length - 1] - mu); } else if (bestD == 1) { ZtQueue.Enqueue(EQArray[EQArray.Length - 1] - EQArray[EQArray.Length - 2] - mu); } else //else if (bestD == 2) << CHECK HERE >> { ZtQueue.Enqueue(EQArray[EQArray.Length - 1] - 2 * EQArray[EQArray.Length - 2] + EQArray[EQArray.Length - 3] - mu); } } //mse /= numberOfForecastTests; //hybridWriter.WriteLine("\n\nMSE => " + mse); //errorPercent /= sumTargets; //hybridWriter.WriteLine("\n\nERROR% => " + errorPercent*100); double _mse = MyErrorParameters.MSE(results.ToArray(), myCategorizedTimeSeri.ForecastTestTargets); double _errorPercent = MyErrorParameters.ERROR_Percent(results.ToArray(), myCategorizedTimeSeri.ForecastTestTargets); hybridWriter.WriteLine("\n\n\nMSE & ERROR% are =>\n\n{0} {1}", _mse, _errorPercent); hybridWriter.Flush(); hybridWriter.Close(); MessageBox.Show( String.Format( "Complex Hybrid Model Created File {0} For Output Successfully Now , Please Check It Out .", OUTPUT_FILE_NAME), "Hybrid SVM Arima Done", MessageBoxButton.OK, MessageBoxImage.Information); #endregion } catch (Exception ex) { MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "ERROR", MessageBoxButton.OK, MessageBoxImage.Error); } }
private SVMClassifier(SVM svm) { _svm = svm; }
public bool set_svm(int num, SVM svm) { bool ret = modshogunPINVOKE.MultiClassSVM_set_svm(swigCPtr, num, SVM.getCPtr(svm)); if (modshogunPINVOKE.SWIGPendingException.Pending) throw modshogunPINVOKE.SWIGPendingException.Retrieve(); return ret; }
public void compute_POIM2(int max_degree, SVM svm) { modshogunPINVOKE.WeightedDegreePositionStringKernel_compute_POIM2(swigCPtr, max_degree, SVM.getCPtr(svm)); if (modshogunPINVOKE.SWIGPendingException.Pending) throw modshogunPINVOKE.SWIGPendingException.Retrieve(); }
internal static HandleRef getCPtr(SVM obj) { return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr; }
public void set_svm(SVM s) { modshogunPINVOKE.MKL_set_svm(swigCPtr, SVM.getCPtr(s)); if (modshogunPINVOKE.SWIGPendingException.Pending) throw modshogunPINVOKE.SWIGPendingException.Retrieve(); }
public void TestSVM() { int trainSampleCount = 150; int sigma = 60; #region Generate the training data and classes Matrix<float> trainData = new Matrix<float>(trainSampleCount, 2); Matrix<float> trainClasses = new Matrix<float>(trainSampleCount, 1); Image<Bgr, Byte> img = new Image<Bgr, byte>(500, 500); Matrix<float> sample = new Matrix<float>(1, 2); Matrix<float> trainData1 = trainData.GetRows(0, trainSampleCount / 3, 1); trainData1.GetCols(0, 1).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma)); trainData1.GetCols(1, 2).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma)); Matrix<float> trainData2 = trainData.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1); trainData2.SetRandNormal(new MCvScalar(400), new MCvScalar(sigma)); Matrix<float> trainData3 = trainData.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1); trainData3.GetCols(0, 1).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma)); trainData3.GetCols(1, 2).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma)); Matrix<float> trainClasses1 = trainClasses.GetRows(0, trainSampleCount / 3, 1); trainClasses1.SetValue(1); Matrix<float> trainClasses2 = trainClasses.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1); trainClasses2.SetValue(2); Matrix<float> trainClasses3 = trainClasses.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1); trainClasses3.SetValue(3); #endregion using (SVM model = new SVM()) { SVMParams p = new SVMParams(); p.KernelType = Emgu.CV.ML.MlEnum.SVM_KERNEL_TYPE.LINEAR; p.SVMType = Emgu.CV.ML.MlEnum.SVM_TYPE.C_SVC; p.C = 1; p.TermCrit = new MCvTermCriteria(100, 0.00001); //bool trained = model.Train(trainData, trainClasses, null, null, p); bool trained = model.TrainAuto(trainData, trainClasses, null, null, p.MCvSVMParams, 5); model.Save("svmModel.xml"); for (int i = 0; i < img.Height; i++) { for (int j = 0; j < img.Width; j++) { sample.Data[0, 0] = j; sample.Data[0, 1] = i; float response = model.Predict(sample); img[i, j] = response == 1 ? new Bgr(90, 0, 0) : response == 2 ? new Bgr(0, 90, 0) : new Bgr(0, 0, 90); } } int c = model.GetSupportVectorCount(); for (int i = 0; i < c; i++) { float[] v = model.GetSupportVector(i); PointF p1 = new PointF(v[0], v[1]); img.Draw(new CircleF(p1, 4), new Bgr(128, 128, 128), 2); } } // display the original training samples for (int i = 0; i < (trainSampleCount / 3); i++) { PointF p1 = new PointF(trainData1[i, 0], trainData1[i, 1]); img.Draw(new CircleF(p1, 2.0f), new Bgr(255, 100, 100), -1); PointF p2 = new PointF(trainData2[i, 0], trainData2[i, 1]); img.Draw(new CircleF(p2, 2.0f), new Bgr(100, 255, 100), -1); PointF p3 = new PointF(trainData3[i, 0], trainData3[i, 1]); img.Draw(new CircleF(p3, 2.0f), new Bgr(100, 100, 255), -1); } }
public MKLClassification(SVM s) : this(modshogunPINVOKE.new_MKLClassification__SWIG_0(SVM.getCPtr(s)), true) { if (modshogunPINVOKE.SWIGPendingException.Pending) { throw modshogunPINVOKE.SWIGPendingException.Retrieve(); } }
private void Btn_svm_Click(object sender, EventArgs e) { int trainSampleCount = 150; int sigma = 60; #region Generate the training data and classes Matrix <float> trainData = new Matrix <float>(trainSampleCount, 2); Matrix <float> trainClasses = new Matrix <float>(trainSampleCount, 1); Image <Bgr, Byte> img = new Image <Bgr, byte>(500, 500); Matrix <float> sample = new Matrix <float>(1, 2); Matrix <float> trainData1 = trainData.GetRows(0, trainSampleCount / 3, 1); trainData1.GetCols(0, 1).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma)); trainData1.GetCols(1, 2).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma)); Matrix <float> trainData2 = trainData.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1); trainData2.SetRandNormal(new MCvScalar(400), new MCvScalar(sigma)); Matrix <float> trainData3 = trainData.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1); trainData3.GetCols(0, 1).SetRandNormal(new MCvScalar(300), new MCvScalar(sigma)); trainData3.GetCols(1, 2).SetRandNormal(new MCvScalar(100), new MCvScalar(sigma)); Matrix <float> trainClasses1 = trainClasses.GetRows(0, trainSampleCount / 3, 1); trainClasses1.SetValue(1); Matrix <float> trainClasses2 = trainClasses.GetRows(trainSampleCount / 3, 2 * trainSampleCount / 3, 1); trainClasses2.SetValue(2); Matrix <float> trainClasses3 = trainClasses.GetRows(2 * trainSampleCount / 3, trainSampleCount, 1); trainClasses3.SetValue(3); #endregion using (SVM model = new SVM()) { //SVMParams p = new SVMParams(); //p.KernelType = Emgu.CV.ML.MlEnum.SVM_KERNEL_TYPE.LINEAR; //p.SVMType = Emgu.CV.ML.MlEnum.SVM_TYPE.C_SVC; //p.C = 1; //p.TermCrit = new MCvTermCriteria(100, 0.00001); //bool trained = model.Train(trainData, trainClasses, null, null, p); //bool trained = model.TrainAuto(trainData, trainClasses, null, null, p.MCvSVMParams, 5); //for (int i = 0; i < img.Height; i++) //{ // for (int j = 0; j < img.Width; j++) // { // sample.Data[0, 0] = j; // sample.Data[0, 1] = i; // float response = model.Predict(sample); // img[i, j] = // response == 1 ? new Bgr(90, 0, 0) : // response == 2 ? new Bgr(0, 90, 0) : // new Bgr(0, 0, 90); // } //} //int c = model.GetSupportVectorCount(); //for (int i = 0; i < c; i++) //{ // float[] v = model.GetSupportVector(i); // PointF p1 = new PointF(v[0], v[1]); // img.Draw(new CircleF(p1, 4), new Bgr(128, 128, 128), 2); //} } // display the original training samples for (int i = 0; i < (trainSampleCount / 3); i++) { PointF p1 = new PointF(trainData1[i, 0], trainData1[i, 1]); img.Draw(new CircleF(p1, 2.0f), new Bgr(255, 100, 100), -1); PointF p2 = new PointF(trainData2[i, 0], trainData2[i, 1]); img.Draw(new CircleF(p2, 2.0f), new Bgr(100, 255, 100), -1); PointF p3 = new PointF(trainData3[i, 0], trainData3[i, 1]); img.Draw(new CircleF(p3, 2.0f), new Bgr(100, 100, 255), -1); } Emgu.CV.UI.ImageViewer.Show(img); }
public MainForm2() { //Thread t = new Thread(new ThreadStart(Loading)); //t.Start(); //for (int i = 0; i <= 1000; i++) // Thread.Sleep(10); // t.Abort(); InitializeComponent(); lbID.Text = ""; svm = SVMExtension.Create(); _lprPlateIn = new LPR(svm); _lprPlateOut = new LPR(svm); lbPlate.Text = ""; _dataContext.LoadTraningFace(out _listFace, out _listLabel); _recognition.Update(_listFace, _listLabel); lbCount.Text = _dataContext.GetCountGoOut().ToString(); #region Khởi tạo Camera nếu là Demo if (_isDemo) { _capturePlateIn = new Emgu.CV.VideoCapture(PATH_PLATE_IN); _capturePlateIn.SetCaptureProperty(CapProp.FrameWidth, 640); _capturePlateIn.SetCaptureProperty(CapProp.FrameHeight, 480); _fpsPlateIn = (Int32)_capturePlateIn.GetCaptureProperty(CapProp.Fps); _totalFramePlateIn = Convert.ToInt32(_capturePlateIn.GetCaptureProperty(CapProp.FrameCount)) - 4; _captureFaceIn = new Emgu.CV.VideoCapture(PATH_FACE_IN); _captureFaceIn.SetCaptureProperty(CapProp.FrameWidth, 320); _captureFaceIn.SetCaptureProperty(CapProp.FrameHeight, 240); _fpsFaceIn = (Int32)_captureFaceIn.GetCaptureProperty(CapProp.Fps); _totalFrameFaceIn = (Int32)_captureFaceIn.GetCaptureProperty(CapProp.FrameCount) - 4; timerFaceIn.Enabled = true; timerPlateIn.Enabled = true; } #endregion #region Khởi tạo Camera bình thường else { _capturePlateIn = new Emgu.CV.VideoCapture(1); _capturePlateIn.SetCaptureProperty(CapProp.FrameWidth, 640); _capturePlateIn.SetCaptureProperty(CapProp.FrameHeight, 480); _captureFaceIn = new Emgu.CV.VideoCapture(0); _captureFaceIn.SetCaptureProperty(CapProp.FrameWidth, 320); _captureFaceIn.SetCaptureProperty(CapProp.FrameHeight, 240); timerFaceIn.Enabled = true; timerPlateIn.Enabled = true; //_capturePlateOut = new Emgu.CV.VideoCapture(1); //_capturePlateOut.SetCaptureProperty(CapProp.FrameWidth, 640); //_capturePlateOut.SetCaptureProperty(CapProp.FrameHeight, 480); //_captureFaceOut = new Emgu.CV.VideoCapture(0); //_captureFaceOut.SetCaptureProperty(CapProp.FrameWidth, 320); //_captureFaceOut.SetCaptureProperty(CapProp.FrameHeight, 240); //timerFaceOut.Enabled = true; //timerPlateOut.Enabled = true; } #endregion //if (_captureFaceOut == null) //{ // Image_Xe_Ra_Truoc.Image = (new Image<Bgr, byte>("Picture\\camera_not_found.png")).Bitmap; // Image_Xe_Ra_Truoc.Update(); //} //if (_capturePlateOut == null) //{ // Image_Xe_Ra_Sau.Image = (new Image<Bgr, byte>("Picture\\camera_not_found.png")).Bitmap; // Image_Xe_Ra_Sau.Update(); //} //if (_captureFaceIn == null) //{ // Image_Xe_Vao_Truoc.Image = (new Image<Bgr, byte>("Picture\\camera_not_found.png")).Bitmap; // Image_Xe_Vao_Truoc.Update(); //} //if (_capturePlateIn == null) //{ // Image_Xe_Vao_Sau.Image = (new Image<Bgr, byte>("Picture\\camera_not_found.png")).Bitmap; // Image_Xe_Vao_Sau.Update(); //} //Application.Idle += new EventHandler(FrameGrabber); //timer1.Interval = 1000 / _fpsFaceIn; //timer1.Tick += new EventHandler(timer1_Tick); //timer1.Start(); }
public static void CrossValidation(this SVMProblem problem, SVMParameter parameter, int nFolds, out double[] target) { SVM.CrossValidation(problem, parameter, nFolds, out target); }
private void sVMPredictOneToolStripMenuItem_Click(object sender, EventArgs e) { if (File.Exists("svm.txt")) { svm = new SVM(); FileStorage file = new FileStorage("svm.txt", FileStorage.Mode.Read); svm.Read(file.GetNode("opencv_ml_svm")); } if (svm == null) { return; } FolderBrowserDialog folder = new FolderBrowserDialog(); string[] imgjpg;//存放所有的jpg if (folder.ShowDialog() == DialogResult.OK) { imgjpg = Directory.GetFileSystemEntries(folder.SelectedPath, "*.jpg"); for (int i = 0; i < imgjpg.Length; i++) { Image <Bgr, byte> predictimg = new Image <Bgr, byte>(imgjpg[i]); pictureBox1.Image = predictimg.Bitmap; LTPtransform(predictimg); List <float[]> testList = new List <float[]>(); List <int> testLabel = new List <int>(); transformstr = transformstr.Remove(0, 1); int firstIndex = transformstr.IndexOf(','); int currentLabel = Convert.ToInt32(transformstr.Substring(0, firstIndex)); float[] data = transformstr.Split(',').Select(x => float.Parse(x)).ToArray(); testList.Add(data); testLabel.Add(currentLabel); TestData = new Matrix <float>(To2D <float>(testList.ToArray())); TestLabel = new Matrix <int>(testLabel.ToArray()); if (TestData == null) { return; } try { for (int t = 0; t < TestData.Rows; t++) { Matrix <float> row = TestData.GetRow(t); float predict = svm.Predict(row); //predict 即是預測class // lblOouputLabel.Text = "Predicted Label: " + predict.ToString(); if (predict.ToString() == "0") { MessageBox.Show("Predicted : WOMAN"); } else if (predict.ToString() == "1") { MessageBox.Show("Predicted : MAN"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
public static SVMModel getExistingModel() { //MessageBox.Show("Model Exists"); return(SVM.LoadModel(Constants.MODEL_PATH)); }
public SVMClassifier(SVM svm) { this.svm = svm; }
static void train_svm() { int n_samples = 0; SURF surf = new SURF(400); List <Bitmap> samples = new List <Bitmap>(); List <Tuple <Bitmap, int> > data = new List <Tuple <Bitmap, int> >(); /* * foreach (string s in System.IO.Directory.GetFiles("mail_samples")) * { * Bitmap f1 = new Bitmap(s);//ImageDecoder.DecodeFromFile(s); * data.Add(new Tuple<Bitmap, int>(f1, +1)); * } * foreach (string s in System.IO.Directory.GetFiles("phone_icons")) * { * Bitmap f1 = new Bitmap(s); //ImageDecoder.DecodeFromFile(s); * data.Add(new Tuple<Bitmap, int>(f1, -1)); * } */ foreach (string s in System.IO.Directory.GetFiles(@"C:\test\iphone_icon")) { Bitmap f1 = new Bitmap(s);//ImageDecoder.DecodeFromFile(s); if (string.Compare(System.IO.Path.GetFileNameWithoutExtension(s), "temp_1") == 0 || string.Compare(System.IO.Path.GetFileNameWithoutExtension(s), "scoll_selected_icon") == 0 ) { data.Add(new Tuple <Bitmap, int>(f1, +1)); } else { data.Add(new Tuple <Bitmap, int>(f1, 0)); } } n_samples = data.Count; // computr bow Mat m = new Mat(); foreach (Tuple <Bitmap, int> v in data) { Image <Bgr, Byte> i = new Image <Bgr, byte>(v.Item1); Mat ii = new Mat(); CvInvoke.CvtColor(i, ii, ColorConversion.Bgr2Gray); MKeyPoint[] kp = surf.Detect(ii); Mat desc = new Mat(); surf.Compute(ii, new VectorOfKeyPoint(kp), desc); m.PushBack(desc); } // Create the vocabulary with KMeans. MCvTermCriteria tc = new MCvTermCriteria(100, 0.00001); BOWKMeansTrainer bowTrainer = new BOWKMeansTrainer(16, tc, 3, KMeansInitType.PPCenters); bowTrainer.Add(m); Mat voca = new Mat(); bowTrainer.Cluster(voca); // BFMatcher matcher = new BFMatcher(DistanceType.L2); BOWImgDescriptorExtractor bowDex = new BOWImgDescriptorExtractor(surf, matcher); bowDex.SetVocabulary(voca); // Mat tDesc = new Mat(); //Matrix<int> tLabel = new Matrix<int>(1, n_samples); Matrix <int> tLabel = new Matrix <int>(n_samples, 1); //foreach (Tuple<Bitmap, int> v in data) for (int j = 0; j < data.Count; j++) { Image <Bgr, Byte> i = new Image <Bgr, byte>(data[j].Item1); Mat ii = new Mat(); CvInvoke.CvtColor(i, ii, ColorConversion.Bgr2Gray); MKeyPoint[] kp = surf.Detect(ii); Mat desc = new Mat(); bowDex.Compute(ii, new VectorOfKeyPoint(kp), desc); tDesc.PushBack(desc); //tLabel[0, j] = data[j].Item2; tLabel[j, 0] = data[j].Item2; } // //SVM model = new SVM(); //model.SetKernel(Emgu.CV.ML.SVM.SvmKernelType.Linear); //model.Type = SVM.SvmType.CSvc; //model.C = 1; //model.TermCriteria = new MCvTermCriteria(100, 0.00001); SVM svm = new SVM(); svm.C = 312.5; svm.Gamma = 0.50625000000000009; svm.SetKernel(SVM.SvmKernelType.Rbf); svm.Type = SVM.SvmType.CSvc; svm.Nu = 0.5; TrainData td = new TrainData(tDesc, Emgu.CV.ML.MlEnum.DataLayoutType.RowSample, tLabel); bool tained = svm.TrainAuto(td); using (FileStorage fs = new FileStorage("voca.yaml", FileStorage.Mode.Write)) { svm.Write(fs); fs.Write(voca, "voca"); } //using (FileStorage fs = new FileStorage("svm.yaml", FileStorage.Mode.Write)) //{ // svm.Write(fs); //} //svm.Save("svm.yaml"); // test { //Image<Bgr, Byte> test_img = new Image<Bgr, byte>(@"C:\test\scroll_left.jpg"); Image <Bgr, Byte> test_img = new Image <Bgr, byte>(@"C:\test\iphone_icon\temp_1.jpg"); //Image<Bgr, Byte> test_img = new Image<Bgr, byte>(@"C:\projects\local\testMQ\testMQ\bin\Debug\phone_icons\icon_2.jpg"); //Image<Bgr, Byte> test_img = new Image<Bgr, byte>(@"C:\test\35928233-email-icon-on-blue-background-clean-vector.jpg"); Mat ii = new Mat(); CvInvoke.CvtColor(test_img, ii, ColorConversion.Bgr2Gray); MKeyPoint[] kp = surf.Detect(ii); Mat desc = new Mat(); bowDex.Compute(ii, new VectorOfKeyPoint(kp), desc); float r = svm.Predict(desc); } }
public bool init_optimization_svm(SVM svm) { bool ret = modshogunPINVOKE.Kernel_init_optimization_svm(swigCPtr, SVM.getCPtr(svm)); if (modshogunPINVOKE.SWIGPendingException.Pending) throw modshogunPINVOKE.SWIGPendingException.Retrieve(); return ret; }
public static bool CheckProbabilityModel(this SVMModel model) { return(SVM.CheckProbabilityModel(model)); }
/// <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 Predict(this SVMModel model, SVMNode[] x) { return(SVM.Predict(model, x)); }
public static void RunTest(string filesLocation) { string[] folders = Directory.GetDirectories(filesLocation); foreach (string fldr in folders) { Console.WriteLine("Running for category:" + fldr); string[] innerfolders = Directory.GetDirectories(fldr); foreach (string innerdir in innerfolders) { Console.Out.Flush(); Console.WriteLine("Running for att:" + innerdir); DomPool.LoadTestDocuments(innerdir.Replace(filesLocation, "testset")); DomPool.LoadDocuments(innerdir); //for(int i= (DomPool.allDocsNames.Count() - 1); i <= (DomPool.allDocsNames.Count()-1)/*DomPool.allDocsNames.Count()*/; i++) for (int i = 1; i <= (DomPool.allDocsNames.Count() - 1); i++) { string[] tools = new string[] { "our", "our-not forgiving", "j48", "svm", "xpath-align", "svm" }; int toolStart = 5; Dictionary <string, string> xpathNonForgiving = new Dictionary <string, string>(); for (int tool = toolStart; tool < 6; tool++) { Console.WriteLine("[-] running for training set size=" + i); IEnumerable <IEnumerable <int> > subsetsIndexes = Subsets(DomPool.allDocsNames.Count(), i); //Reduce size ...for testing only //subsetsIndexes = subsetsIndexes.Take(30); double totalAccuracy = 0; double totalRecall = 0; long totalTime = 0; Console.WriteLine("[-] tool:" + tools[tool]); Console.WriteLine("+ will run " + subsetsIndexes.Count() + " different iterations for the current set size"); int s = 0; Dictionary <String, double> SiteTotalRecall = new Dictionary <string, double>(); Dictionary <String, double> SiteTotalPrecision = new Dictionary <string, double>(); Dictionary <String, double> SiteTotalTests = new Dictionary <string, double>(); foreach (string site in DomPool.allDocsNames) { SiteTotalPrecision[site] = 0; SiteTotalRecall[site] = 0; SiteTotalTests[site] = 0; } foreach (IEnumerable <int> currSubsetIndexes in subsetsIndexes) { List <int> listRep = new List <int>(currSubsetIndexes); string stringRep = listRep.Aggregate("", (b, x) => b + "," + x); s++; if (s % 10 == 0) { //Console.Write("(" + s + "/" + subsetsIndexes.Count() + ") "); Console.Write("."); } //if (tool == toolStart) //{ HashSet <String> currSubset = GetSubSet(DomPool.allDocsNames, currSubsetIndexes); DomPool.Initiate(currSubset); DomPool.ExtractAllFeatures(); //} var runres = new HashSet <HtmlNode>(); //our method if (tool < 2) { string xpath = ""; if (tool == 0) { DecisionNode dn = new DecisionNode(); dn.InitialNodeSet = new HashSet <HtmlNode>(DomPool.TargetNodes.Union(DomPool.NonTargetNodes)); dn.SelectedNegative = new HashSet <HtmlNode>(DomPool.NonTargetNodes.Except(DomPool.TargetNodesPrecision)); dn.SelectedPositive = new HashSet <HtmlNode>(DomPool.TargetNodes); dn.FeatureSet = new HashSet <Feature>(); dn.CalculateEntropy(); DecisionTreeLearning.RecursiveTreeImprovement(dn); xpath = XpathTools.GenerateAForgivingXpath(dn); xpathNonForgiving[stringRep] = XpathTools.DecisionTreeToXpath(dn, new HashSet <Feature>(), 1); xpathNonForgiving[stringRep] = "//*" + (xpathNonForgiving[stringRep].Equals("") ? "" : ("[" + xpathNonForgiving[stringRep] + "]")); } if (tool == 1) { xpath = xpathNonForgiving[stringRep]; } Console.WriteLine("Query:" + xpath); var watch = Stopwatch.StartNew(); runres = DomPool.TESTSeenRunXpathQuery(xpath); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; totalTime = totalTime + elapsedMs; } else { if (tool == 2) { ModelLearner model = new ModelLearner(); model.LearnModel(); var watch = Stopwatch.StartNew(); runres = model.RunOnTestSeenSet(); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; totalTime = totalTime + elapsedMs; } else { if (tool == 3) { NB model = new NB(); model.LearnModel(); var watch = Stopwatch.StartNew(); runres = model.RunOnTestSeenSet(); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; totalTime = totalTime + elapsedMs; } else { if (tool == 4) { XpathAlignment model = new XpathAlignment(); model.LearnModel(); Console.WriteLine("Query:" + model.xpath); var watch = Stopwatch.StartNew(); runres = model.RunOnTestSeenSet(); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; totalTime = totalTime + elapsedMs; } else { SVM model = new SVM(); model.LearnModel(); var watch = Stopwatch.StartNew(); runres = model.RunOnTestSeenSet(); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; totalTime = totalTime + elapsedMs; } } } } HashSet <HtmlNode> spos = new HashSet <HtmlNode>(DomPool.TESTSeenTargetNodes.Intersect(runres)); HashSet <HtmlNode> sposprecision = new HashSet <HtmlNode>(DomPool.TESTSeenTargetNodesPrecision.Intersect(runres)); foreach (var entry in DomPool.testDocsAndNames) { if (!DomPool.trainingDocsNames.Contains(entry.Key)) { continue; } HashSet <HtmlNode> docNodes = new HashSet <HtmlNode>(entry.Value.SelectNodes("//*")); HashSet <HtmlNode> currspos = new HashSet <HtmlNode>(spos.Intersect(docNodes)); HashSet <HtmlNode> currrunres = new HashSet <HtmlNode>(runres.Intersect(docNodes)); HashSet <HtmlNode> currsposprecision = new HashSet <HtmlNode>(sposprecision.Intersect(docNodes)); HashSet <HtmlNode> currTargetNodes = new HashSet <HtmlNode>(DomPool.TESTSeenTargetNodes.Intersect(docNodes)); double currSiteAccuracy = (currsposprecision.Count() / ((double)currrunres.Count())); double currSiteRecall = (currspos.Count() / ((double)currTargetNodes.Count())); if (((double)currrunres.Count()) > 0) { SiteTotalPrecision[entry.Key] = SiteTotalPrecision[entry.Key] + currSiteAccuracy; SiteTotalRecall[entry.Key] = SiteTotalRecall[entry.Key] + currSiteRecall; } SiteTotalTests[entry.Key] = SiteTotalTests[entry.Key] + 1; } double currAccuracy = (sposprecision.Count() / ((double)runres.Count())); double currRecall = (spos.Count() / ((double)DomPool.TESTSeenTargetNodes.Count())); if (runres.Count() > 0) { totalAccuracy = totalAccuracy + currAccuracy; totalRecall = totalRecall + currRecall; } } totalAccuracy = totalAccuracy / subsetsIndexes.Count(); totalRecall = totalRecall / subsetsIndexes.Count(); Console.WriteLine("########## Results " + tools[tool] + " for i=" + i + "##########"); Console.WriteLine("+++++++++ Detailed Results for i=" + i + "++++++++++#"); double count = 0; double totalSumPrecision = 0; double totalSumRecall = 0; double avgRecall = 0; double avgPrecision = 0; double avgFscore = 0; double numPrecision = 0; foreach (string site in DomPool.allDocsNames) { if (SiteTotalTests[site] < 1) { SiteTotalTests[site]++; } else { numPrecision++; } double sitePrecision = SiteTotalPrecision[site] / SiteTotalTests[site]; double siteRecall = SiteTotalRecall[site] / SiteTotalTests[site]; double siteFscore = 2 * (sitePrecision * siteRecall) / (sitePrecision + siteRecall); if (siteRecall == 0 && sitePrecision == 0) { siteFscore = 0; } count++; avgRecall = avgRecall + siteRecall; avgPrecision = avgPrecision + sitePrecision; avgFscore = avgFscore + siteFscore; Console.WriteLine(">" + site + ": Precision:" + sitePrecision + " , Recall:" + siteRecall + ", F-score:" + siteFscore); } Console.WriteLine("++++++++++++++++Total+++++++++++++++++"); avgRecall = avgRecall / count; avgPrecision = avgPrecision / numPrecision; avgFscore = avgFscore / count; Console.WriteLine("Recall:" + avgRecall); Console.WriteLine("Precision:" + avgPrecision); Console.WriteLine("F-score:" + avgFscore); Console.WriteLine("Time:" + totalTime); } } } } Console.ReadLine(); }
public static bool SaveModel(this SVMModel model, string filename) { return(SVM.SaveModel(model, filename)); }
public static double Predict(this SVMNode[] x, IntPtr ptr_model) { return(SVM.Predict(ptr_model, x)); }
// Use this for initialization void Start() { // Data for visual representation int width = 512, height = 512; Mat image = Mat.zeros(height, width, CvType.CV_8UC4); // Set up training data int[] labels = { 1, -1, -1, -1 }; float[] trainingData = { 501, 10, 255, 10, 501, 255, 10, 501 }; Mat trainingDataMat = new Mat(4, 2, CvType.CV_32FC1); trainingDataMat.put(0, 0, trainingData); Mat labelsMat = new Mat(4, 1, CvType.CV_32SC1); labelsMat.put(0, 0, labels); // Train the SVM SVM svm = SVM.create(); svm.setType(SVM.C_SVC); svm.setKernel(SVM.LINEAR); svm.setTermCriteria(new TermCriteria(TermCriteria.MAX_ITER, 100, 1e-6)); svm.train(trainingDataMat, Ml.ROW_SAMPLE, labelsMat); // Show the decision regions given by the SVM byte[] green = { 0, 255, 0, 255 }; byte[] blue = { 0, 0, 255, 255 }; for (int i = 0; i < image.rows(); ++i) { for (int j = 0; j < image.cols(); ++j) { Mat sampleMat = new Mat(1, 2, CvType.CV_32FC1); sampleMat.put(0, 0, j, i); float response = svm.predict(sampleMat); if (response == 1) { image.put(i, j, green); } else if (response == -1) { image.put(i, j, blue); } } } // Show the training data int thickness = -1; int lineType = 8; Imgproc.circle(image, new Point(501, 10), 5, new Scalar(0, 0, 0, 255), thickness, lineType, 0); Imgproc.circle(image, new Point(255, 10), 5, new Scalar(255, 255, 255, 255), thickness, lineType, 0); Imgproc.circle(image, new Point(501, 255), 5, new Scalar(255, 255, 255, 255), thickness, lineType, 0); Imgproc.circle(image, new Point(10, 501), 5, new Scalar(255, 255, 255, 255), thickness, lineType, 0); // Show support vectors thickness = 2; lineType = 8; Mat sv = svm.getUncompressedSupportVectors(); // Debug.Log ("sv.ToString() " + sv.ToString ()); // Debug.Log ("sv.dump() " + sv.dump ()); for (int i = 0; i < sv.rows(); ++i) { Imgproc.circle(image, new Point((int)sv.get(i, 0) [0], (int)sv.get(i, 1) [0]), 6, new Scalar(128, 128, 128, 255), thickness, lineType, 0); } Texture2D texture = new Texture2D(image.width(), image.height(), TextureFormat.RGBA32, false); Utils.matToTexture2D(image, texture); gameObject.GetComponent <Renderer> ().material.mainTexture = texture; }
public static double PredictProbability(this SVMNode[] x, IntPtr ptr_model, out double[] estimations) { return(SVM.PredictProbability(ptr_model, x, out estimations)); }
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();*/ }
public static double PredictValues(this SVMNode[] x, IntPtr ptr_model, out double[] values) { return(SVM.PredictValues(ptr_model, x, out values)); }
private double SVMComplexModelForBestModel(int y, int z, float[] Et, float[] Zt) { double error = -9999999; if (Math.Max(y, z) < myCategorizedTimeSeri.TrainInputs.GetLength(1)) { return(error); } int numOfInp = y + z + 1; int rows = Et.Length - Math.Max(y, z); float[,] inps = new float[rows, numOfInp]; float[] targs = new float[rows]; int l = 0; for (int i = 0; i < rows; i++) { if (y > z) { for (int j = 0; j < y; j++) { inps[i, j] = Et[l + j]; } inps[i, y] = svmModel.Predict(new Matrix <float>(myCategorizedTimeSeri.getInputsForTarget(l + y))); for (int j = 0; j < z; j++) { inps[i, y + j + 1] = Zt[l + y - z + j]; } targs[i] = myCategorizedTimeSeri.TimeSeri[l + y]; } else { for (int j = 0; j < y; j++) { inps[i, j] = Et[l + z - y + j]; } inps[i, y] = svmModel.Predict(new Matrix <float>(myCategorizedTimeSeri.getInputsForTarget(l + z))); for (int j = 0; j < z; j++) { inps[i, j + y + 1] = Zt[l + j]; } targs[i] = myCategorizedTimeSeri.TimeSeri[l + z]; } l++; } float[,] trainInputs = new float[rows - numberOfTests, numOfInp]; float[] trainTargets = new float[rows - numberOfTests]; float[,] testInputs = new float[numberOfTests, numOfInp]; float[] testTargets = new float[numberOfTests]; int t = 0; for (; t < rows - numberOfTests; t++) { for (int j = 0; j < numOfInp; j++) { trainInputs[t, j] = inps[t, j]; } trainTargets[t] = targs[t]; } for (int i = 0; t < rows; i++, t++) { for (int j = 0; j < numOfInp; j++) { testInputs[i, j] = inps[t, j]; } testTargets[i] = targs[t]; } double minError = 9999999; SVM_KERNEL_TYPE bestKernelType = SVM_KERNEL_TYPE.LINEAR; double bestEps = 0.1; SVMParams p; Matrix <float> trainData = new Matrix <float>(trainInputs); Matrix <float> trainClasses = new Matrix <float>(trainTargets); Matrix <float> testData = new Matrix <float>(testInputs); Matrix <float> testClasses = new Matrix <float>(testTargets); foreach (SVM_KERNEL_TYPE tp in Enum.GetValues(typeof(SVM_KERNEL_TYPE))) { for (double eps = 0.1; eps >= 0.00001; eps *= 0.1) { using (SVM model = new SVM()) { p = new SVMParams(); p.KernelType = tp; p.SVMType = SVM_TYPE.EPS_SVR; // for regression p.C = 1; p.TermCrit = new MCvTermCriteria(100, eps); p.Gamma = 1; p.Degree = 1; p.P = 1; p.Nu = 0.1; bool trained = model.TrainAuto(trainData, trainClasses, null, null, p.MCvSVMParams, 10); double err = getSumError(model, testData, testClasses); if (trained && minError > err) { minError = err; bestEps = eps; bestKernelType = tp; } } } } svmModelHybrid = new SVM(); p = new SVMParams(); p.KernelType = bestKernelType; p.SVMType = Emgu.CV.ML.MlEnum.SVM_TYPE.EPS_SVR; // for regression p.C = 1; p.TermCrit = new MCvTermCriteria(100, bestEps); p.Gamma = 1; p.Degree = 1; p.P = 1; p.Nu = 0.1; bool _trained = svmModelHybrid.TrainAuto(trainData, trainClasses, null, null, p.MCvSVMParams, 10); error = -1 * getSumError(svmModelHybrid, testData, testClasses); return(error); }
public MKLClassification(SVM s) : this(modshogunPINVOKE.new_MKLClassification__SWIG_0(SVM.getCPtr(s)), true) { if (modshogunPINVOKE.SWIGPendingException.Pending) throw modshogunPINVOKE.SWIGPendingException.Retrieve(); }
public static string CheckParameter(this SVMProblem problem, SVMParameter parameter) { return(SVM.CheckParameter(problem, parameter)); }
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 static SVMModel Train(this SVMProblem problem, SVMParameter parameter) { return(SVM.Train(problem, parameter)); }
public DomainAdaptationSVM(double C, Kernel k, Labels lab, SVM presvm, double B) : this(modshogunPINVOKE.new_DomainAdaptationSVM__SWIG_1(C, Kernel.getCPtr(k), Labels.getCPtr(lab), SVM.getCPtr(presvm), B), true) { if (modshogunPINVOKE.SWIGPendingException.Pending) throw modshogunPINVOKE.SWIGPendingException.Retrieve(); }