Marshal.FreeHGlobal(x.weight); causes unknown error
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.
Even wrapping them inside try catch causes error :(
The error does not happen if i don't use parameter.WeightLabels
First, sorry for my late answer.
Could you explain how you use weights for each class? By using the properties Weights and WeightLabels in SVMParameter?
Please check the lines between 414 and 424 in this link, just to be sure.
if you use Weights\ WeightLabels and model , you should reload model. like this is OK: SaveModel and LoadModel
parameter.WeightLabels = lableFeaturesBuilder.CreateWeightLables
var model2 = LibSVMsharp.SVM.Train(problem, parameter); model2.SaveModel("roomMatching.model");
var model = LibSVMsharp.SVM.LoadModel("roomMatching.model"); foreach (var room in rooms1) { predictedY = LibSVMsharp.SVM.Predict(model, nodes);
you can see my sample:
https://github.com/zengfr/SVM-Neuro-Matching/blob/master/roomMatching/roomMatching/Program.cs
To reproduce this issue just add weights to LibSVMsharp.Examples.Classification:
// Select the parameter set
SVMParameter parameter = new SVMParameter();
parameter.Type = SVMType.C_SVC;
parameter.Kernel = SVMKernelType.RBF;
parameter.C = 1;
parameter.Gamma = 1;
parameter.WeightLabels = new[] { 1, 2, 3 };
parameter.Weights = new[] { 1.0, 2.0, 3.0 };
I think there is an extra call of SVMModel.Free in SVMModel.Allocate
Commenting this call resolves problem:
// Allocate model.parameter
IntPtr ptr_param = SVMParameter.Allocate(x.Parameter);
y.param = (svm_parameter)Marshal.PtrToStructure(ptr_param, typeof(svm_parameter));
// SVMParameter.Free(ptr_param);