-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw_numbers.py
More file actions
112 lines (86 loc) · 3.34 KB
/
Copy pathdraw_numbers.py
File metadata and controls
112 lines (86 loc) · 3.34 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
import numpy as np
import pandas as pd
import pygame
from neural_network import *
import pygame
from pygame.locals import QUIT
pygame.init()
WHITE = (255,255,255)
BLACK = (0,0,0)
clock = pygame.time.Clock()
framerate = 200
font = pygame.font.SysFont("Arial Black", 30)
def redraw(screen):
screen.fill((70, 70, 70))
# draw white rectangle (canvas background)
for i in range(28):
for j in range(28):
value = canvas[i][j]
value = 255 - 255*value
value = max(0,value)
pygame.draw.rect(screen, (value,value,value), (i*gridsize*28 + 50, j*gridsize*28 + 50, 28*gridsize,28*gridsize))
pygame.display.update()
def draw(gridsize, mousex, mousey, canvas, mid_increment, edge_increment):
for i in range(28):
for j in range(28):
if ((mousex >= j*gridsize*28 + 50 and mousex <= j*gridsize*28 + 50 + gridsize*28) and
(mousey >= i*gridsize*28 + 50 and mousey <= i*gridsize*28 + 50 + gridsize*28)):
canvas[j][i] += mid_increment
canvas[j-1][i] += edge_increment
canvas[j+1][i] += edge_increment
canvas[j][i-1] += edge_increment
canvas[j][i+1] += edge_increment
width = 800
height = 500
screen = pygame.display.set_mode((width, height))
gridsize = 0.5
inPlay = True
clicking = False
canvas = [[0] * 28 for i in range(28)]
while inPlay:
if clicking:
for i in range(200):
draw(gridsize, mousex, mousey, canvas, 0.0002, 0.0001)
if clicking:
for i in range(28):
for j in range(28):
if ((mousex >= j*gridsize*28 + 50 and mousex <= j*gridsize*28 + 50 + gridsize*28) and
(mousey >= i*gridsize*28 + 50 and mousey <= i*gridsize*28 + 50 + gridsize*28)):
canvas[j][i] += 0.01
redraw(screen)
clock.tick(framerate)
mousex,mousey = pygame.mouse.get_pos()
for event in pygame.event.get():
pressed = pygame.key.get_pressed()
if event.type == QUIT:
pygame.quit()
inPlay = False
# used to check if buttons are pressed
if event.type == pygame.MOUSEBUTTONDOWN:
clicking = True
if clicking:
pass
# canvas[j-1][i] += 0.1
# canvas[j+1][i] += 0.1
# canvas[j][i-1] += 0.1
# canvas[j][i+1] += 0.1
# if release mouse button
if event.type == pygame.MOUSEBUTTONUP:
clicking = False
if pressed[pygame.K_BACKSPACE]:
for i in range(len(canvas)):
for j in range(len(canvas[0])):
canvas[i][j] = 0
if pressed[pygame.K_RETURN]:
inp = []
for i in range(len(canvas)):
inp += canvas[i]
inp = np.matrix(inp).reshape(784,1)
# Forward propagation input -> hidden
h_pre = bih + wih @ inp
h = ReLU(h_pre)
# Forward propagation hidden -> output
o_pre = bho + who @ h
o = softmax(o_pre)
print("Prediction:", o.argmax())
print(o)