Exemple #1
0
        public static void CNN_Testing(DigitImage[] digitImagesDatas, bool predictionIsOn, CNN cnn, int iterationCount)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            int    testing_count = digitImagesDatas.Length;
            int    correct_count = 0;
            double timeLimit = 0, error = 0;

            Matrix[] input, targets;

            Console.WriteLine("System is getting tested. You will see the results when it is done...\n");
            if (cursorTopTesting == 0)
            {
                cursorTopTesting = Console.CursorTop;
            }

            InitializeInputAndTarget(out input, out targets);
            timeLimit = stopwatch.ElapsedMilliseconds;

            for (int i = 0; i < testing_count; i++)
            {
                for (int j = 0; j < 28; j++)
                {
                    for (int k = 0; k < 28; k++)
                    {
                        input[0][j, k] = digitImagesDatas[i].pixels[j][k];
                    }
                }

                input[0].Normalize(0f, 255f, 0f, 1f);


                Matrix ans = null;
                if (predictionIsOn)
                {
                    ans = cnn.Predict(input);
                }
                else
                {
                    cnn.Train(input, targets[digitImagesDatas[i].label]);
                    ans = cnn.Layers[cnn.Layers.Length - 1].Output[0];
                }

                if (ans.GetMaxRowIndex() == digitImagesDatas[i].label)
                {
                    correct_count++;
                }

                if (stopwatch.ElapsedMilliseconds > timeLimit)
                {
                    // every 0.5 sec update error
                    timeLimit += 500;
                    error      = cnn.GetError();
                }

                int val = Map(0, testing_count, 0, 100, i);
                ProgressBar(val, i, testing_count, error, stopwatch.ElapsedMilliseconds / 1000.0, cursorTopTesting);
            }
            double accuracy = (correct_count * 1f / testing_count) * 100.0;

            Console.WriteLine("\nIteration Count:" + iterationCount);
            Console.WriteLine("\nTime :" + (stopwatch.ElapsedMilliseconds / 1000.0).ToString("F4"));
            Console.WriteLine("\nAccuracy: %{0:F2}\n", accuracy);
            Console.WriteLine("Correct/All: {0}/{1}", correct_count, testing_count);

            cursorTopTesting = Console.CursorTop;

            if (accuracy >= 95 && iterationCount != -1)
            {
                string name   = accuracy.ToString("F2") + "__" + iterationCount + "__";
                Random random = new Random();
                int    length = 6;
                for (int i = 0; i < length; i++)
                {
                    char c = (char)random.Next('A', 'F' + 1);
                    name += c;
                }
                cnn.SaveData(name + ".json");
            }
        }