コード例 #1
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new SVMApp());
            }
            else
            {
                string datasetPath = args[0];
                string testPath    = args[1];
                string outputPath  = args[2];

                List <Data> dataSets = new List <Data>();
                FillDataset(dataSets, datasetPath);

                var machine = new VectorMachine(new Polynomial(2), 25);
                var learn   = new SequentialOptimization(machine, dataSets.ToArray());
                _ = learn.Run();

                dataSets.Clear();
                List <string> paths = FillDataset(dataSets, testPath);

                double[] output = machine.Compute(dataSets.ToArray());

                using (StreamWriter writer = new StreamWriter(outputPath))
                {
                    for (int i = 0; i < output.Length; i++)
                    {
                        writer.WriteLine(paths[i] + ':' + output[i]);
                    }
                }
            }
        }
コード例 #2
0
        private void LoadDataSetButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                dataSets.Clear();
                string file = openFile.FileName;
                UseBtn.Visible            = true;
                LoadDataSetButton.Visible = false;
                using (StreamReader reader = new StreamReader(file))
                {
                    while (!reader.EndOfStream)
                    {
                        string line     = reader.ReadLine();
                        double expected = double.Parse(line.Substring(0, 1));
                        string path     = line.Substring(2);
                        using (Bitmap bitmap = new Bitmap(path))
                        {
                            dataSets.Add(bitmap.ToDataSet(expected == 0 ? -1 : 1));
                        }
                    }
                }

                machine = new VectorMachine(new Polynomial(2), 25);
                var      learn   = new SequentialOptimization(machine, dataSets.ToArray());
                double[] changes = learn.Run();

                graphicsForm = new SVMGraphic();
                graphicsForm.Show();
                for (int i = 0; i < changes.Length; i++)
                {
                    graphicsForm.SVMData.Add(i, changes[i]);
                    graphicsForm.DrawFucntion();
                }


                isTrained = true;
            }
        }