private void button2_Click(object sender, EventArgs e) { //Save test images to disk so we can view them ImageProcessing.DigitImageCollection images = ImageProcessing.DigitImageCollection.LoadMnistDataSet("TestImages.dat", "TestLabels.dat", 10000); images.SaveImagesToFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TestImages")); }
private void button5_Click(object sender, EventArgs e) { //Test loading a MNIST image, distorting it, blurring it, converting it back to a DigitImage and then saving it to disk ImageProcessing.DigitImageCollection sourceImages = ImageProcessing.DigitImageCollection.LoadMnistDataSet("TrainingImages.dat", "TrainingLabels.dat", 60000); string imageFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ConvertedImages"); if (Directory.Exists(imageFolder)) { Directory.Delete(imageFolder, true); } Directory.CreateDirectory(imageFolder); DigitImage originalDigitImage = sourceImages.DigitImages[0]; originalDigitImage.SaveToFile(Path.Combine(imageFolder, "Original.jpg")); Bitmap distortedImage = sourceImages.DigitImages[0].ToBitmap().DistortionBlurFilter(25); DigitImage distortedDigitImage = new DigitImage(distortedImage, sourceImages.DigitImages[0].Label); distortedDigitImage.SaveToFile(Path.Combine(imageFolder, "Distorted.jpg")); Bitmap blurredImage = sourceImages.DigitImages[0].ToBitmap().ImageBlurFilter(BlurringBitmapExtensions.BlurType.GaussianBlur5x5); DigitImage blurredDigitImage = new DigitImage(blurredImage, sourceImages.DigitImages[0].Label); blurredDigitImage.SaveToFile(Path.Combine(imageFolder, "Blurred.jpg")); }
private void button7_Click(object sender, EventArgs e) { //Load the blurred data set and save the images to a folder for viewing ImageProcessing.DigitImageCollection sourceImages = ImageProcessing.DigitImageCollection.LoadMnistDataSet("BlurredTrainingImages.dat", "BlurredTrainingLabels.dat", 60000); sourceImages.SaveImagesToFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "BlurredTrainingImages")); MessageBox.Show("Done"); }
private void button6_Click(object sender, EventArgs e) { //Load the original Mnist training images and create a new blurred data set from them ImageProcessing.DigitImageCollection sourceImages = ImageProcessing.DigitImageCollection.LoadMnistDataSet("TrainingImages.dat", "TrainingLabels.dat", 60000); DigitImageCollection blurredImages = new DigitImageCollection(); blurredImages.DigitImages = new DigitImage[sourceImages.DigitImages.Length]; Parallel.For(0, sourceImages.DigitImages.Length, i => { int imageIndex = i; DigitImage blurredDigitImage = new DigitImage(sourceImages.DigitImages[imageIndex].ToBitmap().ImageBlurFilter(BlurringBitmapExtensions.BlurType.GaussianBlur5x5), sourceImages.DigitImages[imageIndex].Label); blurredImages.DigitImages[imageIndex] = blurredDigitImage; }); string imageFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "BlurredTrainingDataset"); if (Directory.Exists(imageFolder)) { Directory.Delete(imageFolder, true); } Directory.CreateDirectory(imageFolder); blurredImages.SaveToFile(Path.Combine(imageFolder, "BlurredTrainingImages.dat"), Path.Combine(imageFolder, "BlurredTrainingLabels.dat")); MessageBox.Show("Done"); }
private void button1_Click(object sender, EventArgs e) { //Save training images to disk so we can view them ImageProcessing.DigitImageCollection images = ImageProcessing.DigitImageCollection.LoadMnistDataSet("TrainingImages.dat", "TrainingLabels.dat", 60000); images.SaveImagesToFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TrainingImages")); MessageBox.Show("Done"); }
private void button4_Click(object sender, EventArgs e) { //Load the training images and distort all of the zeroes, then save them to disk for viewing ImageProcessing.DigitImageCollection sourceImages = ImageProcessing.DigitImageCollection.LoadMnistDataSet("TrainingImages.dat", "TrainingLabels.dat", 60000); int imageCount = 0; string imageFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ZeroesDistorted"); if (Directory.Exists(imageFolder)) { Directory.Delete(imageFolder, true); } Directory.CreateDirectory(imageFolder); foreach (ImageProcessing.DigitImage image in sourceImages.DigitImages.Where(i => i.Label == 0)) { Bitmap distortedImage = image.ToBitmap().DistortionBlurFilter(25); distortedImage.Save(Path.Combine(imageFolder, imageCount.ToString() + ".jpg")); imageCount += 1; } }
/// <summary> /// Creates a DigitImageCollection from image and label files that are in the Mnist dataset format /// </summary> /// <param name="imageFile">A path to the file that contains the image data.</param> /// <param name="labelFile">A path to thefile that contains labels for each image.</param> /// <returns>An instance of DigitImageColleciton with all images and labels loaded from the files.</returns> public static DigitImageCollection LoadMnistDataSet(string imageFile, string labelFile, int imageCount) { //Use this link for information about the file formats of the Mnist dataset - http://yann.lecun.com/exdb/mnist/ DigitImageCollection imageCollection = new DigitImageCollection(); //Create file streams and binary readers for the images and labels. using (FileStream imagesFileStream = new FileStream(imageFile, FileMode.Open, FileAccess.Read)) { using (BinaryReader imagesReader = new BinaryReader(imagesFileStream)) { using (FileStream labelsFileStream = new FileStream(labelFile, FileMode.Open, FileAccess.Read)) { using (BinaryReader labelsReader = new BinaryReader(labelsFileStream)) { //The image file header should be in the following format //[offset] [type] [value] [description] //0000 32 bit integer 0x00000803(2051) magic number //0004 32 bit integer 60000 number of images //0008 32 bit integer 28 number of rows //0012 32 bit integer 28 number of columns //Read and discard the "magic number" (not sure why we don't care about this) imagesReader.ReadInt32(); //Get the number of images in the dataset and initialize the image collections DigitImage array int numberOfImages = imageCount; //I do not know why reading the number of images does not work, but it is way off imagesReader.ReadInt32(); imageCollection.DigitImages = new DigitImage[numberOfImages]; //Read and discard the number of rows and columns (not sure why we don't care about these) imagesReader.ReadInt32(); imagesReader.ReadInt32(); //The label file header is similar, it has a magic number and the number of labels but no need for the rows or columns counts //Read and discard the magic number labelsReader.ReadInt32(); //Get the number of labels int numberOfLabels = imageCount; //I do not know why reading the number of labels does not work, but it is way off labelsReader.ReadInt32(); //Make sure the number of labels and images match if (numberOfImages != numberOfLabels) { throw new Exception("The number of images does not match the number of labels."); } //Start reading pixels and creating 28X28 DigitImages for (int imageIndex = 0; imageIndex < numberOfImages; imageIndex++) { DigitImage digitImage = new DigitImage(); imageCollection.DigitImages[imageIndex] = digitImage; for (int y = 0; y < 28; y++) { for (int x = 0; x < 28; x++) { digitImage.Pixels[x][y] = imagesReader.ReadByte(); } } //Get the label for the image digitImage.Label = labelsReader.ReadByte(); } } } } } return(imageCollection); }