Human Activity Recognition is a machine learning model which is predict activity of human as per there accelerometer reading’s such like vibration, motion, force, direction.
In this project we are going to use accelerometer data to train the model so that it can predict the human activity. Accelerometer is a electronic sensor which measures force, motion, vibration , direction of objects, in order to position in space and monitor object’s movement.
In this blog. We also see how we can add AWS cloud to make this project more complete. We will see some feature of AWS cloud like AWS DynamoDB, AWS Lambda and AWS API Gateway. So, After understanding machine learning model working we will see that AWS configuration.
So, Start with making machine learning model of human activity recognition using accelerometer data with CNN.
We already know that neural networks perform very well for image recognition. In particular, a specific type of neural networks called Convolutional Neural Networks (CNNs) are best suited for the task of image recognition. I will now explain how the approach of convolutional neural networks differ from that of traditional neural networks.
So, We are going to use 2D CNN model to build the model.
First of all, as we discussed that we used accelometer dataset for get data about human activity such like walking, sitting, running, talking & more. So, below link is about used dataset:
Dataset Link: http://www.cis.fordham.edu/wisdm/dataset.php
This WISDM dataset contains data collected through controlled, laboratory conditions. The total number of examples is 1,098,207. The dataset contains six different labels (Downstairs, Jogging, Sitting, Standing, Upstairs, Walking).
Import necessary libraries
For this model our first step is import tensorflow-keras library which is necessary for build the CNN model. We are also importing the necessary layers required to build the CNN.
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten, Dense, Dropout, BatchNormalization
from tensorflow.keras.layers import Conv2D, MaxPool2D
from tensorflow.keras.optimizers import Adam
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
Here, in above lines we see that pandas, numpy these all necessary python libraries are imported into model.
Load and Cleaning Dataset
After libraries imported our first step is to clean and process our dataset. and also check whether dataset are matched our model.
In this dataset we have 343416 rows and 6 columns.
file = open('WISDM_ar_v1.1/WISDM_ar_v1.1_raw.txt')
lines = file.readlines()
processedList = 
for i, line in enumerate(lines):
line = line.split(',')
last = line.split(';')
last = last.strip()
if last == '':
temp = [line, line, line, line, line, last]
print('Error at line number: ', i)columns = ['user', 'activity', 'time', 'x', 'y', 'z']
data = pd.DataFrame(data = processedList, columns = columns)
we have index such like:
Index(['Walking', 'Jogging', 'Upstairs', 'Downstairs', 'Sitting', 'Standing'], dtype='object')
Now we will plot
z for each activity for 10 seconds.
def plot_activity(activity, data):
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, figsize=(15, 7), sharex=True)
plot_axis(ax0, data['time'], data['x'], 'X-Axis')
plot_axis(ax1, data['time'], data['y'], 'Y-Axis')
plot_axis(ax2, data['time'], data['z'], 'Z-Axis')
def plot_axis(ax, x, y, title):
ax.plot(x, y, 'g')
ax.set_ylim([min(y) - np.std(y), max(y) + np.std(y)])
for activity in activities:
data_for_plot = data[(data['activity'] == activity)][:Fs*10]
Frame preparation and Train and test data from dataset
import scipy.stats as statsFs = 20
frame_size = Fs*4 # 80
hop_size = Fs*2 # 40def get_frames(df, frame_size, hop_size):
N_FEATURES = 3
frames = 
labels = 
for i in range(0, len(df) - frame_size, hop_size):
x = df['x'].values[i: i + frame_size]
y = df['y'].values[i: i + frame_size]
z = df['z'].values[i: i + frame_size]
# Retrieve the most often used label in this segment
label = stats.mode(df['label'][i: i + frame_size])
frames.append([x, y, z])
# Bring the segments into a better shape
frames = np.asarray(frames).reshape(-1, frame_size, N_FEATURES)
labels = np.asarray(labels)
return frames, labels
X, y = get_frames(scaled_X, frame_size, hop_size)
we get output like this:
((532, 80, 3), (532,))
We have 3555 observations for each of the 6 activities. Hence we have a total of (3555*6) observations.
So, we have to train and test data from datset in ratio of 80–20%.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0, stratify = y)
2D CNN Model
A Sequential model is appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor.
model = Sequential()
model.add(Conv2D(16, (2, 2), activation = 'relu', input_shape = X_train.shape))
model.add(Conv2D(32, (2, 2), activation='relu'))
model.add(Dense(64, activation = 'relu'))
model.add(Dense(6, activation='softmax'))model.compile(optimizer=Adam(learning_rate = 0.001), loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
history = model.fit(X_train, y_train, epochs = 10, validation_data= (X_test, y_test), verbose=1)
These above lines are for model creation.
We will now plot the model accuracy and model loss. In model accuracy we will plot the training accuracy and validation accuracy and in model loss we will plot the training loss and validation loss.
- A confusion matrix is a table that is often used to describe the performance of a classification model (or “classifier”) on a set of test data for which the true values are known.
- All correct predictions are located in the diagonal of the table, so it is easy to visually inspect the table for prediction errors, as they will be represented by values outside the diagonal. For two classes the confusion matrix looks like this-
Now, for get confusion matrix for our model,
mat = confusion_matrix(y_test, y_pred)
As you can see we are getting 100% accuracy for Sitting and Standing. The confusion matrix also tells us that our model is getting confused between Upstairs and Downstairs.
Last, we saved model:
So, we created Human activity recognition model using CNN.
Human Activity Recognition using AWS Cloud
So, as I discussed above, I used AWS cloud technology to create human activity recognition model. And also I mentioned that I used Amazon API Gateway, AWS Lambda and Amazon DynamoDB in this project.
Here, we want accelerometer data from android. This accelerometer sensor is already installed in our android phone. So, using some inbuilt functionality of android studio we can get data from android in real time. But as of now we take static data from dataset as we discussed earlier.
So, we can see that from android the data of accelerometer are transferred to the amazon API gateway, then it will transfer to the aws lambda for processing and store to the DynamoDB. and vice-versa.
Here, we have to setup first of all tables. Here, I make tables named acitity Cloud and activity Edge in the DynamoDB. Before that we have to make some permission to make connection and access management.
Amazon Lambda function:
Do, after making table we have to add data to the tables. So, for that, we use lambda function which read and write data to the DynamoDB.
But we can not directly access to the DynamoDB tables for that first of all we need to create policy. using that policy we can connect to the tables.
So, now we create aws lambda function for read and write data to the dynamodb. for that we use some codes.
AWS API Gateway
Now, AWS API Gateway comes in picture!! So, This is basically as name suggest — gateway. So, it makes transaction of data between android and cloud.
AWS API Gateway makes REST connection to the client. And also make security concern like IAM users only, etc. Here, it will use lambda function for connection.
So, here we got output for accelerometer data. Here, below screenshots shows walking and still activiy of human using X,Y,Z coordinates.