forked from hamuchiwa/AutoRCCar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlp_predict_test.py
51 lines (41 loc) · 1.29 KB
/
mlp_predict_test.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
__author__ = 'zhengwang'
import cv2
import numpy as np
import glob
# load training data
image_array = np.zeros((1, 38400))
label_array = np.zeros((1, 4), 'float')
training_data = glob.glob('testing_data/*.npz')
for single_npz in training_data:
with np.load(single_npz) as data:
print data.files
test_temp = data['train']
test_labels_temp = data['train_labels']
print test_temp.shape
print test_labels_temp.shape
image_array = np.vstack((image_array, test_temp))
label_array = np.vstack((label_array, test_labels_temp))
test = image_array[1:, :]
test_labels = label_array[1:, :]
print test.shape
print test_labels.shape
# create MLP
layer_sizes = np.int32([38400, 32, 4])
model = cv2.ANN_MLP()
model.create(layer_sizes)
model.load('mlp_xml/mlp.xml')
# generate predictions
e0 = cv2.getTickCount()
ret, resp = model.predict(test)
prediction = resp.argmax(-1)
e00 = cv2.getTickCount()
time0 = (e00 - e0)/cv2.getTickFrequency()
print 'Prediction time per frame:', time0/(test.shape[0])
print 'Prediction:', prediction
true_labels = test_labels.argmax(-1)
print 'True labels:', true_labels
print 'Testing...'
num_correct = np.sum( true_labels == prediction )
print(num_correct)
test_rate = np.mean(prediction == true_labels)
print 'Test rate: %f' % (test_rate*100)