/// <summary> /// Project object to zip stream /// </summary> /// <param name="cnnProject">The CNNProject</param> /// <param name="zip">The zip stream</param> /// <param name="fileName">Name of the file within the zip archive</param> public static void ProjectToZip(CNNProject cnnProject, ZipOutputStream zip, string fileName) { byte[] buffer; using (MemoryStream stream = new MemoryStream()) { new SoapFormatter().Serialize(stream, cnnProject); buffer = stream.GetBuffer(); } PutZipEntry(zip, buffer, fileName); }
/// <summary> /// Opens a project from file /// </summary> public void Open() { try { // stops the training, if it is still running CNNProject.ImgDetectionNN.StopTraining = true; CNNProject.ImgDetectionNN.StopTrainingSilently = true; OpenFileDialog dialog = new OpenFileDialog(); dialog.SupportMultiDottedExtensions = true; dialog.Filter = this.DefaultProjectFileFilter; dialog.DefaultExt = this.DefaultProjectFileExt; dialog.FileName = "*." + this.DefaultProjectFileExt; if (dialog.ShowDialog() == DialogResult.OK) { Stream fileStream = File.OpenRead(dialog.FileName); ZipInputStream zip = new ZipInputStream(fileStream); // temp vars List<TmpImageListItem> tmpImageList = new List<TmpImageListItem>(); CNNProject tmpCNNProject = null; INeuralNetwork tmpNetwork = null; ZipEntry entry; while ((entry = zip.GetNextEntry()) != null) { ZipFileEntry zipFileEntry = new ZipFileEntry(zip, entry); if (zipFileEntry.IsImage) { tmpImageList.Add(new TmpImageListItem(zipFileEntry.GetImage(), zipFileEntry.FileNameWithoutExtension, zipFileEntry.DirectoryName)); } else if (zipFileEntry.FileName == this._internZipProjectFileName) { tmpCNNProject = zipFileEntry.GetProject(); } else if (zipFileEntry.FileName == this.DefaultNetworkFileName) { tmpNetwork = zipFileEntry.GetNetwork(); } } zip.Close(); fileStream.Close(); if (tmpCNNProject != null && tmpNetwork != null) { // now we have everything together, so we can rebuild the project _cnnProject = tmpCNNProject; _cnnProject.ResetNonSerializableAttributes(); _cnnProject.ImgDetectionNN.Network = tmpNetwork; // adds images foreach (TmpImageListItem item in tmpImageList) { if (item.Directory == "Matching") { _cnnProject.Matching.Images.Add(item.Name, item.Image); } else if (item.Directory == "NotMatching") { _cnnProject.NotMatching.Images.Add(item.Name, item.Image); } } // fires the event if (ProjectChanged != null) { ProjectChanged(this, new EventArgs()); } } else { MessageBox.Show("This zip file does not contains all necessary items!", "Zip Project File Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (NotImplementedException ex) { StaticClasses.ShowException(ex); } }
public CNNProjectHolder() { _cnnProject = new CNNProject(); }
/// <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 }