Exemplo n.º 1
0
		public void PrepareTrainingData(TrainingRate learningParameters, int epoch, ref int index, ref bool stop)
		{
			if (learningParameters.Distorted)
			{
				if (((epoch-1) % learningParameters.SameDistortionsForNEpochs) == 0)
				{
					bool parameterChanged = false;

					if (SeverityFactor != learningParameters.SeverityFactor)
					{
						SeverityFactor = learningParameters.SeverityFactor;
						parameterChanged = true;
					}

					if (MaxScaling != learningParameters.MaxScaling)
					{
						MaxScaling = learningParameters.MaxScaling;
						parameterChanged = true;
					}

					if (MaxRotation != learningParameters.MaxRotation)
					{
						MaxRotation = learningParameters.MaxRotation;
						parameterChanged = true;
					}

					if (ElasticSigma != learningParameters.ElasticSigma)
					{
						ElasticSigma = learningParameters.ElasticSigma;
						parameterChanged = true;
					}

					if (ElasticScaling != learningParameters.ElasticScaling)
					{
						ElasticScaling = learningParameters.ElasticScaling;
						parameterChanged = true;
					}
					
					if (parameterChanged)
						CalculateGaussianKernel();

					PreparedTrainingPatterns = new DoubleImageData[TrainingPatternsCount];
					for (index = 0; index < TrainingPatternsCount; index++)
					{
						if (stop)
							break;

						PreparedTrainingPatterns[index] =  new DoubleImageData(TrainingPatterns[index].Label, GetDistortedPattern(index, true));
					}
				}
			}
			else
			{
				PreparedTrainingPatterns = TrainingPatterns;
			}
		}
Exemplo n.º 2
0
        private void Calculate()
        {
            (MainView.DataContext as PageViewModel).CommandToolBar[0].IsEnabled = false;
            calculatePageViewModel.IsValid = false;
            MainView.Status.Text = "Calculating...";

            Mouse.OverrideCursor = Cursors.Wait;
            DoubleImageData pattern;

            if (calculatePageViewModel.UseTrainingSet)
            {
                if (calculatePageViewModel.UseDistortions)
                {
                    pageViewModel.DataProvider.SeverityFactor = calculatePageViewModel.SeverityFactor;
                    pageViewModel.DataProvider.MaxRotation = calculatePageViewModel.MaxRotation;
                    pageViewModel.DataProvider.MaxScaling = calculatePageViewModel.MaxScaling;
                    pageViewModel.DataProvider.ElasticScaling = calculatePageViewModel.ElasticScaling;
                    pageViewModel.DataProvider.ElasticSigma = calculatePageViewModel.ElasticSigma;
                    pageViewModel.DataProvider.CalculateGaussianKernel();

                    pattern = new DoubleImageData(pageViewModel.DataProvider.TrainingPatterns[calculatePageViewModel.PatternIndex].Label, pageViewModel.DataProvider.GetDistortedPattern(calculatePageViewModel.PatternIndex, true));
                }
                else
                    pattern = pageViewModel.DataProvider.TrainingPatterns[calculatePageViewModel.PatternIndex];
            }
            else
            {
                if (calculatePageViewModel.UseDistortions)
                {
                    pageViewModel.DataProvider.SeverityFactor = calculatePageViewModel.SeverityFactor;
                    pageViewModel.DataProvider.MaxRotation = calculatePageViewModel.MaxRotation;
                    pageViewModel.DataProvider.MaxScaling = calculatePageViewModel.MaxScaling;
                    pageViewModel.DataProvider.ElasticScaling = calculatePageViewModel.ElasticScaling;
                    pageViewModel.DataProvider.ElasticSigma = calculatePageViewModel.ElasticSigma;
                    pageViewModel.DataProvider.CalculateGaussianKernel();

                    pattern = new DoubleImageData(pageViewModel.DataProvider.TestingPatterns[calculatePageViewModel.PatternIndex].Label, pageViewModel.DataProvider.GetDistortedPattern(calculatePageViewModel.PatternIndex, false));
                }
                else
                    pattern = pageViewModel.DataProvider.TestingPatterns[calculatePageViewModel.PatternIndex];
            }

            Layers firstLayer = pageViewModel.NeuralNetwork.Layers.First();
            for (int i = 0; i < pageViewModel.DataProvider.PatternSize; ++i)
            {
                //pattern[i] = ((pattern[i] * 0.6375D) + 0.5375D); // normalization (-0.1 --> 1.175)
                firstLayer.Neurons[i].Output = pattern.Image[i];
            }

            string result = String.Empty;
            result += "Input: " + pattern.Label.ToString() + "\r\n";

            int bestIndex = 0;
            System.Diagnostics.Stopwatch timer = new Stopwatch();
            timer.Restart();
            pageViewModel.NeuralNetwork.Calculate();
            bestIndex = Recognised();
            timer.Stop();

            result += "\r\n";
            Layers lastLayer = pageViewModel.NeuralNetwork.Layers.Last();
            for (int i = 0; i < lastLayer.NeuronCount; i++)
            {
                result += i.ToString() + " ==> " + lastLayer.Neurons[i].Output.ToString("N17") + "\r\n";
            }
            result += "\r\n";

            double patternMSE = GetPatternMSE(pattern.Label);
            result += "Pattern MSE: " + patternMSE.ToString("N17") + "\r\n";

            result += "Highest prob: " + bestIndex.ToString() + "\r\n";
            if (bestIndex == pattern.Label)
                result += "Correctly recognized";
            else
                result += "Not correctly recognized";

            result += "\r\nTime: " + timer.ElapsedMilliseconds.ToString() + " ms.";

            calculatePageViewModel.Result = result;

            calculatePageViewModel.TabItems = new ObservableCollection<TabItem>();

            foreach (Layers layer in pageViewModel.NeuralNetwork.Layers)
            {
                if (layer.UseMapInfo)
                {
                    TabItem tb = new TabItem();
                    tb.BeginInit();
                    tb.UseLayoutRounding = true;
                    tb.SnapsToDevicePixels = true;
                    tb.Header = layer.LayerIndex.ToString();
                    tb.Content = layer.GetMappedLayerOutputs();
                    tb.EndInit();
                    calculatePageViewModel.TabItems.Add(tb);
                }
            }

            Mouse.OverrideCursor = null;

            MainView.Status.Text = "Ready";
            calculatePageViewModel.IsValid = true;
            (MainView.DataContext as PageViewModel).CommandToolBar[0].IsEnabled = true;
        }
Exemplo n.º 3
0
		public void GenerateTrainingData(bool flipGrayscale, int width, int height)
		{
			FlipGrayscale = flipGrayscale;
			PatternWidth = width;
			PatternHeight = height;
			PatternSize = width * height;
			patternSize = PatternSize;

			if (FlipGrayscale)
			{
				Background = 1;
				Foreground = -1;
			}
			else
			{
				Background = -1;
				Foreground = 1;
			}

			int xOffset = (PatternWidth - MNistWidth) / 2;
			int yOffset = (PatternHeight - MNistHeight) / 2;
			Divisor = 255D / (Math.Abs(Background) + Math.Abs(Foreground));
			
			TrainingPatterns = new DoubleImageData[TrainingPatternsCount];
			RandomTrainingPattern = new int[TrainingPatternsCount];

			Parallel.For(0, TrainingPatternsCount, parallelOption, i =>
			{
				DoubleImageData pattern = new DoubleImageData(i, new double[PatternSize]);
				for (int j = 0; j < PatternSize; j++)
				{
					pattern.Image[j] = Background;
				}
				TrainingPatterns[i] = pattern;

				RandomTrainingPattern[i] = i;
			});

			Parallel.For(0, TrainingPatternsCount, parallelOption, i =>
			{
				byte temp;
				DoubleImageData trainingPattern = TrainingPatterns[i];
				byte[] image = MNISTTraining[i].Image;
				trainingPattern.Label = MNISTTraining[i].Label;
				
				for (int y = 0; y < MNistHeight; y++)
				{
					for (int x = 0; x < MNistWidth; x++)
					{
						temp = image[x + (y * MNistWidth)];
						{
							trainingPattern.Image[(x + xOffset) + ((y + yOffset) * PatternWidth)] = ((((double)temp) / Divisor) - 1D);
						}
					}
				}

				TrainingPatterns[i] = trainingPattern;
			});

			MNISTTraining = null;
			TestingPatterns = new DoubleImageData[TestingPatternsCount];
			Parallel.For(0, TestingPatternsCount, parallelOption, i =>
			{
				DoubleImageData pattern = new DoubleImageData(i, new double[PatternSize]);
				for (int j = 0; j < PatternSize; j++)
				{
					pattern.Image[j] = Background;
				}
				TestingPatterns[i] = pattern;
			});

			Parallel.For(0, TestingPatternsCount, parallelOption, i =>
			{
				byte temp;
				DoubleImageData testingPattern = TestingPatterns[i];
				byte[] image = MNISTTesting[i].Image;
				testingPattern.Label = MNISTTesting[i].Label;
				
				for (int y = 0; y < MNistHeight; y++)
				{
					for (int x = 0; x < MNistWidth; x++)
					{
						temp = image[x + (y * MNistWidth)];
						{
							testingPattern.Image[(x + xOffset) + ((y + yOffset) * PatternWidth)] = ((((double)temp / Divisor)) - 1D);
						}
					}
				}

				TestingPatterns[i] = testingPattern;
			});

			MNISTTesting = null;
		}
Exemplo n.º 4
0
        private int SetTestingPattern(int index)
        {
            if (pageViewModel.CurrentContext ==  ContextTypes.Training)
            {
                // Training PageView
                Layers inputLayer = pageViewModel.NeuralNetwork.Layers.First();

                for (int i = 0; i < pageViewModel.DataProvider.PatternSize; i++)
                {
                    inputLayer.Neurons[i].Output = pageViewModel.DataProvider.TestingPatterns[index].Image[i];
                }

                return (pageViewModel.DataProvider.TestingPatterns[index].Label);
            }
            else
            {
                // Testing PageView
                DoubleImageData pattern;

                if (!testingPageViewModel.UseTrainingSet)
                {
                    if (testingPageViewModel.UseDistortions)
                       pattern = new DoubleImageData(pageViewModel.DataProvider.TestingPatterns[index].Label, pageViewModel.DataProvider.GetDistortedPattern(index, false));
                    else
                        pattern = pageViewModel.DataProvider.TestingPatterns[index];
                }
                else
                {
                    if (testingPageViewModel.UseDistortions)
                        pattern = new DoubleImageData(pageViewModel.DataProvider.TrainingPatterns[index].Label, pageViewModel.DataProvider.GetDistortedPattern(index, true));
                    else
                        pattern = pageViewModel.DataProvider.TrainingPatterns[index];
                }

                Layers inputLayer = pageViewModel.NeuralNetwork.Layers.First();
                for (int i = 0; i < pageViewModel.DataProvider.PatternSize; i++)
                {
                    inputLayer.Neurons[i].Output = pattern.Image[i];
                }

                return (pattern.Label);
            }
        }