-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict_all.py
More file actions
89 lines (59 loc) · 2.55 KB
/
predict_all.py
File metadata and controls
89 lines (59 loc) · 2.55 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
71
72
73
74
75
76
77
from pystruct.learners import OneSlackSSVM, NSlackSSVM
from pystruct.models import EdgeFeatureGraphCRF
from options import *
import pickle
import numpy as np
from sklearn.metrics import confusion_matrix, accuracy_score
import time
import csv
results = []
# k nearest neighbors
for k in [1, 2, 3, 4, 5, 6, 7, 8, 9, 100]:
print("K: {} nearest neighbors".format(k))
# activity features
for pattern in range(3):
print("Level {} activity features".format(pattern))
X = []
Y = []
for vid_ in sorted(os.listdir(param_fdp_videos)):
vid = vid_[:-4]
x, y = pickle.load(open(os.path.join(param_fdp_results, "{}_{}_{}.npd".format(k, pattern, vid)), "rb"))
X.append(x)
Y.append(y)
X = np.concatenate(X, axis=0)
Y = np.concatenate(Y, axis=0)
train_inds = []
test_inds = []
for i in range(len(X)):
if i % 2 == 0:
test_inds.append(i)
else:
train_inds.append(i)
train_inds = np.array(train_inds)
test_inds = np.array(test_inds)
X_train, X_test = X[train_inds], X[test_inds]
Y_train, Y_test = Y[train_inds], Y[test_inds]
crf = EdgeFeatureGraphCRF()
ssvm = NSlackSSVM(crf, check_constraints=False, max_iter=50, batch_size=1, tol=0.1, n_jobs=-1, verbose=3)
print("Training started")
start = time.time()
ssvm.fit(X_train, Y_train)
train_time = time.time() - start
print("Training completed and testing started")
print("Time used for training: {}".format(train_time))
pickle.dump(ssvm, open(os.path.join(param_fdp_results, "results_{}_{}_model.p".format(k, pattern)), "wb"))
start = time.time()
Y_pred = ssvm.predict(X_test)
test_time = time.time()-start
acc = accuracy_score(np.hstack(Y_test), np.hstack(Y_pred))
cm = confusion_matrix(np.hstack(Y_test), np.hstack(Y_pred))
pickle.dump(cm, open(os.path.join(param_fdp_results, "results_{}_{}_cm.p".format(k, pattern)), "wb"))
print("Test completed with an accuracy: %.3f" % acc)
print(cm)
print("Time used for testing: {}".format(test_time))
results.append([k, pattern, acc, train_time, test_time, len(X_train), len(X_test)])
with open(os.path.join(param_fdp_results, "log.csv".format(k, pattern)), "w") as myfile:
wr = csv.writer(myfile)
wr.writerow(["K", "act_feature", "acc", "time_train", "time_test", "X_train", "X_test"])
for r in results:
wr.writerow(r)