Exemple #1
0
        //---------------------------------------


        public void GradientDescentWeightOptimization(KernelType k, double noIterations, double paramA, double paramB)
        {
            //Lets do a gradient descent

            //Find the derivative
            double eps = 0.1;
            double SumSqrrdError;

            ResetPointWheights();


            //The direct approach, good for debug, and aquiring ideas!
            for (int iteration = 0; iteration < noIterations; iteration++)
            {
                SumSqrrdError = 0;

                foreach (Point p in dataPoints)
                {
                    //Find the derivative of the sqrErr
                    double err1 = GetSqrErr(k, p, paramA, paramB);
                    p.Weight += 0.001;
                    double err2 = GetSqrErr(k, p, paramA, paramB);
                    SumSqrrdError += err2;
                    double derivative = (err1 - err2) / 0.001;
                    double step       = (eps * derivative) - 0.001;

                    //if (iteration > 0) {step /= iteration;}

                    p.Weight += step;
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// Specify the kernel and associated parameters
 /// </summary>
 /// <param name="kernelType">Set type of kernel function</param>
 /// <param name="gamma">Set gamma in kernel function</param>
 /// <param name="r">Set coef0 in kernel function, used for polynomial kernel Only</param>
 /// <param name="degree">Set degree in kernel function, used in polynomial kernel only</param>
 /// <remarks>The class is used to facilitate the management of parameters</remarks>
 /// <seealso cref="KernelHelper"/>
 public Kernel(KernelType kernelType, double gamma, double r, int degree)
 {
     this.KernelType = kernelType;
     this.Gamma = gamma;
     this.R = r;
     this.Degree = degree;
 }
Exemple #3
0
        /// <summary>
        /// Returns the estimated y value for the kernel regression
        /// </summary>
        /// <param name="chargeHypothesis">the kernel type</param>
        /// <param name="x">the value for which the y estimate should be computed</param>
        /// <param name="ParamA">any special kernel parameter, such as alfa for the gaussian kernel</param>
        /// <returns></returns>
        public double KernelCompute(KernelType k, double x, double ParamA, double paramB)
        {
            double wSum = 0;
            double kSum = 0;


            if (k.Equals(KernelType.Gaussian))
            {
                double speedParam = 2 * Math.Pow(ParamA, 2) * (-1);
                foreach (Point p in dataPoints)
                {
                    double r = Math.Pow(Math.E, (Math.Pow(x - p.X, 2) / (speedParam)));
                    wSum += r * p.Weight;
                    kSum += r;
                }
            }
            else
            {
                //We are using the Norm kernel

                foreach (Point p in dataPoints)
                {
                    double r = -1 * (Math.Abs(x - p.X) / ParamA) + paramB;
                    wSum += r * p.Weight;
                    kSum += r;
                }
            }



            double result = wSum / kSum;

            return(result);
        }
Exemple #4
0
 public void getSvmModel(string modelPath, bool BuildModel = false)
 {
     using (System.IO.StreamReader sr = new System.IO.StreamReader(modelPath))
     {
         string     mType = sr.ReadLine();
         modelTypes m     = (modelTypes)Enum.Parse(typeof(modelTypes), mType);
         if (m != modelTypes.SVM)
         {
             System.Windows.Forms.MessageBox.Show("Model file specified is not a DA!!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
             return;
         }
         InTablePath           = sr.ReadLine();
         IndependentFieldNames = sr.ReadLine().Split(new char[] { ',' });
         DependentFieldNames   = sr.ReadLine().Split(new char[] { ',' });
         ClassFieldNames       = sr.ReadLine().Split(new char[] { ',' });
         n         = System.Convert.ToInt32(sr.ReadLine());
         nvars     = System.Convert.ToInt32(sr.ReadLine());
         sserror   = System.Convert.ToDouble(sr.ReadLine());
         kTyp      = (KernelType)Enum.Parse(typeof(KernelType), sr.ReadLine());
         minValues = (from string s in (sr.ReadLine().Split(new char[] { ' ' })) select System.Convert.ToDouble(s)).ToArray();
         maxValues = (from string s in (sr.ReadLine().Split(new char[] { ' ' })) select System.Convert.ToDouble(s)).ToArray();
         sumValues = (from string s in (sr.ReadLine().Split(new char[] { ' ' })) select System.Convert.ToDouble(s)).ToArray();
         sr.Close();
     }
     if (BuildModel)
     {
         string svmMPath = modelPath.Replace(".mdl", ".svm");
         MulticlassSupportVectorMachine.Load(svmMPath);
     }
 }
Exemple #5
0
        /*
         * the coefficient b is in this function:     y=Trans(W_vector)(x_vector)+b
         *
         */
        public SupportVectorMachine(double[][] TrainingData, int[] Traininglabels, KernelType tp)
        {
            if (TrainingData.Length > 0 && TrainingData.Length == Traininglabels.Length)
            {
                this.INPUTVALUES = TrainingData;
                this.LABELS      = Traininglabels;


                SimplifiedSMO SSMO = new SimplifiedSMO(INPUTVALUES, LABELS, 1e1, tp);
                for (int i = 0; i < SSMO.ALPHAs.Length; i++)
                {
                    Console.WriteLine("alpha[{0}] is:{1}", i, SSMO.ALPHAs[i]);
                }
                W_vector = Wvector_Evaluate(INPUTVALUES, LABELS, SSMO.ALPHAs);
                b        = SSMO.b;
            }
            else
            {
                if (TrainingData.Length <= 0)
                {
                    Console.WriteLine("Length of Traning Data is wrong");
                }
                if (Traininglabels.Length != TrainingData.Length)
                {
                    Console.WriteLine("data length of labels and inputvalues are not the same");
                }
            }
        }
Exemple #6
0
 public void getDaModel(string modelPath, bool BuildModel = false)
 {
     using (System.IO.StreamReader sr = new System.IO.StreamReader(modelPath))
     {
         string     mType = sr.ReadLine();
         modelTypes m     = (modelTypes)Enum.Parse(typeof(modelTypes), mType);
         if (m != modelTypes.KDA)
         {
             System.Windows.Forms.MessageBox.Show("Model file specified is not a DA!!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
             return;
         }
         InTablePath           = sr.ReadLine();
         IndependentFieldNames = sr.ReadLine().Split(new char[] { ',' });
         DependentFieldNames   = sr.ReadLine().Split(new char[] { ',' });
         ClassFieldNames       = sr.ReadLine().Split(new char[] { ',' });
         n     = System.Convert.ToInt32(sr.ReadLine());
         nvars = System.Convert.ToInt32(sr.ReadLine());
         kTyp  = (KernelType)Enum.Parse(typeof(KernelType), sr.ReadLine());
         setKernalType(kTyp);
         minValues  = (from string s in (sr.ReadLine().Split(new char[] { ' ' })) select System.Convert.ToDouble(s)).ToArray();
         maxValues  = (from string s in (sr.ReadLine().Split(new char[] { ' ' })) select System.Convert.ToDouble(s)).ToArray();
         sumValues  = (from string s in (sr.ReadLine().Split(new char[] { ' ' })) select System.Convert.ToDouble(s)).ToArray();
         meanValues = (from string s in (sr.ReadLine().Split(new char[] { ' ' })) select System.Convert.ToDouble(s)).ToArray();
         stdValues  = (from string s in (sr.ReadLine().Split(new char[] { ' ' })) select System.Convert.ToDouble(s)).ToArray();
         sr.Close();
     }
     if (BuildModel)
     {
         buildModel();
     }
 }
Exemple #7
0
 /// <summary>
 /// Specify the kernel and associated parameters
 /// </summary>
 /// <param name="kernelType">Set type of kernel function</param>
 /// <param name="gamma">Set gamma in kernel function</param>
 /// <param name="r">Set coef0 in kernel function, used for polynomial kernel Only</param>
 /// <param name="degree">Set degree in kernel function, used in polynomial kernel only</param>
 /// <remarks>The class is used to facilitate the management of parameters</remarks>
 /// <seealso cref="KernelHelper"/>
 public Kernel(KernelType kernelType, double gamma, double r, int degree)
 {
     this.KernelType = kernelType;
     this.Gamma      = gamma;
     this.R          = r;
     this.Degree     = degree;
 }
Exemple #8
0
        /// <summary>
        ///     Returns whether or not an enumeration instance a valid value.
        ///     This method is designed to be used with ValidateValueCallback, and thus
        ///     matches it's prototype.
        /// </summary>
        /// <param name="valueObject">
        ///     Enumeration value to validate.
        /// </param>
        /// <returns> 'true' if the enumeration contains a valid value, 'false' otherwise. </returns>
        public static bool IsKernelTypeValid(object valueObject)
        {
            KernelType value = (KernelType)valueObject;

            return((value == KernelType.Gaussian) ||
                   (value == KernelType.Box));
        }
        /// <summary>
        /// Returns a Effect that emulates this BlurBitmapEffect.
        /// </summary>
        internal override Effect GetEmulatingEffect()
        {
            if (_imageEffectEmulation != null && _imageEffectEmulation.IsFrozen)
            {
                return(_imageEffectEmulation);
            }

            if (_imageEffectEmulation == null)
            {
                _imageEffectEmulation = new BlurEffect();
            }

            double radius = Radius;

            if (_imageEffectEmulation.Radius != radius)
            {
                _imageEffectEmulation.Radius = radius;
            }

            KernelType kernelType = KernelType;

            if (_imageEffectEmulation.KernelType != kernelType)
            {
                _imageEffectEmulation.KernelType = kernelType;
            }

            _imageEffectEmulation.RenderingBias = RenderingBias.Performance;

            if (this.IsFrozen)
            {
                _imageEffectEmulation.Freeze();
            }

            return(_imageEffectEmulation);
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="Lambda">Kernel parameter</param>
 /// <param name="newC">Regularization parameter</param>
 /// <param name="newTol">Numerical tolerance</param>
 /// <param name="newMaxPasses">Max # of times to iterate over alphas without changing</param>
 /// <param name="newKernelType">Type of kernel function index</param>
 public ProblemConfig(float Lambda, float newC, float newTol, int newMaxPasses, KernelType newKernelType)
 {
     this.lambda = Lambda;
     c = newC;
     tol = newTol;
     maxPasses = newMaxPasses;
     kernelType = newKernelType;
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="Lambda">Kernel parameter</param>
 /// <param name="newC">Regularization parameter</param>
 /// <param name="newTol">Numerical tolerance</param>
 /// <param name="newMaxPasses">Max # of times to iterate over alphas without changing</param>
 /// <param name="newKernelType">Type of kernel function index</param>
 public ProblemConfig(float Lambda, float newC, float newTol, int newMaxPasses, KernelType newKernelType)
 {
     this.lambda = Lambda;
     c           = newC;
     tol         = newTol;
     maxPasses   = newMaxPasses;
     kernelType  = newKernelType;
 }
Exemple #12
0
        /// <summary>
        /// Applies the pixel scaler for float32 images.
        /// </summary>
        /// <param name="type">The type of scaler to use.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="centeredGrid">if set to <c>true</c> [centered grid].</param>
        /// <param name="filterRegion">The filter region, if any.</param>
        /// <returns>
        /// The rescaled image.
        /// </returns>
        public cImage ApplyScaler(KernelType type, int width, int height, bool centeredGrid, Rectangle?filterRegion = null)
        {
            var fpImage  = FloatImage.FromImage(this, filterRegion);
            var fpResult = fpImage.Resize(width, height, type, centeredGrid);
            var result   = fpResult.ToImage();

            return(result);
        }
Exemple #13
0
 public dataPrepSvmReg(string tablePath, string dependentField, string independentFields, string categoricalFields, KernelType kType = KernelType.Sigmoid)
 {
     InTablePath           = tablePath;
     DependentFieldNames   = dependentField.Split(new char[] { ',' });
     IndependentFieldNames = independentFields.Split(new char[] { ',' });
     ClassFieldNames       = categoricalFields.Split(new char[] { ',' });
     kTyp = kType;
     buildModel();
 }
 public dataPrepDiscriminantAnalysis(ITable table, string[] dependentField, string[] independentFields, string[] categoricalFields,KernelType k)
 {
     InTable = table;
     DependentFieldNames = dependentField;
     IndependentFieldNames = independentFields;
     ClassFieldNames = categoricalFields;
     setKernalType(k);
     buildModel();
 }
 public dataPrepDiscriminantAnalysis(string tablePath, string dependentField, string independentFields, string categoricalFields,KernelType k)
 {
     InTablePath = tablePath;
     DependentFieldNames = dependentField.Split(new char[] { ',' });
     IndependentFieldNames = independentFields.Split(new char[] { ',' });
     ClassFieldNames = categoricalFields.Split(new char[] { ',' });
     setKernalType(k);
     buildModel();
 }
Exemple #16
0
 public dataPrepSvmReg(ITable table, string[] dependentField, string[] independentFields, string[] categoricalFields, KernelType kType = KernelType.Sigmoid)
 {
     InTable               = table;
     DependentFieldNames   = dependentField;
     IndependentFieldNames = independentFields;
     ClassFieldNames       = categoricalFields;
     kTyp = kType;
     buildModel();
 }
Exemple #17
0
        public void Generate()
        {
            var m = Rows(dx);
            var n = Cols(dx);

            var idx = 0;

            for (var i = 0; i < m; i++)
            {
                if (Math.Abs(alpha[i]) > 0)
                {
                    idx++;
                }
            }

            ManagedOps.Free(ModelX, ModelY, Alpha, W, KernelParam);

            ModelX      = new ManagedArray(Cols(dx), idx);
            ModelY      = new ManagedArray(1, idx);
            Alpha       = new ManagedArray(1, idx);
            KernelParam = new ManagedArray(kparam);

            var ii = 0;

            for (var i = 0; i < m; i++)
            {
                if (Math.Abs(alpha[i]) > 0)
                {
                    for (int j = 0; j < n; j++)
                    {
                        ModelX[j, ii] = dx[j, i];
                    }

                    ModelY[ii] = dy[i];

                    Alpha[ii] = alpha[i];

                    ii++;
                }
            }

            B      = b;
            Passes = Iterations;
            ManagedOps.Copy2D(KernelParam, kparam, 0, 0);
            Type = ktype;

            var axy = ManagedMatrix.BSXMUL(alpha, dy);
            var tay = ManagedMatrix.Transpose(axy);
            var txx = ManagedMatrix.Multiply(tay, dx);

            W = ManagedMatrix.Transpose(txx);

            Trained = true;

            ManagedOps.Free(dx, dy, K, kparam, E, alpha, axy, tay, txx);
        }
        private void Ok_OnClick(object sender, RoutedEventArgs e) {
            switch (cmbFilters.SelectionBoxItem.ToString()) {
                case "Gaussian (3x3)":
                    kernelType = KernelType.Gaussian3x3;
                    break;
                case "Gaussian (5x5)":
                    kernelType = KernelType.Gaussian5x5;
                    break;
                case "Gaussian (7x7)":
                    kernelType = KernelType.Gaussian7x7;
                    break;
                case "Mean (3x3)":
                    kernelType = KernelType.Mean3x3;
                    break;
                case "Mean (5x5)":
                    kernelType = KernelType.Mean5x5;
                    break;
                case "Mean (7x7)":
                    kernelType = KernelType.Mean7x7;
                    break;
                case "Median (3x3)":
                    kernelType = KernelType.Median3x3;
                    break;
                case "Median (5x5)":
                    kernelType = KernelType.Median5x5;
                    break;
                case "Median (7x7)":
                    kernelType = KernelType.Median7x7;
                    break;
                case "Median (9x9)":
                    kernelType = KernelType.Median9x9;
                    break;
                case "Low pass (3x3)":
                    kernelType = KernelType.LowPass3x3;
                    break;
                case "Low pass (5x5)":
                    kernelType = KernelType.LowPass5x5;
                    break;
                case "Sharpen (3x3)":
                    kernelType = KernelType.Sharpen3x3;
                    break;
                case "Sharpen (5x5)":
                    kernelType = KernelType.Sharpen5x5;
                    break;
                case "Sharpen (7x7)":
                    kernelType = KernelType.Sharpen7x7;
                    break;
                case "None":
                    kernelType = KernelType.None;
                    break;
            }

            threshold = (byte)sldThreshold.Value;
            m_backgroundWorker.RunWorkerAsync();
            Close();
        }
 public dataPrepSvm(string tablePath, string dependentField, string independentFields, string categoricalFields,KernelType kType= KernelType.Sigmoid)
 {
     InTablePath = tablePath;
     DependentFieldNames = dependentField.Split(new char[] { ',' });
     IndependentFieldNames = independentFields.Split(new char[] { ',' });
     ClassFieldNames = categoricalFields.Split(new char[] { ',' });
     kTyp = kType;
     setKernalType(kTyp);
     buildModel();
 }
        public static TrainingHeader Create(KernelType type, SvmType svmType)
        {
            TrainingHeader header = new TrainingHeader();

            header.GridSelection = true;
            header.Normalization = NormalizationType.None;
            header.Kernel        = type;
            header.SvmType       = svmType;
            return(header);
        }
 public dataPrepSvm(ITable table, string[] dependentField, string[] independentFields, string[] categoricalFields,KernelType kType= KernelType.Sigmoid)
 {
     InTable = table;
     DependentFieldNames = dependentField;
     IndependentFieldNames = independentFields;
     ClassFieldNames = categoricalFields;
     kTyp = kType;
     setKernalType(kTyp);
     buildModel();
 }
Exemple #22
0
        public double EstimateMeanSquaredError(KernelType k, double paramA, double paramB)
        {
            double squaredError = 0;

            foreach (Point p in dataPoints)
            {
                squaredError += GetSqrErr(k, p, paramA, paramB);
            }

            return(squaredError / dataPoints.Count);
        }
Exemple #23
0
        // SVMTRAIN Trains an SVM classifier using a simplified version of the SMO
        // algorithm.
        //
        // [model] = svm_train(X, Y, C, kernelFunction, kernelParam, tol, max_passes) trains an
        // SVM classifier and returns trained model. X is the matrix of training
        // examples.  Each row is a training example, and the jth column holds the
        // jth feature.  Y is a column matrix containing 1 for positive examples
        // and 0 for negative examples.  C is the standard SVM regularization
        // parameter.  tol is a tolerance value used for determining equality of
        // floating point numbers. max_passes controls the number of iterations
        // over the dataset (without changes to alpha) before the algorithm quits.
        //
        // Note: This is a simplified version of the SMO algorithm for training
        // SVMs. In practice, if you want to train an SVM classifier, we
        // recommend using an optimized package such as:
        //
        // LIBSVM   (http://www.csie.ntu.edu.tw/~cjlin/libsvm/)
        // SVMLight (http://svmlight.joachims.org/)
        //
        // Converted to R by: SD Separa (2016/03/18)
        // Converted to C# by: SD Separa (2018/09/29)
        //
        public void Train(ManagedArray x, ManagedArray y, double c, KernelType kernel, ManagedArray param, double tolerance = 0.001, int maxpasses = 5, int category = 1)
        {
            Setup(x, y, c, kernel, param, tolerance, maxpasses, category);

            // Train
            while (!Step())
            {
            }

            Generate();
        }
Exemple #24
0
 public Model(ManagedArray x, ManagedArray y, KernelType type, ManagedArray kernelParam, ManagedArray alpha, double b, ManagedArray w, int passes)
 {
     ModelX      = x;
     ModelY      = y;
     Type        = type;
     KernelParam = kernelParam;
     Alpha       = alpha;
     B           = b;
     W           = w;
     Passes      = passes;
     Trained     = true;
 }
        public void TestMulticlassProbability()
        {
            SvmType[]    svmTypes    = new SvmType[] { SvmType.C_SVC, SvmType.NU_SVC };
            KernelType[] kernelTypes = new KernelType[] { KernelType.LINEAR, KernelType.POLY, KernelType.RBF, KernelType.SIGMOID };

            foreach (SvmType svm in svmTypes)
            {
                foreach (KernelType kernel in kernelTypes)
                {
                    double score = testMulticlassModel(8, 100, svm, kernel, true);

                    Assert.AreEqual(1, score, .1, string.Format("SVM {0} with Kernel {1} did not train correctly", svm, kernel));
                }
            }
        }
Exemple #26
0
        public Kernel(int l, Node[][] x_, Parameter param)
        {
            _kernelType = param.KernelType;
              _degree = param.Degree;
              _gamma = param.Gamma;
              _coef0 = param.Coefficient0;

              _x = (Node[][])x_.Clone();

              if (_kernelType == KernelType.RBF) {
            _xSquare = new double[l];
            for (int i = 0; i < l; i++)
              _xSquare[i] = dot(_x[i], _x[i]);
              } else _xSquare = null;
        }
Exemple #27
0
 /// <summary>
 /// Default Constructor.  Gives good default values to all parameters.
 /// </summary>
 public Parameter()
 {
     _svmType     = SvmType.NU_SVR;
     _kernelType  = KernelType.LINEAR;
     _degree      = 3;
     _gamma       = 0; // 1/k
     _coef0       = 0;
     _nu          = 0.5;
     _cacheSize   = 100;
     _C           = 2;
     _eps         = 1e-3;
     _p           = 0.1;
     _shrinking   = true;
     _probability = false;
     _weights     = new Dictionary <int, double>();
 }
Exemple #28
0
 public override int GetHashCode()
 {
     return(C.GetHashCode() +
            CacheSize.GetHashCode() +
            Coefficient0.GetHashCode() +
            Degree.GetHashCode() +
            EPS.GetHashCode() +
            Gamma.GetHashCode() +
            KernelType.GetHashCode() +
            Nu.GetHashCode() +
            P.GetHashCode() +
            Probability.GetHashCode() +
            Shrinking.GetHashCode() +
            SvmType.GetHashCode() +
            Weights.ToArray().ComputeHashcode());
 }
Exemple #29
0
        public void TestRegression()
        {
            SvmType[] svmTypes = new SvmType[] { SvmType.NU_SVR, SvmType.EPSILON_SVR };
            // LINEAR kernel is pretty horrible for regression
            KernelType[] kernelTypes = new KernelType[] { KernelType.LINEAR, KernelType.RBF, KernelType.SIGMOID };

            foreach (SvmType svm in svmTypes)
            {
                foreach (KernelType kernel in kernelTypes)
                {
                    double error = testRegressionModel(100, svm, kernel);

                    Assert.AreEqual(0, error, 2, string.Format("SVM {0} with Kernel {1} did not train correctly", svm, kernel));
                }
            }
        }
Exemple #30
0
 public Parameter()
 {
     this._svmType     = SvmType.C_SVC;
     this._kernelType  = KernelType.RBF;
     this._degree      = 3;
     this._gamma       = 0.0;
     this._coef0       = 0.0;
     this._nu          = 0.5;
     this._cacheSize   = 40.0;
     this._C           = 1.0;
     this._eps         = 0.001;
     this._p           = 0.1;
     this._shrinking   = true;
     this._probability = false;
     this._weights     = new Dictionary <int, double>();
 }
Exemple #31
0
        /// <summary>
        /// Creates a new instance of the Kernel file reader
        /// </summary>
        /// <param name="kernel">Path to the KERNEL.BIN or kernel2.bin file to open.</param>
        /// <param name="kernelFile">Whether the file is KERNEL.BIN or kernel2.bin</param>
        public KernelReader(string kernel, KernelType kernelFile = KernelType.KernelBin)
        {
            if (kernelFile == KernelType.KernelBin)
            {
                // Load file and decompress
                this._kernelData = DecompressKernel(kernel);
            }
            else if (kernelFile == KernelType.Kernel2Bin)
            {
                this._kernelData = DecompressKernel2(kernel);
            } else {
                throw new NotSupportedException("This type of kernel is not yet supported.");
            }

            // Sections
            this.LoadSections();
        }
Exemple #32
0
        public void SetStrategy(KernelType kernel)
        {
            switch (kernel)
            {
            case KernelType.Boxcar:
                this.kernel = new BoxcarKernel();
                break;

            case KernelType.Epanechnikov:
                this.kernel = new EpanechnikovKernel();
                break;

            case KernelType.Gaussian:
                this.kernel = new GaussianKernel();
                break;
            }
        }
Exemple #33
0
        protected Kernel(int l, SvmNode[][] x_, SvmParameter param)
        {
            this.kernel_type = param.KernelType;
              this.degree = param.Degree;
              this.gamma = param.Gamma;
              this.coef0 = param.Coef0;

              x = (SvmNode[][])x_.Clone();

              if (kernel_type == KernelType.Rbf)
              {
            x_square = new double[l];
            for (int i = 0; i < l; i++)
              x_square[i] = dot(x[i], x[i]);
              }
              else x_square = null;
        }
Exemple #34
0
        private void setKernalType(KernelType k)
        {
            switch (k)
            {
            case KernelType.Linear:
                kernel = new Linear();
                break;

            case KernelType.Quadratic:
                kernel = new Quadratic();
                break;

            case KernelType.Sigmoid:
                kernel = Sigmoid.Estimate(independentVls);
                break;

            case KernelType.Spline:
                kernel = new Spline();
                break;

            case KernelType.ChiSquared:
                kernel = new ChiSquare();
                break;

            case KernelType.Gaussian:
                kernel = Gaussian.Estimate(independentVls);
                break;

            case KernelType.Multiquadric:
                kernel = new Multiquadric();
                break;

            case KernelType.InverseMultquadric:
                kernel = new InverseMultiquadric();
                break;

            case KernelType.Laplacian:
                kernel = Laplacian.Estimate(independentVls);
                break;

            default:
                kernel = new Polynomial(2);
                break;
            }
        }
Exemple #35
0
        private static int[,] ConverKernelType(KernelType kernelType = KernelType.KernelGaussianBlur5x5)
        {
            var converKernel = WriteableBitmapExtensions.KernelGaussianBlur5x5;

            if (kernelType == KernelType.KernelGaussianBlur5x5)
            {
                converKernel = WriteableBitmapExtensions.KernelGaussianBlur5x5;
            }
            else if (kernelType == KernelType.KernelGaussianBlur3x3)
            {
                converKernel = WriteableBitmapExtensions.KernelGaussianBlur3x3;
            }
            else
            {
                converKernel = WriteableBitmapExtensions.KernelSharpen3x3;
            }
            return(converKernel);
        }
Exemple #36
0
        private static int ConverKernelValue(KernelType kernelType = KernelType.KernelGaussianBlur5x5)
        {
            var kernelValue = 5;

            if (kernelType == KernelType.KernelGaussianBlur5x5)
            {
                kernelValue = 5;
            }
            else if (kernelType == KernelType.KernelGaussianBlur3x3)
            {
                kernelValue = 3;
            }
            else
            {
                kernelValue = 3;
            }
            return(kernelValue);
        }
Exemple #37
0
        private void setKernalType(KernelType k)
        {
            switch (k)
            {
            case KernelType.Linear:
                kernel = new Linear();
                break;

            case KernelType.Quadratic:
                kernel = new Quadratic();
                break;

            case KernelType.Sigmoid:
                kernel = new Sigmoid();
                break;

            case KernelType.Spline:
                kernel = new Spline();
                break;

            case KernelType.ChiSquared:
                kernel = new ChiSquare();
                break;

            case KernelType.Gaussian:
                kernel = new Gaussian();
                break;

            case KernelType.Multiquadric:
                kernel = new Multiquadric();
                break;

            case KernelType.InverseMultquadric:
                kernel = new InverseMultiquadric();
                break;

            case KernelType.Laplacian:
                kernel = new Laplacian();
                break;

            default:
                break;
            }
        }
    public KernelClass(string name, KernelType type, List <double> parameters, List <string> names)
    {
        Name = name;
        Type = type;

        if (parameters != null && parameters.Count > 0)
        {
            Parameters.Clear();
            Parameters.AddRange(parameters);

            FreeParameters = parameters.Count;
        }

        if (names != null && names.Count > 0)
        {
            ParameterNames.Clear();
            ParameterNames.AddRange(names);
        }
    }
Exemple #39
0
        private double testRegressionModel(int count, SvmType svm, KernelType kernel, string outputFile = null)
        {
            Problem        train     = SVMUtilities.CreateRegressionProblem(count);
            Parameter      param     = new Parameter();
            RangeTransform transform = RangeTransform.Compute(train);
            Problem        scaled    = transform.Scale(train);

            param.Gamma      = 1.0 / 2;
            param.SvmType    = svm;
            param.KernelType = kernel;
            param.Degree     = 2;

            Model model = Training.Train(scaled, param);

            Problem test = SVMUtilities.CreateRegressionProblem(count, false);

            scaled = transform.Scale(test);
            return(Prediction.Predict(scaled, outputFile, model, false));
        }
Exemple #40
0
 /// <summary>
 /// Construct the object.
 /// </summary>
 public SVMPattern()
 {
     Regression = true;
     _kernelType = KernelType.RadialBasisFunction;
     _svmType = SVMType.EpsilonSupportVectorRegression;
 }
Exemple #41
0
 /// <summary>
 /// Resizes the image to the specified dimensions using the given kernel type.
 /// </summary>
 /// <param name="destWidth">Width of the destination.</param>
 /// <param name="destHeight">Height of the destination.</param>
 /// <param name="method">The kernel method.</param>
 /// <param name="centeredGrid">if set to <c>true</c> using a centered grid; otherwise, using top-left aligned.</param>
 /// <returns>The resized image</returns>
 public FloatImage Resize(int destWidth, int destHeight, KernelType method, bool centeredGrid) {
   return (this._Resize(destWidth, destHeight, Kernels.KERNELS[method], centeredGrid));
 }
Exemple #42
0
 public Resampler(KernelType type) {
   this._type = type;
 }
        /// <summary>
        /// Cartoon effect filter.
        /// </summary>
        /// <param name="data">Image data.</param>
        /// <param name="threshold">Threshold.</param>
        /// <param name="smoothFilter">Smoothing filter.</param>
        /// <returns>Execution time.</returns>
        public static TimeSpan CartoonEffect(ImageData data, byte threshold = 0, KernelType smoothFilter = KernelType.None) {
            // Choose a smoothing filter
            switch (smoothFilter) {
                case KernelType.None:
                    break;
                case KernelType.Gaussian3x3:
                    ImageConvolution(data, 3, Kernel.M_Gaussian3x3);
                    break;
                case KernelType.Gaussian5x5:
                    ImageConvolution(data, 5, Kernel.M_Gaussian5x5);
                    break;
                case KernelType.Gaussian7x7:
                    ImageConvolution(data, 7, Kernel.M_Gaussian7x7);
                    break;
                case KernelType.Median3x3:
                    NoiseReduction_Median(data, 3);
                    break;
                case KernelType.Median5x5:
                    NoiseReduction_Median(data, 5);
                    break;
                case KernelType.Median7x7:
                    NoiseReduction_Median(data, 7);
                    break;
                case KernelType.Median9x9:
                    NoiseReduction_Median(data, 9);
                    break;
                case KernelType.Mean3x3:
                    ImageConvolution(data, 3, Kernel.M_Mean3x3);
                    break;
                case KernelType.Mean5x5:
                    ImageConvolution(data, 5, Kernel.M_Mean5x5);
                    break;
                case KernelType.Mean7x7:
                    ImageConvolution(data, 7, Kernel.M_Mean7x7);
                    break;
                case KernelType.LowPass3x3:
                    ImageConvolution(data, 3, Kernel.M_LowPass3x3);
                    break;
                case KernelType.LowPass5x5:
                    ImageConvolution(data, 5, Kernel.M_LowPass5x5);
                    break;
                case KernelType.Sharpen3x3:
                    ImageConvolution(data, 3, Kernel.M_Sharpen3x3);
                    break;
                case KernelType.Sharpen5x5:
                    ImageConvolution(data, 5, Kernel.M_Sharpen5x5);
                    break;
                case KernelType.Sharpen7x7:
                    ImageConvolution(data, 7, Kernel.M_Sharpen7x7);
                    break;
            }

            // Lock the bitmap's bits.  
            BitmapData bmpData = data.M_bitmap.LockBits(new Rectangle(0, 0, data.M_width, data.M_height), ImageLockMode.ReadWrite, data.M_bitmap.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare 2 arrays to hold the bytes of the bitmap.
            // The first to read from and then write to the scond.
            int bytes = bmpData.Stride*bmpData.Height;
            byte[] rgbValues = new byte[bytes];
            byte[] bgrValues = new byte[bytes];

            // Get the bytes per pixel value.
            int bytesPerPixel = Image.GetPixelFormatSize(data.M_bitmap.PixelFormat)/8;

            Stopwatch watch = Stopwatch.StartNew();

            // Copy the RGB values into the array.
            Marshal.Copy(ptr, rgbValues, 0, bytes);

            #region Algorithm
            Parallel.For(1, bmpData.Height - 1, i => {
                for (int j = 1; j < bmpData.Width - 1; j++) {
                    int index = i*bmpData.Stride + j*bytesPerPixel;

                    int bGradient = Math.Abs(rgbValues[index - bytesPerPixel] - rgbValues[index + bytesPerPixel]);
                    int gGradient = Math.Abs(rgbValues[index - bytesPerPixel] - rgbValues[index + bytesPerPixel]);
                    int rGradient = Math.Abs(rgbValues[index - bytesPerPixel] - rgbValues[index + bytesPerPixel]);

                    bGradient += Math.Abs(rgbValues[index - bmpData.Stride] - rgbValues[index + bmpData.Stride]);
                    gGradient += Math.Abs(rgbValues[index - bmpData.Stride] - rgbValues[index + bmpData.Stride]);
                    rGradient += Math.Abs(rgbValues[index - bmpData.Stride] - rgbValues[index + bmpData.Stride]);

                    bool exceedsThreshold = false;
                    if (bGradient + gGradient + rGradient > threshold) {
                        exceedsThreshold = true;
                    } else {
                        bGradient = Math.Abs(rgbValues[index - bytesPerPixel] - rgbValues[index + bytesPerPixel]);
                        gGradient = Math.Abs(rgbValues[index - bytesPerPixel] - rgbValues[index + bytesPerPixel]);
                        rGradient = Math.Abs(rgbValues[index - bytesPerPixel] - rgbValues[index + bytesPerPixel]);

                        if (bGradient + gGradient + rGradient > threshold) {
                            exceedsThreshold = true;
                        } else {
                            bGradient = Math.Abs(rgbValues[index - bmpData.Stride] - rgbValues[index + bmpData.Stride]);
                            gGradient = Math.Abs(rgbValues[index - bmpData.Stride] - rgbValues[index + bmpData.Stride]);
                            rGradient = Math.Abs(rgbValues[index - bmpData.Stride] - rgbValues[index + bmpData.Stride]);

                            if (bGradient + gGradient + rGradient > threshold) {
                                exceedsThreshold = true;
                            } else {
                                bGradient = Math.Abs(rgbValues[index - bytesPerPixel - bmpData.Stride] - rgbValues[index + bytesPerPixel + bmpData.Stride]);
                                gGradient = Math.Abs(rgbValues[index - bytesPerPixel - bmpData.Stride] - rgbValues[index + bytesPerPixel + bmpData.Stride]);
                                rGradient = Math.Abs(rgbValues[index - bytesPerPixel - bmpData.Stride] - rgbValues[index + bytesPerPixel + bmpData.Stride]);

                                gGradient += Math.Abs(rgbValues[index - bmpData.Stride + bytesPerPixel] - rgbValues[index + bmpData.Stride - bytesPerPixel]);
                                bGradient += Math.Abs(rgbValues[index - bmpData.Stride + bytesPerPixel] - rgbValues[index + bmpData.Stride - bytesPerPixel]);
                                rGradient += Math.Abs(rgbValues[index - bmpData.Stride + bytesPerPixel] - rgbValues[index + bmpData.Stride - bytesPerPixel]);

                                if (bGradient + gGradient + rGradient > threshold) {
                                    exceedsThreshold = true;
                                } else {
                                    exceedsThreshold = false;
                                }
                            }
                        }
                    }

                    int b = 0;
                    int g = 0;
                    int r = 0;
                    if (exceedsThreshold) {
                        b = 0;
                        g = 0;
                        r = 0;
                    } else {
                        b = rgbValues[index];
                        g = rgbValues[index + 1];
                        r = rgbValues[index + 2];
                    }

                    if (b > 255) {
                        b = 255;
                    } else if (b < 0) {
                        b = 0;
                    }
                    if (g > 255) {
                        g = 255;
                    } else if (g < 0) {
                        g = 0;
                    }
                    if (r > 255) {
                        r = 255;
                    } else if (r < 0) {
                        r = 0;
                    }

                    bgrValues[index] = (byte)b;
                    bgrValues[index + 1] = (byte)g;
                    bgrValues[index + 2] = (byte)r;
                }
            });
            #endregion

            Marshal.Copy(bgrValues, 0, ptr, bytes);

            watch.Stop();
            TimeSpan elapsedTime = watch.Elapsed;

            // Unlock the bits.
            data.M_bitmap.UnlockBits(bmpData);

            return elapsedTime;
        }
 /// <summary>
 /// Applies the pixel scaler for float32 images.
 /// </summary>
 /// <param name="type">The type of scaler to use.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="centeredGrid">if set to <c>true</c> [centered grid].</param>
 /// <param name="filterRegion">The filter region, if any.</param>
 /// <returns>
 /// The rescaled image.
 /// </returns>
 public cImage ApplyScaler(KernelType type, int width, int height, bool centeredGrid, Rectangle? filterRegion = null) {
   var fpImage = FloatImage.FromImage(this, filterRegion);
   var fpResult = fpImage.Resize(width, height, type, centeredGrid);
   var result = fpResult.ToImage();
   return (result);
 }
 private void setKernalType(KernelType k)
 {
     switch (k)
     {
         case KernelType.Linear:
             kernel = new Linear();
             break;
         case KernelType.Quadratic:
             kernel = new Quadratic();
             break;
         case KernelType.Sigmoid:
             kernel = new Sigmoid();
             break;
         case KernelType.Spline:
             kernel = new Spline();
             break;
         case KernelType.ChiSquared:
             kernel = new ChiSquare();
             break;
         case KernelType.Gaussian:
             kernel = new Gaussian();
             break;
         case KernelType.Multiquadric:
             kernel = new Multiquadric();
             break;
         case KernelType.InverseMultquadric:
             kernel = new InverseMultiquadric();
             break;
         case KernelType.Laplacian:
             kernel = new Laplacian();
             break;
         default:
             break;
     }
 }
        public void getSvmModel(string modelPath, bool BuildModel = false)
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(modelPath))
            {
                string mType = sr.ReadLine();
                modelTypes m = (modelTypes)Enum.Parse(typeof(modelTypes), mType);
                if (m != modelTypes.SVM)
                {
                    System.Windows.Forms.MessageBox.Show("Model file specified is not a DA!!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return;
                }
                InTablePath = sr.ReadLine();
                IndependentFieldNames = sr.ReadLine().Split(new char[] { ',' });
                DependentFieldNames = sr.ReadLine().Split(new char[] { ',' });
                ClassFieldNames = sr.ReadLine().Split(new char[] { ',' });
                n = System.Convert.ToInt32(sr.ReadLine());
                nvars = System.Convert.ToInt32(sr.ReadLine());
                sserror = System.Convert.ToDouble(sr.ReadLine());
                kTyp = (KernelType)Enum.Parse(typeof(KernelType),sr.ReadLine());
                setKernalType(kTyp);
                minValues = (from string s in (sr.ReadLine().Split(new char[] { ' ' })) select System.Convert.ToDouble(s)).ToArray();
                maxValues = (from string s in (sr.ReadLine().Split(new char[] { ' ' })) select System.Convert.ToDouble(s)).ToArray();
                sumValues = (from string s in (sr.ReadLine().Split(new char[] { ' ' })) select System.Convert.ToDouble(s)).ToArray();
                sr.Close();

            }
            if (BuildModel)
            {
                string svmMPath = modelPath.Replace(".mdl", ".svm");
                MulticlassSupportVectorMachine.Load(svmMPath);
            }
        }
Exemple #47
0
 /// <summary>
 /// Default Constructor.  Gives good default values to all parameters.
 /// </summary>
 public Parameter()
 {
     _svmType = SvmType.C_SVC;
     _kernelType = KernelType.RBF;
     _degree = 3;
     _gamma = 0; // 1/k
     _coef0 = 0;
     _nu = 0.5;
     _cacheSize = 40;
     _C = 1;
     _eps = 1e-3;
     _p = 0.1;
     _shrinking = true;
     _probability = false;
     _weights = new Dictionary<int, double>();
 }
        /// <summary>
        /// Construct a SVM network.
        /// </summary>
        ///
        /// <param name="theInputCount">The input count.</param>
        /// <param name="svmType">The type of SVM.</param>
        /// <param name="kernelType">The SVM kernal type.</param>
        public SupportVectorMachine(int theInputCount, SVMType svmType,
            KernelType kernelType)
        {
            _inputCount = theInputCount;

            _paras = new svm_parameter();

            switch (svmType)
            {
                case SVMType.SupportVectorClassification:
                    _paras.svm_type = svm_parameter.C_SVC;
                    break;
                case SVMType.NewSupportVectorClassification:
                    _paras.svm_type = svm_parameter.NU_SVC;
                    break;
                case SVMType.SupportVectorOneClass:
                    _paras.svm_type = svm_parameter.ONE_CLASS;
                    break;
                case SVMType.EpsilonSupportVectorRegression:
                    _paras.svm_type = svm_parameter.EPSILON_SVR;
                    break;
                case SVMType.NewSupportVectorRegression:
                    _paras.svm_type = svm_parameter.NU_SVR;
                    break;
                default:
                    throw new NeuralNetworkError("Invalid svm type");
            }

            switch (kernelType)
            {
                case KernelType.Linear:
                    _paras.kernel_type = svm_parameter.LINEAR;
                    break;
                case KernelType.Poly:
                    _paras.kernel_type = svm_parameter.POLY;
                    break;
                case KernelType.RadialBasisFunction:
                    _paras.kernel_type = svm_parameter.RBF;
                    break;
                case KernelType.Sigmoid:
                    _paras.kernel_type = svm_parameter.SIGMOID;
                    break;
                    /*case Encog.ML.SVM.KernelType.Precomputed:
                this.paras.kernel_type = Encog.MathUtil.LIBSVM.svm_parameter.PRECOMPUTED;
                break;*/
                default:
                    throw new NeuralNetworkError("Invalid kernel type");
            }

            // params[i].kernel_type = svm_parameter.RBF;
            _paras.degree = DefaultDegree;
            _paras.coef0 = 0;
            _paras.nu = DefaultNu;
            _paras.cache_size = DefaultCacheSize;
            _paras.C = 1;
            _paras.eps = DefaultEps;
            _paras.p = DefaultP;
            _paras.shrinking = 1;
            _paras.probability = 0;
            _paras.nr_weight = 0;
            _paras.weight_label = new int[0];
            _paras.weight = new double[0];
            _paras.gamma = 1.0d/_inputCount;
        }
        /// <summary>
        /// Construct a SVM network. 
        /// </summary>
        /// <param name="inputCount">The input count.</param>
        /// <param name="outputCount">The output count.</param>
        /// <param name="svmType">The type of SVM.</param>
        /// <param name="kernelType">The SVM kernal type.</param>
        public SVMNetwork(int inputCount, int outputCount, SVMType svmType,
                KernelType kernelType)
        {
            this.inputCount = inputCount;
            this.outputCount = outputCount;
            this.kernelType = kernelType;
            this.svmType = svmType;

            models = new svm_model[outputCount];
            parameters = new svm_parameter[outputCount];

            for (int i = 0; i < outputCount; i++)
            {
                parameters[i] = new svm_parameter();

                switch (svmType)
                {
                    case SVMType.SupportVectorClassification:
                        parameters[i].svm_type = svm_parameter.C_SVC;
                        break;
                    case SVMType.NewSupportVectorClassification:
                        parameters[i].svm_type = svm_parameter.NU_SVC;
                        break;
                    case SVMType.SupportVectorOneClass:
                        parameters[i].svm_type = svm_parameter.ONE_CLASS;
                        break;
                    case SVMType.EpsilonSupportVectorRegression:
                        parameters[i].svm_type = svm_parameter.EPSILON_SVR;
                        break;
                    case SVMType.NewSupportVectorRegression:
                        parameters[i].svm_type = svm_parameter.NU_SVR;
                        break;
                }

                switch (kernelType)
                {
                    case KernelType.Linear:
                        parameters[i].kernel_type = svm_parameter.LINEAR;
                        break;
                    case KernelType.Poly:
                        parameters[i].kernel_type = svm_parameter.POLY;
                        break;
                    case KernelType.RadialBasisFunction:
                        parameters[i].kernel_type = svm_parameter.RBF;
                        break;
                    case KernelType.Sigmoid:
                        parameters[i].kernel_type = svm_parameter.SIGMOID;
                        break;
                }

                parameters[i].kernel_type = svm_parameter.RBF;
                parameters[i].degree = 3;
                parameters[i].coef0 = 0;
                parameters[i].nu = 0.5;
                parameters[i].cache_size = 100;
                parameters[i].C = 1;
                parameters[i].eps = 1e-3;
                parameters[i].p = 0.1;
                parameters[i].shrinking = 1;
                parameters[i].probability = 0;
                parameters[i].nr_weight = 0;
                parameters[i].weight_label = new int[0];
                parameters[i].weight = new double[0];
                parameters[i].gamma = 1.0 / inputCount;
            }
        }