-
Notifications
You must be signed in to change notification settings - Fork 80
/
measure_performance.py
57 lines (45 loc) · 1.71 KB
/
measure_performance.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
from CNN.utils import *
from tqdm import tqdm
import argparse
import matplotlib.pyplot as plt
import pickle
parser = argparse.ArgumentParser(description='Predict the network accuracy.')
parser.add_argument('parameters', metavar = 'parameters', help='name of file parameters were saved in. These parameters will be used to measure the accuracy.')
if __name__ == '__main__':
args = parser.parse_args()
save_path = args.parameters
params, cost = pickle.load(open(save_path, 'rb'))
[f1, f2, w3, w4, b1, b2, b3, b4] = params
# Get test data
m =10000
X = extract_data('t10k-images-idx3-ubyte.gz', m, 28)
y_dash = extract_labels('t10k-labels-idx1-ubyte.gz', m).reshape(m,1)
# Normalize the data
X-= int(np.mean(X)) # subtract mean
X/= int(np.std(X)) # divide by standard deviation
test_data = np.hstack((X,y_dash))
X = test_data[:,0:-1]
X = X.reshape(len(test_data), 1, 28, 28)
y = test_data[:,-1]
corr = 0
digit_count = [0 for i in range(10)]
digit_correct = [0 for i in range(10)]
print()
print("Computing accuracy over test set:")
t = tqdm(range(len(X)), leave=True)
for i in t:
x = X[i]
pred, prob = predict(x, f1, f2, w3, w4, b1, b2, b3, b4)
digit_count[int(y[i])]+=1
if pred==y[i]:
corr+=1
digit_correct[pred]+=1
t.set_description("Acc:%0.2f%%" % (float(corr/(i+1))*100))
print("Overall Accuracy: %.2f" % (float(corr/len(test_data)*100)))
x = np.arange(10)
digit_recall = [x/y for x,y in zip(digit_correct, digit_count)]
plt.xlabel('Digits')
plt.ylabel('Recall')
plt.title("Recall on Test Set")
plt.bar(x,digit_recall)
plt.show()