-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize_features.py
265 lines (236 loc) · 8.2 KB
/
visualize_features.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from src import dataset, dataviz, humoments, image_io, mhi, extraction, model
import numpy as np
import cv2
import os
import pickle
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix
data_dir="images/action_dataset"
pickle_file='data_viz_no_close_05.pkl'
data = dataset.Dataset(data_dir)
def plot_action_features():
data = dataset.Dataset(data_dir)
moments = []
for act in data.actions:
mhi_extractor = mhi.MHI()
images_action = image_io.get_image_collection(data.get(1, act))
images_H = mhi_extractor.process(images_action)[-1]
cv2.imshow("H value for %s" % act, images_H / 255.)
print("Min H", np.min(images_H))
print("Max H", np.max(images_H))
images_moments = humoments.hu_moments(images_H)
moments.append(np.array(images_moments))
moments = np.vstack(moments)
dataviz.plot(moments)
cv2.waitKey(0)
i=0
image_sets = []
img_ax = [None]*6
pickle_file='H_no_blur_no_open.pkl'
# optional arguments: blur, use_open, use_close, theta, tau
def display_mhi():
global image_sets, img_ax, data
for act in data.actions:
images = image_io.get_image_collection(data.get(13, act), blur=True)
mhi_extractor = mhi.MHI(tau=20, theta=0.1, use_open=True, use_close=True)
H_values = []
for img in images:
H_values.append(mhi_extractor.add_image(img))
image_sets.append(H_values)
# images = np.array(images)
fig, axes = plt.subplots(2, 3, sharex=False, sharey=True)
for j, act in enumerate(data.actions):
ax = axes[j // 3, np.mod(j, 3)]
ax.set_title(act)
idx = 10
img = np.zeros([120, 160]) if image_sets[j][idx] is None else image_sets[j][idx] / 255.
img_ax[j] = ax.imshow(img)
fig.show()
def updatefig(*args):
global image_sets, data, img_ax, i
i+=1
for j, act in enumerate(data.actions):
idx = np.minimum(i, len(image_sets[j])-1)
# idx = 30
# img = np.zeros([120, 160]) if image_sets[j][idx] is None else image_sets[j][idx] / 255.
if image_sets[j][idx] is None:
continue
img_ax[j].set_array(image_sets[j][idx] / 255.)
return img_ax[0], img_ax[1], img_ax[2], img_ax[3], img_ax[4], img_ax[5]
ani = animation.FuncAnimation(fig, updatefig, interval=100, blit=True)
plt.show()
def display_mei_mhi():
global image_sets, img_ax, data
images = image_io.get_image_collection(data.get(13, 'boxing'), blur=True)
mhi_extractor = mhi.MHI(tau=20, theta=0.04, use_open=True, use_close=True)
H_values = []
for img in images:
H = mhi_extractor.add_image(img)
if H is not None:
H_values.append(H)
# images = np.array(images)
fig, axes = plt.subplots(1, 2, sharex=False, sharey=True)
idx = 30
img_ax[0] = axes[0].imshow(H_values[idx] / 255.)
img_ax[1] = axes[1].imshow(np.where(H_values[idx] > 0, 255, 0))
fig.show()
def updatefig(*args):
global image_sets, data, img_ax, i
i+=1
idx = np.minimum(i, len(H_values)-1)
img_ax[0].set_array(H_values[idx] / 255.)
img_ax[1].set_array(np.where(H_values[idx] > 0, 255, 0))
return img_ax[0], img_ax[1]
ani = animation.FuncAnimation(fig, updatefig, interval=100, blit=True)
plt.show()
# Plots a graph of the mean value of each feature for each label
def plot_features():
if not os.path.exists(pickle_file):
moments = []
X, y, images, _= extraction.extract_data(data, [13, 14, 15, 19, 20])
pickle.dump({'X': X, 'y': y, "images": images}, open(pickle_file, 'wb'))
else:
d = pickle.load(open(pickle_file, 'rb'))
X = d['X']
y = d['y']
images = d['images']
print("Data labels:")
print(data.get_labels())
dataviz.plot_feature_averages(X, y)
dataviz.plot_feature_boxplot(X, y)
def display_labelled_images():
# pickle_file='labelled_images_13.pkl'
# if not os.path.exists(pickle_file):
# moments = []
# X, y, Hs, images = extraction.extract_data(data, [13])
# pickle.dump({'X': X, 'y': y, "H": Hs, "images": images}, open(pickle_file, 'wb'))
# else:
# d = pickle.load(open(pickle_file, 'rb'))
# X = d['X']
# y = d['y']
# images = d['images']
idx = 8
print("This works, rihgt?")
tau_dict = {'running': 30, 'handwaving': 30, 'walking': 22, 'jogging': 25, 'boxing': 15, 'handclapping': 25}
trainer = model.Trainer(data_dir, tau=tau_dict, get_data_on_init=False)
print("Ok, got here")
trainer._classifier = pickle.load(open('trained_multiple_tau_adjusted.pkl', 'rb'))
# Change this for every action
act = data.actions[5]
print("Loaded classifier")
images = image_io.get_image_collection(data.get(idx, act))
trainer.reset_predictor()
print("Going into labels:")
dataviz.show_label(images, lambda im: trainer.predict(im), data.actions, show_plot=True)
# plot.show()
def compare_moments():
# Pick random indices from the training data
pickle_file = "compare_moments.pkl"
if not os.path.exists(pickle_file):
indices = [13]
X_values = []
y_values = []
labels = data.get_labels()
for idx in indices:
image_database = pickle.load(open('image_sequences_%d.pkl' % idx, 'rb'))
for act in image_database['image_seqs']:
images_action = image_database['image_seqs'][act]
for dval in images_action:
mhi_obj = mhi.MHI()
images_dval = images_action[dval]
for img in images_dval:
mhi_obj.add_image(img)
H = mhi_obj.get_H_sequence()
# MEI = mhi.get_MEI_sequence()
H_moments = humoments.hu_moments(np.array(H))
H_labels = np.array([labels[act]] * H_moments.shape[1])
X_values.append(H_moments.T)
y_values.append(H_labels)
X = np.vstack(X_values)
X_sign_log = extraction.signed_log(X.T).T
X_sign_root = extraction.signed_roots(X.T).T
X /= np.mean(X, axis=1, keepdims=True)
print("X shape")
print(X.shape)
y = np.concatenate(y_values)
pickle.dump({"X": X, "X_sign_log": X_sign_log, "X_sign_root": X_sign_root, "y": y}, open(pickle_file, 'wb'))
else:
d = pickle.load(open(pickle_file, 'rb'))
X = d['X']
X_sign_log = d['X_sign_log']
X_sign_root = d['X_sign_root']
y = d['y']
print("Shapes:")
print(X.shape)
print(y.shape)
dataviz.plot_feature_boxplot(X, y)
dataviz.plot_feature_boxplot(X_sign_log, y)
dataviz.plot_feature_boxplot(X_sign_root, y)
for X_in in [X, X_sign_log, X_sign_root]:
X_train, X_test, y_train, y_test = train_test_split(X_in, y, test_size = 0.2)
parameters = {'C':[1, 10, 100, 1000]}
svm = SVC()
cv = GridSearchCV(svm, parameters)
cv.fit(X_train, y_train)
print("Best Params")
print(cv.best_params_)
print("CV score")
print(cv.score(X_test, y_test))
print("Confusion matrix")
y_pred = cv.predict(X_test)
conf = confusion_matrix(y_test, y_pred)
dataviz.plot_confusion_matrix(conf, data.actions)
plt.show()
def best_tau_per_action():
large_tau=50
H = []
y= []
for i in [13, 14]:
for j, act in enumerate(data.actions):
images = image_io.get_image_collection(data.get(i, act), blur=True)
mhi_extractor = mhi.MHI(tau=large_tau, theta=0.04, use_open=True, use_close=True)
H_values = []
for img in images:
mhi_extractor.add_image(img)
H_values = mhi_extractor.get_H_sequence()
H += H_values
y+= [j] * len(H_values)
H = np.array(H)
y= np.array(y)
pickle_file = 'best_tau.pkl'
def test_H(MHI, y, tau):
one_iter = 255. / large_tau
H_normal = MHI - (large_tau - tau) * one_iter
H_normal = np.maximum(H_normal, 0) * 255. / tau
MEI_normal = np.where(H_normal > 0, 255, 0)
hu_h_moments = extraction.signed_roots(humoments.hu_moments(H_normal)).T
hu_mei_moments = extraction.signed_roots(humoments.hu_moments(MEI_normal)).T
X = np.hstack([hu_h_moments, hu_mei_moments])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
svm = SVC()
svm.fit(X_train, y_train)
scores = [None] * len(data.actions)
for l in range(np.max(y)+1):
scores[l] = svm.score(X_test[y_test==l], y_test[y_test==l])
return scores
score_matrix = []
for j in range(1, 30):
print(j)
score_matrix.append(test_H(H, y, j))
score_matrix = np.vstack(score_matrix)
print("Score matrix")
for row in score_matrix:
print("%.03f %.03f %.03f %.03f %.03f %0.03f" % (row[0], row[1], row[2], row[3], row[4], row[5]))
print(score_matrix)
if __name__ == "__main__":
plot_features()
display_mhi()
display_mei_mhi()
display_labelled_images()
plot_action_features()
compare_moments()
best_tau_per_action()