private List <InputOutputPair> ReadFromDirectory(string directory)
        {
            List <InputOutputPair> result = new List <InputOutputPair>();

            string[] directories = Directory.GetDirectories(directory);

            Task[] tasks = new Task[directories.Length];

            for (int i = 0; i < directories.Length; i++)
            {
                int newI = 0 + i;
                tasks[i] = Task.Run(() =>
                {
                    foreach (string input in Directory.GetFiles(directories[newI]))
                    {
                        InputOutputPair pair = new InputOutputPair(input, directories[newI]);
                        Monitor.Enter(result);
                        result.Add(pair);
                        Monitor.Exit(result);
                    }
                });
            }

            Task.WaitAll(tasks);

            return(result);
        }
Beispiel #2
0
        public void TrainMnist(InputOutputPair pair, double learningRate)
        {
            FilteredImage input;

            if (ColorScheme == "rgb")
            {
                input = ImageProcessing.GetNormalizedFilteredImage(new Bitmap(pair.Input));
            }
            else
            {
                //input = ImageProcessing.GetNormalizedGrayscaleFilteredImage(new Bitmap(pair.Input));
                input = ImageProcessing.GetNormalizedMnist(new Bitmap(pair.Input));
            }

            double[] actualOutput = RecogniseImage(input);

            Backpropagate(actualOutput, pair.Output, pair.OutputChar, learningRate);

            if (Math.Abs(((ConvolutionalLayer)NetworkLayers[0]).Filters[0].Kernels[0].ElementSum) > 1000)
            {
                throw new Exception("Weights over 1000");
            }

            if (Double.IsNaN(((ConvolutionalLayer)NetworkLayers[0]).Filters[0].Kernels[0].Values[0, 0]))
            {
                throw new Exception("Nan error");
            }
        }
        private List <InputOutputPair> Shuffle(List <InputOutputPair> set)
        {
            for (int i = 0; i < set.Count; i++)
            {
                int             x   = GlobalRandom.GetRandomInt(i, set.Count);
                InputOutputPair aux = set[x];
                set[x] = set[i];
                set[i] = aux;
            }

            return(set);
        }
Beispiel #4
0
        public EvaluationMetrics EvaluateParallel(List <InputOutputPair> set)
        {
            int[,] matrix = new int[26, 26];

            int N = 26;

            int total = set.Count;

            Task[] tasks = new Task[N];

            int chunkSize = (N + total - 1) / N;

            for (int t = 0; t < N; t++)
            {
                int start = t * chunkSize;
                int end   = Math.Min(start + chunkSize, total);
                tasks[t] = Task.Run(() =>
                {
                    for (int i = start; i < end; i++)
                    {
                        InputOutputPair pair = set[i];

                        FilteredImage input;
                        if (ColorScheme == "rgb")
                        {
                            input = ImageProcessing.GetNormalizedFilteredImage(new Bitmap(pair.Input));
                        }
                        else
                        {
                            input = ImageProcessing.GetNormalizedGrayscaleFilteredImage(new Bitmap(pair.Input));
                        }

                        double[] actualOutput = RecogniseImage(input);

                        int expected = pair.OutputChar - 65;
                        int obtained = GetReconisedChar(actualOutput) - 65;

                        Monitor.Enter(matrix);
                        matrix[expected, obtained]++;
                        Monitor.Exit(matrix);
                    }
                });
            }

            Task.WaitAll(tasks);

            return(new EvaluationMetrics(matrix, total));
        }
Beispiel #5
0
        public EvaluationMetrics EvaluateMetrics(List <InputOutputPair> set)
        {
            //double error = 0;
            //int correct = 0, total = 0;

            int[,] matrix = new int[26, 26];
            int total = 0;

            int N = set.Count;

            for (int i = 0; i < N; i++)
            {
                InputOutputPair pair = set[i];

                FilteredImage input;
                if (ColorScheme == "rgb")
                {
                    input = ImageProcessing.GetNormalizedFilteredImage(new Bitmap(pair.Input));
                }
                else
                {
                    input = ImageProcessing.GetNormalizedGrayscaleFilteredImage(new Bitmap(pair.Input));
                }

                double[] actualOutput = RecogniseImage(input);

                //error += LeastSquaredError(actualOutput, pair.Output);

                //total++;

                //if (GetReconisedChar(actualOutput) == pair.OutputChar)
                //{
                //    correct++;
                //}

                int expected = pair.OutputChar - 65;
                int obtained = GetReconisedChar(actualOutput) - 65;

                matrix[expected, obtained]++;

                total++;
            }


            return(new EvaluationMetrics(matrix, total));
        }
Beispiel #6
0
        public double Evaluate(List <InputOutputPair> set)
        {
            double error = 0;
            int    correct = 0, total = 0;

            int N = set.Count;

            for (int i = 0; i < N; i++)
            {
                InputOutputPair pair = set[i];

                FilteredImage input;
                if (ColorScheme == "rgb")
                {
                    input = ImageProcessing.GetNormalizedFilteredImage(new Bitmap(pair.Input));
                }
                else
                {
                    input = ImageProcessing.GetNormalizedGrayscaleFilteredImage(new Bitmap(pair.Input));
                }

                double[] actualOutput = RecogniseImage(input);

                error += LeastSquaredError(actualOutput, pair.Output);

                total++;

                if (GetReconisedChar(actualOutput) == pair.OutputChar)
                {
                    correct++;
                }

                int expected = pair.OutputChar - 65;
                int obtained = GetReconisedChar(actualOutput) - 65;

                total++;
            }


            return((double)correct / total);
        }