public static SVMParameter Convert(IntPtr ptr) { if (ptr == IntPtr.Zero) { return(null); } svm_parameter x = (svm_parameter)Marshal.PtrToStructure(ptr, typeof(svm_parameter)); return(SVMParameter.Convert(x)); }
public static SVMModel Convert(svm_model x) { SVMModel y = new SVMModel(); y.Creation = (CreationType)x.free_sv; y.ClassCount = x.nr_class; y.TotalSVCount = x.l; if (y.Creation == CreationType.LOAD_MODEL) { y.Parameter = new SVMParameter(); y.Parameter.Type = (SVMType)x.param.svm_type; y.Parameter.Kernel = (SVMKernelType)x.param.kernel_type; switch (y.Parameter.Kernel) { case SVMKernelType.LINEAR: break; case SVMKernelType.POLY: y.Parameter.Gamma = x.param.gamma; y.Parameter.Coef0 = x.param.coef0; y.Parameter.Degree = x.param.degree; break; case SVMKernelType.RBF: y.Parameter.Gamma = x.param.gamma; break; case SVMKernelType.SIGMOID: y.Parameter.Gamma = x.param.gamma; y.Parameter.Coef0 = x.param.coef0; break; } } else { y.Parameter = SVMParameter.Convert(x.param); } int problemCount = (int)(y.ClassCount * (y.ClassCount - 1) * 0.5); y.Rho = new double[problemCount]; Marshal.Copy(x.rho, y.Rho, 0, y.Rho.Length); y.ProbabilityA = null; if (x.probA != IntPtr.Zero) { y.ProbabilityA = new double[problemCount]; Marshal.Copy(x.probA, y.ProbabilityA, 0, y.ProbabilityA.Length); } y.ProbabilityB = null; if (x.probB != IntPtr.Zero) { y.ProbabilityB = new double[problemCount]; Marshal.Copy(x.probB, y.ProbabilityB, 0, y.ProbabilityB.Length); } 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); y.SVCoefs = new List <double[]>(y.ClassCount - 1); IntPtr i_ptr_svcoef = x.sv_coef; for (int i = 0; i < y.ClassCount - 1; i++) { y.SVCoefs.Add(new double[y.TotalSVCount]); IntPtr coef_ptr = (IntPtr)Marshal.PtrToStructure(i_ptr_svcoef, typeof(IntPtr)); Marshal.Copy(coef_ptr, y.SVCoefs[i], 0, y.SVCoefs[i].Length); i_ptr_svcoef = IntPtr.Add(i_ptr_svcoef, Marshal.SizeOf(typeof(IntPtr))); } y.SVIndices = null; if (x.sv_indices != IntPtr.Zero) { y.SVIndices = new int[y.TotalSVCount]; Marshal.Copy(x.sv_indices, y.SVIndices, 0, y.SVIndices.Length); } y.SV = new List <SVMNode[]>(); IntPtr i_ptr_sv = x.SV; for (int i = 0; i < x.l; i++) { IntPtr ptr_nodes = (IntPtr)Marshal.PtrToStructure(i_ptr_sv, typeof(IntPtr)); SVMNode[] nodes = SVMNode.Convert(ptr_nodes); y.SV.Add(nodes); i_ptr_sv = IntPtr.Add(i_ptr_sv, Marshal.SizeOf(typeof(IntPtr))); } return(y); }