Beispiel #1
0
        public void OnTrainNeuralNetwork()
        {
            if (LocalImageCollection.Count == 0)
            {
                MessageBox.Show("Немає зображень в локальному списку для тестування!", "Додайте зображення");
                return;
            }

            this.GraphData.ClearDataCollection();

            trainingTaskCancellationToken = new CancellationTokenSource();
            trainingTaskCancellationToken.Token.Register(() =>
            {
            });
            if (NeuralNetwork == null)
            {
                MessageBox.Show("Нейронна мережа несумісна", "Помилка нейронної мережі");
                return;
            }
            UpdateParametersFromNeuralNetwork();
            this.TrainingNeuralNetwork = true;
            var  trainingProgressMessage = "Тренування в прогресі. Будь ласка, зачекайте... ";
            Task task = new Task(() => {
                App.Current.Dispatcher.Invoke(() =>
                {
                    this.StatusMessage         = trainingProgressMessage;
                    this.TrainingNeuralNetwork = true;
                    foreach (var localImage in LocalImageCollection)
                    {
                        localImage.TrainingInProgress = true;
                        localImage.TestFailed         = false;
                        localImage.TestPassed         = false;
                    }
                });

                var imagesOnEachPage = NeuralNetwork.GetBatchSubSize();
                var totalPages       = (int)Math.Ceiling((double)LocalImageCollection.Count / (double)imagesOnEachPage);

                foreach (var mainIteration in Enumerable.Range(1, MaxIterations))
                {
                    double avrageIterationError = 0;
                    App.Current.Dispatcher.Invoke(() =>
                    {
                        Randomizer.Shuffle(LocalImageCollection);
                    });

                    foreach (var currentPage in Enumerable.Range(1, totalPages))
                    {
                        double averagePageError             = 0;
                        var currentPageImages               = LocalImageCollection.ToPagedList(currentPage, imagesOnEachPage);
                        (var batchInputs, var batchOutputs) = GetBatchDataFromImages(currentPageImages);

                        int batchTrainCounter = 0;
                        NeuralNetwork.BatchTrain(batchInputs, batchOutputs, MaxIterationsOnEachPage, (currentError, pageIteration, message) => {
                            this.StatusMessage = $"{trainingProgressMessage} Повідомлення: {message}; Ітерація: {mainIteration}; Сторінка: {currentPage}/{totalPages}; Ітерація сторінки: {pageIteration}; Помилка: {currentError}";
                            foreach (var localImage in currentPageImages)
                            {
                                localImage.TrainedIterations  = (pageIteration) * (mainIteration);
                                localImage.TrainingInProgress = true;
                            }
                            if (currentError <= StopPageTrainingErrorRate || pageIteration > MaxIterationsOnEachPage)
                            {
                                NeuralNetwork.StopBatchTraining();
                            }
                            GraphData.AddDataPoint(currentError);
                            averagePageError += currentError;
                            batchTrainCounter++;
                        });
                        averagePageError /= (double)batchTrainCounter;

                        GraphData.AddPageChanged();
                        avrageIterationError += averagePageError;
                        GC.Collect();
                    }
                    GraphData.AddIterationChanged();

                    avrageIterationError /= totalPages;
                    if (avrageIterationError < StopIterationTrainingErrorRate)
                    {
                        break;
                    }
                }

                App.Current.Dispatcher.Invoke(() =>
                {
                    this.StatusMessage = trainingProgressMessage;
                });
            }, trainingTaskCancellationToken.Token);

            task.Start();
            task.ContinueWith((t) => {
                App.Current.Dispatcher.Invoke(() =>
                {
                    this.StatusMessage         = "Готово";
                    this.TrainingNeuralNetwork = false;
                    foreach (var localImage in LocalImageCollection)
                    {
                        localImage.TrainingInProgress = false;
                    }
                });
            });
        }
Beispiel #2
0
        public void OnTrainNeuralNetwork()
        {
            if (LocalImageCollection.Count == 0)
            {
                MessageBox.Show("No Images in Local Image List to Train With!", "Add Local Images");
                return;
            }

            this.GraphData.ClearDataCollection();

            trainingTaskCancellationToken = new CancellationTokenSource();
            trainingTaskCancellationToken.Token.Register(() =>
            {
            });
            if (NeuralNetwork == null)
            {
                MessageBox.Show("Neural Network Incompatible", "Error in Neural Network");
                return;
            }
            UpdateParametersFromNeuralNetwork();
            this.TrainingNeuralNetwork = true;
            var  trainingProgressMessage = "Training in Progress. Please Wait... ";
            Task task = new Task(() => {
                App.Current.Dispatcher.Invoke(() =>
                {
                    this.StatusMessage         = trainingProgressMessage;
                    this.TrainingNeuralNetwork = true;
                    foreach (var localImage in LocalImageCollection)
                    {
                        localImage.TrainingInProgress = true;
                        localImage.TestFailed         = false;
                        localImage.TestPassed         = false;
                    }
                });

                var imagesOnEachPage = NeuralNetwork.GetBatchSubSize();
                var totalPages       = (int)Math.Ceiling((double)LocalImageCollection.Count / (double)imagesOnEachPage);

                foreach (var mainIteration in Enumerable.Range(1, MaxIterations))
                {
                    double avrageIterationError = 0;
                    App.Current.Dispatcher.Invoke(() =>
                    {
                        Randomizer.Shuffle(LocalImageCollection);
                    });

                    foreach (var currentPage in Enumerable.Range(1, totalPages))
                    {
                        double averagePageError             = 0;
                        var currentPageImages               = LocalImageCollection.ToPagedList(currentPage, imagesOnEachPage);
                        (var batchInputs, var batchOutputs) = GetBatchDataFromImages(currentPageImages);

                        int batchTrainCounter = 0;
                        NeuralNetwork.BatchTrain(batchInputs, batchOutputs, MaxIterationsOnEachPage, (currentError, pageIteration, message) => {
                            this.StatusMessage = $"{trainingProgressMessage} Message: {message}; Iteration: {mainIteration}; Page: {currentPage}/{totalPages}; Page Iteration: {pageIteration}; Error: {currentError}";
                            foreach (var localImage in currentPageImages)
                            {
                                localImage.TrainedIterations  = (pageIteration) * (mainIteration);
                                localImage.TrainingInProgress = true;
                            }
                            if (currentError <= StopPageTrainingErrorRate || pageIteration > MaxIterationsOnEachPage)
                            {
                                NeuralNetwork.StopBatchTraining();
                            }
                            GraphData.AddDataPoint(currentError);
                            averagePageError += currentError;
                            batchTrainCounter++;
                        });
                        averagePageError /= (double)batchTrainCounter;

                        GraphData.AddPageChanged();
                        avrageIterationError += averagePageError;
                        GC.Collect();
                    }
                    GraphData.AddIterationChanged();

                    avrageIterationError /= totalPages;
                    if (avrageIterationError < StopIterationTrainingErrorRate)
                    {
                        break;
                    }
                }

                App.Current.Dispatcher.Invoke(() =>
                {
                    this.StatusMessage = trainingProgressMessage;
                });
            }, trainingTaskCancellationToken.Token);

            task.Start();
            task.ContinueWith((t) => {
                App.Current.Dispatcher.Invoke(() =>
                {
                    this.StatusMessage         = "Ready";
                    this.TrainingNeuralNetwork = false;
                    foreach (var localImage in LocalImageCollection)
                    {
                        localImage.TrainingInProgress = false;
                    }
                });
            });
        }