Exemple #1
0
        //识别
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (machine == null)
            {
                MessageBox.Show("please select a machine first!");
                return;
            }

            RenderTargetBitmap bmp = new RenderTargetBitmap((int)WritingBoard.ActualWidth, (int)WritingBoard.ActualHeight, 0, 0, PixelFormats.Default);

            bmp.Render(WritingBoard);

            //
            var con = new FormatConvertedBitmap();

            con.BeginInit();
            con.Source            = bmp;
            con.DestinationFormat = PixelFormats.Gray8;
            con.EndInit();

            var trans = new TransformedBitmap();

            trans.BeginInit();
            trans.Source    = con;
            trans.Transform = new ScaleTransform(0.1, 0.1);
            trans.EndInit();

            var buff = BitmapSourceToArray(trans);
            var data = new double[buff.Length];

            for (int i = 0; i < buff.Length; i++)
            {
                data[i] = 255 - buff[i];
            }

            var tensor  = new TensorOld(data, 1, buff.Length);
            var predict = machine.Predict(tensor);

            PredictText.Text = predict[0];
            var predictProb = machine.LastRawResult.Max();

            ProbText.Text = $"Probability:{predictProb.ToString("F8")}";
            ShowProbability();
        }
Exemple #2
0
        public void Play()
        {
            var iris   = AyxCsvReader.Instance.ReadCsvFileDataTable("Data/iris.csv");
            var labels = DataFormat.DataTableToList(iris, "Species").ToList();
            var codec  = new OneHotCodec(labels);
            var y      = codec.Encode(labels);
            var X      = DataFormat.DataTableToTensor(iris, 1, 2, 3, 4);

            var model = new NeuralNetwork()
                        .AddFullLayer(5)
                        .AddReLU()
                        .AddFullLayer(5)
                        .AddReLU()
                        .AddFullLayer(codec.Length)
                        .AddSoftmax()
                        .UseCrossEntropyLoss()
                        .UseGradientDescent(0.001);

            var trainer = new Trainer(model, 16, 200, true);

            trainer.StartTrain(X, y, null, null);

            var machine = new ClassificationMachine(model);

            machine.LabelCodec = codec;

            while (true)
            {
                Console.Write("input Sepal.Length:");
                var a1 = double.Parse(Console.ReadLine());
                Console.Write("input Sepal.Width:");
                var a2 = double.Parse(Console.ReadLine());
                Console.Write("input Petal.Length:");
                var a3 = double.Parse(Console.ReadLine());
                Console.Write("input Petal.Width:");
                var a4 = double.Parse(Console.ReadLine());

                var input = new TensorOld(new double[] { a1, a2, a3, a4 }, 1, 4);
                Console.WriteLine($"you input is {machine.Predict(input).First()},  Codec:{machine.LastCodecResult},  Raw:{machine.LastRawResult}");
            }
        }