public void Testing(int outClass, Dictionary <string, double[]> keyValuePairs, MNIST mnist /* Mnist */) { Console.WriteLine("Starting Test..."); var recognition = 0; for (int i = 0; i < /*keyValuePairs.Count*/ mnist.TestImgs.Count; i++) { Console.WriteLine("\t Processing testdata {0} of {1}", i + 1, /*keyValuePairs.Count*/ mnist.TestImgs.Count); double[] output = this.ForwardPass(outClass, /*keyValuePairs.ElementAt(i).Key*/ null, mnist.TestImgs[i].Pixels); // recognition rate computation recognition += RecognitionRate(output, /*keyValuePairs.ElementAt(i).Value*/ mnist.TestImgs[i].Label); //if (RecognitionRate(output, keyValuePairs.ElementAt(i).Value) == 0) //{ // Console.WriteLine("\t\t Testdata {0} wrong classified", keyValuePairs.ElementAt(i).Key); //} } var recRate = (double)recognition / (double)/*keyValuePairs.Count*/ mnist.TestImgs.Count; Console.WriteLine("Recognition rate in testdata: {0}", recRate); }
public void Learning(Dictionary <string, double[]> keyValuePairs, int outClass, int epochs, int miniBatchSize, string filePath, MNIST mnist /* Mnist */) { TextWriter textWriter = new StreamWriter(filePath + "learning_report.csv"); for (int e = 0; e < epochs; e++) { Console.WriteLine("Processing epoch {0} of {1}", e + 1, epochs + 1); var recognition = 0; var recRate = 0.0; // shuffle training input //var list = keyValuePairs.Keys.ToList(); //var rnd = new Random(); //var rndList = list.OrderBy(x => rnd.Next()).ToList(); //Dictionary<string, double[]> rndKeyValuePairs = new Dictionary<string, double[]>(); //foreach (var key in rndList) //{ // rndKeyValuePairs.Add(key, keyValuePairs[key]); //} // ------------------------ MNIST Dataset ------------------------ mnist.ShuffleTrainingImgs(); // ------------------------ MNIST Dataset ------------------------ // training data for (int i = 0; i < /*rndKeyValuePairs.Count*/ mnist.TrainingImgs.Count; i++) { double[] output = new double[outClass]; // forward pass for (int j = 0; j < this.Layers.Count; j++) { if (this.Layers[j].GetType().Equals(typeof(InputLayer))) { this.Layers[j].FeedForward(/*new Bitmap(rndKeyValuePairs.ElementAt(i).Key)*/ null, null, /*null*/ mnist.TrainingImgs[i].Pixels); // MNIST continue; } if (this.Layers[j].GetType().Equals(typeof(ConvolutionLayer))) { if (e == 0 && i == 0) { this.Layers[j].RandInitFilter(); } this.Layers[j].FeedForward(null, null, this.Layers[j - 1].ImgMatrix); continue; } if (this.Layers[j].GetType().Equals(typeof(PoolingLayer))) { this.Layers[j].FeedForward(null, null, this.Layers[j - 1].ImgMatrix); if (this.Layers[j + 1].GetType().Equals(typeof(FullyConnectedLayer))) { this.Layers[j].Flattening(); } continue; } if (this.Layers[j].GetType().Equals(typeof(FullyConnectedLayer))) { if (this.Layers[j + 1].GetType().Equals(typeof(OutputLayer))) { this.Layers[j].InitLayer(this.Layers[j - 1].FlatArray.Length, outClass); } else { this.Layers[j].InitLayer(this.Layers[j - 1].FlatArray.Length, this.Layers[j - 1].FlatArray.Length); } if (e == 0 && i == 0) { this.Layers[j].RandInitLayerMat(); } this.Layers[j].FeedForward(null, this.Layers[j - 1].FlatArray, null); continue; } if (this.Layers[j].GetType().Equals(typeof(OutputLayer))) { this.Layers[j].FeedForward(null, this.Layers[j - 1].FlatArray, null); output = this.Layers[j].GetOutputArray(); } } // backward pass for (int j = this.Layers.Count - 1; j >= 0; j--) { if (this.Layers[j].GetType().Equals(typeof(ConvolutionLayer))) { this.Layers[j].BackwardPass(null, this.Layers[j + 1].DeltaMatrix); continue; } if (this.Layers[j].GetType().Equals(typeof(PoolingLayer))) { if (this.Layers[j + 1].GetType().Equals(typeof(FullyConnectedLayer))) { this.Layers[j].BackwardPass(this.Layers[j + 1].DeltaArray, null); } else { this.Layers[j].BackwardPass(null, this.Layers[j + 1].DeltaMatrix); } continue; } if (this.Layers[j].GetType().Equals(typeof(FullyConnectedLayer))) { this.Layers[j].BackwardPass(this.Layers[j + 1].DeltaArray, null); continue; } if (this.Layers[j].GetType().Equals(typeof(OutputLayer))) { this.Layers[j].BackwardPass(/*rndKeyValuePairs.ElementAt(i).Value*/ mnist.TrainingImgs[i].Label, null); // MNIST } } // update for every mini Batch if (i % miniBatchSize == miniBatchSize - 1) { // update forward pass for (int j = 0; j < this.Layers.Count; j++) { if (this.Layers[j].GetType().Equals(typeof(ConvolutionLayer)) || this.Layers[j].GetType().Equals(typeof(FullyConnectedLayer))) { this.Layers[j].UpdateWeights(miniBatchSize); } } //Console.WriteLine("\t" + "Weights updated after {0} inputs", i + 1); } // recognition rate computation recognition += RecognitionRate(output, /*rndKeyValuePairs.ElementAt(i).Value*/ mnist.TrainingImgs[i].Label); // MNIST } recRate = (double)recognition / (double)/*rndKeyValuePairs.Count*/ mnist.TrainingImgs.Count; // MNIST Console.WriteLine("Recognition rate in epoch {0}: {1}", e + 1, recRate); // save recognition rate to csv after each epoch textWriter.WriteLine(recRate); textWriter.Flush(); // store weights for (int j = 0; j < this.Layers.Count; j++) { if (this.Layers[j].GetType().Equals(typeof(ConvolutionLayer)) || this.Layers[j].GetType().Equals(typeof(FullyConnectedLayer))) { // store weights this.Layers[j].StoreWeights(); } } if (recRate == 1) { Console.WriteLine("Learning stopped in epoch {0} of {1}", e + 1, epochs); break; } } }
public static void Main(string[] args) { NeuralNetwork network = new NeuralNetwork(ExecMode.Learning, 1 * Math.Pow(10, -6)); string imageFilePath = @"C:\Users\cleist\source\repos\LicensPlateRecognition\LicensPlateRecognition\LicensPlateRecognition\Image\"; string[] trainingData = Directory.GetFiles(imageFilePath + "TrainingData", "*"); string[] testData = Directory.GetFiles(imageFilePath + "TestData", "*"); // key value pairs for training or test input and desired output Dictionary <string, double[]> keyValuePairs = new Dictionary <string, double[]>(); // Declare network layers: declare in order of traversion! Since it will be the order of the layers list in network class InputLayer inputLayer = new InputLayer(28, 28, 1, network); ConvolutionLayer convLayer1 = new ConvolutionLayer(new Filter(5, 5, inputLayer.Depth), 20, 1, network); //ConvolutionLayer convLayer2 = new ConvolutionLayer(new Filter(5, 5, convLayer1.Filters.Count), 20, 1, network); PoolingLayer pooling1 = new PoolingLayer(network); ConvolutionLayer convLayer3 = new ConvolutionLayer(new Filter(5, 5, convLayer1.Filters.Count), 40, 1, network); //ConvolutionLayer convLayer4 = new ConvolutionLayer(new Filter(3, 3, convLayer3.Filters.Count), 40, 1, network); PoolingLayer pooling2 = new PoolingLayer(network); FullyConnectedLayer fullyConnectedLayer1 = new FullyConnectedLayer(network); //FullyConnectedLayer fullyConnectedLayer2 = new FullyConnectedLayer(network); OutputLayer outputLayer = new OutputLayer(network); // Declare Output Classes int outClass = 10; // ------------------------ MNIST Dataset ------------------------ MNIST mnist = new MNIST(); // ------------------------ MNIST Dataset ------------------------ if (network.ExecMode == ExecMode.Learning) { // create a csv with tuple of image and class value //network.CreateCSV(imageFilePath, trainingData, "training.csv"); //network.LoadCSV(imageFilePath, keyValuePairs, "training.csv", outClass); // ------------------------ MNIST Dataset ------------------------ mnist.ReadTrainMNIST(); // ------------------------ MNIST Dataset ------------------------ var epochs = 59; // must be divisible through number of training data var miniBatchSize = 10; network.Learning(keyValuePairs, outClass, epochs, miniBatchSize, imageFilePath, mnist /* Mnist */); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } if (network.ExecMode == ExecMode.Testing) { // create a csv with tuple of image and class value //network.CreateCSV(imageFilePath, testData, "testing.csv"); //network.LoadCSV(imageFilePath, keyValuePairs, "testing.csv", outClass); // ------------------------ MNIST Dataset ------------------------ mnist.ReadTestMNIST(); // ------------------------ MNIST Dataset ------------------------ network.Testing(outClass, keyValuePairs, mnist /* Mnist */); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } if (network.ExecMode == ExecMode.Normal) { while (true) { Console.WriteLine("Please Insert an image filepath..."); try { string image = Console.ReadLine(); double[] output = network.ForwardPass(outClass, image, null); for (int i = 0; i < output.Length; i++) { Console.Write("{0} ", output[i]); } Console.WriteLine(); } catch { Console.WriteLine("No image or supported image format!"); } } } }