-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
68 lines (44 loc) · 1.63 KB
/
predict.py
File metadata and controls
68 lines (44 loc) · 1.63 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
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
X = []
Y = []
# a1, only use a1 features
# a2, only use a2 features
# all, use a1 and a2 features
pattern = "a2"
for vid in os.listdir(os.path.join(param_fdp_labels, pattern)):
if os.path.splitext(vid)[1] == '.npd':
x, y = pickle.load(open(os.path.join(param_fdp_labels, 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]
start = time.time()
print(start)
# crf = EdgeFeatureGraphCRF(inference_method='qpbo')
# ssvm = OneSlackSSVM(crf, inference_cache=50, C=.1, tol=.1, switch_to='ad3', n_jobs=-1, verbose=4)
crf = EdgeFeatureGraphCRF()
ssvm = NSlackSSVM(crf, check_constraints=False, max_iter=50, batch_size=1, tol=0.1, n_jobs=-1, verbose=4)
ssvm.fit(X_train, Y_train)
pickle.dump(ssvm, open(os.path.join(param_fdp_labels, pattern, "ssvm.model"), "wb"))
Y_pred2 = ssvm.predict(X_test)
print("Results using also input features for edges")
print("Test accuracy: %.3f" % accuracy_score(np.hstack(Y_test), np.hstack(Y_pred2)))
print(confusion_matrix(np.hstack(Y_test), np.hstack(Y_pred2)))
print("Time used: {}".format(time.time()-start))