ccerhan / libsvmsharp Goto Github PK
View Code? Open in Web Editor NEWC# wrapper of LibSVM
License: MIT License
C# wrapper of LibSVM
License: MIT License
I'm running on Windows 8.1 with Visual Studio 2013 on an x64 (I7) machine. I had to build and run with a platform target of x32 to make it work.
Is there a x64 release?
Thanks in advance.
Charles
I've used SVM (correctly, SVR) using Scikitlearn and libSVM using c# wrapper.
I just think that the Scikitlearn version is using the same base cpp svm implementation file.
However, the performance is different even though the data is the same.
And the optimized parameter setting is also different between scikitlearn and libsvm.
For example, in scikitlearn, epsilon=0.37, gamma=0.265, kernel=rbf, c=1.0, tol=0.001 is optimized and its precision performance achieves 70%.
However, in LibSVM, epsilon(P)=0.7, gamma=0.265, kernel=rbf,c=1.0,tol=0.001 is optimized and its precision performance achieves about 62%.
And by the analysis of my trial the number of support vector is also different between two versions, 2936 in python version, 2413 in c# wrapper version.
How should I approach to solve this problem?
I wanted to use SVM.PredictProbability but unfortunately the method crashes:
This can reproduce the issue:
SVMProblem trainingSet = SVMProblemHelper.Load(@"problem.txt");
SVMParameter parameter = new SVMParameter();
parameter.Type = SVMType.C_SVC;
parameter.Kernel = SVMKernelType.RBF;
parameter.C = 1;
parameter.Gamma = 0.5;
SVMModel model = trainingSet.Train(parameter);
for (int i = 0; i < trainingSet.Length; i++)
{
double[] estimations;
var prob = SVM.PredictProbability(model, trainingSet.X[i], out estimations);
}
OK, it does not crash, when I set parameter.Probability = true but then I always get -1 for the probabilits.
When i call LibSVM inside task this error happens after second task started.
If i call libsvm inside main thread, no error happens ever.
If i only start 1 task the error does not happen
As can be seen at the very first image the error is not related to my application or my functions. It is caused by either wrapper or the libSVM.dll itself i dont know which one.
I am using windows 8.1, x64, visual studio 2013, WPF .net 4.5.1 application, 32 gb ram memory on this computer
First here error message
Second error message
Third what works and what causes error
I really need help ty very much
I am tring to read a trained SVM model in unity. It's quite strange here, sometimes i could successfully read the model but sometime it gives me an error for:
"NullReferenceException: Object reference not set to an instance of an object"
The error occurs at this line:
svm_node node = (svm_node)Marshal.PtrToStructure(i_ptr_nodes, typeof(svm_node));
The whole function is:
public static SVMNode[] Convert(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
return null;
List<SVMNode> nodes = new List<SVMNode>();
IntPtr i_ptr_nodes = ptr;
while (true)
{
svm_node node = (svm_node)Marshal.PtrToStructure(i_ptr_nodes, typeof(svm_node));
i_ptr_nodes = ptrExtentions.Add(i_ptr_nodes, Marshal.SizeOf(typeof(svm_node)));
if (node.index > 0)
{
nodes.Add(new SVMNode(node.index, node.value));
}
else
{
break;
}
}
return nodes.ToArray();
}
The whole track is:
NullReferenceException: Object reference not set to an instance of an object
at (wrapper unknown) intptr:PtrToStructure (intptr,object)
at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal:PtrToStructure (intptr,System.Type)
at LibSVMsharp.SVMModel.Convert (svm_model x) [0x003a7] in D:\AttentionDemo-copy\racing-game-course-csharp\Assets\Libsvm\SVMModel.cs:205
at LibSVMsharp.SVMModel.Convert (IntPtr ptr) [0x0002f] in D:\AttentionDemo-copy\racing-game-course-csharp\Assets\Libsvm\SVMModel.cs:219
at LibSVMsharp.SVM.LoadModel (System.String filename) [0x00053] in D:\AttentionDemo-copy\racing-game-course-csharp\Assets\Libsvm\SVM.cs:177
at RunOnlineTraining.run_online_training (System.String inDataPath, System.String mdlPath, System.String cfgPath) [0x001e1] in D:\AttentionDemo-copy\racing-game-course-csharp\Assets\Scripts\RunOnlineTraining.cs:113
at CalibManager.trainModel () [0x00016] in D:\AttentionDemo-copy\racing-game-course-csharp\Assets\Scripts\CalibManager.cs:533
at CalibManager.FinishSession () [0x00279] in D:\AttentionDemo-copy\racing-game-course-csharp\Assets\Scripts\CalibManager.cs:519
at CalibManager.Update () [0x000d4] in D:\AttentionDemo-copy\racing-game-course-csharp\Assets\Scripts\CalibManager.cs:102(Filename: D:/AttentionDemo-copy/racing-game-course-csharp/Assets/Libsvm/SVMModel.cs Line: 205)
I have been stuck here for quite long time. If anyone has any suggestion, it would be a great appreciate.
hello every one..... please can any one help me in use the "libsvmsharp-64bit" in satellite image classification and explain me the step by step ..... thanks for your answer previously
best regards
SVMModel has save feature which saves the model into a text file
But i can not find how to load it into a SVMModel object. I mean create SVMModel object from a text file previously saved.
Any help is greatly appreciated ty
When I run the example of classification, the function of SaveModel returns true, but there is no model file created.
it is a The endless loop , in class SVMModelExtensions model.PredictValues
public static class SVMModelExtensions
{
public static double PredictValues(this SVMModel model, SVMNode[] x, out double[] values)
{
return model.PredictValues(x, out values);
}
Here's a minimal snippet to reproduce the problem:
private static void Main(string[] args)
{
SVMProblem testSet = SVMProblemHelper.Load(@"Dataset\wine.txt");
var x = testSet.Train(new SVMParameter{Type=SVMType.ONE_CLASS});
}
This fails in SVMModel Convert(svm_model x)
, at
y.SVCounts = new int[y.ClassCount];
Marshal.Copy(x.nSV, y.SVCounts, 0, y.SVCounts.Length);
y.Labels = new int[y.ClassCount];
Marshal.Copy(x.label, y.Labels, 0, y.Labels.Length);
because both x.nSV and x.label are 0 (null pointers).
I don't think it's a problem with the data because CrossValidation does not have a similar issue:
private static void Main(string[] args)
{
SVMProblem testSet = SVMProblemHelper.Load(@"Dataset\wine.txt");
double[] target;
testSet.CrossValidation(new SVMParameter{Type=SVMType.ONE_CLASS}, 5, out target);
}
I've tried to debug it myself with no success, it's not clear to me why LibSVM is returning null pointers for nSV and label.
Hello. I have downloaded libSVMsharp
And i downloaded latest libsvm dll (http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm.cgi?+http://www.csie.ntu.edu.tw/~cjlin/libsvm+zip) and replace it with libSVMsharp dll
the below code works fine at x86 application
SVMProblem problem = SVMProblemHelper.Load(@"train.txt");
SVMProblem testProblem = SVMProblemHelper.Load(@"test.txt");
SVMParameter parameter = new SVMParameter();
parameter.Type = SVMType.C_SVC;
parameter.Kernel = SVMKernelType.RBF;
parameter.C = 1;
parameter.Gamma = 1;
SVMModel model = SVM.Train(problem, parameter);
double[] target = new double[testProblem.Length];
for (int i = 0; i < testProblem.Length; i++)
target[i] = SVM.Predict(model, testProblem.X[i]);
double accuracy = SVMHelper.EvaluateClassificationProblem(testProblem, target);
however when i turn my c# 4.5.2 WPF application to x64 it fails. where can i find x64 version of this dll?
or how can i make it work?
Now the other thing is i want to do cross validation to find best parameters
How do i do it? i could not find any example on your github
can you write me example to Use cross-validation to find the best parameter C and γ
Should this be a call to PredictValues rather than PredictProbability?
I have a SVM model, name as insulin_model.txt. When I execute the below code:
//Load the model
SVMModel load_model = SVM.LoadModel(@"insulin_model.txt");
//Set SVMNode values
SVMNode[] nodes = new SVMNode[8];
nodes[0] = new SVMNode(1, -0.628);
nodes[1] = new SVMNode(2, -0.818);
nodes[2] = new SVMNode(3, -0.778);
nodes[3] = new SVMNode(4, -0.333);
nodes[4] = new SVMNode(5, -0.093);
nodes[5] = new SVMNode(6, -0.941);
nodes[6] = new SVMNode(7, -0.88);
nodes[7] = new SVMNode(8, 0.336);
double[] predict_values;
double _r = SVM.PredictProbability(load_model, nodes, out predict_values);
Console.WriteLine("SVM.PredictProbability:" + _r.ToString());
I got Marshal.FreeHGlobal(x.rho) unknown error. I am very pleasure that someone can help me. Thanks.
Hello,
The SVM.SetPrintStringFunction, which corresponds to the svm_set_print_string_function in libsvm, is not working. Normally, the output string is logged on the console (stdout) while training is going on.
// Default print function in libsvm
static void print_string_stdout(const char *s)
{
fputs(s,stdout);
fflush(stdout);
}
// The original function located in libsvm
void svm_set_print_string_function(void (*print_func)(const char *))
{
if(print_func == NULL)
svm_print_string = &print_string_stdout;
else
svm_print_string = print_func;
}
The problem is that I could not change the function pointer from C# layer. I set a delegate pointer for the callback. When I am in debug mode, I can go into the callback function I defined. However, when I continue (pressing F5), the console which I executed the test program is suddenly terminated without an error. The test code is shown below.
static void Main(string[] args)
{
SVM.SetPrintStringFunction(new SVMPrintFunction(Output));
// some code ...
SVMModel model = SVM.Train(problem, parameter); // Logging is started here
// some code ....
}
public static void Output(string log) // I can see the log value in the first time in debug mode
{
Console.WriteLine(""); // DEBUG POINT
}
Please solve this issue, and send a pull request.
Can Erhan.
Hello, Sorry to disturb you!
I'm a beginner of C#, and I have a project, by using Kinect to capture the feature points of faces to classify different emotions (I'm using VS 2013). Now, I've got the data of feature points, and I want to use LibSVMsharp to do the classification. I've downloaded your LibSVMsharp, and done the installation "PM> Install-Package LibSVMsharp" successfully.
Then I met two problems.
First, when I tried to add libsvm.dll to the reference, it failed, and alerted that "A reference to .......\Debug\libsvm.dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component." So I changed another method, by using DllImport, like below.
public class Libsvm
{
/// <param name="prob">svm_problem</param>
/// <param name="param">svm_parameter</param>
/// <returns>svm_model</returns>
[DllImport("libsvm.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr svm_train(IntPtr prob, IntPtr param);
/// <param name="prob">svm_problem</param>
/// <param name="param">svm_parameter</param>
/// <param name="nr_fold">int</param>
/// <param name="target">double[]</param>
[DllImport("libsvm.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void svm_cross_validation(IntPtr prob, IntPtr param, int nr_fold, IntPtr target);
.........
}
And then when I copy the "####Simple Classification" code to my project, there are many errors, like this:
Error 6 The type or namespace name 'SVMProblem' could not be found (are you missing a using directive or an assembly reference?) C:\Program Files\Microsoft SDKs\Kinect\Developer Toolkit v1.8.0\Samples\C#\FaceTrackingBasics-WPF\MainWindow.xaml.cs 339 13 FaceTrackingBasics-WPF
Error 14 The type or namespace name 'SVMModel' could not be found (are you missing a using directive or an assembly reference?) C:\Program Files\Microsoft SDKs\Kinect\Developer Toolkit v1.8.0\Samples\C#\FaceTrackingBasics-WPF\MainWindow.xaml.cs 348 13 FaceTrackingBasics-WPF
........
I've searched a lot, but still couldn't solve my problems, can you help me?
Many thankss!
Shirley Ma
hello, I'm using your project to support my experiments in C#. When I set up the parameters of LibSVMsharp: parameter.C = 6, parameter.Gamma = 0, parameter.Degree=3, it gives me a relatively low accuracy. In comparison, when I set up the same parameters in java LibSVM or WEKA LibSVM on the same train data, both of them output a higher accuracy.
I need your help, cause I have to cary my experiments in C#, if your parameters do not work, please give me some advise.
Hello. I am trying to use weights for each class. When i call
double[] testResults = testSet.Predict(model);
The error happens inside SVMModel.Free(ptr_model);
public static double[] Predict(this SVMProblem problem, SVMModel model)
{
IntPtr ptr_model = SVMModel.Allocate(model);
double[] target = problem.X.Select(x => x.Predict(ptr_model)).ToArray();
SVMModel.Free(ptr_model);
return target;
}
inside
SVMModel.Free(x);
inside
SVMParameter.Free(x.param);
It causes error at this function
public static void Free(svm_parameter x)
{
Marshal.FreeHGlobal(x.weight);
x.weight = IntPtr.Zero;
Marshal.FreeHGlobal(x.weight_label);
x.weight_label = IntPtr.Zero;
}
The error giving line is :
Marshal.FreeHGlobal(x.weight);
Even in debug mode i don't get any particular error.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.