コード例 #1
0
ファイル: SVM.cs プロジェクト: oloopy/LibSVMsharp
        /// <summary>
        ///
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static SVMModel LoadModel(string filename)
        {
            if (String.IsNullOrWhiteSpace(filename) || !File.Exists(filename))
            {
                return(null);
            }

            IntPtr ptr_filename = Marshal.StringToHGlobalAnsi(filename);

            IntPtr ptr_model = libsvm.svm_load_model(ptr_filename);

            Marshal.FreeHGlobal(ptr_filename);
            ptr_filename = IntPtr.Zero;

            if (ptr_model == IntPtr.Zero)
            {
                return(null);
            }
            else
            {
                SVMModel model = SVMModel.Convert(ptr_model);

                // There is a little memory leackage here !!!
                libsvm.svm_free_model_content(ptr_model);
                ptr_model = IntPtr.Zero;

                return(model);
            }
        }
コード例 #2
0
        public static SVMModel Convert(IntPtr ptr)
        {
            if (ptr == IntPtr.Zero)
            {
                return(null);
            }

            svm_model x = (svm_model)Marshal.PtrToStructure(ptr, typeof(svm_model));

            return(SVMModel.Convert(x));
        }
コード例 #3
0
ファイル: SVM.cs プロジェクト: oloopy/LibSVMsharp
        /// <summary>
        /// This function constructs and returns an SVM model according to the given training data and parameters.
        /// </summary>
        /// <param name="problem">Training data.</param>
        /// <param name="parameter">Parameter set.</param>
        /// <returns>SVM model according to the given training data and parameters.</returns>
        public static SVMModel Train(SVMProblem problem, SVMParameter parameter)
        {
            IntPtr ptr_problem   = SVMProblem.Allocate(problem);
            IntPtr ptr_parameter = SVMParameter.Allocate(parameter);

            IntPtr   ptr_model = libsvm.svm_train(ptr_problem, ptr_parameter);
            SVMModel model     = SVMModel.Convert(ptr_model);

            SVMProblem.Free(ptr_problem);
            SVMParameter.Free(ptr_parameter);
            libsvm.svm_free_model_content(ptr_model);

            return(model);
        }
コード例 #4
0
ファイル: SVM.cs プロジェクト: tome-beta/FaceJudge
        /// <summary>
        /// This function constructs and returns an SVM model according to the given training data and parameters.
        /// </summary>
        /// <param name="problem">Training data.</param>
        /// <param name="parameter">Parameter set.</param>
        /// <returns>SVM model according to the given training data and parameters.</returns>
        public static SVMModel Train(SVMProblem problem, SVMParameter parameter)
        {
            if (problem == null)
            {
                throw new ArgumentNullException("problem");
            }

            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            IntPtr ptr_problem   = SVMProblem.Allocate(problem);
            IntPtr ptr_parameter = SVMParameter.Allocate(parameter);

            IntPtr   ptr_model = libsvm.svm_train(ptr_problem, ptr_parameter);
            SVMModel model     = SVMModel.Convert(ptr_model);

            SVMProblem.Free(ptr_problem);
            SVMParameter.Free(ptr_parameter);
            libsvm.svm_free_model_content(ptr_model);

            return(model);
        }