/
App.cpp
208 lines (176 loc) · 4.31 KB
/
App.cpp
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
#include "App.h"
#include <SDL/SDL_opengl.h>
#include <GL/gl.h>
#include "Engine/SDL/Font.h"
#include "Engine/SDL/SDL_SurfacePtr.h"
App::App()
{
}
App::~App()
{
}
void App::init_GL()
{
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
glClearColor( 1.0, 1.0, 1.0, 1.0);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, Engine::Width, Engine::Height, 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glEnable( GL_TEXTURE_2D );
glEnable( GL_BLEND );
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void App::additional_init()
{
m_State = state_loading;
Font font("Ayu.ttf", 70);
std::string s = "Training the Brain\nPlease Wait ...";
SDL_SurfacePtr surf = font.render_Blended(s);
m_LoadingImage.load( surf.get() );
}
void App::initGUI()
{
//Title Image
Font f("Ayu.ttf", 72);
SDL_SurfacePtr surf= f.render_Blended("Digit Recognizer");
m_TitleImage.load(surf.get());
//Grid
m_Grid.init();
//Buttons
Font font("Ayu.ttf", 50);
m_Digits.reserve(10);
for(unsigned int i=0; i<10; i++)
m_Digits.push_back( new Digit_Button( this, &font, i) );
m_pNoiseButton = std::auto_ptr<Noise_Button>( new Noise_Button(this, &font));
m_pRecognizeButton = std::auto_ptr<Recognize_Button>( new Recognize_Button(this, &font) );
m_pLabel = std::auto_ptr<Label>(new Label);
//Set Positions
int x = 40;
int y = 80;
for(int i=0; i<10; i++)
{
m_Digits[i].setPos(x,y);
x += 40;
}
m_Grid.setPos(40, 130);
m_pNoiseButton->setPos(270, 150);
m_pRecognizeButton->setPos(270, 200);
m_pLabel->setPos(270, 270);
}
void App::initNN()
{
//Neural Network
m_NN.addLayer( Grid::width * Grid::height ); //Input
m_NN.addLayer(47); //Hidden
m_NN.addLayer(47); //Hidden
m_NN.addLayer(10); //Output
//Train the NN
m_NN.randomWeights();
std::vector<float> input( Grid::width * Grid::height);
std::vector<float> output(10);
const float minError = 0.0001;
float error = 1.0;
while( error > minError )
{
error = 0;
for(int i=0; i<10; i++)
{
m_Grid.setDigit(i);
m_Grid.getInput(input);
for(int j=0; j<10; j++)
if( i == j )
output[j] = 1.0;
else
output[j] = 0.0;
float e = m_NN.train(input, output);
error += e;
}
error /= 10;
std::cout << error << std::endl;
}
}
void App::render()
{
glLoadIdentity();
switch( m_State )
{
case state_normal :
m_TitleImage.render(35, 5);
m_Grid.render();
for(int i=0; i<10; i++)
m_Digits[i].render();
m_pNoiseButton->render();
m_pRecognizeButton->render();
m_pLabel->render();
break;
case state_loading :
m_LoadingImage.render(30, 130);
break;
}
}
void App::calc(float timeElapsed)
{
switch( m_State )
{
case state_normal :
m_Grid.update(timeElapsed);
for(unsigned int i=0; i<m_Digits.size(); i++)
m_Digits[i].update(timeElapsed);
m_pNoiseButton->update(timeElapsed);
m_pRecognizeButton->update(timeElapsed);
break;
case state_loading :
static int first = true;
if (first == true)
{
first = false;
return;
}
initGUI();
initNN();
m_State = state_normal;
break;
}
}
void App::setDigit(int d)
{
m_Grid.setDigit(d);
}
void App::addNoise()
{
m_Grid.addNoise( 5 + (rand() % 10) );
}
struct Score {
int num;
float per;
bool operator<(const Score & s) const
{ return per < s.per; }
};
void App::recognize()
{
std::vector<float> input( Grid::width * Grid::height);
std::vector<float> output(10);
m_Grid.getInput(input);
m_NN.run(input, output);
std::vector<Score> sc(10);
for(int i=0; i<10; i++)
{
Score s;
s.num = i;
s.per = output[i];
sc[i] = s;
}
std::sort( sc.rbegin(), sc.rend() );
std::string s("Analysis : \n");
for(int i=0; i<3; i++)
{
s += "#";
s += '0' + sc[i].num;
s += " -- ";
s += toString(sc[i].per*100);
s += "%\n";
}
m_pLabel->set(s);
}