-
Notifications
You must be signed in to change notification settings - Fork 7
/
ai_trainer_nb.py
245 lines (181 loc) · 9.49 KB
/
ai_trainer_nb.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
import pandas as pd
import numpy as np
import pickle
import sys
import os
from sklearn import preprocessing
from pprint import pprint
from collections import defaultdict
sys.path.append('data')
sys.path.append('inference')
sys.path.append('feedback')
#=====[Preppend ../ for notebook]=====
sys.path.append('../inference')
sys.path.append('../data')
sys.path.append('../feedback')
#=====[ Import our utils ]=====
import rep_separation as rs
import featurizer as fz
import pu_featurizer as pfz
import utils as ut
import result_interpretation as advice
#=====[ Import Data ]=====
import coordKeysZ as keysXYZ
import label_columns as feature_names
class Personal_Trainer:
def __init__(self, keys, auto_start=False):
self.keys = keys
self.reps = defaultdict(list)
self.labels = defaultdict(list)
self.file_names = defaultdict(list)
self.classifiers = {}
#=====[ Rehydrate classifiers if auto_start enabled ]=====
if auto_start:
if 'squat' in keys:
self.classifiers['squat'] = pickle.load(open(os.path.join('../inference/','squat_classifiers_ftopt.p'),'rb'))
if 'pushup' in keys:
self.classifiers['pushup'] = pickle.load(open(os.path.join('../inference/','pushup_classifiers_ftopt.p'),'rb'))
#=====[ Loads a pickled file and stores squat values ]=====
def load_reps(self, exercise, file):
data = pickle.load(open(file,"rb"))
self.reps[exercise] = data['X']
self.labels[exercise] = data['Y']
self.file_names[exercise] = data['file_names']
#=====[ Does basic preprocessing for squats from data source: squat separation, normalization, etc. ]=====
def analyze_reps(self, exercise, data_file, labels=None, epsilon=0.15, gamma=20, delta=0.5, beta=1, auto_analyze=False, verbose=False):
reps = [rep for rep in rs.separate_reps(data_file, exercise, self.keys[exercise], keysXYZ.columns)]
if verbose:
ut.print_success('Reps segmented and normalized for ' + exercise)
if not auto_analyze:
return reps
#=====[ Get feature vector ]=====
feature_vectors = self.get_prediction_features_opt(exercise, reps, verbose)
#=====[ Get results for classifications and populate dictionary ]=====
results = {}
if verbose:
print "\n\n###################################################################"
print "######################## Classification ###########################"
print "###################################################################\n\n"
for key in feature_vectors:
X = feature_vectors[key]
classification = self.classify(exercise, key, X, verbose)
results[key] = classification
if verbose:
print '\n\n', key ,':\n', classification, '\n'
#=====[ Print advice based on results ]=====
print "\n\n###################################################################"
print "########################### Feedback ##############################"
print "###################################################################\n\n"
return self.get_advice(exercise, results)
#=====[ Adds reps to personal trainer's list of squats ]=====
def add_reps(self, exercise, reps):
self.reps[exercise].extend(reps)
#=====[ Provides the client with an array of squat DataFrames ]=====
def get_reps(self, exercise):
return self.reps[exercise]
#=====[ Provides the client with a DataFrame of squat labels ]=====
def get_labels(self, exercise):
return self.labels[exercise]
#=====[ Provides the client with an array of squat DataFrames ]=====
def get_file_names(self, exercise):
return self.file_names[exercise]
#=====[ Sets classifiers for the personal trainer ]=====
def set_classifiers(self, exercise, classifiers):
self.classifiers[exercise] = classifiers
ut.print_success("Classifiers stored for " + exercise)
#=====[ Classies an example based on a specified key ]=====
def classify(self, exercise, key, X, verbose=False):
try:
prediction = self.classifiers[exercise][key].predict(X)
if verbose:
ut.print_success(key + ': reps classified')
return prediction
except Exception as e:
print e
ut.print_failure(key + ': reps not classified')
return None
def get_classifiers(self, exercise):
return self.classifiers[exercise]
def get_advice(self, exercise, results):
to_return = ""
for message in advice.advice(exercise, results):
print message
to_return += message + '\n'
return to_return
#=====[ Gets feature vectors for prediction of data ]=====
def get_prediction_features(self, exercise, reps):
if exercise is 'squat':
#=====[ Retreives relevant training data for each classifier ]=====
X0, Y, file_names = self.extract_advanced_features(reps=reps, multiples=[0.5], predict=True)
X1, Y, file_names = self.extract_advanced_features(reps=reps, multiples=[0.2, 0.4, 0.6, 0.8], predict=True)
X3, Y, file_names = self.extract_advanced_features(reps=reps, multiples=[0.05, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95], predict=True)
#=====[ Sets up dictionary of feature vectors ]=====
X= {}
X['bend_hips_knees'] = preprocessing.StandardScaler().fit_transform(X3['bend_hips_knees'])
X['stance_width'] = preprocessing.StandardScaler().fit_transform(X1['stance_width'])
X['squat_depth'] = preprocessing.StandardScaler().fit_transform(X0['squat_depth'])
X['knees_over_toes'] = preprocessing.StandardScaler().fit_transform(np.concatenate([X3[x] for x in X3],axis=1))
X['back_hip_angle'] = preprocessing.StandardScaler().fit_transform(np.concatenate([X0[x] for x in X0],axis=1))
elif exercise is 'pushup':
#=====[ Retreives relevant training data for each classifier ]=====
X3, Y, file_names = self.extract_pu_features(reps=reps, multiples=[0.05, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95], predict=True)
X4, Y, file_names = self.extract_pu_features(reps=reps, multiples=[float(x)/100 for x in range(100)], predict=True)
X30 = np.concatenate([X3[x] for x in X3],axis=1)
X40 = np.concatenate([X4[x] for x in X4],axis=1)
#=====[ Sets up dictionary of feature vectors ]=====
X = {}
X['head_back'] = preprocessing.StandardScaler().fit_transform(X40)
X['knees_straight'] = preprocessing.StandardScaler().fit_transform(X30)
X['elbow_angle'] = preprocessing.StandardScaler().fit_transform(X3['elbow_angle'])
ut.print_success('Features extracted for ' + exercise)
return X
def get_prediction_features_opt(self, exercise, reps, verbose=False):
if exercise is 'squat':
#=====[ Load feature indicies ]=====
feature_indices = pickle.load(open(os.path.join('../inference/','squat_feature_indices.p'),'rb'))
#=====[ Retreives relevant training data for each classifier ]=====
X3, Y, file_names = self.extract_advanced_features(reps=reps, multiples=[float(x)/20 for x in range(1,20)],predict=True)
X30 = np.concatenate([X3[x] for x in X3],axis=1)
#=====[ Sets up dictionary of feature vectors ]=====
X= {}
X['bend_hips_knees'] = X30[:,feature_indices['bend_hips_knees']]
X['stance_width'] = X30[:,feature_indices['stance_width']]
X['squat_depth'] = X30[:,feature_indices['squat_depth']]
X['knees_over_toes'] = X30[:,feature_indices['knees_over_toes']]
X['back_hip_angle'] = X30[:,feature_indices['back_hip_angle']]
elif exercise is 'pushup':
#=====[ Load feature indicies ]=====
feature_indices = pickle.load(open(os.path.join('../inference/','pushup_feature_indices.p'),'rb'))
#=====[ Retreives relevant training data for each classifier ]=====
X3, Y, file_names = self.extract_pu_features(reps=reps, multiples=[0.05, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95], predict=True)
X30 = np.concatenate([X3[x] for x in X3],axis=1)
#=====[ Sets up dictionary of feature vectors ]=====
X = {}
X['head_back'] = X30[:,feature_indices['head_back']]
X['knees_straight'] = X30[:,feature_indices['knees_straight']]
X['elbow_angle'] = X30[:,feature_indices['elbow_angle']]
if verbose:
ut.print_success('Features extracted for ' + exercise)
return X
#=====[ Extracts advanced features from pushups and prepares X, a dictionary of mxn matrices with m squats and n features per squat for each of various keys ]=====
def extract_pu_features(self, multiples=[0.5], reps=None, labels=None, toIgnore=[], predict=False):
#=====[ If no set of squats passed in to extract features from, extracts features from self.reps ]=====
if reps is None:
reps = self.reps['pushup']
labels = self.labels['pushup']
#=====[ Get Feature Vector ]=====
advanced_feature_vector = pfz.get_advanced_feature_vector(reps,self.keys['pushup'],multiples)
#=====[ Set data to have 0 mean and unit variance ]=====
X, Y = fz.transform_data(advanced_feature_vector, labels, toIgnore, predict)
return X, Y, self.file_names['pushup']
#=====[ Extracts advanced features from squats and prepares X, a dictionary of mxn matrices with m squats and n features per squat for each of various keys ]=====
def extract_advanced_features(self, multiples=[0.5], reps=None, labels=None, toIgnore=[], predict=False):
#=====[ If no set of squats passed in to extract features from, extracts features from self.reps ]=====
if reps is None:
reps = self.reps['squat']
labels = self.labels['squat']
#=====[ Get Feature Vector ]=====
advanced_feature_vector = fz.get_advanced_feature_vector(reps,self.keys['squat'],multiples)
#=====[ Set data to have 0 mean and unit variance ]=====
X, Y = fz.transform_data(advanced_feature_vector, labels, toIgnore, predict)
return X, Y, self.file_names['squat']