-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model.py
More file actions
70 lines (61 loc) · 2.57 KB
/
Copy pathtest_model.py
File metadata and controls
70 lines (61 loc) · 2.57 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
import init_path
from socket import *
from select import *
import sys, os
import csv
import numpy as np
import readData, tools
from time import ctime, gmtime, strftime
from keras.models import Sequential, model_from_json
from keras.layers import LSTM, Dense
import tools
active_class = ['stop', 'motion']
motion_class = ['left', 'right', 'left_r', 'right_r', 'up', 'down', 'arm_down', 'arm_up']
print "model load..."
filePath = os.path.join("server", "result", "active_model_config.json");
active_model = model_from_json(open(filePath).read())
filePath = os.path.join("server", "result", "active_model_weight.h5");
active_model.load_weights(filePath)
active_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=["accuracy"])
filePath = os.path.join("server", "result", "motion_model_config.json");
motion_model = model_from_json(open(filePath).read())
filePath = os.path.join("server", "result", "motion_model_weight.h5");
motion_model.load_weights(filePath)
motion_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=["accuracy"])
print "model load complete"
# test_on_batch
m_threshold = 0.5
a_threshold = 0.5
def main():
# sensor_test_data = readData.read_sensor_data("data/processed_data/all_test_active.txt");
timesteps = 30
data_dim = 6
nb_classes = 2
sensor_test_data = readData.read_sensor_data("server/data/demo_data.txt");
# X_test, y_test = tools.data_processing(sensor_test_data, timesteps, data_dim, nb_classes);
# val = active_model.evaluate(X_test, y_test, batch_size=1, show_accuracy=True)
f_out = open("demo_output.txt", "w")
sensorWindow = np.zeros((1, timesteps, data_dim));
# print sensor_test_data.num_data-30
for i in xrange(0, sensor_test_data.num_data-30):
sensorWindow[0] = sensor_test_data.all_features[i:i+30]
# print sensorWindow.shape
softmax_active = active_model.predict(sensorWindow)
# print softmax_active
a_index = np.where(softmax_active[0]==max(softmax_active[0]))[0][0]
# print max(softmax_active), active_class[index]
softmax_motion = motion_model.predict(sensorWindow)
m_index = np.where(softmax_motion[0]==max(softmax_motion[0]))[0][0]
if a_index == 1:
if max(softmax_motion[0]) > m_threshold:
output = motion_class[m_index] + " " + str(max(softmax_motion[0])) + "\n"
f_out.write(output)
print motion_class[m_index], max(softmax_motion[0])
else:
if max(softmax_active[0]) > a_threshold:
output = active_class[a_index] + " " + str(max(softmax_active[0])) + "\n"
f_out.write(output)
print active_class[a_index], max(softmax_active[0])
f_out.close();
if __name__ == "__main__":
main()