Human Exercise Recognition (HAR) is a vital space in ubiquitous computing and has purposes in well being monitoring, sensible houses, and extra. This mission goals to leverage deep studying methods to precisely predict six completely different actions (strolling, strolling upstairs, strolling downstairs, sitting, standing, and laying) utilizing knowledge from gyroscopes and accelerometers. The dataset is collected from the UCI Machine Studying Repository (UCI HAR) and entails 30 contributors performing the actions whereas sporting a Samsung Galaxy S II smartphone.
Knowledge Exploration and Preprocessing
The dataset contains readings from the smartphone’s accelerometer and gyroscope, captured at 50Hz. It consists of preprocessed knowledge partitioned into coaching (70%) and take a look at (30%) units. Every participant’s knowledge was reworked into 561-feature vectors derived from time and frequency domains.
Loading and Normalizing the Knowledge
We load the information and separate the options and goal variable. The options are then normalized utilizing MinMaxScaler, and the exercise labels are encoded utilizing LabelEncoder.
import pandas as pd
from sklearn.preprocessing import MinMaxScaler, LabelEncoder
from sklearn.model_selection import train_test_split# Load the dataset
train_data = pd.read_csv('practice.csv')
test_data = pd.read_csv('take a look at.csv')
# Separate options and goal variable
X_train_full = train_data.drop(columns=['subject', 'Activity'])
y_train_full = train_data['Activity']
X_test = test_data.drop(columns=['subject', 'Activity'])
y_test = test_data['Activity']
# Normalize the function vectors utilizing MinMaxScaler
scaler = MinMaxScaler()
X_train_full_scaled = scaler.fit_transform(X_train_full)
X_test_scaled = scaler.rework(X_test)
# Encode exercise labels utilizing LabelEncoder
label_encoder = LabelEncoder()
y_train_full_encoded = label_encoder.fit_transform(y_train_full)
y_test_encoded = label_encoder.rework(y_test)
Characteristic Extraction and Visualization
To raised perceive the information, we visualize some options utilizing scatter matrix plots. This helps in figuring out patterns and correlations within the knowledge.
import plotly.categorical as px# Convert X_train to a DataFrame for visualization
X_train_df = pd.DataFrame(X_train_full_scaled, columns=train_data.columns[:-2])
X_train_df['Activity'] = label_encoder.inverse_transform(y_train_full_encoded)
# Visualize just a few options
fig = px.scatter_matrix(X_train_df, dimensions=['tBodyAcc-mean()-X', 'tBodyAcc-mean()-Y', 'tBodyAcc-mean()-Z'], shade='Exercise')
fig.present()
Mannequin Structure and Design Selections
We use a deep neural community (DNN) to categorise the actions. The mannequin consists of a number of dense layers with ReLU activation and dropout layers to forestall overfitting.
import tensorflow as tf
from tensorflow.keras.fashions import Sequential
from tensorflow.keras.layers import Dense, Dropout# Outline the mannequin
mannequin = Sequential([
Dense(128, activation='relu', input_shape=(X_train_full_scaled.shape[1],)),
Dropout(0.5),
Dense(64, activation='relu'),
Dropout(0.5),
Dense(32, activation='relu'),
Dense(6, activation='softmax') # 6 lessons for six actions
])
# Compile the mannequin
mannequin.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Prepare the mannequin
historical past = mannequin.match(X_train_full_scaled, y_train_full_encoded, epochs=50, batch_size=32, validation_split=0.2)
layers = [100, 25, 12, 6, 6]layers_str = ["Input"] + ["Hidden"] * (len(layers) - 2) + ["Output"]
layers_col = ["none"] + ["none"] * (len(layers) - 2) + ["none"]
layers_fill = ["black"] + ["gray"] * (len(layers) - 2) + ["black"]
penwidth = 10
font = "Hilda 10"
print("digraph G {")
print("tfontname = "{}"".format(font))
print("trankdir=LR")
print("tsplines=line")
print("tnodesep=.15;")
print("tranksep=15;")
print("tedge [color=black, arrowsize=.5];")
print("tnode [fixedsize=true,label="",style=filled," +
"color=none,fillcolor=gray,shape=circle]n")
# Clusters
for i in vary(0, len(layers)):
print(("tsubgraph cluster_{} {{".format(i)))
print(("ttcolor={};".format(layers_col[i])))
print(("ttnode [style=filled, color=white, penwidth={},"
"fillcolor={} shape=circle];".format(
penwidth,
layers_fill[i])))
print(("tt"), finish=' ')
for a in vary(layers[i]):
print("l{}{} ".format(i + 1, a), finish=' ')
print(";")
print(("ttlabel = {};".format(layers_str[i])))
print("t}n")
# Nodes
for i in vary(1, len(layers)):
for a in vary(layers[i - 1]):
for b in vary(layers[i]):
print("tl{}{} -> l{}{}".format(i, a, i + 1, b))
print("}")
Hyperparameter Tuning
Utilizing KerasTuner, we carry out hyperparameter tuning to search out the optimum mannequin structure. This entails adjusting the variety of models in every layer and the variety of layers.
import keras_tuner as ktdef build_model(hp):
mannequin = Sequential()
mannequin.add(Dense(models=hp.Int('models', min_value=32, max_value=512, step=32), activation='relu', input_shape=(X_train_full_scaled.form[1],)))
mannequin.add(Dropout(0.5))
for i in vary(hp.Int('num_layers', 1, 3)):
mannequin.add(Dense(models=hp.Int('units_' + str(i), min_value=32, max_value=512, step=32), activation='relu'))
mannequin.add(Dropout(0.5))
mannequin.add(Dense(6, activation='softmax'))
mannequin.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
return mannequin
tuner = kt.RandomSearch(build_model, goal='val_accuracy', max_trials=5, executions_per_trial=3, listing='my_dir', project_name='activity_recognition')
tuner.search(X_train_full_scaled, y_train_full_encoded, epochs=50, validation_split=0.2)
best_hps = tuner.get_best_hyperparameters(num_trials=1)[0]
Last Mannequin Efficiency and Accuracy
The most effective mannequin from hyperparameter tuning is educated and evaluated on the take a look at set, attaining spectacular accuracy.
# Consider the perfect mannequin on the take a look at set
best_model = tuner.hypermodel.construct(best_hps)
best_model.match(X_train_full_scaled, y_train_full_encoded, epochs=50, validation_split=0.2)
test_loss, test_acc = best_model.consider(X_test_scaled, y_test_encoded)
print(f'Take a look at accuracy: {test_acc}')
Challenges Confronted and Options Applied
One of many major challenges was stopping overfitting as a result of small dataset dimension. We addressed this through the use of dropout layers and hyperparameter tuning. One other problem was making certain the mannequin generalizes properly to unseen knowledge, which we tackled by splitting the information into coaching and validation units and utilizing early stopping throughout coaching.
Conclusion and Future Work
This mission demonstrates the potential of deep studying in HAR duties, attaining excessive accuracy in predicting actions. Future work might discover utilizing extra complicated fashions similar to Convolutional Neural Networks (CNNs) or Recurrent Neural Networks (RNNs) and incorporating extra sensor knowledge for improved accuracy.
Visualizations
- Coaching Historical past:
import matplotlib.pyplot as pltplt.determine(figsize=(12, 4))
# Plot coaching & validation accuracy values
plt.subplot(1, 2, 1)
plt.plot(historical past.historical past['accuracy'])
plt.plot(historical past.historical past['val_accuracy'])
plt.title('Mannequin accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='higher left')
# Plot coaching & validation loss values
plt.subplot(1, 2, 2)
plt.plot(historical past.historical past['loss'])
plt.plot(historical past.historical past['val_loss'])
plt.title('Mannequin loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='higher left')
plt.present()
2. Confusion Matrix:
# Predictions on the take a look at set
y_pred = best_model.predict(X_test_scaled)
y_pred_classes = y_pred.argmax(axis=1)# Confusion matrix
conf_matrix = confusion_matrix(y_test_encoded, y_pred_classes)
plt.determine(figsize=(10, 7))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=label_encoder.classes_, yticklabels=label_encoder.classes_)
plt.xlabel('Predicted')
plt.ylabel('Precise')
plt.title('Confusion Matrix')
plt.present()
# Classification report
print(classification_report(y_test_encoded, y_pred_classes, target_names=label_encoder.classes_))
References
UCI Human Activity Recognition Data Set