public void TestLibsvmClassify()
        {
            var advancedClassify = new AdvancedClassify();
            var numericalset     = advancedClassify.LoadNumerical();
            var result           = advancedClassify.ScaleData(numericalset);
            var scaledSet        = result.Item1;
            var scalef           = result.Item2;
            var prob             = new SVMProblem();

            foreach (var matchRow in scaledSet)
            {
                prob.Add(matchRow.NumData.Select((v, i) => new SVMNode(i + 1, v)).ToArray(), matchRow.Match);
            }
            var param = new SVMParameter()
            {
                Kernel = SVMKernelType.RBF
            };
            var m = prob.Train(param);

            m.SaveModel("trainModel");
            Func <double[], SVMNode[]> makeInput = ma => scalef(ma).Select((v, i) => new SVMNode(i + 1, v)).ToArray();
            var newrow = new[] { 28, -1, -1, 26, -1, 1, 2, 0.8 };//男士不想要小孩,而女士想要

            TestOutput(m.Predict(makeInput(newrow)));
            newrow = new[] { 28, -1, 1, 26, -1, 1, 2, 0.8 };//双方都想要小孩
            TestOutput(m.Predict(makeInput(newrow)));
        }
Beispiel #2
0
        public static SVMProblem Load(IEnumerable <IEnumerable <string> > data, bool isWithClass)
        {
            NumberFormatInfo provider = new NumberFormatInfo();

            SVMProblem problem = new SVMProblem();

            foreach (var row in data)
            {
                var doubleRows = row
                                 .Select(v => Convert.ToDouble(v))
                                 .Select((d, i) => new { d, i })
                                 .Reverse();

                SVMNode[] nodes = doubleRows
                                  .Skip(isWithClass ? 1 : 0)
                                  .Select(d => new SVMNode()
                {
                    Index = d.i + 1, Value = d.d
                })
                                  .ToArray();

                double y = isWithClass ? doubleRows.First().d : 0;
                problem.Add(nodes, y);
            }

            return(problem);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="problem"></param>
        /// <returns></returns>
        public static SVMProblem RemoveDuplicates(SVMProblem problem)
        {
            SVMProblem temp = new SVMProblem();

            for (int i = 0; i < problem.Length; i++)
            {
                bool same = false;
                for (int j = i + 1; j < problem.Length; j++)
                {
                    same |= SVMProblemHelper.IsEqual(problem.X[i], problem.Y[i], problem.X[j], problem.Y[j]);

                    if (same)
                    {
                        break;
                    }
                }

                if (!same)
                {
                    temp.Add(problem.X[i], problem.Y[i]);
                }
            }

            return(temp);
        }
Beispiel #4
0
        public void Training(IResults results, int start, int end)
        {
            for (int scanNum = start; scanNum <= end; scanNum++)
            {
                if (results.Contains(scanNum))
                {
                    List <IScore> scores = results.GetResult(scanNum);
                    foreach (IScore score in scores)
                    {
                        double         y = (score as FDRScoreProxy).IsDecoy() ? 0 : 1;
                        List <SVMNode> X = new List <SVMNode>();
                        // store score value in X
                        int idx = 0;
                        foreach (MassType type in types)
                        {
                            SVMNode node = new SVMNode();
                            node.Index = idx;
                            node.Value = score.GetScore(type);
                            X.Add(node);
                            idx++;
                        }
                        problem.Add(X.ToArray(), y);
                    }
                }
            }

            // training
            SVMParameter parameter = new SVMParameter();

            parameter.Probability = true;
            model = problem.Train(parameter);
        }
Beispiel #5
0
        public void TrainAndTestSvmTest()
        {
            SVMProblem train  = new SVMProblem();
            Random     random = new Random();

            for (int i = 0; i < 300; i++)
            {
                int value = random.Next() % 2;
                train.Add(new SVMNode[]
                {
                    new SVMNode(1, value),
                }, value);
            }

            SVMProblem test = new SVMProblem();

            for (int i = 0; i < 100; i++)
            {
                int value = random.Next() % 2;
                test.Add(new SVMNode[]
                {
                    new SVMNode(1, value),
                }, value);
            }

            ProblemHandler.SvmResult result = ProblemHandler.TrainAndTestSvm(train, test);
            Assert.True(result.TestAccuracy > 95);
        }
        public void face_training(SVMProblem f_training)
        {
            SVMProblem trainingSet = SVMProblemHelper.Load(@"C:\Users\temp\Desktop\0921_towp.txt");
            SVMProblem testSet     = SVMProblemHelper.Load(@"C:\Users\temp\Desktop\0921_towpt.txt");

            // f_training.Save(@"C:\Users\temp\Desktop\1005f.txt");
            //  trainingSet.Insert(index, f_training.X[0], 2);
            trainingSet.Add(f_training.X[0], 1);
            trainingSet.Save(@"C:\Users\temp\Desktop\flag.txt");
            //   trainingSet.Save(@"C:\Users\temp\Desktop\1005.txt");
            // Console.WriteLine();
            //   SVMNode node = new SVMNode();
            //  node.Index = Convert.ToInt32(o);
            //  node.Value = Convert.ToDouble(f_training.X);
            //  nodes.Add(node);
            //  trainingSet.Add(nodes.ToArray(), 1);
            //  int number = randon.Next(0, trainingSet.X.Count);
            //  int trainingsample = Convert.ToInt32(trainingSet.X.Count * 2 / 3);
            //  int testingsample = Convert.ToInt32(trainingSet.X.Count / 3);

            trainingSet = trainingSet.Normalize(SVMNormType.L2);
            testSet     = testSet.Normalize(SVMNormType.L2);

            SVMParameter parameter = new SVMParameter();

            parameter.Type        = SVMType.NU_SVC;
            parameter.Kernel      = SVMKernelType.SIGMOID;
            parameter.C           = 1;
            parameter.Gamma       = 1;
            parameter.Probability = true;
            int        nFold = 10;
            MainWindow main  = new MainWindow();

            double[] crossValidationResults; // output labels
            trainingSet.CrossValidation(parameter, nFold, out crossValidationResults);
            double   crossValidationAccuracy = trainingSet.EvaluateClassificationProblem(crossValidationResults);
            SVMModel model = SVM.Train(trainingSet, parameter);

            // SVMModel model = trainingSet.Train(parameter);

            SVM.SaveModel(model, @"C:\Users\temp\Desktop\1005.txt");

            double[] testResults = testSet.Predict(model);
            //     Console.WriteLine("");
            int[,] confusionMatrix;
            double testAccuracy = testSet.EvaluateClassificationProblem(testResults, model.Labels, out confusionMatrix);

            // Console.WriteLine("\n\nCross validation accuracy: " + crossValidationAccuracy);
            //  Console.WriteLine("testAccuracy:" + testAccuracy);
            //  Console.WriteLine(Convert.ToString(trainingSet.X.Count));
            main.Training_result.Content    = "testAccuracy:" + testAccuracy + "\nCross validation accuracy: " + crossValidationAccuracy + "\nCount " + trainingSet.X.Count;
            main.Training_result.FontSize   = 14;
            main.Training_result.FontStyle  = FontStyles.Normal;
            main.Training_result.Foreground = Brushes.Red;
            main.Training_result.Background = Brushes.Black;
            // Console.WriteLine(trainingSet1.Length);
            //  trainingSet.Save(@"C:\Users\temp\Desktop\1005.txt");
            index++;
        }
        public void LibsvmFirstLook()
        {
            var prob = new SVMProblem();

            prob.Add(new[] { new SVMNode(1, 1), new SVMNode(2, 0), new SVMNode(3, 1) }, 1);
            prob.Add(new[] { new SVMNode(1, -1), new SVMNode(2, 0), new SVMNode(3, -1) }, -1);
            var param = new SVMParameter();

            param.Kernel = SVMKernelType.LINEAR;
            param.C      = 10;
            var m = prob.Train(param);

            TestOutput(m.Predict(new [] { new SVMNode(1, 1), new SVMNode(2, 1), new SVMNode(3, 1) }));
            m.SaveModel("trainModel");
            var ml = SVM.LoadModel("trainModel");

            TestOutput(ml.Predict(new[] { new SVMNode(1, 1), new SVMNode(2, 1), new SVMNode(3, 1) }));
        }
Beispiel #8
0
        public OneClassClassifier(List <SVMNode[]> trainingData)
        {
            SVMProblem problem = new SVMProblem();

            for (int i = 0; i < trainingData.Count; i++)
            {
                problem.Add(trainingData[i], 1);
            }
            _trainingData = problem;
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="problem"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static SVMProblem Normalize(SVMProblem problem, SVMNormType type)
 {
     SVMProblem temp = new SVMProblem();
     for (int i = 0; i < problem.Length; i++)
     {
         SVMNode[] x = SVMNodeHelper.Normalize(problem.X[i], type);
         temp.Add(x, problem.Y[i]);
     }
     return temp;
 }
        public void AddToProblem(SVMProblem problem, string lable, IEnumerable <KeyValuePair <string, double> > xValues)
        {
            var xx = CreateNodes(xValues);

            if (!Lables.ContainsKey(lable))
            {
                Lables.Add(lable, Lables.Count + 100 + 1);
            }
            problem.Add(xx, Lables[lable]);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static SVMProblem Normalize(SVMProblem problem, SVMNormType type)
        {
            SVMProblem temp = new SVMProblem();

            for (int i = 0; i < problem.Length; i++)
            {
                SVMNode[] x = SVMProblemHelper.Normalize(problem.X[i], type);
                temp.Add(x, problem.Y[i]);
            }
            return(temp);
        }
Beispiel #12
0
        public static SVMProblem CreateCompleteProblem(this IEnumerable <List <double> > original, SAMData sam, SAMDataPoint.FeelingModel feelingModel)
        {
            SVMProblem completeProblem = new SVMProblem();

            for (int i = 0; i < original.Count(); i++)
            {
                SVMNode[] nodeSet = new SVMNode[original.ElementAt(i).Count];
                for (int j = 0; j < original.ElementAt(i).Count; j++)
                {
                    SVMNode currentNode = new SVMNode();
                    currentNode.Index = j + 1;
                    currentNode.Value = original.ElementAt(i)[j];
                    nodeSet[j]        = currentNode;
                }
                completeProblem.Add(nodeSet, sam.dataPoints[i].ToAVCoordinate(feelingModel));
            }

            return(completeProblem);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static SVMProblem Load(string filename)
        {
            if (String.IsNullOrWhiteSpace(filename) || !File.Exists(filename))
            {
                return(null);
            }

            NumberFormatInfo provider = new NumberFormatInfo();

            provider.NumberDecimalSeparator = ".";

            SVMProblem problem = new SVMProblem();

            using (StreamReader sr = new StreamReader(filename))
            {
                while (true)
                {
                    string line = sr.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    string[] list = line.Trim().Split(' ');

                    double y = Convert.ToDouble(list[0].Trim());

                    List <SVMNode> nodes = new List <SVMNode>();
                    for (int i = 1; i < list.Length; i++)
                    {
                        string[] temp = list[i].Split(':');
                        SVMNode  node = new SVMNode();
                        node.Index = Convert.ToInt32(temp[0].Trim());
                        node.Value = Convert.ToDouble(temp[1].Trim(), provider);
                        nodes.Add(node);
                    }

                    problem.Add(nodes.ToArray(), y);
                }
            }

            return(problem);
        }
        public static void Train()
        {
            SVMProblem svmproblem = new SVMProblem();
            List <Tuple <Sentence, string> > sentences = new List <Tuple <Sentence, string> >();
            StreamReader sr = new StreamReader("training_data.txt");
            string       line;

            while ((line = sr.ReadLine()) != null)
            {
                string[] lines = line.Split(',');
                lines[0] = lines[0].Substring(1, lines[0].Length - 2);
                string sentence = lines[0];
                sentences.Add(Tuple.Create(Sentence.ParseIntoSentence(sentence, 0, false), lines[1]));
            }

            List <SVMNode[]> modelNodes = new List <SVMNode[]>();

            for (int i = 0; i < sentences.Count; i++)
            {
                List <SVMNode> nodes = new List <SVMNode>();
                for (int j = 0; j < Corpus.count; j++)
                {
                    SVMNode sentenceNode = new SVMNode(j, 0);
                    nodes.Add(sentenceNode);
                }
                for (int j = 0; j < sentences[i].Item1.SentenceWords.Count; j++)
                {
                    CorpusWord cw                   = Corpus.CorpusList.Find(c => c.Word.Equals(sentences[i].Item1.SentenceWords[j].Stem.Word) && c.Attribute.Equals(sentences[i].Item1.SentenceWords[j].Stem.Attribute));
                    SVMNode    sentenceNode         = new SVMNode(Corpus.CorpusList.IndexOf(cw), 1);
                    SVMNode    sentenceNodeToRemove = new SVMNode(Corpus.CorpusList.IndexOf(cw), 0);
                    nodes.Remove(sentenceNodeToRemove);
                    nodes.Add(sentenceNode);
                }
                SVMNode[] sentenceNodes = nodes.ToArray();
                sentenceNodes = sentenceNodes.OrderBy(x => x.Index).ToArray();

                svmproblem.Add(sentenceNodes, Corpus.CorpusList.IndexOf(Corpus.CorpusList.Find(c => c.Word.Equals(sentences[i].Item2))));
            }

            model = SVM.Train(svmproblem, parameter);

            sr.Close();
        }
Beispiel #15
0
        public static SVMProblem CreateCompleteProblemOneClass(this IEnumerable <List <double> > original)
        {
            SVMProblem completeProblem = new SVMProblem();

            for (int i = 0; i < original.Count(); i++)
            {
                SVMNode[] nodeSet = new SVMNode[original.ElementAt(i).Count];
                for (int j = 0; j < original.ElementAt(i).Count; j++)
                {
                    SVMNode currentNode = new SVMNode();
                    currentNode.Index = j + 1;
                    currentNode.Value = original.ElementAt(i)[j];
                    nodeSet[j]        = currentNode;
                }
                completeProblem.Add(nodeSet, 1);
            }

            return(completeProblem);
        }
Beispiel #16
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="problem"></param>
        /// <returns></returns>
        public static SVMProblem RemoveDuplicates(SVMProblem problem)
        {
            SVMProblem temp = new SVMProblem();
            for (int i = 0; i < problem.Length; i++)
            {
                bool same = false;
                for (int j = i + 1; j < problem.Length; j++)
                {
                    same |= SVMNodeHelper.IsEqual(problem.X[i], problem.Y[i], problem.X[j], problem.Y[j]);

                    if (same)
                    {
                        break;
                    }
                }

                if (!same)
                {
                    temp.Add(problem.X[i], problem.Y[i]);
                }
            }

            return temp;
        }
Beispiel #17
0
        private void button2_Click(object sender, EventArgs e)
        {
            SVMProblem trainingSet = new SVMProblem();
            SVMProblem testSet     = trainingSet;

            foreach (DataInfo info in mList)
            {
                SVMNode[] node = new SVMNode[2];
                node[0] = new SVMNode(1, info.X / mWidth);
                node[1] = new SVMNode(2, info.Y / mHeight);
                trainingSet.Add(node, info.Group);
            }


            // Normalize the datasets if you want: L2 Norm => x / ||x||
            //trainingSet = trainingSet.Normalize(SVMNormType.L2);

            // Select the parameter set
            SVMParameter parameter = new SVMParameter();

            parameter.Type   = SVMType.C_SVC;
            parameter.Kernel = SVMKernelType.RBF;
            parameter.C      = 1;
            parameter.Gamma  = 4;
            parameter.Coef0  = hScrollBar1.Value;
            parameter.Degree = 3;

            // Do cross validation to check this parameter set is correct for the dataset or not
            double[] crossValidationResults; // output labels
            int      nFold = 5;

            trainingSet.CrossValidation(parameter, nFold, out crossValidationResults);

            // Evaluate the cross validation result
            // If it is not good enough, select the parameter set again
            double crossValidationAccuracy = trainingSet.EvaluateClassificationProblem(crossValidationResults);

            // Train the model, If your parameter set gives good result on cross validation
            SVMModel model = trainingSet.Train(parameter);

            // Save the model
            SVM.SaveModel(model, FILE_MODEL);

            // Predict the instances in the test set
            double[] testResults = testSet.Predict(model);

            // Evaluate the test results
            int[,] confusionMatrix;
            double testAccuracy = testSet.EvaluateClassificationProblem(testResults, model.Labels, out confusionMatrix);

            // Print the resutls
            Console.WriteLine("\n\nCross validation accuracy: " + crossValidationAccuracy);
            Console.WriteLine("\nTest accuracy: " + testAccuracy);
            Console.WriteLine("\nConfusion matrix:\n");

            // Print formatted confusion matrix
            Console.Write(String.Format("{0,6}", ""));
            for (int i = 0; i < model.Labels.Length; i++)
            {
                Console.Write(String.Format("{0,5}", "(" + model.Labels[i] + ")"));
            }
            Console.WriteLine();
            for (int i = 0; i < confusionMatrix.GetLength(0); i++)
            {
                Console.Write(String.Format("{0,5}", "(" + model.Labels[i] + ")"));
                for (int j = 0; j < confusionMatrix.GetLength(1); j++)
                {
                    Console.Write(String.Format("{0,5}", confusionMatrix[i, j]));
                }
                Console.WriteLine();
            }

            Pen[] pen = new Pen[4];
            pen[0] = new Pen(Color.Black, 1);
            pen[1] = new Pen(Color.Red, 1);
            pen[2] = new Pen(Color.LightGreen, 1);
            pen[3] = new Pen(Color.Blue, 1);

            Pen[] pen2 = new Pen[4];
            pen2[0] = new Pen(Color.LightGray, 1);
            pen2[1] = new Pen(Color.DarkRed, 1);
            pen2[2] = new Pen(Color.DarkGreen, 1);
            pen2[3] = new Pen(Color.DarkBlue, 1);

            Bitmap canvas = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);

            using (Graphics g = Graphics.FromImage(canvas))
            {
                for (int i = 0; i < pictureBox1.ClientSize.Width; i++)
                {
                    for (int j = 0; j < pictureBox1.ClientSize.Height; j++)
                    {
                        SVMNode[] node = new SVMNode[2];
                        node[0] = new SVMNode(1, (double)i / (double)mWidth);
                        node[1] = new SVMNode(2, (double)j / (double)mHeight);

                        double result = SVM.Predict(model, node);
                        g.DrawRectangle(pen2[(int)result], i, j, 1, 1);
                    }
                }

                foreach (DataInfo info in mList)
                {
                    g.DrawEllipse(pen[(int)info.Group], (float)info.X - 5, (float)info.Y - 5, 5, 5);
                }
            }

            Bitmap image = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);

            pictureBox1.BackgroundImage = canvas; // 設置為背景層
            pictureBox1.Refresh();
            pictureBox1.CreateGraphics().DrawImage(canvas, 0, 0);
        }
Beispiel #18
0
        public void GetProblemFromImageModelResultListTest()
        {
            ushort[,] image1 = new ushort[, ]
            {
                { 1, 2, 3, 4, 5 },
                { 6, 2, 8, 9, 10 },
                { 11, 8, 2, 10, 11 },
                { 16, 13, 11, 15, 16 },
                { 21, 2, 1, 1, 25 },
            };

            ushort[,] image2 = new ushort[, ]
            {
                { 1, 2, 3, 4, 5 },
                { 6, 7, 8, 9, 10 },
                { 11, 8, 9, 10, 11 },
                { 16, 13, 14, 15, 16 },
                { 21, 2, 23, 24, 25 },
            };

            ImageWithResultModel resultImage1 = new ImageWithResultModel();

            resultImage1.Image  = new UShortArrayAsImage(image1);
            resultImage1.Result = 1;


            ImageWithResultModel resultImage2 = new ImageWithResultModel();

            resultImage2.Image  = new UShortArrayAsImage(image2);
            resultImage2.Result = 0;

            var images = new List <ImageWithResultModel>()
            {
                resultImage1,
                resultImage2
            };

            File.Delete(@"pca_model-100x100-CC-Train-MassCalc-BI.bin");

            PCA pca  = TrainingHelper.GetPca(images);
            PCA pca2 = TrainingHelper.GetPca(images);

            SVMProblem problem = ProblemHandler.GetProblemFromImageModelResultList(images, pca, 10);

            //Expected value
            SVMProblem realValue = new SVMProblem();

            realValue.Add(new SVMNode[]
            {
                new SVMNode(1, 20.056853498561878),
                new SVMNode(2, 13.190302568584602),
                new SVMNode(3, -1.0813980605883611),
                new SVMNode(4, 0.38976510872122916),
                new SVMNode(5, 8.8596355929840787),
                new SVMNode(6, -7.3433006502883726),
                new SVMNode(7, 10.837768344992746),
                new SVMNode(8, 20.626727358988219),
                new SVMNode(9, -1.7552480617394235),
                new SVMNode(10, 25),
            }, 0);

            realValue.Add(new SVMNode[]
            {
                new SVMNode(1, 22.292105243883533),
                new SVMNode(2, 11.126898461982794),
                new SVMNode(3, -2.3028333386433371),
                new SVMNode(4, -6.2077696783291429),
                new SVMNode(5, 12.172991455602181),
                new SVMNode(6, -4.385545310384054),
                new SVMNode(7, 13.837367251719812),
                new SVMNode(8, 20.646721554636255),
                new SVMNode(9, -1.8000434956830436),
                new SVMNode(10, 25),
            }, 1);

            bool fail = false;

            for (int i = 0; i < realValue.Y.Count; i++)
            {
                // Expected values
                var y = realValue.Y[i];
                var x = realValue.X[i];

                // Actual values
                var py = problem.Y[i];
                var px = problem.X[i];

                for (int j = 0; j < x.Length; j++)
                {
                    if (x[j].Value != px[j].Value)
                    {
                        fail = true;
                    }
                }
            }

            if (!fail)
            {
                Assert.Pass();
            }
            else
            {
                Assert.Fail();
            }
        }
        public void FindBestHyperparametersGivenAnswersTest()
        {
            SVMProblem problem = new SVMProblem();

            problem.Add(new SVMNode[]
            {
                new SVMNode(1, 1),
                new SVMNode(2, 10),
                new SVMNode(3, 72),
                new SVMNode(4, 55),
                new SVMNode(5, 1),
            }, 1);

            problem.Add(new SVMNode[]
            {
                new SVMNode(1, 1),
                new SVMNode(2, 10),
                new SVMNode(3, 2),
                new SVMNode(4, 95),
                new SVMNode(5, 16),
            }, 1);

            problem.Add(new SVMNode[]
            {
                new SVMNode(1, 1),
                new SVMNode(2, 12),
                new SVMNode(3, 13),
                new SVMNode(4, 14),
                new SVMNode(5, 15),
            }, 1);

            problem.Add(new SVMNode[]
            {
                new SVMNode(1, 0),
                new SVMNode(2, 13),
                new SVMNode(3, 37),
                new SVMNode(4, 4),
                new SVMNode(5, 18),
            }, 0);

            problem.Add(new SVMNode[]
            {
                new SVMNode(1, 0),
                new SVMNode(2, 100),
                new SVMNode(3, 720),
                new SVMNode(4, 550),
                new SVMNode(5, 10),
            }, 0);


            //Setup test SVMParameter
            SVMParameter parameter = new SVMParameter
            {
                Type        = SVMType.C_SVC,
                Kernel      = SVMKernelType.RBF,
                C           = 2,
                Gamma       = 1,
                Probability = true,
            };


            var actualParameter = TrainingHelper.FindBestHyperparameters(problem, parameter);

            Assert.AreEqual(0.015625d, actualParameter.C);
            Assert.AreEqual(0.125d, actualParameter.Gamma);
        }
Beispiel #20
0
        /// <summary>
        /// Item1 is the training set, Item2 is the prediction set.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="original"></param>
        /// <param name="nFold"></param>
        /// <returns>returns null if the collection can't be nfolded</returns>
        public static List <Tuple <SVMProblem, SVMProblem> > GetCrossValidationSets <T>(this IEnumerable <List <double> > original, SAMData samData, SAMDataPoint.FeelingModel feelingsmodel, int nFold, bool UseIAPSRatings = false)
        {
            //TODO: Needs to be tested, can't test before data can be loaded into the program
            List <Tuple <SVMProblem, SVMProblem> > allSets = new List <Tuple <SVMProblem, SVMProblem> >();

            if (original.Count() % nFold != 0)
            {
                return(null);
            }

            //Full List of indicies
            List <int> counter = new List <int>();

            for (int k = 0; k < original.Count(); k++)
            {
                counter.Add(k);
            }
            //Divide indicies into correct nfold
            List <List <int> > trainIndicies   = new List <List <int> >();
            List <List <int> > predictIndicies = new List <List <int> >();

            for (int i = 0; i < original.Count(); i += nFold)
            {
                var temp = counter.Skip(i).Take(nFold).ToList();
                predictIndicies.Add(temp);
                trainIndicies.Add(counter.Except(temp).ToList());
            }


            for (int j = 0; j < original.Count(); j++)
            {
                //Create training problem
                SVMProblem trainSVMProblem = new SVMProblem();
                //Foreach training index, add features to the problem
                foreach (int trainIndex in trainIndicies[j])
                {
                    SVMNode[] featureVector = new SVMNode[original.ElementAt(trainIndex).Count];
                    for (int w = 0; w < original.ElementAt(trainIndex).Count; w++)
                    {
                        featureVector[w] = new SVMNode(w + 1, original.ElementAt(trainIndex)[w]);
                    }
                    trainSVMProblem.Add(featureVector, samData.dataPoints[trainIndex].ToAVCoordinate(feelingsmodel, UseIAPSRatings));
                }


                //Create predict problem
                SVMProblem predictSVMProblem = new SVMProblem();
                //Foreach predict index, add features to the problem
                foreach (int predictIndex in predictIndicies[j])
                {
                    SVMNode[] featureVector = new SVMNode[original.ElementAt(predictIndex).Count];
                    for (int w = 0; w < original.ElementAt(predictIndex).Count; w++)
                    {
                        featureVector[w] = new SVMNode(w + 1, original.ElementAt(predictIndex)[w]);
                    }
                    predictSVMProblem.Add(featureVector, samData.dataPoints[predictIndex].ToAVCoordinate(feelingsmodel, UseIAPSRatings));
                }

                allSets.Add(new Tuple <SVMProblem, SVMProblem>(trainSVMProblem, predictSVMProblem));
            }

            return(allSets);
        }
        private async Task PerformAnalysis(String path, Rectangle rectangle)
        {
            UShortArrayAsImage image = null;

            double[] pcaComponents = null;
            int      tasksComplete = 0;

            UpdateStatus(path, startingImageStatusStr);
            List <Task> tasks = new List <Task>()
            {
                new Task(() =>
                {
                    var file = db.FileStorage.FindById($"images/{path}");
                    var ms   = new MemoryStream();
                    file.CopyTo(ms);
                    ms.Seek(0, 0);
                    image = DicomFile.Open(ms).GetUshortImageInfo();

                    UpdateStatus(path, loadedImageStatusStr);
                }),
                new Task(() =>
                {
                    image = Normalization.GetNormalizedImage(image, rectangle,
                                                             int.Parse(Configuration.Get("sizeImageToAnalyze")));
                    db.FileStorage.Upload($"images/{path}-cropped", $"{path}-cropped",
                                          image.GetPngAsMemoryStream());

                    UpdateStatus(path, croppedImageStatusStr);
                }),
                new Task(() =>
                {
                    image = Contrast.ApplyHistogramEqualization(image);

                    db.FileStorage.Upload($"images/{path}-croppedContrast", $"{path}-croppedContrast",
                                          image.GetPngAsMemoryStream());

                    UpdateStatus(path, contrastImageStatusStr);
                }),
                new Task(() =>
                {
                    //PCA
                    PCA pca = PCA.LoadModelFromFile(Configuration.Get("PcaModelLocation"));

                    if (!int.TryParse(Configuration.Get("componentsToUse"), out int components))
                    {
                        components = pca.Eigenvalues.Length;
                    }
                    pcaComponents = pca.GetComponentsFromImage(image, components);
                    UpdateStatus(path, pcaImageStatusStr);
                }),
                new Task(() =>
                {
                    //SVM
                    SVMProblem svmProblem = new SVMProblem();

                    // add all the components to an SVMNode[]
                    SVMNode[] nodes = new SVMNode[pcaComponents.Length];
                    for (int i = 0; i < pcaComponents.Length; i++)
                    {
                        nodes[i] = new SVMNode(i + 1, pcaComponents[i]);
                    }

                    svmProblem.Add(nodes, 0);

                    svmProblem = svmProblem.Normalize(SVMNormType.L2);

                    SVMModel svmModel = SVM.LoadModel(Configuration.Get("ModelLocation"));

                    double[] results = svmProblem.PredictProbability(svmModel, out var probabilities);

                    var analysis              = db.GetCollection <Analysis>("analysis");
                    Analysis currentAnalysis  = analysis.FindOne(x => x.Id.ToString().Equals(path));
                    currentAnalysis.Certainty = results[0] == 0 ? probabilities[0][1] * 100 : probabilities[0][0] * 100;
                    currentAnalysis.Diagnosis = results[0] == 0
                        ? DdsmImage.Pathologies.Benign
                        : DdsmImage.Pathologies
                                                .Malignant;
                    analysis.Update(currentAnalysis);


                    UpdateStatus(path, svmImageStatusStr);
                })
            };

            foreach (Task task in tasks)
            {
                task.Start();
                await task;

                // lets set percentage done:
                var      analysis        = db.GetCollection <Analysis>("analysis");
                Analysis currentAnalysis = analysis.FindOne(x => x.Id.ToString().Equals(path));
                currentAnalysis.PercentageDone = (++tasksComplete * 100) / tasks.Count;
                analysis.Update(currentAnalysis);
            }

            UpdateStatus(path, doneStatusStr);
        }
Beispiel #22
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static SVMProblem Load(string filename)
        {
            if (String.IsNullOrWhiteSpace(filename) || !File.Exists(filename))
            {
                return null;
            }

            NumberFormatInfo provider = new NumberFormatInfo();
            provider.NumberDecimalSeparator = ".";

            SVMProblem problem = new SVMProblem();
            using (StreamReader sr = new StreamReader(filename))
            {
                while (true)
                {
                    string line = sr.ReadLine();
                    if (line == null)
                        break;

                    string[] list = line.Trim().Split(' ');

                    double y = Convert.ToDouble(list[0].Trim(), provider);

                    List<SVMNode> nodes = new List<SVMNode>();
                    for (int i = 1; i < list.Length; i++)
                    {
                        string[] temp = list[i].Split(':');
                        SVMNode node = new SVMNode();
                        node.Index = Convert.ToInt32(temp[0].Trim());
                        node.Value = Convert.ToDouble(temp[1].Trim(), provider);
                        nodes.Add(node);
                    }

                    problem.Add(nodes.ToArray(), y);
                }
            }

            return problem;
        }