Ejemplo n.º 1
0
        private void RunBenchmark(IPredictionModel model)
        {
            _loggingService.Log("Running benchmark has started");

            _benchmarkView.IsBenchmarkRunning = true;

            var provider = new BatchDataProvider(DirectoryHelper.TestLabelsPath, DirectoryHelper.TestImagesPath, 100);

            var acc = 0;

            for (var i = 0; i < 100; i++)
            {
                if (!_benchmarkView.IsBenchmarkRunning)
                {
                    break;
                }

                MnistImageBatch data = provider.GetData();

                int[] predictions = data.Pixels.Select(model.Predict).Select(x => x.ArgMax()).ToArray();

                acc += data.Labels.Where((lbl, pred) => lbl == predictions[pred]).Count();

                _benchmarkView.PerformProgressStep();

                _benchmarkView.DrawGrid(new ImageGridModel(data, predictions));
            }

            _benchmarkView.SetAccuracy(acc);

            _benchmarkView.IsBenchmarkRunning = false;

            _loggingService.Log("Running benchmark has completed");
        }
Ejemplo n.º 2
0
        public ImageGridModel(MnistImageBatch batch, int[] predictions)
        {
            Labels = new List <int>(batch.Labels);

            Predictions = new List <int>(predictions);

            Images = new List <Image>(ProcessImages(batch.Pixels));
        }
Ejemplo n.º 3
0
        private static void Main()
        {
            Console.Title = "Digit Recognizer - Engine";

            var learningRate         = 0.00035;
            var epochs               = 10;
            var regularizationFactor = 15.0;

            LearningPipeline pipeline = new LearningPipeline()
                                        .UseGradientClipping()
                                        .UseL2Regularization(regularizationFactor)
                                        .UseDropout(0.5)
                                        .SetWeightsInitializer(InitializerType.RandomInitialization)
                                        .SetEpochCount(epochs);

            var layers = new List <NnLayer>
            {
                new NnLayer(784, 200, new LeakyRelu()),
                new NnLayer(200, 100, new LeakyRelu()),
                new NnLayer(100, 10, new Softmax())
            };

            var nn = new NeuralNetwork(layers, learningRate);

            var optimizer = new MomentumOptimizer(nn, new CrossEntropy(), 0.93);

            var provider = new BatchDataProvider(DirectoryHelper.ExpandedTrainLabelsPath, DirectoryHelper.ExpandedTrainImagesPath, 100);

            pipeline.Add(optimizer);

            pipeline.Add(nn);

            pipeline.Add(provider);

            PredictionModel model = pipeline.Run();

            var provider1 = new BatchDataProvider(DirectoryHelper.TestLabelsPath, DirectoryHelper.TestImagesPath, 10000);
            var acc       = 0.0;

            MnistImageBatch data = provider1.GetData();

            List <double[]> predictions = data.Pixels.Select(pixels => model.Predict(pixels)).ToList();

            for (var i = 0; i < data.Labels.Length; i++)
            {
                if (data.Labels[i] == predictions[i].ArgMax())
                {
                    acc++;
                }
            }

            acc /= 10000.0;

            Console.WriteLine($"Accuracy on the test data is: {acc:P2}");

            string basePath = Path.GetFullPath(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) +
                                               DirectoryHelper.ModelsFolder);

            string modelName = $"{Guid.NewGuid()}-{acc:N4}.nn";

            string filename = $"{basePath}/{modelName}";

            model.Save(filename);

            Console.ReadKey();
        }