-
Notifications
You must be signed in to change notification settings - Fork 8
/
DataSetItem.cs
185 lines (156 loc) · 6.68 KB
/
DataSetItem.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdvancedOCR
{
struct DataSetItem
{
public double[] Inputs;
public char Character;
private static DataSetItem[] LoadLeCunSet(string imagePath, string labelPath)
{
FileStream ImageStream = new FileStream(imagePath, FileMode.Open);
FileStream LabelStream = new FileStream(labelPath, FileMode.Open);
BinaryReader brImage = new BinaryReader(ImageStream);
BinaryReader brLabel = new BinaryReader(LabelStream);
if (ReadBigEndianInteger(brImage) != 2051)
throw new InvalidDataException("Invalid magic in specified image file.");
if (ReadBigEndianInteger(brLabel) != 2049)
throw new InvalidDataException("Invalid magic in specified label file.");
int ItemCount = ReadBigEndianInteger(brImage);
if (ReadBigEndianInteger(brLabel) != ItemCount)
throw new InvalidDataException("Number of images and labels do not match.");
int rows = ReadBigEndianInteger(brImage);
int columns = ReadBigEndianInteger(brImage);
DataSetItem[] items = new DataSetItem[ItemCount];
for (int i = 0; i < ItemCount; i++)
{
char character = (char)(brLabel.ReadByte().ToString()[0]);
// Read image with border of 2 on all sides.
double[] inputs = new double[(rows + 4) * (columns + 4)];
for (int x = 0; x < (rows + 4); x++)
for (int y = 0; y < (columns + 4); y++)
{
inputs[x + y * (rows + 4)] = -0.1;
}
int inputIndex = 2;
for (int y = 2; y < (columns + 2); y++)
{
for (int x = 2; x < (rows + 2); x++)
{
// Background pixel is -0.1 (black) and Foreground is 1.175.
// Refer to page 7 of LeCun's document on Gradient-based learning applied to document recognition.
inputs[inputIndex] = (((double)brImage.ReadByte()) / 255.0) * 1.275 - 0.1;
inputIndex += 1;
}
inputIndex += 4;
}
items[i] = new DataSetItem() { Inputs = inputs, Character = character };
}
return items;
}
private static void SaveNativeSet(string filePath, DataSetItem[] set)
{
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(set.Length);
bw.Write(set[0].Inputs.Length);
for (int i = 0; i < set.Length; i++)
{
bw.Write(set[i].Character);
for (int j = 0; j < set[i].Inputs.Length; j++)
{
double value = Math.Round(((set[i].Inputs[j] + 0.1) / 1.275) * 255.0);
byte byteValue = (byte)value;
bw.Write(byteValue);
}
}
bw.Flush();
fs.SetLength(fs.Position);
fs.Close();
}
private static DataSetItem[] LoadNativeSet(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader br = new BinaryReader(fs);
int count = br.ReadInt32();
int inputCount = br.ReadInt32();
DataSetItem[] result = new DataSetItem[count];
for (int i = 0; i < result.Length; i++)
{
char character = br.ReadChar();
double[] inputs = new double[inputCount];
for (int j = 0; j < inputCount; j++)
{
byte b = br.ReadByte();
inputs[j] = (((double)b) / 255.0) * 1.275 - 0.1;
}
DataSetItem item = new DataSetItem();
item.Character = character;
item.Inputs = inputs;
result[i] =item;
}
br.Close();
return result;
}
private static int ReadBigEndianInteger(BinaryReader br)
{
byte[] data = br.ReadBytes(4);
byte[] result = new byte[] { data[3], data[2], data[1], data[0] };
return BitConverter.ToInt32(result, 0);
}
public const string TrainingDatasetImages = @"MNIST\MNIST-train-images.dat";
public const string TrainingDatasetLabels = @"MNIST\MNIST-train-labels.dat";
public const string GeneralisationDatasetImages = @"MNIST\MNIST-test-images.dat";
public const string GeneralisationDatasetLabels = @"MNIST\MNIST-test-labels.dat";
public const string TrainingDatasetCache = "MNIST-training.dat";
public const string GeneralisationDatasetCache = "MNIST-generalisation.dat";
private static IList<DataSetItem> trainingDataset;
private static IList<DataSetItem> generalisationDataset;
private static IList<DataSetItem> LoadTrainingDataset()
{
if (!File.Exists(TrainingDatasetCache))
{
DataSetItem[] set = DataSetItem.LoadLeCunSet(TrainingDatasetImages, TrainingDatasetLabels);
DataSetItem.SaveNativeSet(TrainingDatasetCache, set);
return set;
}
else
{
return DataSetItem.LoadNativeSet(TrainingDatasetCache);
}
}
private static IList<DataSetItem> LoadGeneralisationDataset()
{
if (!File.Exists(GeneralisationDatasetCache))
{
DataSetItem[] set = DataSetItem.LoadLeCunSet(GeneralisationDatasetImages, GeneralisationDatasetLabels);
DataSetItem.SaveNativeSet(GeneralisationDatasetCache, set);
return set;
}
else
{
return DataSetItem.LoadNativeSet(GeneralisationDatasetCache);
}
}
public static IList<DataSetItem> GetTrainingSet()
{
if (trainingDataset == null)
{
trainingDataset = LoadTrainingDataset();
}
return trainingDataset;
}
public static IList<DataSetItem> GetGeneralisationSet()
{
if (generalisationDataset == null)
{
generalisationDataset = LoadGeneralisationDataset();
}
return generalisationDataset;
}
}
}