-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
120 lines (100 loc) · 3.63 KB
/
helpers.py
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
import csv
import os
import sys
import neural as nn
import image_pro.data as image
import config as cfg
import glob
__author__ = 'timothy'
# Normalizes the results into a confidence matrix
def normalize(array):
sum = 0
for n in array:
sum += n
for i in range(len(array)):
array[i] = array[i]/sum
return array
# Loads previously converted training data
def load_training_data():
# Check that training data has been converted:
if not os.access("./test_data/hearts/converted.csv", os.R_OK) or \
not os.access("./test_data/laugh/converted.csv", os.R_OK) or \
not os.access("./test_data/sad/converted.csv", os.R_OK) or \
not os.access("./test_data/smile/converted.csv", os.R_OK):
print("training data can't be accessed. Make sure it's converted")
sys.exit()
#initialize training data
readers = [
csv.reader(open("./test_data/hearts/converted.csv")),
csv.reader(open("./test_data/laugh/converted.csv")),
csv.reader(open("./test_data/sad/converted.csv")),
csv.reader(open("./test_data/smile/converted.csv")),
]
training_inputs = [[], [], [], []]
for i in range(4):
for row in readers[i]:
inp = []
for el in row:
if el == "0":
inp.append(0)
else:
inp.append(1)
training_inputs[i].append(inp)
return training_inputs
# run an image through the net and pretty print the results
def test_image(path, net):
# Run the image
inp = image.convert_to_1d(image.binary_image(path, cfg.RES))
result = net.forward_pass(inp)
normalize(result)
# print out result
print(path + ":")
print(" heart eyes:" + str(format((result[0]*100), '.2f')) + "%")
print(" laugh:" + str(format((result[1]*100), '.2f')) + "%")
print(" sad:" + str(format((result[2]*100), '.2f')) + "%")
print(" smile:" + str(format((result[3]*100), '.2f')) + "%")
# test net with preset testing data
def training_results(net):
# choose an emotion:
for emotion in cfg.emojis:
# go through the test cases
print("_____________________________")
print(emotion + ": ")
test_cases = glob.glob('./test_data/non-training/' + emotion + '/*.png')
for test_case in test_cases:
# run the test case through the net
test_image(test_case, net)
print("_____________________________")
# get the average squared error from the testing data
def get_testing_error(net):
# choose an emotion:
error = [0.0, 0.0, 0.0, 0.0]
for i, emotion in enumerate(cfg.emojis):
test_cases = glob.glob('./test_data/non-training/' + emotion + '/*.png')
for test_case in test_cases:
inp = image.convert_to_1d(image.binary_image(test_case, cfg.RES))
e = net.forward_pass(inp)
error[i] += nn.error(cfg.outputs[i], e)
error[i] /= len(test_cases)
return sum(error)
def save_net(net, name):
# choose an emotion
path = "./nets/" + name
data = net.get_weights()
with open(path, 'wb+') as csv_file:
weight_writer = csv.writer(csv_file, delimiter=',')
weight_writer.writerow(data)
def load_net(name):
print("Loading net...")
path = "./nets/" + name
input = open(path, "r")
net = nn.init_net()
weights = []
with open(path, 'rb') as csv_file:
weight_reader = csv.reader(csv_file, delimiter=',')
for row in weight_reader:
for el in row:
weights.append(float(el))
net.put_weights1d(weights)
print("Net loaded!")
return net