-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
282 lines (213 loc) · 9.06 KB
/
main.cc
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
#include <iostream>
#include <cstring>
#include <fstream>
#include <Eigen/Dense>
#include <vector>
#include <functional>
#include <random>
#include <algorithm>
#include <chrono>
#include <thread>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "grid_search.h"
#include "math_functions.h"
#include "data.h"
#include "nn.h"
constexpr uint FONT_SIZE = 22;
constexpr uint FONT_SIZE_SMALL = 12;
// constexpr uint FONT_SIZE = 24;
// constexpr float LEARNING_RATE = 2e-5f;
// constexpr float LEARNING_RATE = 2e-2f;
constexpr float LEARNING_RATE = 1e-1f;
constexpr float REGULARISATION_PARAMETER = 0.001f;
constexpr float L1L2_ALPHA_PAREMETER = 0.5;
// constexpr unsigned EPOCHS = 0;
constexpr unsigned EPOCHS = 100;
// constexpr unsigned BATCH_SIZE = 256;
constexpr unsigned BATCH_SIZE = 64;
using namespace std::chrono_literals;
int main() {
using NN = NeuralNetwork;
std::random_device rd;
std::mt19937 rng(rd());
NeuralNetwork nn(
784, // INPUT LAYER
std::make_pair(relu, relu_derivative), 16,
std::make_pair(relu, relu_derivative), 16,
std::make_pair(nullptr, nullptr), 10 // SOFTMAX
);
// std::cout << nn.nnInfoString() << std::endl;
std::cout << "Loading training data..." << std::endl;
std::vector<pixel_vector> x_test = read_mnist_pixel_data("x_test.txt", 10000);
std::vector<label_vector> y_test = read_mnist_label_data("y_test.txt", 10000);
std::vector<pixel_vector> x_data = read_mnist_pixel_data("x_train.txt", 60000);
std::vector<label_vector> y_data = read_mnist_label_data("y_train.txt", 60000);
std::vector<pixel_vector> x_train(x_data.begin(), x_data.begin() + 50000);
std::vector<label_vector> y_train(y_data.begin(), y_data.begin() + 50000);
std::vector<pixel_vector> x_val(x_data.begin()+50000, x_data.end());
std::vector<label_vector> y_val(y_data.begin()+50000, y_data.end());
std::vector<pixel_vector>().swap(x_data);
std::vector<label_vector>().swap(y_data);
std::cout << "Loaded training data." << std::endl;
nn.printLayerDims();
for(int e = 0; e < EPOCHS; e++){
// float train_accuracy = nn.trainSGD(x_train, y_train, LEARNING_RATE, NN::CROSS_ENTROPY_LOSS);
float train_accuracy = nn.trainMiniBatchGD(x_train, y_train, BATCH_SIZE, LEARNING_RATE, REGULARISATION_PARAMETER, L1L2_ALPHA_PAREMETER);
auto [val_accuracy,_] = nn.evaluate(x_val, y_val);
std::cout << "Epoch " << e+1 << " completed!" << std::endl;
std::cout << "Training Accuracy: " << train_accuracy << std::endl;
std::cout << "Validation Accuracy: " << val_accuracy << std::endl;
two_vec_shuffle<pixel_vector, label_vector>(x_train, y_train, rng);
}
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
SDL_Texture *texture = nullptr;
TTF_Font *font = nullptr;
TTF_Font *font_small = nullptr;
// SDL_Surface *text_surface = nullptr;
// SDL_Texture *text_texture = nullptr;
SDL_Color textColor = { 255, 255, 255, 0 }; // white
SDL_Color textGreen = { 0, 255, 0, 0 };
SDL_Color textRed = { 255, 0, 0, 0 };
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("Could not initialize sdl2: %s\n", SDL_GetError());
return 1;
}
if (TTF_Init() == -1) {
printf("TTF_Init: %s\n", TTF_GetError());
return 2;
}
window = SDL_CreateWindow("Handwritten Digit Classifier", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr) {
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr) {
printf("Could not create renderer: %s\n", SDL_GetError());
return 1;
}
font = TTF_OpenFont("sf-pro-text-regular.ttf", FONT_SIZE);
if (font == nullptr) {
printf("Could not load font: %s\n", TTF_GetError());
return 1;
}
font_small = TTF_OpenFont("sf-pro-text-regular.ttf", FONT_SIZE_SMALL);
if (font == nullptr) {
printf("Could not load font: %s\n", TTF_GetError());
return 1;
}
SDL_Rect srcRect = { 0, 0, IMG_WIDTH, IMG_HEIGHT };
SDL_Rect destRect = { 0, 0, IMG_WIDTH * SCALE_FACTOR, IMG_HEIGHT * SCALE_FACTOR };
// SDL_Rect textRect = { TEXT_POS_X, TEXT_POS_Y, text_surface->w, text_surface->h };
SDL_Event event;
int running = 1;
int counter = 0;
char str[ENOUGH];
char str2[ENOUGH];
char str3[ENOUGH];
float correct = 0;
float accuracy = 0;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = 0;
}
}
SDL_Surface *text_surface = nullptr;
SDL_Texture *text_texture = nullptr;
SDL_Surface *text_surface2 = nullptr;
SDL_Texture *text_texture2 = nullptr;
SDL_Color textcol2;
SDL_Surface *text_surface3 = nullptr;
SDL_Texture *text_texture3 = nullptr;
SDL_Surface *text_surface4 = nullptr;
SDL_Texture *text_texture4 = nullptr;
std::pair<uint, Eigen::VectorXf> result = nn.predict(x_test.at(counter));
uint predicted;
Eigen::VectorXf probabilities;
std::tie(predicted, probabilities) = result;
uint actual = one_hot_decode(y_test.at(counter));
if (actual == predicted) {
correct++;
textcol2 = textGreen;
} else textcol2 = textRed;
accuracy = correct/(float)(counter+1);
// "Label: %hhu\nPredicted: %hhu\n", + some correct or incorrect text and running correct ratio and percentage
// printf("%d", actual);
sprintf(str, "Label: %hhu", (uint8_t)actual);
sprintf(str2, "Predicted: %hhu", (uint8_t)predicted);
sprintf(str3, "Accuracy: %f\n%d/%d", accuracy, (int)correct, counter+1);
text_surface = TTF_RenderText_Blended(font, str, textColor);
if (text_surface == nullptr) {
printf("Could not create text surface: %s\n", TTF_GetError());
return 1;
}
text_texture = SDL_CreateTextureFromSurface(renderer, text_surface);
if (text_texture == nullptr) {
printf("Could not create text texture: %s\n", SDL_GetError());
return 1;
}
text_surface2 = TTF_RenderText_Blended(font, str2, textcol2);
if (text_surface2 == nullptr) {
printf("Could not create text surface: %s\n", TTF_GetError());
return 1;
}
text_texture2 = SDL_CreateTextureFromSurface(renderer, text_surface2);
if (text_texture2 == nullptr) {
printf("Could not create text texture: %s\n", SDL_GetError());
return 1;
}
text_surface3 = TTF_RenderText_Blended_Wrapped(font, str3, textColor, 0);
if (text_surface3 == nullptr) {
printf("Could not create text surface: %s\n", TTF_GetError());
return 1;
}
text_texture3 = SDL_CreateTextureFromSurface(renderer, text_surface3);
if (text_texture3 == nullptr) {
printf("Could not create text texture: %s\n", SDL_GetError());
return 1;
}
text_surface4 = TTF_RenderText_Blended_Wrapped(font_small, nn.nnInfoString().c_str(), textColor, 0);
if (text_surface4 == nullptr) {
printf("Could not create text surface: %s\n", TTF_GetError());
return 1;
}
text_texture4 = SDL_CreateTextureFromSurface(renderer, text_surface4);
if (text_texture4 == nullptr) {
printf("Could not create text texture: %s\n", SDL_GetError());
return 1;
}
texture = createGrayscaleImageTexture(x_test.at(counter).data(), renderer);
if (texture == nullptr) {
return 1;
}
// We don't need the surface anymore
SDL_FreeSurface(text_surface);
SDL_Rect textRect = { TEXT_POS_X, TEXT_POS_Y, text_surface->w, text_surface->h };
SDL_Rect textRect2 = { TEXT_POS_X, TEXT_POS_Y+40, text_surface2->w, text_surface2->h };
SDL_Rect textRect3 = { TEXT_POS_X, TEXT_POS_Y+120, text_surface3->w, text_surface3->h };
SDL_Rect textRect4 = { TEXT_POS_X, TEXT_POS_Y-65, text_surface4->w, text_surface4->h };
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, &srcRect, &destRect);
SDL_RenderCopy(renderer, text_texture, nullptr, &textRect);
SDL_RenderCopy(renderer, text_texture2, nullptr, &textRect2);
SDL_RenderCopy(renderer, text_texture3, nullptr, &textRect3);
SDL_RenderCopy(renderer, text_texture4, nullptr, &textRect4);
SDL_RenderPresent(renderer);
if(++counter > y_test.size()) break;
std::this_thread::sleep_for(0.1s);
}
// free()
// Clean up
TTF_CloseFont(font);
SDL_DestroyTexture(texture);
// SDL_DestroyTexture(text_texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
}