Human Activity Recognition

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

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).

Tensor flow -keras (V 2.1.0)

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.

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.

Output of Head()

we have index such like:

Now we will plot x, y, z for each activity for 10 seconds.

X,Y,Z axes of map for upstair data from dataset
X,Y,Z axes of map for standing data from dataset

Frame preparation and Train and test data from dataset

we get output like this:

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%.

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.

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.

Confusion Matrix

  • 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,

Confusion Matrix

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.

Diagram for HAR using AWS

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.

Amazon DynamoDB:

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.

DyanamoDB Tables — activityEdge and activityCloud

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.

Lambda Functions
for the activityEdge table
for the activityCloud table

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.

API Gateway for function_0 lambda function
Method Execution for function_0 lambda function using Amazon API Gateway

After deployment:

So, here we got output for accelerometer data. Here, below screenshots shows walking and still activiy of human using X,Y,Z coordinates.

References:

https://kgptalkie.com/, https://www.hackster.io/circ_93/human-activity-recognition-with-accelerometer-aws-lambda-320bf3