Skip to content

Instantly share code, notes, and snippets.

@jonny64bit
Created October 31, 2020 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonny64bit/a0e18cb99c0ee102ab0c69f0d77ca26d to your computer and use it in GitHub Desktop.
Save jonny64bit/a0e18cb99c0ee102ab0c69f0d77ca26d to your computer and use it in GitHub Desktop.
MINST Database
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
namespace LearningML.ImageViewer
{
class Program
{
static void Main(string[] args)
{
var images = MnistReader.ReadTrainingData();
foreach (var image in images)
{
var pixels = image.Data;
var bitmap = new Bitmap(28, 28);
for (int x = 0; x < 28; x++)
{
for (int y = 0; y < 28; y++)
{
bitmap.SetPixel(y, x, Color.FromArgb(255 - pixels[x, y], 255 - pixels[x, y], 255 - pixels[x, y])); //invert colors to have 0,0,0 be white as specified by mnist
}
}
bitmap.Save(@"C:\Users\Jonathandent\Desktop\LearningML\ExampleData\image." + image.Label + "." + Guid.NewGuid().ToString() + ".bmp");
}
var c = 0;
}
}
public static class MnistReader
{
private const string TrainImages = @"C:\Users\Jonathandent\Desktop\LearningML\train-images.idx3-ubyte";
private const string TrainLabels = @"C:\Users\Jonathandent\Desktop\LearningML\train-labels.idx1-ubyte";
private const string TestImages = "mnist/t10k-images.idx3-ubyte";
private const string TestLabels = "mnist/t10k-labels.idx1-ubyte";
public static IEnumerable<Image> ReadTrainingData()
{
foreach (var item in Read(TrainImages, TrainLabels))
{
yield return item;
}
}
public static IEnumerable<Image> ReadTestData()
{
foreach (var item in Read(TestImages, TestLabels))
{
yield return item;
}
}
private static IEnumerable<Image> Read(string imagesPath, string labelsPath)
{
BinaryReader labels = new BinaryReader(new FileStream(labelsPath, FileMode.Open));
BinaryReader images = new BinaryReader(new FileStream(imagesPath, FileMode.Open));
int magicNumber = images.ReadBigInt32();
int numberOfImages = images.ReadBigInt32();
int width = images.ReadBigInt32();
int height = images.ReadBigInt32();
int magicLabel = labels.ReadBigInt32();
int numberOfLabels = labels.ReadBigInt32();
for (int i = 0; i < numberOfImages; i++)
{
var bytes = images.ReadBytes(width * height);
var arr = new byte[height, width];
arr.ForEach((j, k) => arr[j, k] = bytes[j * height + k]);
yield return new Image()
{
Data = arr,
Label = labels.ReadByte()
};
}
}
}
public class Image
{
public byte Label { get; set; }
public byte[,] Data { get; set; }
}
public static class Extenstions
{
public static int ReadBigInt32(this BinaryReader br)
{
var bytes = br.ReadBytes(sizeof(Int32));
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
return BitConverter.ToInt32(bytes, 0);
}
public static void ForEach<T>(this T[,] source, Action<int, int> action)
{
for (int w = 0; w < source.GetLength(0); w++)
{
for (int h = 0; h < source.GetLength(1); h++)
{
action(w, h);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment