-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_graphical.lua
More file actions
172 lines (143 loc) · 3.93 KB
/
test_graphical.lua
File metadata and controls
172 lines (143 loc) · 3.93 KB
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
local function requireModules()
require "lib.general"
require "lib.inject"
require "log"
require "utils"
require "nn.nn"
require "nn.gfx"
require "nn.data"
end
local function setupInjection()
inject:Setup()
inject:Bind("NeuralNetwork", nn.NeuralNetwork)
end
local network
local scratchpad
function init()
requireModules()
setupInjection()
network = nn.data.loadNeuralNetwork(arg[2])
scratchpad = {}
for i in range(1, 784) do
scratchpad[i] = math.random()
end
local font = love.graphics.newFont(36)
love.graphics.setFont(font)
end
local scratchpad_square_x = 650
local scratchpad_square_y = 200
local scratchpad_scale = 16
local function panScratchpad(dx, dy)
local new_scratchpad = {}
for y = 0, 27 do
local new_y = y + dy
if new_y < 0 then new_y = new_y + 28 end if new_y >= 28 then new_y = new_y - 28 end
for x = 0, 27 do
local new_x = x + dx
if new_x < 0 then new_x = new_x + 28 end
if new_x >= 28 then new_x = new_x - 28 end
local p = y * 28 + x + 1
local new_p = new_y * 28 + new_x + 1
new_scratchpad[new_p] = scratchpad[p]
end
end
scratchpad = new_scratchpad
end
function update(dt)
if love.keyboard.isDown "space" then
for i in range(1, 784) do
scratchpad[i] = 0
end
end
-- Drawing on scratchpad
if love.mouse.isDown(1) then
local mx, my = love.mouse.getPosition()
if (mx >= scratchpad_square_x) and (my >= scratchpad_square_y)
and (mx < scratchpad_square_x + scratchpad_scale * 28)
and (my < scratchpad_square_y + scratchpad_scale * 28) then
mx = mx - scratchpad_square_x
my = my - scratchpad_square_y
mx = math.floor(mx / scratchpad_scale)
my = math.floor(my / scratchpad_scale)
for yy = -1, 1 do
if my + yy >= 0 and my + yy < 28 then
for xx = -1, 1 do
if mx + xx >= 0 and mx + xx < 28 then
local idx = (my + yy) * 28 + (mx + xx) + 1
scratchpad[idx] = scratchpad[idx] + 0.2
if scratchpad[idx] >= 1 then
scratchpad[idx] = 1
end
end
end
end
end
end
network:activate(scratchpad)
end
if love.keyboard.isDown "up" then
panScratchpad(0, -1)
network:activate(scratchpad)
elseif love.keyboard.isDown "down" then
panScratchpad(0, 1)
network:activate(scratchpad)
elseif love.keyboard.isDown "left" then
panScratchpad(-1, 0)
network:activate(scratchpad)
elseif love.keyboard.isDown "right" then
panScratchpad(1, 0)
network:activate(scratchpad)
end
end
function draw()
local curr = i
love.graphics.setLineWidth(4)
local w, h = love.window.getMode()
network:draw(0, 0, w / 2, h, 2)
-- Draw scratchpad
love.graphics.setLineWidth(1)
for y = 0, 27 do
for x = 0, 27 do
local col = scratchpad[y * 28 + x + 1] * 0.8 + 0.2
love.graphics.setColor(col, col, col)
love.graphics.rectangle("fill",
scratchpad_square_x + x * scratchpad_scale,
scratchpad_square_y + y * scratchpad_scale,
scratchpad_scale, scratchpad_scale
)
end
end
love.graphics.setColor(0, 0, 0)
for y = 0, 27 do
love.graphics.line(
scratchpad_square_x, scratchpad_square_y + scratchpad_scale * y,
scratchpad_square_x + 28 * scratchpad_scale, scratchpad_square_y + scratchpad_scale * y
)
end
for x = 0, 27 do
love.graphics.line(
scratchpad_square_x + scratchpad_scale * x, scratchpad_square_y,
scratchpad_square_x + scratchpad_scale * x, scratchpad_square_y + scratchpad_scale * 28
)
end
local highestConfidence = 0
local whichNumber = -1
for i = 1, 10 do
local con = network.network[#network.network].neurons[i].value
if con > highestConfidence then
highestConfidence = con
whichNumber = i - 1
end
end
love.graphics.setColor(1, 1, 1)
love.graphics.printf(
tostring(whichNumber) .. ", confidence: " .. tostring(highestConfidence * 100) .. "%",
scratchpad_square_x, scratchpad_square_y - 120, scratchpad_scale * 28, "center"
)
love.graphics.printf("Made by:\nBrendan Hansen\nCollin Rumpca", w / 2, h - 150, w / 2, "center")
end
return {
init = init;
update = update;
draw = draw;
}