public static Bitmap MakeBitmapFromMnist(MnistImage mnsIm, int coof) { Bitmap bmI = new Bitmap(mnsIm.width * coof, mnsIm.height * coof); Graphics gr = Graphics.FromImage(bmI); for (int i = 0; i < mnsIm.width; i++) { for (int j = 0; j < mnsIm.height; j++) { int pixelColor = 255 - mnsIm.pixels[i][j]; System.Drawing.Color c = System.Drawing.Color.FromArgb(pixelColor, pixelColor, pixelColor); SolidBrush sb = new SolidBrush(c); gr.FillRectangle(sb, j * coof, i * coof, coof, coof); } } return(bmI); }
public static List <MnistImage> ReadMnistBase(string imageFilePath, string labelFilePath) { List <MnistImage> images = new List <MnistImage>(); System.IO.FileStream fsImage = new System.IO.FileStream(imageFilePath, System.IO.FileMode.Open); System.IO.FileStream fsLabel = new System.IO.FileStream(labelFilePath, System.IO.FileMode.Open); System.IO.BinaryReader brImage = new System.IO.BinaryReader(fsImage); System.IO.BinaryReader brLabel = new System.IO.BinaryReader(fsLabel); int magic1 = ReverseByte(brImage.ReadInt32()); int imageCount = ReverseByte(brImage.ReadInt32()); int rowsCount = ReverseByte(brImage.ReadInt32()); int colsCount = ReverseByte(brImage.ReadInt32()); int magic2 = ReverseByte(brLabel.ReadInt32()); int labesCount = ReverseByte(brLabel.ReadInt32()); for (int n = 0; n < imageCount; n++) { List <List <byte> > bytes = new List <List <byte> >(); for (int i = 0; i < rowsCount; i++) { List <byte> col = new List <byte>(); for (int j = 0; j < colsCount; j++) { col.Add(brImage.ReadByte()); } bytes.Add(col); } byte label = brLabel.ReadByte(); MnistImage img = new MnistImage(rowsCount, colsCount, bytes, label); images.Add(img); } fsImage.Close(); fsLabel.Close(); brImage.Close(); brLabel.Close(); return(images); }
public void MakeMnistTeacherList(string pathToImages, string pathToLabels) { List <MnistImage> images = MnistImage.ReadMnistBase(pathToImages, pathToLabels); double average = 0, sum = 0, max = double.MinValue; int n = 0; for (int i = 0; i < images.Count; i++) { for (int l = 0; l < images[i].height; l++) { for (int m = 0; m < images[i].width; m++) { sum += images[i].pixels[m][l]; n++; if (max < images[i].pixels[m][l]) { max = images[i].pixels[m][l]; } } } } average = sum / n; List <List <double> > im = new List <List <double> >(); for (int i = 0; i < images.Count; i++) { DataParticle tp = new DataParticle(); for (int l = 0; l < images[i].height; l++) { List <double> temp = new List <double>(); for (int m = 0; m < images[i].width; m++) { temp.Add((images[i].pixels[m][l] - average) / max); } tp.image.Add(temp); } string s = ""; for (int l = 0; l < 9; l++) { if (images[i].label != l) { s += "0 "; } else { s += "1 "; } } if (images[i].label != 9) { s += "0"; } else { s += "1"; } tp.label = s; List.Add(tp); tp.answ = images[i].label.ToString(); } batchLenght = images.Count(); }