-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhar.py
More file actions
308 lines (260 loc) · 10.2 KB
/
Copy pathhar.py
File metadata and controls
308 lines (260 loc) · 10.2 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# -*- coding: utf-8 -*-
"""har.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/18oVywlnM3O2e46eB_YD8REwNsWTZjMu4
"""
!pip install coremltools
# Commented out IPython magic to ensure Python compatibility.
from __future__ import print_function
from matplotlib import pyplot as plt
# %matplotlib inline
import numpy as np
import pandas as pd
import seaborn as sns
import coremltools
from scipy import stats
from IPython.display import display, HTML
from sklearn import metrics
from sklearn.metrics import classification_report
from sklearn import preprocessing
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Reshape
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import np_utils
# Set some standard parameters upfront
pd.options.display.float_format = '{:.1f}'.format
sns.set() # Default seaborn look and feel
plt.style.use('ggplot')
print('keras version ', keras.__version__)
# Same labels will be reused throughout the program
LABELS = ['downstair',
'úpstair',
'jogging',
'situp',
'walkonheel',
'walkontoe',
'normalwalk']
# The number of steps within one time segment
TIME_PERIODS = 80
# The steps to take from one segment to the next; if this value is equal to
# TIME_PERIODS, then there is no overlap between the segments
STEP_DISTANCE = 40
def read_data(file_path):
df = pd.read_csv (file_path,index_col=False)
# ... and then this column must be transformed to float explicitly
df['ax'] = df['ax'].apply(convert_to_float)
df['ay'] = df['ay'].apply(convert_to_float)
df['az'] = df['az'].apply(convert_to_float)
df['wx'] = df['wx'].apply(convert_to_float)
df['wy'] = df['wy'].apply(convert_to_float)
df['wz'] = df['wz'].apply(convert_to_float)
df['angleX'] = df['angleX'].apply(convert_to_float)
df['angleY'] = df['angleY'].apply(convert_to_float)
df['angleZ'] = df['angleZ'].apply(convert_to_float)
df['temp'] = df['temp'].apply(convert_to_float)
# This is very important otherwise the model will not fit and loss
# will show up as NAN
# print(df)
df.dropna(axis=0, how='any', inplace=True)
# print(df)
return df
def convert_to_float(x):
try:
return np.float(x)
except:
return np.nan
def show_basic_dataframe_info(dataframe):
# Shape and how many rows and columns
print('Number of columns in the dataframe: %i' % (dataframe.shape[1]))
print('Number of rows in the dataframe: %i\n' % (dataframe.shape[0]))
# Load data set containing all the data from csv
df = read_data(r'allf.csv')
# Describe the data
show_basic_dataframe_info(df)
df.tail(10)
# Show how many training examples exist for each of the six activities
df['activity'].value_counts().plot(kind='bar',
title='Training Examples by Activity Type')
plt.show()
# Better understand how the recordings are spread across the different
# users who participated in the study
df['uid'].value_counts().plot(kind='bar',
title='Training Examples by User')
plt.show()
def plot_activity(activity, data):
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3,
figsize=(15, 10),
sharex=True)
plot_axis(ax0, data['time'], data['ax'], 'X-Axis')
plot_axis(ax1, data['time'], data['ay'], 'Y-Axis')
plot_axis(ax2, data['time'], data['az'], 'Z-Axis')
plt.subplots_adjust(hspace=0.2)
fig.suptitle(activity)
plt.subplots_adjust(top=0.90)
plt.show()
def plot_axis(ax, x, y, title):
ax.plot(x, y, 'r')
ax.set_title(title)
ax.xaxis.set_visible(False)
ax.set_ylim([min(y) - np.std(y), max(y) + np.std(y)])
ax.set_xlim([min(x), max(x)])
ax.grid(True)
for activity in np.unique(df['activity']):
subset = df[df['activity'] == activity][:180]
plot_activity(activity, subset)
# Define column name of the label vector
LABEL = 'ActivityEncoded'
# Transform the labels from String to Integer via LabelEncoder
le = preprocessing.LabelEncoder()
# Add a new column to the existing DataFrame with the encoded values
df[LABEL] = le.fit_transform(df['activity'].values.ravel())
# Differentiate between test set and training set
df_test = df[df['uid'] > 7]
df_train = df[df['uid'] <= 7]
print(df_test["ax"])
# Normalize features for training data set (values between 0 and 1)
# Surpress warning for next 3 operation
pd.options.mode.chained_assignment = None # default='warn'
df_train['ax'] = df_train['ax'] / df_train['ax'].max()
df_train['ay'] = df_train['ay'] / df_train['ay'].max()
df_train['az'] = df_train['az'] / df_train['az'].max()
# print(df_test['ax'])
df_test['ax'] = df_test['ax'] / df_test['ax'].max()
df_test['ay'] = df_test['ay'] / df_test['ay'].max()
df_test['az'] = df_test['az'] / df_test['az'].max()
# Round numbers
print(df_test['ax'])
# df_train = df_train.round({'ax': 4, 'ay': 4, 'az': 4})
def create_segments_and_labels(df, time_steps, step, label_name):
# x, y, z acceleration as features
N_FEATURES = 3
# Number of steps to advance in each iteration (for me, it should always
# be equal to the time_steps in order to have no overlap between segments)
# step = time_steps
segments = []
labels = []
for i in range(0, len(df) - time_steps, step):
xs = df['ax'].values[i: i + time_steps]
ys = df['ay'].values[i: i + time_steps]
zs = df['az'].values[i: i + time_steps]
# Retrieve the most often used label in this segment
label = stats.mode(df[label_name][i: i + time_steps])[0][0]
segments.append([xs, ys, zs])
labels.append(label)
# Bring the segments into a better shape
reshaped_segments = np.asarray(segments, dtype= np.float32).reshape(-1, time_steps, N_FEATURES)
labels = np.asarray(labels)
return reshaped_segments, labels
x_train, y_train = create_segments_and_labels(df_train,
TIME_PERIODS,
STEP_DISTANCE,
LABEL)
x_test, y_test = create_segments_and_labels(df_test,
TIME_PERIODS,
STEP_DISTANCE,
LABEL)
# print(x_train)
# print(x_test)
# print(y_test)
# print(y_train)
# print(x_test)
print('x_train shape: ', x_train.shape)
print(x_train.shape[0], 'training samples')
print('y_train shape: ', y_train.shape)
print('x_test shape: ', x_test.shape)
print(x_test.shape[0], 'testing samples')
print('y_test shape: ', y_test.shape)
# Set input & output dimensions
num_time_periods, num_sensors = x_train.shape[1], x_train.shape[2]
num_classes = le.classes_.size
print(list(le.classes_))
input_shape = (num_time_periods*num_sensors)
x_train = x_train.reshape(x_train.shape[0], input_shape)
print('x_train shape:', x_train.shape)
print('input_shape:', input_shape)
x_test = x_test.reshape(x_test.shape[0], input_shape)
print('x_test shape:', x_test.shape)
print('input_shape:', input_shape)
x_train = x_train.astype('float32')
y_train = y_train.astype('float32')
x_test = x_test.astype('float32')
y_test = y_test.astype('float32')
y_train_hot = np_utils.to_categorical(y_train, num_classes)
print('New y_train shape: ', y_train_hot.shape)
y_test_hot = np_utils.to_categorical(y_test, num_classes)
print('New y_test shape: ', y_test_hot.shape)
model_m = Sequential()
# Remark: since coreml cannot accept vector shapes of complex shape like
# [80,3] this workaround is used in order to reshape the vector internally
# prior feeding it into the network
model_m.add(Reshape((TIME_PERIODS, 3), input_shape=(input_shape,)))
model_m.add(Dense(100, activation='relu'))
model_m.add(Dense(100, activation='relu'))
model_m.add(Dense(100, activation='relu'))
model_m.add(Flatten())
model_m.add(Dense(num_classes, activation='softmax'))
print(model_m.summary())
callbacks_list = [
keras.callbacks.ModelCheckpoint(
filepath='best_model.{epoch:02d}-{val_loss:.2f}.h5',
monitor='val_loss', save_best_only=True),
keras.callbacks.EarlyStopping(monitor='acc', patience=1)
]
model_m.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['accuracy'])
# Hyper-parameters
BATCH_SIZE = 400
EPOCHS = 50
# Enable validation to use ModelCheckpoint and EarlyStopping callbacks.
history = model_m.fit(x_train,
y_train_hot,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
callbacks=callbacks_list,
validation_split=0.2,
verbose=1)
history_dict = history.history
history_dict.keys()
plt.figure(figsize=(6, 4))
plt.plot(history.history['accuracy'], 'r', label='Accuracy of training data')
plt.plot(history.history['val_accuracy'], 'b', label='Accuracy of validation data')
plt.plot(history.history['loss'], 'r--', label='Loss of training data')
plt.plot(history.history['val_loss'], 'b--', label='Loss of validation data')
plt.title('Model Accuracy and Loss')
plt.ylabel('Accuracy and Loss')
plt.xlabel('Training Epoch')
plt.ylim(0)
plt.legend()
plt.show()
# Print confusion matrix for training data
y_pred_train = model_m.predict(x_train)
# Take the class with the highest probability from the train predictions
max_y_pred_train = np.argmax(y_pred_train, axis=1)
print(classification_report(y_train, max_y_pred_train))
def show_confusion_matrix(validations, predictions):
matrix = metrics.confusion_matrix(validations, predictions)
plt.figure(figsize=(6, 4))
sns.heatmap(matrix,
cmap='coolwarm',
linecolor='white',
linewidths=1,
xticklabels=LABELS,
yticklabels=LABELS,
annot=True,
fmt='d')
plt.title('Confusion Matrix')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.show()
print(x_test)
y_pred_test = model_m.predict(x_test)
# print(y_pred_test)
# Take the class with the highest probability from the test predictions
max_y_pred_test = np.argmax(y_pred_test, axis=1)
print(y_test_hot.shape)
print(y_pred_test.shape)
max_y_test = np.argmax(y_test_hot, axis=1)
show_confusion_matrix(max_y_test, max_y_pred_test)
print(classification_report(max_y_test, max_y_pred_test))