-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw.cpp
157 lines (127 loc) · 3.14 KB
/
draw.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
/**
* @file draw.cpp
* @author
* @brief This file lets you draw a number real time and see what is predicted.
* @version 0.1
* @date 2022-02-22
*
* @copyright Copyright (c) 2022
*
* To compile:
* g++ -std=c++11 draw.cpp -o sketch -Iinclude -Llib -lxd -lglad -lglfw3 -lrt -lm -ldl -lX11 -lpthread
*
* Copy a trained number neural network to same folder named "trained.net"
* (This can be constructed via examples/MNIST/mnist.cpp)
*
* run:
* ./sketch
*
*/
#include <xd/xd.hpp>
#include <vector>
#include "../include/crank.h"
#include "../include/mnist/mnist.h"
#include <string>
using namespace xd;
MNIST_DATASET * dataset;
static const int cols = 600;
static const int rows = 600;
float mouseX;
float mouseY;
static bool pressed = false;
static int prediction = 0;
std::vector<std::vector<bool>> mypixels;
NeuralNetworkFF net("trained.net");
int make_prediction(std::vector<std::vector<bool>> & pix){
std::vector<double> input(784);
int index = 0;
for(int i = 0; i < 28; ++i){
for(int j = 0; j < 28; ++j){
input[index] = pix[i][j] ? 1 : 0;
dataset->test_images[0][index] = pix[i][j] ? 100 : 0;
++index;
}
}
dataset->display_image(0, dataset->test_images);
auto result = net.forwardPass(input);
int max_index = 0;
double max = result[0];
for(int i = 0; i < 10; ++i){
std::cout << result[i] << " ";
if(result[i] > max){
max = result[i];
max_index = i;
}
}
std::cout << std::endl;
return max_index;
}
void onKeyPressed(int key){
for(int i = 0; i < 28; ++i){
for(int j = 0; j < 28; ++j){
mypixels[i][j] = false;
}
}
}
void onMouseMoved(float x, float y)
{
mouseX = x;
mouseY = y;
}
void onMousePressed(int button)
{
pressed = true;
}
void onMouseReleased(int button)
{
pressed = false;
}
void setup()
{
dataset = read_dataset();
mypixels = std::vector<std::vector<bool>>(28, std::vector<bool>(28, false));
mouseReleased(onMouseReleased);
mouseMoved(onMouseMoved);
mousePressed(onMousePressed);
keyPressed(onKeyPressed);
size(cols, rows);
}
void draw()
{
if (pressed)
{
int row = round((mouseY / rows) * 28);
int col = round((mouseX / cols) * 28);
mypixels[row][col] = true;
}
background(vec4(255));
stroke(vec4(0, 0, 0, 100));
strokeWeight(2);
for (double i = 0; i < cols; i += (cols / 28.0))
{
line(i, 0, i, rows);
}
for (double i = 0; i < rows; i += (rows / 28.0))
{
line(0, i, cols, i);
}
fill(vec4(0, 0, 0, 255));
for (int row = 0; row < mypixels.size(); ++row)
{
for (int col = 0; col < mypixels[0].size(); ++col)
{
if (mypixels[row][col])
{
rect(col * (cols / 28.0), row * (rows / 28.0), (cols / 28.0), (rows / 28.0));
}
}
}
prediction = make_prediction(mypixels);
fill(vec4(255));
rect(0, 0, 50, 50);
fill(vec4(0, 0, 0, 100));
text(std::to_string(prediction), 5.0f, 20.0f);
}
void destroy()
{
}