-
Notifications
You must be signed in to change notification settings - Fork 4
/
feature_engineering_classifier.py
169 lines (142 loc) · 5.61 KB
/
feature_engineering_classifier.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
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.svm import SVC
from sklearn.preprocessing import RobustScaler
import sklearn_tda as tda
import numpy as np
from ..transforms import TranslateChunks, SmoothChunks, FlattenTo3D, Persistence, \
ExtractKeypoints, InterpolateKeypoints
from ..features import AverageSpeed, AngleChangeSpeed, AmountOfMovement, KeypointDistance
from ..util import COCOKeypoints, coco_connections
class FeatureEngineeringClassifier(BaseEstimator, ClassifierMixin):
"""Classifier for actions.
Makes use of features extracted from the data using other vectorisations
from sklearn_tda, and from the features module.
Parameters
----------
use_tda_vectorisations : boolean, optional, default = False
Determines if the vectorisations from the sklearn_tda library
should be used. *Note*: Can only use one thread, since some
parts of sklearn_tda vectorisations are not pickable. This also
means that the model can't be saved to disk.
"""
def __init__(self, use_tda_vectorisations=False):
self.use_tda_vectorisations = use_tda_vectorisations
self.keypoint_distance_connections = [(k1.value, k2.value) for k1, k2 in [
(COCOKeypoints.RWrist, COCOKeypoints.LWrist),
(COCOKeypoints.RElbow, COCOKeypoints.LElbow),
(COCOKeypoints.Neck, COCOKeypoints.LAnkle),
(COCOKeypoints.Neck, COCOKeypoints.RAnkle),
(COCOKeypoints.LWrist, COCOKeypoints.LAnkle),
(COCOKeypoints.RWrist, COCOKeypoints.RAnkle)
]]
self.angle_change_connections = np.array(coco_connections)
self.speed_keypoints = range(14)
self.selected_keypoints = [k.value for k in [
COCOKeypoints.Neck,
COCOKeypoints.RWrist,
COCOKeypoints.LWrist,
COCOKeypoints.LAnkle,
COCOKeypoints.RAnkle
]]
self.keypoint_connections = [(0, 1), (2, 3), (4, 5), (6, 7)]
def fit(self, X, y, **fit_params):
"""Fit the model.
Parameters
----------
X : iterable
Training data
y : iterable
Training labels.
fit_params : dict
ignored for now.
Returns
-------
self
"""
classifier = Pipeline([
("Union", self._feature_engineering_union()),
("Estimator", SVC(probability=True))
])
self.classifier = classifier.fit(X, y)
self.classes_ = classifier.classes_
return self
def predict(self, X):
"""Predicts using the pipeline.
Parameters
----------
X : iterable
Data to predict labels for.
Returns
-------
y_pred : array-like
"""
return self.classifier.predict(X)
def predict_proba(self, X):
"""Predicts using the pipeline.
Parameters
----------
X : iterable
Data to predict labels for.
Returns
-------
y_proba : array-like, shape = [n_samples, n_classes]
"""
return self.classifier.predict_proba(X)
def _feature_engineering_union(self):
transformer_list = [
("AverageSpeed", Pipeline([
("Feature", AverageSpeed(self.speed_keypoints)),
("Scaler", RobustScaler())
])),
("AngleChangeSpeed", Pipeline([
("Feature", AngleChangeSpeed(self.angle_change_connections)),
("Scaler", RobustScaler())
])),
("Movement", Pipeline([
("Feature", AmountOfMovement(range(14))),
("Scaler", RobustScaler())
])),
("KeypointDistance", Pipeline([
("Feature", KeypointDistance(self.keypoint_distance_connections)),
("Scaler", RobustScaler())
]))
]
if self.use_tda_vectorisations:
transformer_list.append(("TDAVectorisations", self._tda_vectorisations_pipeline()))
return FeatureUnion(transformer_list)
def _tda_vectorisations_pipeline(self):
persistence_image = Pipeline([
("Rotator", tda.DiagramPreprocessor(scaler=tda.BirthPersistenceTransform())),
("PersistenceImage", tda.PersistenceImage()),
("Scaler", RobustScaler())
])
return Pipeline([
("Translate", TranslateChunks()),
("Extract", ExtractKeypoints(self.selected_keypoints)),
("Smoothing", SmoothChunks()),
("Flattening", FlattenTo3D()),
("Persistence", Persistence(max_alpha_square=1, complex_='alpha')),
("Separator", tda.DiagramSelector(limit=np.inf, point_type="finite")),
("Prominent", tda.ProminentPoints()),
("Union", FeatureUnion([
("PersistenceImage", persistence_image),
("Landscape", Pipeline([
("TDA", tda.Landscape(resolution=10)),
("Scaler", RobustScaler())
])),
("TopologicalVector", Pipeline([
("TDA", tda.TopologicalVector()),
("Scaler", RobustScaler())
])),
("Silhouette", Pipeline([
("TDA", tda.Silhouette()),
("Scaler", RobustScaler())
])),
("BettiCurve", Pipeline([
("TDA", tda.BettiCurve()),
("Scaler", RobustScaler())
]))
])),
("Scaler", RobustScaler())
])