/// <summary> /// Routine to train the network /// </summary> /// <param name="cnnProject">The CNN project.</param> /// <param name="numberOfRounds">The number of training rounds.</param> public void TrainPattern(CNNProject cnnProject, long numberOfTrainingRounds) { // 1. preparation - both project's imageLists generalized images to the shuffled trainingItem-list #region 1. preparation List<TrainingItem> trainingItemList = new List<TrainingItem>(); #region fills the trainingItemList ImageList imgList; bool matching; for (int i = 0; i < 2; i++) { // at first we include the matching images if (i == 0) { imgList = cnnProject.Matching; matching = true; } // and then the not matching images else { imgList = cnnProject.Matching; matching = false; } foreach (Image img in imgList.Images) { // generalizes the image Image generalizedImg = ImageHandling.GeneralizeImage(img); trainingItemList.Add(new TrainingItem(generalizedImg, matching)); } } #endregion // filled list gets shuffled // (maybe this optimizes the result) StaticClasses.Shuffle<TrainingItem>(trainingItemList); #endregion // 2. build of training data items and add it to the helper #region 2. trainingItem // used later on to create the training thread NetworkHelper helper = new NetworkHelper(_network); foreach (TrainingItem trainingItem in trainingItemList) { Image img = trainingItem.Image; ArrayList arryListInput; #region fills arryListInput // Converts an image of any size to a pattern that can be feed to the network. ImageProcessingHelper imgHelper = new ImageProcessingHelper(); //note: this is a (monochrome) collection of 0's and 1's !! arryListInput = imgHelper.ArrayListFromImage(img); if (img.Width * img.Height != _network.InputLayer.Count) { throw new InvalidInputException("The number of pixels in the input image doesn't match the number of input layer neurons!", null); } #region Debugging /* // Convert an arrayList by rounding each value to a pattern of 0s and 1s PatternProcessingHelper patHelper = new PatternProcessingHelper(); String tmpPatern = patHelper.PatternFromArraylist(tmpImgList); Debug.WriteLine("Added : " + tmpPatern); */ #endregion #endregion ArrayList arryListOutput; #region fills arryListOutput arryListOutput = new ArrayList(); // true is going to be a single 1, false a single 0 arryListOutput.Add(trainingItem.Matching ? 1 : 0); #endregion // a training data item is used for a single training round TrainingData trainingDataItem = new TrainingData(arryListInput, arryListOutput); // this could be also used; one training round directly //_network.TrainNetwork(trainingDataItem); helper.AddTrainingData(trainingDataItem); } #endregion // Let's go! _trainStart = DateTime.Now; // 3. training #region 3. training // ShowProgress delegate helper.TrainingProgress += new NetworkHelper.TrainingProgressEventHandler(ShowProgress); // Start training // --- here we are going to wait -- helper.Train(numberOfTrainingRounds, true); // <-- // releasing helper.TrainingProgress -= new NetworkHelper.TrainingProgressEventHandler(ShowProgress); // show message box if (StopTrainingSilently == false) { MessageBox.Show("Training of the neuronal network completed at " + DateTime.Now, "Training Completed", MessageBoxButtons.OK, MessageBoxIcon.Information); } #endregion }
/// <summary> /// Routine to detect an image (right sized!) /// </summary> /// <param name="img">The resized and generalized image.</param> /// <returns></returns> public bool DetectPattern(Image img) { //Step 1 : Convert the image to an arrayList ArrayList arryListInput; #region fills arryListInput // Converts an image of any size to a pattern that can be feed to the network. ImageProcessingHelper imgHelper = new ImageProcessingHelper(); //note: this is a (monochrome) collection of 0's and 1's !! arryListInput = imgHelper.ArrayListFromImage(img); if (img.Width * img.Height != _network.InputLayer.Count) { throw new InvalidInputException("The number of pixels in the input image doesn't match the number of input layer neurons!", null); } #endregion //Step 2: Run the network and obtain the output ArrayList output = null; output = _network.RunNetwork(arryListInput); // finally: the result is in the first and only index float result = (float)output[0]; // displays rounded result (better readable) Debug.WriteLine("Detection Result: " + Math.Round(result, 3) + " (" + result + ")"); //TODO: what is the best value for this? if (result <= 0.5) { return false; } else { return true; } }