-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
95 lines (66 loc) · 2.09 KB
/
evaluate.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
from ann import useModel
from knn import knn_classifier
from emnist import extract_training_samples
from imgUtils import printImage
import numpy as np
import os
from sys import argv
lettere_nostre = [2, 3, 5, 18, 21, 23]
nuoveLables = {
'2': '0',
'3': '1',
'5': '2',
'18': '3',
'21': '4',
'23': '5'
}
def ann_evaluate(test, newlables):
models = os.listdir('./Models')
risultati = []
for elem in models:
result = useModel(f"./Models/{elem}", test, toMatrix=False)
cmp = result == newlables
zeros = np.count_nonzero(cmp == False)
accuracy = 1 - (zeros/len(newlables))
risultati.append((elem, accuracy))
os.system('cls' if os.name == 'nt' else 'clear')
risultati.sort(key=lambda x: x[1])
for elem in risultati:
print(f"Model Name: {elem[0]}")
print(f"Accuracy: {elem[1]}\n")
def knn_evaluate(test, newlables):
ks = [1, 3, 5, 7, 11]
for k in ks:
result = knn_classifier(test, k=k,toMatrix=False)
cmp = result == newlables
zeros = np.count_nonzero(cmp == False)
accuracy = 1 - (zeros/len(newlables))
print(f"K: {k}")
print(f"Accuracy: {accuracy}\n")
def main(argvs):
images, labels = extract_training_samples('letters')
labels = labels - 1
newimgs = []
newlables = []
for i in range(len(labels)):
tmp = labels[i]
if tmp in lettere_nostre:
newimgs.append(np.array(images[i].flatten(), dtype="float32"))
newlables.append(tmp)
tmpLable = []
for elem in newlables:
tmp = nuoveLables[str(elem)]
tmpLable.append(int(tmp))
newlables = np.array(tmpLable, dtype="float32")
test = np.array(newimgs, dtype="float32")
if len(argvs) > 0:
if argvs[0] == "--ann":
ann_evaluate(test, newlables)
elif argvs[0] == "--knn":
knn_evaluate(test, newlables)
else:
print("Specifica --ann o --knn !")
else:
print("Specifica --ann o --knn !")
if __name__ == "__main__":
main(argv[1:])