-
Notifications
You must be signed in to change notification settings - Fork 0
/
not_used_nndataset.h
executable file
·286 lines (251 loc) · 7.57 KB
/
not_used_nndataset.h
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifndef NNDATASET
#define NNDATASET
#include <vector>
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <random>
#include <algorithm>
template <typename DataType = double,
typename FileDataType = uint8_t,
typename LabelType = uint8_t>
struct NNSample {
NNSample(size_t DataSize,
size_t OutputSize,
size_t id,
LabelType label,
FileDataType* data)
: Id(id), Label(label), MSE(0), Error(false) {
Data = new FileDataType[DataSize];
Input = new DataType[DataSize];
Output = new DataType[OutputSize];
for (size_t j = 0; j < DataSize; ++j)
Data[j] = data[j];
}
NNSample(size_t DataSize, size_t OutputSize)
: Id(0), Label(0), MSE(0), Error(false) {
Data = new FileDataType[DataSize];
Input = new DataType[DataSize];
Output = new DataType[OutputSize];
}
~NNSample() {
delete Data;
delete Input;
delete Output;
}
FileDataType* Data;
DataType* Input;
DataType* Output;
size_t Id;
LabelType Label;
DataType MSE;
bool Error;
};
template <typename DataType = double,
typename FileDataType = uint8_t,
typename LabelType = uint8_t>
class NNDataset {
public:
typedef NNSample<DataType, FileDataType, LabelType> SampleType;
typedef typename std::vector<
NNSample<DataType, FileDataType, LabelType>*>::iterator Iterator;
NNDataset(size_t Rows, size_t Cols, size_t OutputSize)
: MeanSample(0),
N(0),
Size(Rows * Cols),
Cols(Cols),
Rows(Rows),
OutputSize(OutputSize),
CurrentId(0) {}
NNDataset(size_t Size, size_t OutputSize)
: MeanSample(0),
N(0),
Size(Size),
Cols(Size),
Rows(1),
OutputSize(OutputSize),
CurrentId(0) {}
size_t getN() { return Filter.size(); }
size_t getSize() { return Size; }
size_t getRows() { return Rows; }
size_t getCols() { return Cols; }
size_t getOutputSize() { return OutputSize; }
size_t getNSamples() { return N; }
Iterator begin() { return Filter.begin(); }
Iterator end() { return Filter.end(); }
bool load(size_t x0,
size_t n,
const std::string& DataFile,
const std::string& LabelsFile,
size_t DataOffset = 0,
size_t LabelsOffset = 0) {
std::vector<FileDataType> Data(n * Size);
std::vector<LabelType> Labels(n);
std::ifstream ifsd(DataFile.c_str(),
std::ifstream::in | std::ifstream::binary);
if (!ifsd.is_open()) {
std::cout << "Error Data file no found : " << DataFile << std::endl;
return false;
}
ifsd.seekg(DataOffset + x0 * Size * sizeof(FileDataType));
ifsd.read((char*)&Data[0], n * Size * sizeof(FileDataType));
ifsd.close();
std::ifstream ifsl(LabelsFile.c_str(),
std::ifstream::in | std::ifstream::binary);
if (!ifsl.is_open()) {
std::cout << "Error Labels file no found : " << LabelsFile << std::endl;
return false;
}
ifsl.seekg(LabelsOffset + x0 * sizeof(uint8_t));
ifsl.read((char*)&Labels[0], n * sizeof(uint8_t));
ifsl.close();
N = n;
Samples.resize(N);
Filter.resize(N);
for (size_t i = 0; i < N; ++i) {
SampleType* S = new SampleType(
Size, OutputSize, i, Labels[i], (uint8_t*)&Data[i * Size]);
Samples[i] = S;
Filter[i] = S;
}
generateOutputs();
return true;
}
void preProcessingInputs(const SampleType* Sample = 0) {
normalizeInputs();
if (!Sample)
computeMeanVector();
else
setMeanSample(Sample);
subtractMeanInput();
}
SampleType* getMeanSample() { return MeanSample; }
void setMeanSample(const SampleType* Sample) {
if (MeanSample)
delete MeanSample;
MeanSample = new SampleType(Size, OutputSize);
for (size_t i = 0; i < Size; ++i)
MeanSample->Input[i] = Sample->Input[i];
}
void computeMeanVector() {
if (MeanSample)
delete MeanSample;
MeanSample = new SampleType(Size, OutputSize);
for (size_t i = 0; i < N; ++i)
for (size_t j = 0; j < Size; ++j)
MeanSample->Input[j] += Samples[i]->Input[j];
for (size_t j = 0; j < Size; ++j)
MeanSample->Input[j] /= N;
}
void generateOutputs() {
for (size_t i = 0; i < N; ++i)
for (size_t j = 0; j < OutputSize; ++j)
Samples[i]->Output[j] = Samples[i]->Label == j ? 1 : -1;
}
void normalizeInputs() {
for (size_t i = 0; i < N; ++i)
for (size_t j = 0; j < Size; ++j)
Samples[i]->Input[j] = DataType(Samples[i]->Data[j]) / 256.0f;
}
void subtractMeanInput() {
for (size_t i = 0; i < N; ++i)
for (size_t j = 0; j < Size; ++j)
Samples[i]->Input[j] -= MeanSample->Input[j];
}
void addNoiseNormal(double sigma = 64) {
std::default_random_engine generator;
std::normal_distribution<double> normal(0, sigma);
for (size_t i = 0; i < N; ++i)
for (size_t j = 0; j < Size; ++j) {
int v = Samples[i]->Data[j] + int(normal(generator));
if (v < 0)
v = 0;
else if (v > 255)
v = 255;
Samples[i]->Data[j] = v;
}
}
void addNoiseSaltPepper(double P = 0.1) {
std::default_random_engine generator;
std::uniform_int_distribution<size_t> d1(0, Size);
std::uniform_int_distribution<size_t> d2(0, 1);
for (size_t i = 0; i < N; ++i)
for (size_t j = 0; j < Size; ++j)
if (d1(generator) < Size * P) {
if (d2(generator) > 0)
Samples[i]->Data[j] = 255;
else
Samples[i]->Data[j] = 0;
}
}
void blurXY() {
int radius = 1;
int st = (2 * radius + 1) * (2 * radius + 1);
for (size_t i = 0; i < N; ++i) {
for (size_t y = 0; y < Rows; ++y) {
for (size_t x = 0; x < Cols; ++x) {
int y0 = y >= radius ? y - radius : 0;
int y1 = y + radius < Rows ? y + radius + 1 : Rows;
int x0 = x >= radius ? x - radius : 0;
int x1 = x + radius < Cols ? x + radius + 1 : Cols;
float s = 0;
int nn = 0;
for (int yr = y0; yr < y1; ++yr)
for (int xr = x0; xr < x1; ++xr) {
s += Samples[i]->Data[yr * Cols + xr];
nn++;
}
if (nn < 4)
std::cout << "Errrprrrrrrr " << y0 << " " << y1 << " " << x0 << " "
<< x1 << std::endl;
s /= nn;
Samples[i]->Data[y * Cols + x] = s;
}
}
}
}
void addFilterByLabel(LabelType Label) {
std::vector<SampleType*> FilterTemp = Filter;
Filter.clear();
for (auto& e : FilterTemp)
if (e->Label == Label)
Filter.push_back(e);
}
void addFilterByError() {
std::vector<SampleType*> FilterTemp = Filter;
Filter.clear();
for (auto& e : FilterTemp)
if (e->Error)
Filter.push_back(e);
}
void clearFilter() {
Filter.clear();
for (auto& e : Samples)
Filter.push_back(e);
}
void sortByMSE() {
std::sort(Filter.begin(),
Filter.end(),
[](const SampleType* a, const SampleType* b)
-> bool { return a->MSE > b->MSE; });
}
void randomizeOrder() {
std::default_random_engine generator;
std::uniform_int_distribution<size_t> d(0, Filter.size() - 1);
for (size_t i = 0; i < Filter.size(); ++i) {
size_t iRand = d(generator);
std::swap(Filter[i], Filter[iRand]);
}
}
private:
std::vector<SampleType*> Samples;
std::vector<SampleType*> Filter;
SampleType* MeanSample;
size_t N;
size_t Size;
size_t Cols;
size_t Rows;
size_t OutputSize;
size_t CurrentId;
};
#endif // NNDATASET