diff options
author | matthewsotoudeh <matthewsot@outlook.com> | 2016-04-12 16:47:06 -0700 |
---|---|---|
committer | matthewsotoudeh <matthewsot@outlook.com> | 2016-04-12 16:47:06 -0700 |
commit | b0cea92dd1fcd0ee325069652f5598b4a783a99c (patch) | |
tree | 7da7d1ae7cc825fc953d0221c2e60b4d75cad867 | |
parent | 53236277587cf2dcaaeafec83702e678d47ec95f (diff) |
supports 'maxTrainingSets' in the nimg.config
also uses doubles correctly
-rw-r--r-- | NImg/NImg/Compressor.cs | 10 | ||||
-rw-r--r-- | NImg/NImg/Loader.cs | 21 | ||||
-rw-r--r-- | NImg/NImg/Optimizer.cs | 6 | ||||
-rw-r--r-- | NImg/NImg/Program.cs | 13 | ||||
-rw-r--r-- | NImg/NImg/Reconstructor.cs | 18 | ||||
-rw-r--r-- | NImg/NImg/nimg.config | 3 |
6 files changed, 52 insertions, 19 deletions
diff --git a/NImg/NImg/Compressor.cs b/NImg/NImg/Compressor.cs index ad80436..de04bb9 100644 --- a/NImg/NImg/Compressor.cs +++ b/NImg/NImg/Compressor.cs @@ -22,6 +22,7 @@ namespace NImg var writeTolerance = 10; var colorIndexTolerance = 5; var trainingRounds = 5; + var maxTrainingSets = -1; if (File.Exists("nimg.config")) { @@ -54,12 +55,15 @@ namespace NImg case "trainingRounds": trainingRounds = int.Parse(parts[1]); break; + case "maxTrainingSets": + maxTrainingSets = int.Parse(parts[1]); + break; } } } } - var trainingSets = Loader.LoadTrainingSets(files, inputPixels); + var trainingSets = Loader.LoadTrainingSets(files, inputPixels, maxTrainingSets); Network network = new Network(inputPixels * 3, innerLayers, neuronsPerLayer, 3); var biasNeurons = 1; @@ -70,7 +74,7 @@ namespace NImg Writer.WriteWeights(network, inputPixels, innerLayers, neuronsPerLayer, biasNeurons); using (var originalImage = new Bitmap(file)) - { + { using (var writer = new BinaryWriter(new FileStream(@"Output\image.data", FileMode.Create))) { using (var colorsWriter = new BinaryWriter(new FileStream(@"Output\colors.list", FileMode.Create))) @@ -239,8 +243,6 @@ namespace NImg Console.WriteLine("Original Size: " + originalSize + " New Size: " + newSize); Console.WriteLine("Reduction: " + ((double)100 - ((newSize * (double)100) / originalSize)) + "%"); - - Console.ReadLine(); } } } diff --git a/NImg/NImg/Loader.cs b/NImg/NImg/Loader.cs index a434c9c..2383d96 100644 --- a/NImg/NImg/Loader.cs +++ b/NImg/NImg/Loader.cs @@ -6,7 +6,7 @@ namespace NImg { static class Loader { - public static TrainingSet[] LoadTrainingSets(string[] paths, int inputPixels = 3) + public static TrainingSet[] LoadTrainingSets(string[] paths, int inputPixels = 3, int maxTrainingSets = -1) { var trainingSets = new List<TrainingSet>(); foreach (var path in paths) @@ -14,13 +14,14 @@ namespace NImg // Load the unnormalized data in using (var image = new Bitmap(path)) { + var sets = 0; for (var yPixel = 0; yPixel < image.Height; yPixel++) { var input = new double[inputPixels * 3]; for (var xPixel = 0; xPixel < image.Width; xPixel++) { var pixel = image.GetPixel(xPixel, yPixel); - var output = new double[] { (pixel.R / 255), (pixel.G / 255), (pixel.B / 255) }; + var output = new double[] { (double)pixel.R / 255, (double)pixel.G / 255, (double)pixel.B / 255 }; trainingSets.Add(new TrainingSet((double[])input.Clone(), output)); @@ -28,9 +29,19 @@ namespace NImg { input[i] = input[i + 3]; } - input[input.Length - 3] = pixel.R / 255; - input[input.Length - 2] = pixel.G / 255; - input[input.Length - 1] = pixel.B / 255; + input[input.Length - 3] = (double)pixel.R / 255; + input[input.Length - 2] = (double)pixel.G / 255; + input[input.Length - 1] = (double)pixel.B / 255; + + sets++; + if (maxTrainingSets > 0 && sets >= maxTrainingSets) + { + break; + } + } + if (maxTrainingSets > 0 && sets >= maxTrainingSets) + { + break; } } } diff --git a/NImg/NImg/Optimizer.cs b/NImg/NImg/Optimizer.cs index 6044c64..34fd249 100644 --- a/NImg/NImg/Optimizer.cs +++ b/NImg/NImg/Optimizer.cs @@ -26,12 +26,14 @@ namespace NImg int tries = 0; do { - Console.WriteLine("Optimizing..."); network.Weights = BackPropOptimizer.Optimize(network, trainingSets, 0.5, 1); var newError = BackPropOptimizer.Error(network, trainingSets, network.Weights); deltaDrop = error - newError; error = newError; - Console.WriteLine("Error from last optimization attempt: " + error); + if (tries % 10 == 0) + { + Console.WriteLine("Error from last optimization attempt: " + error); + } tries++; } while (tries < trainingRounds /*false || error > 10 || deltaDrop < 1*/); diff --git a/NImg/NImg/Program.cs b/NImg/NImg/Program.cs index aac8aa6..cd5c9e1 100644 --- a/NImg/NImg/Program.cs +++ b/NImg/NImg/Program.cs @@ -1,4 +1,6 @@ -namespace NImg +using System; + +namespace NImg { class Program { @@ -10,9 +12,16 @@ Compressor.Compress(args[1]); break; case "reconstruct": - Reconstructor.Reconstruct(args[1]); + if (args.Length >= 3 && args[2] == "demo") + { + Console.WriteLine("You've enabled demo mode, which will output the image with the network-predicted pixels highlighted in red."); + Reconstructor.Reconstruct(args[1], true); + break; + } + Reconstructor.Reconstruct(args[1], false); break; } + Console.ReadLine(); } } } diff --git a/NImg/NImg/Reconstructor.cs b/NImg/NImg/Reconstructor.cs index dcc32c5..ae136e6 100644 --- a/NImg/NImg/Reconstructor.cs +++ b/NImg/NImg/Reconstructor.cs @@ -9,7 +9,7 @@ namespace NImg { static class Reconstructor { - public static void Reconstruct(string path) + public static void Reconstruct(string path, bool demoMode) { var folderPath = path.Replace(".nimg", ".nimg-temp"); if (Directory.Exists(folderPath)) Directory.Delete(folderPath, true); @@ -81,6 +81,14 @@ namespace NImg var inARow = Convert.ToInt32(byteStr.Substring(4).PadLeft(8, '0'), 2); for (var i = 0; i < inARow; i++) { + if (demoMode) + { + reconstructedImage.SetPixel(xPixel, yPixel, + Color.FromArgb(255, 0, 0)); + xPixel++; + continue; + } + var output = network.Pulse(input); var roundedOutput = new int[] { (int)Math.Round(output[0] * 255), @@ -129,13 +137,13 @@ namespace NImg { input[j] = input[j + 3]; } - input[input.Length - 3] = colorToUse.R; - input[input.Length - 2] = colorToUse.G; - input[input.Length - 1] = colorToUse.B; + input[input.Length - 3] = (double)colorToUse.R / 255; + input[input.Length - 2] = (double)colorToUse.G / 255; + input[input.Length - 1] = (double)colorToUse.B / 255; } } } - reconstructedImage.Save(path.Replace(".nimg", ".png"), System.Drawing.Imaging.ImageFormat.Png); + reconstructedImage.Save(path.Replace(".nimg", ".reconstructed.png"), System.Drawing.Imaging.ImageFormat.Png); } } Directory.Delete(folderPath, true); diff --git a/NImg/NImg/nimg.config b/NImg/NImg/nimg.config index a7eaf79..d20b6aa 100644 --- a/NImg/NImg/nimg.config +++ b/NImg/NImg/nimg.config @@ -4,4 +4,5 @@ neuronsPerLayer 5 colorIndexBytes 2 writeTolerance 10 colorIndexTolerance 5 -trainingRounds 3
\ No newline at end of file +trainingRounds 50 +maxTrainingSets 5
\ No newline at end of file |