Example #1
0
        public static MnistModel ReadImage(string path, bool normalize, bool trainAsEncoder)
        {
            var bitmap = new Bitmap(path);

            var fileName = path.Substring(path.LastIndexOfAny(FileSeparators) + 1);
            var label    = int.Parse(fileName[0].ToString());

            var values = new DenseVector(bitmap.Height * bitmap.Width);

            for (var i = 0; i < bitmap.Height; i++)
            {
                for (var j = 0; j < bitmap.Width; j++)
                {
                    {
                        var color = bitmap.GetPixel(j, i);

                        var gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);

                        values[j + i * bitmap.Width] = 1 - gray / 255.0;
                    }
                }
            }

            if (normalize)
            {
                var max = values.Maximum();
                max /= 2;
                values.MapInplace(v => v - max);
            }

            //values.CoerceZero(0.005);
            //values.MapInplace(v => v > 0.995 ? 1 : v);

            Vector solution;

            if (trainAsEncoder)
            {
                solution = values;
            }
            else
            {
                solution = new DenseVector(10)
                {
                    [label] = 1.0
                };
            }

            var image = new MnistModel
            {
                Width            = bitmap.Width,
                Height           = bitmap.Height,
                Values           = values,
                FileName         = fileName,
                Label            = label,
                ExpectedSolution = solution
            };

            return(image);
        }
Example #2
0
        public static string Dump(MnistModel image)
        {
            var sb = new StringBuilder();

            sb.Append(Print(image.Values, image.Width));
            sb.AppendLine($"Filename - {image.FileName}");
            sb.AppendLine($"Label - {image.Label}");
            sb.AppendLine($"Width - {image.Width}");
            sb.Append($"Height - {image.Height}");

            return(sb.ToString());
        }
Example #3
0
        public static MnistModel[] ReadAll(string pathToDirectory, bool normalize, bool trainAsEncoder)
        {
            var directoryInfo = new DirectoryInfo(pathToDirectory);

            var files  = directoryInfo.GetFiles("*.png");
            var count  = files.Length;
            var models = new MnistModel[count];

            for (var i = 0; i < files.Length; i++)
            {
                models[i] = ReadImage(files[i].FullName, normalize, trainAsEncoder);
            }

            return(models);
        }