Example #1
0
        /// <summary>
        /// Выполнить обучение.
        /// </summary>
        private static void DoLearnWork()
        {
            Console.BackgroundColor = ConsoleColor.Green;
            Console.ForegroundColor = ConsoleColor.Black;

            Console.WriteLine(ConsoleMessageConstants.LEARN_RESULT_MESSAGE);

            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Green;

            var pathToFiles     = GetPathToFiles();
            var pathToResources = PathHelper.GetResourcesPath();

            if (string.IsNullOrEmpty(pathToFiles))
            {
                pathToFiles = pathToResources;
            }

            var images = Directory.GetFiles(pathToFiles).ToList();

            var converter            = new ImageConverterUtil(images);
            var listOfPicturesMatrix = converter.ConvertImagesToMatrix();

            var iterationsCount = listOfPicturesMatrix.Count;

            var configuration = new Configuration
            {
                Alpha                  = 3.1,
                Epsilon                = 3.1,
                EpochCount             = 3,
                IdealResult            = 1,
                IterationsInEpochCount = iterationsCount
            };

            var layers     = new List <Layer>();
            var filterCore = FilterCoreModel.Initialize();

            LayersInitialize(listOfPicturesMatrix, layers, filterCore,
                             out Dictionary <string, double> inputLayerWeights,
                             out List <NeuronModel> convolutionalLayerNeurons,
                             out List <NeuronModel> hiddenLayerNeurons,
                             out NeuronModel outputNeuron);

            var learningUtil = new LearningUtil(layers, configuration, listOfPicturesMatrix);

            learningUtil.StartToLearn();
        }
Example #2
0
        /// <summary>
        /// Распознать изображение.
        /// </summary>
        /// <param name="data">Данные весов.</param>
        private static void RecognizeImage(Dictionary <BL.Enums.WeightsType, Dictionary <int, List <double> > > data)
        {
            Console.WriteLine(ConsoleMessageConstants.PATH_MESSAGE);

            var pathToImages = Console.ReadLine();

            if (string.IsNullOrEmpty(pathToImages))
            {
                pathToImages = PathHelper.GetResourcesPath();
            }

            if (!Directory.Exists(pathToImages))
            {
                ErrorHelper.DirectoryError();
            }

            var filesInDirectory = Directory.GetFiles(pathToImages).ToList();

            var imagesInDirectory = filesInDirectory.FindAll(file =>
                                                             file.Contains(FileConstants.IMAGE_EXTENSION));

            ErrorHelper.CheckFiles(imagesInDirectory);

            var imageName = string.Empty;
            var breakFlag = false;

            do
            {
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.Black;

                Console.WriteLine(ConsoleMessageConstants.IMAGES_IN_DIRECTORY_MESSAGE);

                imagesInDirectory.ForEach(imagePath =>
                                          Console.WriteLine(Path.GetFileNameWithoutExtension(imagePath) + " "));

                Console.Write(ConsoleMessageConstants.FILE_NAME_MESSAGE);
                imageName = Console.ReadLine();

                if (imagesInDirectory.Contains(Path.Combine(pathToImages, imageName + FileConstants.IMAGE_EXTENSION)))
                {
                    breakFlag = true;
                }

                Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.Green;
            } while (!breakFlag);

            var fullPath = Path.Combine(pathToImages, $"{imageName}{FileConstants.IMAGE_EXTENSION}");

            if (!data.TryGetValue(BL.Enums.WeightsType.Hidden, out var hiddenLayerWeights))
            {
                ErrorHelper.GetDataError();
            }

            if (!data.TryGetValue(BL.Enums.WeightsType.Output, out var outputLayerWeights))
            {
                ErrorHelper.GetDataError();
            }

            var imageConverterUtil = new ImageConverterUtil(new List <string> {
                fullPath
            });
            var imageMatrixList = imageConverterUtil.ConvertImagesToMatrix();

            var imageData = imageMatrixList.FirstOrDefault();

            if (imageData == null)
            {
                ErrorHelper.GetDataError();
            }

            var recognizeUtil = new RecognizeUtil(outputLayerWeights, hiddenLayerWeights, imageData);

            recognizeUtil.InitializeNetwork();
        }