-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNIST.cs
248 lines (227 loc) · 8.72 KB
/
MNIST.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System.Runtime.InteropServices;
internal static class MNISTFileHandler
{
/*
Array.Copy has parameters like this:
Source array, Where to start at source array, New array, Where to start at new array, How many elements to copy
It is used to get a small section of the files to read how many elements in the file there is, but it is unreadable
*/
/*
Image File Format:
[offset] [type] [value] [description]
0000 32 bit integer 0x00000803(2051) magic number (shows what type of file it is)
0004 32 bit integer 10000 number of images
0008 32 bit integer 28 number of rows (height of each image)
0012 32 bit integer 28 number of columns (width of each image)
0016 unsigned byte ?? pixel (0 to 255, 0 meaning white and 255 meaning black)
0017 unsigned byte ?? pixel
0018 unsigned byte ?? pixel
........
xxxx unsigned byte ?? pixel
*/
public static byte[,,] GetImages(string pathToImageFile)
{
byte[] imageFile = File.ReadAllBytes(pathToImageFile);
//Finds the number of images
byte[] numberOfImagesArray = new byte[4];
Array.Copy(imageFile, 4, numberOfImagesArray, 0, 4);
int numberOfImages = ByteArrayToInt(numberOfImagesArray);
//Finds the width of the images
byte[] widthOfImagesArray = new byte[4];
Array.Copy(imageFile, 12, widthOfImagesArray, 0, 4);
int width = ByteArrayToInt(widthOfImagesArray);
//Finds the height of the images
byte[] heightOfImagesArray = new byte[4];
Array.Copy(imageFile, 8, heightOfImagesArray, 0, 4);
int height = ByteArrayToInt(heightOfImagesArray);
//Puts all the images into a 3D array, like a picture book
byte[,,] images = new byte[numberOfImages, height, width];
for(int imagesIndex = 0; imagesIndex < numberOfImages; imagesIndex++)
{
for(int heightIndex = 0; heightIndex < height; heightIndex++)
{
for(int widthIndex = 0; widthIndex < width; widthIndex++)
{
images[imagesIndex, heightIndex, widthIndex] = imageFile[784 * imagesIndex + 28 * heightIndex + widthIndex];
}
}
}
//For some reason the file is storing the left half of the image on the right, so this method flips that
images = SplitFlipAll(images);
return images;
}
public static void WriteImage(byte[,] image)
{
string row = "";
for(int heightIndex = 0; heightIndex < image.GetLength(0); heightIndex++)
{
for(int widthIndex = 0; widthIndex < image.GetLength(1); widthIndex++)
{
row += ConvertToPixel(Convert.ToString(image[heightIndex, widthIndex]));
}
Console.WriteLine(row);
row = "";
}
}
private static byte[,,] SplitFlipAll(byte[,,] images)
{
for(int imageIndex = 0; imageIndex < images.GetLength(0); imageIndex++)
{
for(int heightIndex = 0; heightIndex < 28; heightIndex++)
{
byte[] row = new byte[28];
for(int widthIndex = 0; widthIndex < 28; widthIndex++)
{
row[widthIndex] = images[imageIndex, heightIndex, widthIndex];
}
row = SplitFlipRow(row);
for(int widthIndex = 0; widthIndex < 28; widthIndex++)
{
images[imageIndex, heightIndex, widthIndex] = row[widthIndex];
}
}
}
return images;
}
private static byte[] SplitFlipRow(byte[] row)
{
byte[] leftSide = new byte[14];
Array.Copy(row, 0, leftSide, 0, 14);
byte[] rightSide = new byte[14];
Array.Copy(row, 14, rightSide, 0, 14);
byte[] finalArray = new byte[28];
Array.Copy(rightSide, 0, finalArray, 0, 14);
Array.Copy(leftSide, 0, finalArray, 14, 14);
return finalArray;
}
public static byte[] ImageToByteArray(byte[,] image)
{
int imageHeight = image.GetLength(0);
int imageWidth = image.GetLength(1);
byte[] finalArray = new byte[imageWidth * imageHeight];
for(int heightIndex = 0; heightIndex < imageHeight; heightIndex++)
{
for(int widthIndex = 0; widthIndex < imageWidth; widthIndex++)
{
finalArray[heightIndex * imageWidth + widthIndex] = image[heightIndex, widthIndex];
}
}
return finalArray;
}
private static string ConvertToPixel(string number)
{
byte shade = Convert.ToByte(number);
if(shade < 51)
{
return "█";
}
else if(shade >= 51 && shade < 102)
{
return "▓";
}
else if(shade >= 102 && shade < 153)
{
return "▒";
}
else if(shade >= 153 && shade < 204)
{
return "░";
}
return " ";
}
/*
Label File Format:
[offset] [type] [value] [description]
0000 32 bit integer 0x00000801(2049) magic number (shows what type of file it is)
0004 32 bit integer 10000 number of labels
0008 unsigned byte ?? label (the number that the image represents, 0 to 9)
0009 unsigned byte ?? label
0010 unsigned byte ?? label
........
xxxx unsigned byte ?? label
*/
public static byte[] GetLabels(string pathToLabelFile)
{
//Gets data in the label file
byte[] labelFile = File.ReadAllBytes(pathToLabelFile);
//Finds the number of labels
byte[] numberOfLabelsArray = new byte[4];
Array.Copy(labelFile, 4, numberOfLabelsArray, 0, 4);
int numberOfLabels = ByteArrayToInt(numberOfLabelsArray);
//Puts the labels into an array
byte[] labels = new byte[numberOfLabels];
for(int i = 8; i < numberOfLabels + 8; i++)
{
labels[i - 8] = labelFile[i];
}
return labels;
}
//Used to read certain parts of the MNIST database files
private static int ByteArrayToInt(byte[] bytes)
{
//If the computer uses Little Endian, change it to Big Endian because the image and label files are in Big Endian
if(BitConverter.IsLittleEndian)
{
Array.Reverse(bytes);
}
return BitConverter.ToInt32(bytes, 0);
}
//Gets a row from a 2D array
private static T[] GetRow<T>(this T[,] array, int row)
{
if(!typeof(T).IsPrimitive) throw new InvalidOperationException("Not supported for managed types.");
if(array == null) throw new ArgumentNullException("array");
int cols = array.GetUpperBound(1) + 1;
T[] result = new T[cols];
int size;
if(typeof(T) == typeof(bool)) size = 1;
else if(typeof(T) == typeof(char)) size = 2;
else size = Marshal.SizeOf<T>();
Buffer.BlockCopy(array, row * cols * size, result, 0, cols * size);
return result;
}
public static byte[,] GetImage(byte[,,] images, int imageIndex)
{
int imageWidth = images.GetLength(2);
int imageHeight = images.GetLength(1);
byte[,] image = new byte[imageHeight, imageWidth];
for(int heightIndex = 0; heightIndex < imageHeight; heightIndex++)
{
for(int widthIndex = 0; widthIndex < imageWidth; widthIndex++)
{
image[heightIndex, widthIndex] = images[imageIndex, heightIndex, widthIndex];
}
}
return image;
}
public static double[] LabelToExpectedValues(byte label)
{
double[,] expectedValues =
{
{1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,0,0,1},
};
return GetRow<double>(expectedValues, Convert.ToInt32(label));
}
public static byte[,] RandomizedImage()
{
byte[,] image = new byte[28, 28];
Random random = new Random();
for(int heightIndex = 0; heightIndex < image.GetLength(0); heightIndex++)
{
for(int widthIndex = 0; widthIndex < image.GetLength(1); widthIndex++)
{
image[heightIndex, widthIndex] = Convert.ToByte(random.Next(0, 256));
}
}
return image;
}
}