示例#1
0
        public static void KnnMain()
        {
            Console.WriteLine("Using demo setup?[y/n]");
            string str = Console.ReadLine();

            if (str.ToLower().Contains("y") == false)
            {
                Console.WriteLine("Input node maximum value:(Default:10000)");
                NodesMaxValueSet = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input node count:(Default:150)");
                NodesSet = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input groups count for random points to divid into:(Default:3, Min:1)");
                DataGroupCount = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input fluctuation ratio:(Default:0.5, Min:0.1)");
                FluctuationRatio = Convert.ToSingle(Console.ReadLine());

                Console.WriteLine("Input new node count:(Default:10)");
                NewNodesCount = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Input k set:(Default:7)");
                k = Convert.ToInt32(Console.ReadLine());
            }

            //Gen grouped random nodes
            Random2DPoints.OnGeneratePointGroups += Random2DPoints_OnGeneratePointGroups;
            var ClassifiedPoints = Random2DPoints.GenerateRandomPointsGroup(NodesMaxValueSet, NodesSet, DataGroupCount, FluctuationRatio);
            var Dataset          = new VectorCollection <Point2D>(ClassifiedPoints);

            Dataset.Print();

            var           knnTrainer = new KnnTrainer <Point2D>(Dataset);
            Knn <Point2D> knn        = new Knn <Point2D>(knnTrainer.Train());

            knn.OnClassify += Knn_OnClassify;

            while (Retry)
            {
                var NewNodes = Random2DPoints.GenerateRandomPoints(NodesMaxValueSet, NewNodesCount);

                foreach (var NewNode in NewNodes)
                {
                    knn.Classify(k, NewNode);
                }

                InputCommand();
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            KnnTrainResult <SURFFeature> TrainedFeatures  = new KnnTrainResult <SURFFeature>();
            SURFFeatureExtractor         FeatureExtractor = new SURFFeatureExtractor(threshold, octaves, initial)
            {
                ExtractNegativeOnly = true, MinimumScale = minimumScale
            };

            //Training
            foreach (var SamplePathTag_Pair in SamplePathTag_Pairs)
            {
                string Path = SamplePathTag_Pair.Key;
                string Tag  = SamplePathTag_Pair.Value;

                BitmapReader TestSampleBitmapReader = new BitmapReader(Path);
                var          Bitmaps = TestSampleBitmapReader.GetBitmaps();

                Console.WriteLine("Extract features from tag:{0}, path:{1}", Tag, Path);

                var features = FeatureExtractor.ExtractFeatures(Bitmaps, Tag);
                Console.WriteLine("Features extract ,Count:{0}", features.Count());

                var knnTrainer     = new KnnTrainer <SURFFeature>(features);
                var knnTrainResult = knnTrainer.Train();
                TrainedFeatures.AddRange(knnTrainResult);

                foreach (var b in Bitmaps)
                {
                    b.Dispose();
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            Application.EnableVisualStyles();
            Application.Run(new MainForm(FeatureExtractor, TrainedFeatures));
        }
示例#3
0
        static void Main(string[] args)
        {
            VectorCollection <RawImage28x28> Dataset = null;
            Knn <RawImage28x28> knn = null;
            KmeansTrainResult <RawImage28x28> KmeansTrainResult = null;
            Kmeans <RawImage28x28>            kmeans            = null;

            //CheckFiles
            if (File.Exists(MnistPixelFilePath) == false)
            {
                MnistPixelFilePath = Environment.CurrentDirectory + "\\train-images.idx3-ubyte";
            }

            if (File.Exists(MnistLabelFilePath) == false)
            {
                MnistLabelFilePath = Environment.CurrentDirectory + "\\train-labels.idx1-ubyte";
            }

            if (File.Exists(KmeansTrainResultFilePath) == false)
            {
                KmeansTrainResultFilePath = Environment.CurrentDirectory + "\\KmeansTrainResult.json";
            }


            //LoadFiles
            if (File.Exists(MnistPixelFilePath) && File.Exists(MnistLabelFilePath))
            {
                var MnistDataSet = MnistDataSetLoader.LoadData(MnistPixelFilePath, MnistLabelFilePath);
                Dataset = new VectorCollection <RawImage28x28>(MnistDataSet);
            }

            if (File.Exists(KmeansTrainResultFilePath))
            {
                KmeansTrainResult = new KmeansTrainResult <RawImage28x28>(KmeansTrainResultFilePath);
            }

            if (Dataset != null)
            {
                var knnTrainer = new KnnTrainer <RawImage28x28>(Dataset);
                knn = new Knn <RawImage28x28>(knnTrainer.Train());

                if (KmeansTrainResult == null)
                {
                    Console.WriteLine("Can't find Kmeans train data.");
                    KmeansTrainResult = AskKmeansTrainData(false);
                }
                else if (KmeansTrainResult != null)
                {
                    Console.WriteLine("Found Kmeans train data.");
                    Console.WriteLine("[U]se trained data or [t]rain again? [u/t]");
                    if (Console.ReadLine().ToLower().Contains('t'))
                    {
                        KmeansTrainResult = null;
                        KmeansTrainResult = AskKmeansTrainData(true);
                    }
                }
            }
            else
            {
                Console.WriteLine("Can't find Knn train data, ignored");
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
            }

            if (KmeansTrainResult != null)
            {
                kmeans = new Kmeans <RawImage28x28>(KmeansTrainResult);
            }

            if (kmeans == null && knn == null)
            {
                Console.WriteLine("Fatal error: Can not find any trained data.");
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                return;
            }

            MainForm form = new MainForm(knn, kmeans);

            Application.EnableVisualStyles();
            Application.Run(form);
        }