diff options
author | matthewsotoudeh <matthewsot@outlook.com> | 2016-04-12 17:58:36 -0700 |
---|---|---|
committer | matthewsotoudeh <matthewsot@outlook.com> | 2016-04-12 17:58:36 -0700 |
commit | 28c09ec5bc48463bb279cc82a97866f7bfc9c4f3 (patch) | |
tree | ca0f8e90bcbf90b10b4a0c1d6974784c4b9c0c13 | |
parent | 0c9bbfd7f5733cb04561250016fc0b5ed3fe7535 (diff) |
select training data randomly
-rw-r--r-- | NImg/NImg/Loader.cs | 29 | ||||
-rw-r--r-- | NImg/NImg/Zoltar/Optimizers/BruteOptimizer.cs | 2 |
2 files changed, 29 insertions, 2 deletions
diff --git a/NImg/NImg/Loader.cs b/NImg/NImg/Loader.cs index 2383d96..2d7f671 100644 --- a/NImg/NImg/Loader.cs +++ b/NImg/NImg/Loader.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Drawing; using Zoltar; @@ -15,6 +16,32 @@ namespace NImg using (var image = new Bitmap(path)) { var sets = 0; + if (maxTrainingSets != -1) + { + for (var i = 0; i < maxTrainingSets; i++) + { + var xPixel = BruteOptimizer.Random.Next(0, image.Width); + var yPixel = BruteOptimizer.Random.Next(0, image.Height); + var pixel = image.GetPixel(xPixel, yPixel); + + var output = new double[] { (double)pixel.R / 255, (double)pixel.G / 255, (double)pixel.B / 255 }; + + var input = new double[inputPixels * 3]; + + for (var x = (xPixel - 1); x >= Math.Max(0, xPixel - inputPixels); x--) + { + var thisPixel = image.GetPixel(x, yPixel); + var inputOffsetPixels = (xPixel - 1) - x; + input[input.Length - (inputOffsetPixels * 3) - 3] = (double)thisPixel.R / 255; + input[input.Length - (inputOffsetPixels * 3) - 2] = (double)thisPixel.G / 255; + input[input.Length - (inputOffsetPixels * 3) - 1] = (double)thisPixel.B / 255; + } + + trainingSets.Add(new TrainingSet(input, output)); + } + continue; + } + for (var yPixel = 0; yPixel < image.Height; yPixel++) { var input = new double[inputPixels * 3]; diff --git a/NImg/NImg/Zoltar/Optimizers/BruteOptimizer.cs b/NImg/NImg/Zoltar/Optimizers/BruteOptimizer.cs index aa1fd55..1cb6a85 100644 --- a/NImg/NImg/Zoltar/Optimizers/BruteOptimizer.cs +++ b/NImg/NImg/Zoltar/Optimizers/BruteOptimizer.cs @@ -4,7 +4,7 @@ namespace Zoltar { static class BruteOptimizer { - static Random Random = new Random(123); + public static Random Random = new Random(123); public static double Error(this Network network, TrainingSet trainingSet, double[][][] weights) { var outputs = network.Pulse(trainingSet.Inputs, weights); |