-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecognitionFunctions.py
169 lines (99 loc) · 4.32 KB
/
recognitionFunctions.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 27 23:57:41 2020
@author: aguasharo
"""
from readDataset import *
from preProcessing import *
from featureExtraction import *
from classificationEMG import *
from sklearn.manifold import TSNE
import seaborn as sns
import keras
from keras.models import Sequential
from keras.layers import Dense
from sklearn.preprocessing import LabelEncoder
from keras.utils import np_utils
from keras.optimizers import Adam, SGD
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler
import collections
from collections import Counter
def get_x_train(user,sample):
# This function reads the time series(x) of the user (Training Sample)
train_samples = user['trainingSamples']
x = (train_samples[sample]['emg'])
# Divide to 128 for having a signal between -1 and 1
df = pd.DataFrame.from_dict(x) / 128
# Apply filter
train_filtered_X = df.apply(preProcessEMGSegment)
# Segment the filtered EMG signal
train_segment_X = EMG_segment(train_filtered_X)
return train_segment_X
def get_x_test(user,sample):
# This function reads the time series(x) of the user (Testing Sample)
test_samples = user['testingSamples']
x = (test_samples[sample]['emg'])
df = pd.DataFrame.from_dict(x) / 128
return df
def decode_targets(y_train):
# Encode targets to train the Neural Network
encoder = LabelEncoder()
encoder.fit(y_train)
encoded_Y = encoder.transform(y_train)
target = np_utils.to_categorical(encoded_Y)
return target
def get_xy_val(X_train, targets):
# Get validation data to train Neural Network
data_val = X_train.copy()
data_val['6'] = targets
xy_val = data_val.sample(frac=1).reset_index(drop=True)
X_val = xy_val.iloc[:,0:6]
y_val = decode_targets(xy_val['6'])
return X_val, y_val
def bestCenter_Class(train_segment_X):
# This function returns a set of time series called centers
# for each gesture class
g1 = train_segment_X[0:25]
g2 = train_segment_X[25:50]
g3 = train_segment_X[50:75]
g4 = train_segment_X[75:100]
g5 = train_segment_X[100:125]
g6 = train_segment_X[125:150]
gen = [g1, g2, g3, g4, g5 ,g6]
c = [findCentersClass(g) for g in gen]
return c
def getFeatureExtraction(emg_filtered, centers):
features = featureExtractionf(emg_filtered, centers)
dataX = preProcessFeatureVector(features)
return dataX
def trainFeedForwardNetwork(X_train,y_train, X_test, y_test):
# This function trains an artificial feed-forward neural networks
# Cost lost: categorical cross entropy
# Hidden Layer: Tanh
# Output Layer: softmax
classifier = Sequential()
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = None, input_dim = 6))
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'tanh'))
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'softmax'))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
classifier.fit(X_train, y_train, batch_size = 150, epochs = 1500, validation_data = (X_test, y_test), verbose = 0 )
return classifier
def testing_prediction(user,sample,centers,estimator):
test_RawX = get_x_test(user,sample)
[predictedSeq, vec_time, time_seq]= classifyEMG_SegmentationNN(test_RawX, centers, estimator)
predicted_label, t_post = post_ProcessLabels(predictedSeq)
# Computing the time of processing
estimatedTime = [sum(x) for x in zip(time_seq, t_post)]
return predicted_label, predictedSeq, vec_time, estimatedTime
def recognition_results(results):
# This function save the responses of each user into a dictionary
d = collections.defaultdict(dict)
for i in range(0,150):
d['idx_%s' %i]['class'] = code2gesture(results[i][0])
d['idx_%s' %i]['vectorOfLabels'] = code2gesture_labels(results[i][1])
d['idx_%s' %i]['vectorOfTimePoints'] = results[i][2]
d['idx_%s' %i]['vectorOfProcessingTime']= results[i][3]
return d