Mic52 commited on
Commit
b630813
·
verified ·
1 Parent(s): 63a9961

Upload HumanActivityLSTM.ipynb

Browse files
Files changed (1) hide show
  1. HumanActivityLSTM.ipynb +361 -0
HumanActivityLSTM.ipynb ADDED
@@ -0,0 +1,361 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "ceedc8f9",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import numpy as np\n",
11
+ "import tensorflow as tf\n",
12
+ "import pandas as pd\n",
13
+ "import matplotlib.pyplot as plt\n",
14
+ "import seaborn as sns\n",
15
+ "from tensorflow.keras.models import Sequential, load_model\n",
16
+ "from tensorflow.keras.layers import LSTM, Bidirectional, Dense, Dropout\n",
17
+ "from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\n",
18
+ "from sklearn.preprocessing import LabelEncoder\n",
19
+ "from sklearn.metrics import classification_report, confusion_matrix"
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "markdown",
24
+ "id": "98ef0040",
25
+ "metadata": {},
26
+ "source": [
27
+ "## 1. Data Loading and Preprocessing"
28
+ ]
29
+ },
30
+ {
31
+ "cell_type": "code",
32
+ "execution_count": null,
33
+ "id": "b85b2bfc",
34
+ "metadata": {},
35
+ "outputs": [],
36
+ "source": [
37
+ "# Dataset path\n",
38
+ "PATH = \"UCI HAR Dataset/\""
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": null,
44
+ "id": "42787451",
45
+ "metadata": {},
46
+ "outputs": [],
47
+ "source": [
48
+ "# Load training and test data\n",
49
+ "X_train = np.loadtxt(PATH + \"train/X_train.txt\")\n",
50
+ "X_test = np.loadtxt(PATH + \"test/X_test.txt\")\n",
51
+ "y_train = np.loadtxt(PATH + \"train/y_train.txt\")\n",
52
+ "y_test = np.loadtxt(PATH + \"test/y_test.txt\")"
53
+ ]
54
+ },
55
+ {
56
+ "cell_type": "code",
57
+ "execution_count": null,
58
+ "id": "ec4ab34f",
59
+ "metadata": {},
60
+ "outputs": [],
61
+ "source": [
62
+ "# Reshape for LSTM (batch_size, timesteps, features)\n",
63
+ "X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])\n",
64
+ "X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])"
65
+ ]
66
+ },
67
+ {
68
+ "cell_type": "code",
69
+ "execution_count": null,
70
+ "id": "9661f547",
71
+ "metadata": {},
72
+ "outputs": [],
73
+ "source": [
74
+ "# Label encoding\n",
75
+ "encoder = LabelEncoder()\n",
76
+ "y_train = encoder.fit_transform(y_train)\n",
77
+ "y_test = encoder.transform(y_test)"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "markdown",
82
+ "id": "ae516913",
83
+ "metadata": {},
84
+ "source": [
85
+ "## 2. LSTM Model Creation and Training"
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": null,
91
+ "id": "a02036f5",
92
+ "metadata": {},
93
+ "outputs": [],
94
+ "source": [
95
+ "# Model creation\n",
96
+ "model = Sequential([\n",
97
+ " Bidirectional(LSTM(128, return_sequences=True, activation=\"tanh\"), input_shape=(X_train.shape[1], X_train.shape[2])),\n",
98
+ " Dropout(0.2),\n",
99
+ " Bidirectional(LSTM(64, return_sequences=False, activation=\"tanh\")),\n",
100
+ " Dropout(0.2),\n",
101
+ " Dense(64, activation=\"relu\"),\n",
102
+ " Dropout(0.1),\n",
103
+ " Dense(len(np.unique(y_train)), activation=\"softmax\")\n",
104
+ "])"
105
+ ]
106
+ },
107
+ {
108
+ "cell_type": "code",
109
+ "execution_count": null,
110
+ "id": "c1eae86c",
111
+ "metadata": {},
112
+ "outputs": [],
113
+ "source": [
114
+ "# Model compilation\n",
115
+ "optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)\n",
116
+ "model.compile(loss=\"sparse_categorical_crossentropy\", optimizer=optimizer, metrics=[\"accuracy\"])"
117
+ ]
118
+ },
119
+ {
120
+ "cell_type": "code",
121
+ "execution_count": null,
122
+ "id": "5421c2a2-c39f-400d-a06e-50617c27b1b1",
123
+ "metadata": {},
124
+ "outputs": [],
125
+ "source": []
126
+ },
127
+ {
128
+ "cell_type": "code",
129
+ "execution_count": null,
130
+ "id": "21b40e87",
131
+ "metadata": {},
132
+ "outputs": [],
133
+ "source": [
134
+ "# Callbacks to adjust learning rate and stop training early\n",
135
+ "reduce_lr = ReduceLROnPlateau(monitor=\"val_loss\", factor=0.5, patience=5, min_lr=0.00001, verbose=1)\n",
136
+ "early_stop = EarlyStopping(monitor=\"val_loss\", patience=10, restore_best_weights=True, verbose=1)"
137
+ ]
138
+ },
139
+ {
140
+ "cell_type": "code",
141
+ "execution_count": null,
142
+ "id": "5bc38cd5",
143
+ "metadata": {},
144
+ "outputs": [],
145
+ "source": [
146
+ "# Model training\n",
147
+ "history = model.fit(X_train, y_train, epochs=100, batch_size=128, validation_data=(X_test, y_test), callbacks=[reduce_lr, early_stop])"
148
+ ]
149
+ },
150
+ {
151
+ "cell_type": "markdown",
152
+ "id": "164393aa",
153
+ "metadata": {},
154
+ "source": [
155
+ "## 3. Model Evaluation"
156
+ ]
157
+ },
158
+ {
159
+ "cell_type": "code",
160
+ "execution_count": null,
161
+ "id": "82798c6b",
162
+ "metadata": {},
163
+ "outputs": [],
164
+ "source": [
165
+ "loss, accuracy = model.evaluate(X_test, y_test)\n",
166
+ "print(f\"Test Accuracy: {accuracy:.4f}\")"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type": "code",
171
+ "execution_count": null,
172
+ "id": "971d6071",
173
+ "metadata": {},
174
+ "outputs": [],
175
+ "source": [
176
+ "# Save the model\n",
177
+ "model.save(\"optimized_lstm_human_activity.h5\")\n",
178
+ "print(\"✅ Optimized model successfully saved!\")"
179
+ ]
180
+ },
181
+ {
182
+ "cell_type": "code",
183
+ "execution_count": null,
184
+ "id": "6bf569ae",
185
+ "metadata": {},
186
+ "outputs": [],
187
+ "source": [
188
+ "# Predictions on test set\n",
189
+ "predictions = model.predict(X_test)\n",
190
+ "predicted_labels = np.argmax(predictions, axis=1)"
191
+ ]
192
+ },
193
+ {
194
+ "cell_type": "code",
195
+ "execution_count": null,
196
+ "id": "41a82991",
197
+ "metadata": {},
198
+ "outputs": [],
199
+ "source": [
200
+ "# Classification report\n",
201
+ "class_labels = [str(label) for label in np.unique(y_test)]\n",
202
+ "print(\"\\n🔹 Classification Report:\")\n",
203
+ "print(classification_report(y_test, predicted_labels, target_names=class_labels))"
204
+ ]
205
+ },
206
+ {
207
+ "cell_type": "code",
208
+ "execution_count": null,
209
+ "id": "2a89410f",
210
+ "metadata": {},
211
+ "outputs": [],
212
+ "source": [
213
+ "# Confusion matrix\n",
214
+ "cm = confusion_matrix(y_test, predicted_labels)\n",
215
+ "plt.figure(figsize=(8,6))\n",
216
+ "sns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", xticklabels=class_labels, yticklabels=class_labels)\n",
217
+ "plt.xlabel(\"Predicted Class\")\n",
218
+ "plt.ylabel(\"True Class\")\n",
219
+ "plt.title(\"Confusion Matrix - Optimized Model\")\n",
220
+ "plt.show()"
221
+ ]
222
+ },
223
+ {
224
+ "cell_type": "markdown",
225
+ "id": "edc4005a",
226
+ "metadata": {},
227
+ "source": [
228
+ "## 4. Validation on Synthetic Data"
229
+ ]
230
+ },
231
+ {
232
+ "cell_type": "code",
233
+ "execution_count": null,
234
+ "id": "7120363c",
235
+ "metadata": {},
236
+ "outputs": [],
237
+ "source": [
238
+ "# Load the saved model\n",
239
+ "model = load_model(\"optimized_lstm_human_activity.h5\")"
240
+ ]
241
+ },
242
+ {
243
+ "cell_type": "code",
244
+ "execution_count": null,
245
+ "id": "0687a5ac",
246
+ "metadata": {},
247
+ "outputs": [],
248
+ "source": [
249
+ "# Generate synthetic data based on mean and standard deviation of each class\n",
250
+ "samples_per_class = 50\n",
251
+ "generated_data = []\n",
252
+ "generated_labels = []"
253
+ ]
254
+ },
255
+ {
256
+ "cell_type": "code",
257
+ "execution_count": null,
258
+ "id": "0b8f027e",
259
+ "metadata": {},
260
+ "outputs": [],
261
+ "source": [
262
+ "for activity in range(1, 7): # Classes from 1 to 6\n",
263
+ " class_data = X_train[y_train == activity]\n",
264
+ " feature_mean = np.mean(class_data, axis=0)\n",
265
+ " feature_std = np.std(class_data, axis=0)\n",
266
+ " \n",
267
+ " for _ in range(samples_per_class):\n",
268
+ " synthetic_sample = np.random.normal(loc=feature_mean, scale=feature_std)\n",
269
+ " generated_data.append(synthetic_sample)\n",
270
+ " generated_labels.append(activity)"
271
+ ]
272
+ },
273
+ {
274
+ "cell_type": "code",
275
+ "execution_count": null,
276
+ "id": "695de361",
277
+ "metadata": {},
278
+ "outputs": [],
279
+ "source": [
280
+ "# Convert to array\n",
281
+ "generated_data = np.array(generated_data).reshape(len(generated_data), 1, -1)\n",
282
+ "generated_labels = np.array(generated_labels)"
283
+ ]
284
+ },
285
+ {
286
+ "cell_type": "code",
287
+ "execution_count": null,
288
+ "id": "0f4a73d0",
289
+ "metadata": {},
290
+ "outputs": [],
291
+ "source": [
292
+ "# Predictions\n",
293
+ "predictions = model.predict(generated_data)\n",
294
+ "predicted_labels = np.argmax(predictions, axis=1) + 1"
295
+ ]
296
+ },
297
+ {
298
+ "cell_type": "markdown",
299
+ "id": "fc8e71c0",
300
+ "metadata": {},
301
+ "source": [
302
+ "## 5. Visualization of Results on Synthetic Data"
303
+ ]
304
+ },
305
+ {
306
+ "cell_type": "code",
307
+ "execution_count": null,
308
+ "id": "aec02a8d",
309
+ "metadata": {},
310
+ "outputs": [],
311
+ "source": [
312
+ "# Confusion matrix\n",
313
+ "cm = confusion_matrix(generated_labels, predicted_labels)\n",
314
+ "plt.figure(figsize=(8,6))\n",
315
+ "sns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", xticklabels=range(1,7), yticklabels=range(1,7))\n",
316
+ "plt.xlabel(\"Predicted Class\")\n",
317
+ "plt.ylabel(\"True Class\")\n",
318
+ "plt.title(\"Confusion Matrix - Synthetic Data\")\n",
319
+ "plt.show()"
320
+ ]
321
+ },
322
+ {
323
+ "cell_type": "code",
324
+ "execution_count": null,
325
+ "id": "97b59f79",
326
+ "metadata": {},
327
+ "outputs": [],
328
+ "source": [
329
+ "# Classification report\n",
330
+ "print(\"\\n🔹 Classification Report:\")\n",
331
+ "print(classification_report(generated_labels, predicted_labels))"
332
+ ]
333
+ }
334
+ ],
335
+ "metadata": {
336
+ "jupytext": {
337
+ "cell_metadata_filter": "-all",
338
+ "main_language": "python",
339
+ "notebook_metadata_filter": "-all"
340
+ },
341
+ "kernelspec": {
342
+ "display_name": "Python 3 (ipykernel)",
343
+ "language": "python",
344
+ "name": "python3"
345
+ },
346
+ "language_info": {
347
+ "codemirror_mode": {
348
+ "name": "ipython",
349
+ "version": 3
350
+ },
351
+ "file_extension": ".py",
352
+ "mimetype": "text/x-python",
353
+ "name": "python",
354
+ "nbconvert_exporter": "python",
355
+ "pygments_lexer": "ipython3",
356
+ "version": "3.12.3"
357
+ }
358
+ },
359
+ "nbformat": 4,
360
+ "nbformat_minor": 5
361
+ }