Human Practice Recognition (HAR) is a crucial space in ubiquitous computing and has capabilities in correctly being monitoring, good properties, and additional. This mission targets to leverage deep studying methods to precisely predict six totally completely totally 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 Discovering out Repository (UCI HAR) and entails 30 contributors performing the actions whereas sporting a Samsung Galaxy S II smartphone.
Knowledge Exploration and Preprocessing
The dataset incorporates readings from the smartphone’s accelerometer and gyroscope, captured at 50Hz. It consists of preprocessed knowledge partitioned into educating (70%) and take a look at (30%) fashions. 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 alternatives and function variable. The alternatives are then normalized utilizing MinMaxScaler, and the prepare 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('observe.csv')
test_data = pd.read_csv('try.csv')
# Separate selections and function 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 prepare 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)
Attribute Extraction and Visualization
To raised perceive the information, we visualize some selections utilizing scatter matrix plots. This helps in figuring out patterns and correlations all through the info.
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 only some selections
fig = px.scatter_matrix(X_train_df, dimensions=['tBodyAcc-mean()-X', 'tBodyAcc-mean()-Y', 'tBodyAcc-mean()-Z'], shade='Practice')
fig.present()
Mannequin Building and Design Decisions
We use a deep neural group (DNN) to categorise the actions. The mannequin consists of quite a lot 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 courses for six actions
])
# Compile the mannequin
mannequin.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Put collectively the mannequin
historic earlier = 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 provide out hyperparameter tuning to go looking the optimum mannequin development. This entails adjusting the variety of fashions in every layer and the variety of layers.
import keras_tuner as ktdef build_model(hp):
mannequin = Sequential()
mannequin.add(Dense(fashions=hp.Int('fashions', min_value=32, max_value=512, step=32), activation='relu', input_shape=(X_train_full_scaled.variety[1],)))
mannequin.add(Dropout(0.5))
for i in vary(hp.Int('num_layers', 1, 3)):
mannequin.add(Dense(fashions=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, function='val_accuracy', max_trials=5, executions_per_trial=3, itemizing='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]
Remaining Mannequin Effectivity and Accuracy
The best mannequin from hyperparameter tuning is educated and evaluated on the try set, attaining spectacular accuracy.
# Ponder the precise mannequin on the try set
best_model = tuner.hypermodel.assemble(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.take note of(X_test_scaled, y_test_encoded)
print(f'Try accuracy: {test_acc}')
Challenges Confronted and Decisions Utilized
Thought-about one in every of many predominant challenges was stopping overfitting due to small dataset dimension. We addressed this by the use of the utilization of dropout layers and hyperparameter tuning. One totally different disadvantage was guaranteeing the mannequin generalizes appropriately to unseen knowledge, which we tackled by splitting the information into educating and validation fashions and utilizing early stopping all by way of educating.
Conclusion and Future Work
This mission demonstrates the potential of deep studying in HAR duties, attaining excessive accuracy in predicting actions. Future work may uncover utilizing extra refined fashions identical to Convolutional Neural Networks (CNNs) or Recurrent Neural Networks (RNNs) and incorporating extra sensor knowledge for improved accuracy.
Visualizations
- Instructing Historic earlier:
import matplotlib.pyplot as pltplt.resolve(figsize=(12, 4))
# Plot educating & validation accuracy values
plt.subplot(1, 2, 1)
plt.plot(historic earlier.historic earlier['accuracy'])
plt.plot(historic earlier.historic earlier['val_accuracy'])
plt.title('Mannequin accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='elevated left')
# Plot educating & validation loss values
plt.subplot(1, 2, 2)
plt.plot(historic earlier.historic earlier['loss'])
plt.plot(historic earlier.historic earlier['val_loss'])
plt.title('Mannequin loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='elevated left')
plt.present()
2. Confusion Matrix:
# Predictions on the try 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.resolve(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