public static Dataset Create() { Dataset result = new Dataset(); Bitmap bmp = new Bitmap(width, height); StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; float size = 8; Rectangle rect = new Rectangle(0, 0, width, height); using (Graphics g = Graphics.FromImage(bmp)) { g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; for (int x = 0; x < 100; ++x) { // var fonts = fontsToTrain.Shuffle().ToList(); // foreach (var f in fonts) { var ai = Enumerable.Range(0, alphabet.Length).Shuffle().ToList(); // for (int i = 0; i < alphabet.Length; ++i) foreach (var i in ai) { var f = fontsToTrain[Tools.rnd.Next(fontsToTrain.Length)]; size = 9.0f + (float)Tools.rnd.NextDouble() * 4.0f; using (Font font = new Font(f, size)) { var a = alphabet[i]; g.Clear(Color.Black); g.TextRenderingHint = TextRenderingHint.AntiAlias; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.TranslateTransform(width / 2, height / 2); g.RotateTransform(((float)Tools.rnd.NextDouble() - 0.5f) * 45); g.TranslateTransform(-width / 2, -height / 2); var dx = ((float)Tools.rnd.NextDouble() - 0.5f) * 2.0f; var dy = ((float)Tools.rnd.NextDouble() - 0.5f) * 2.0f; g.TranslateTransform(dx, dy); g.DrawString(a, font, Brushes.White, rect, stringFormat); g.ResetTransform(); var inp = Tools.bmp_to_float(bmp); var outp = new float[alphabet.Length]; outp[i] = 1.0f; result.AddPair(inp, outp); } } } } } result.VisualizeInput = visualizeInput; result.VisualizeOutput = visualizeOutput; return(result); }
public static Dataset Create() { Dataset result = new Dataset(); Bitmap bmp = new Bitmap(width, height); StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; float size = 8; Rectangle rect = new Rectangle(0, 0, width, height); using (Graphics g = Graphics.FromImage(bmp)) { g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; for (int x = 0; x < 100; ++x) { // var fonts = fontsToTrain.Shuffle().ToList(); // foreach (var f in fonts) { var ai = Enumerable.Range(0, alphabet.Length).Shuffle().ToList(); // for (int i = 0; i < alphabet.Length; ++i) foreach (var i in ai) { var f = fontsToTrain[Tools.rnd.Next(fontsToTrain.Length)]; size = 9.0f + (float)Tools.rnd.NextDouble() * 4.0f; using (Font font = new Font(f, size)) { var a = alphabet[i]; g.Clear(Color.Black); g.TextRenderingHint = TextRenderingHint.AntiAlias; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.TranslateTransform(width / 2, height / 2); g.RotateTransform(((float)Tools.rnd.NextDouble()-0.5f) * 45); g.TranslateTransform(-width / 2, -height / 2); var dx = ((float)Tools.rnd.NextDouble() - 0.5f) * 2.0f; var dy = ((float)Tools.rnd.NextDouble() - 0.5f) * 2.0f; g.TranslateTransform(dx, dy); g.DrawString(a, font, Brushes.White, rect, stringFormat); g.ResetTransform(); var inp = Tools.bmp_to_float(bmp); var outp = new float[alphabet.Length]; outp[i] = 1.0f; result.AddPair(inp, outp); } } } } } result.VisualizeInput = visualizeInput; result.VisualizeOutput = visualizeOutput; return result; }
private static void addData(float[] input, float[] output, Dataset data) { Monitor.Enter(data); if (data.input.Count % 1000 == 0) { Console.WriteLine("{0}", data.input.Count); } data.AddPair(input, output); Monitor.Exit(data); }
public static Dataset LoadResultData() { Dataset result = new Dataset(); var reader = new StreamReader(Path.Combine(dataPath, "test.csv")); // ignore first line var line = reader.ReadLine(); while ((line = reader.ReadLine()) != null) { var ls = line.Split(','); int pos = 0; var output = default(float[]); var pic = ls.Last().Split(' '); var img = new float[inputSize]; for (int j = 0; j < inputSize; ++j) { img[j] = float.Parse(pic[j]) / 255.0f; } var input = default(float[]); using (var bmp = Tools.float_to_bmp(img, origImgSize, origImgSize)) { using (var filtered = applyFilters(bmp)) { input = Tools.bmp_to_float(filtered); } } result.AddPair(input, output); } return(result); }
public static Dataset LoadTrainData() { Dataset result = new Dataset(); var net = new PragmaLearn.Learner.BackpropNeuralNetwork(); // net.Open("network.dat"); var reader = new StreamReader(Path.Combine(dataPath, "training.csv")); // ignore first line var line = reader.ReadLine(); while ((line = reader.ReadLine()) != null) { var ls = line.Split(','); bool skipped = false; var keypoints = new double[outputSize]; for (int i = 0; i < outputSize; ++i) { if (string.IsNullOrEmpty(ls[i])) { Console.Write("."); keypoints[i] = -1; // output[i] = Tools.rnd.NextDouble(); skipped = true; // break; } else { keypoints[i] = double.Parse(ls[i]); } } //if (skipped) // continue; var pic = ls.Last().Split(' '); var img = new float[inputSize]; for (int j = 0; j < inputSize; ++j) { img[j] = float.Parse(pic[j]) / 255.0f; } //if (skipped) //{ // using (var bmp = Tools.double_to_bmp(img, origImgSize, origImgSize)) // { // using (var filtered = applyFilters(bmp)) // { // var input = Tools.bmp_to_double(filtered); // var p = net.Predict(input); // for (int i = 0; i < keypoints.Length; ++i) // { // if (keypoints[i] == -1) // keypoints[i] = p[i] * origImgSize; // } // } // } //} for (int x = 0; x < 20; ++x) { var points = new PointF[keypoints.Length / 2]; for (int i = 0; i < keypoints.Length; i += 2) { points[i / 2] = new PointF((float)keypoints[i], (float)keypoints[i + 1]); } var input = default(float[]); using (var bmp = Tools.float_to_bmp(img, origImgSize, origImgSize)) { if (x != 0) { using (var transformed = transformRandom(bmp, points)) { using (var filtered = applyFilters(transformed)) { input = Tools.bmp_to_float(filtered); } } } else { using (var filtered = applyFilters(bmp)) { input = Tools.bmp_to_float(filtered); } } } var output = new float[keypoints.Length]; for (int i = 0; i < points.Length; i++) { output[2 * i] = points[i].X != -1 ? points[i].X / origImgSize : -1; output[2 * i + 1] = points[i].Y != -1 ? points[i].Y / origImgSize : -1; } result.AddPair(input, output); } //if (skipped) //{ // var p = net.Predict(input); // for (int i = 0; i < output.Length; ++i) // { // if (output[i] == -1) // output[i] = p[i]; // } //} } reader.Dispose(); return(result); }