Exemplo n.º 1
0
        public bool LoadModelFromFile()
        {
            var filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            if (!File.Exists($"{filePath}//ModelMean.csv"))
            {
                return(false);
            }
            if (!File.Exists($"{filePath}//ModelCov.csv"))
            {
                return(false);
            }
            ModelMean.Clear();
            ModelCov.Clear();

            StreamReader fileReader_mean = new StreamReader($"{filePath}//ModelMean.csv");
            string       strLine         = "";

            while (strLine != null)
            {
                strLine = fileReader_mean.ReadLine();
                if (!string.IsNullOrEmpty(strLine))
                {
                    List <double> tempDoubles = new List <double>();
                    var           temp        = strLine.Split(',');
                    foreach (var str in temp)
                    {
                        double tempdouble = 0f;
                        double.TryParse(str, out tempdouble);
                        if (Math.Abs(tempdouble) > 0)
                        {
                            tempDoubles.Add(tempdouble);
                        }
                    }
                    ModelMean.Add(tempDoubles);
                }
            }
            fileReader_mean.Close();

            StreamReader fileReader_cov = new StreamReader($"{filePath}//ModelCov.csv");
            string       strLine_cov    = "";

            while (strLine_cov != null)
            {
                strLine_cov = fileReader_cov.ReadLine();
                if (!string.IsNullOrEmpty(strLine_cov))
                {
                    List <double> tempDoubles = new List <double>();
                    var           temp        = strLine_cov.Split(',');
                    foreach (var str in temp)
                    {
                        double tempdouble = 0f;
                        double.TryParse(str, out tempdouble);
                        if (Math.Abs(tempdouble) > 0)
                        {
                            tempDoubles.Add(tempdouble);
                        }
                    }
                    ModelCov.Add(tempDoubles);
                }
            }
            fileReader_cov.Close();

            for (var i = 0; i < ModelMean.Count; i++)
            {
                _mClassLabel.Add(i);
            }
            return(true);
        }
Exemplo n.º 2
0
        private bool BayesTrain(List <List <double> > feature, List <int> label)
        {
            var featNum = feature.Count;
            var featDim = feature[0].Count;
            var labNum  = label.Count;

            if (labNum != featNum)
            {
                return(false);
            }

            _mClassLabel.Clear();
            for (var i = 0; i < GestureNumber; i++)
            {
                _mClassLabel.Add(i);
            }

            Matrix <double> featMat = new DenseMatrix(featNum, featDim);

            for (var i = 0; i < featNum; i++)
            {
                for (var j = 0; j < featDim; j++)
                {
                    featMat[i, j] = feature[i][j];
                }
            }

            var             cNum        = _mClassLabel.Count;
            Matrix <double> meanMat     = new DenseMatrix(cNum, featDim);
            Matrix <double> covMat      = new DenseMatrix(featDim * cNum, featDim);
            Matrix <double> poolCovMat  = new DenseMatrix(featDim, featDim);
            Vector <double> numPerClass = new DenseVector(cNum);

            // compute the mean vector for each class
            for (var i = 0; i < featNum; i++)
            {
                for (var j = 0; j < cNum; j++)
                {
                    if (label[i] != _mClassLabel[j])
                    {
                        continue;
                    }
                    meanMat.SetRow(j, meanMat.Row(j) + featMat.Row(i));
                    numPerClass.At(j, numPerClass.At(j) + 1);
                }
            }
            for (var i = 0; i < cNum; i++)
            {
                meanMat.SetRow(i, meanMat.Row(i) / numPerClass.At(i));
            }
            //compute the covariance matrix for each class and pool covariance matrix
            for (var i = 0; i < featNum; i++)
            {
                for (var j = 0; j < cNum; j++)
                {
                    if (label[i] == _mClassLabel[j])
                    {
                        covMat.SetSubMatrix(j * featDim, featDim, 0, featDim,
                                            covMat.SubMatrix(j * featDim, featDim, 0, featDim) +
                                            (featMat.Row(i) - meanMat.Row(j)).OuterProduct(featMat.Row(i) - meanMat.Row(j)));
                    }
                }
            }
            for (var i = 0; i < cNum; i++)
            {
                poolCovMat += covMat.SubMatrix(i * featDim, featDim, 0, featDim);
                //PoolCovMat += CovMat.block(i* feat_dim,0,feat_dim,feat_dim);
                covMat.SetSubMatrix(i * featDim, featDim, 0, featDim,
                                    covMat.SubMatrix(i * featDim, featDim, 0, featDim) / (numPerClass.At(i) - 1));
                //CovMat.block(i* feat_dim,0,feat_dim,feat_dim)=CovMat.block(i* feat_dim,0,feat_dim,feat_dim)/(feat_num_perclass(i)-1);
            }
            poolCovMat /= featNum - cNum;
            poolCovMat  = poolCovMat.Inverse();

            //transform the data format from Eigen to member vectors
            ModelMean?.Clear();
            ModelCov?.Clear();

            List <double> temp;

            for (var i = 0; i < cNum; i++)
            {
                temp = new List <double>();
                for (var j = 0; j < featDim; j++)
                {
                    temp.Add(meanMat[i, j]);
                }
                ModelMean?.Add(temp);
            }
            for (var i = 0; i < featDim; i++)
            {
                temp = new List <double>();
                for (var j = 0; j < featDim; j++)
                {
                    temp.Add(poolCovMat[i, j]);
                }
                ModelCov?.Add(temp);
            }
            return(true);
        }