How to Recognize Human Activities with 1D Convolutional Neural Networks in Keras

Feb 13, 2021 | Data Science

Welcome to the exciting world of Human Activity Recognition (HAR)! In this tutorial, we will explore how to harness the power of 1D Convolutional Neural Networks (CNNs) using Python and Keras to recognize various types of human movement based on accelerometer data. So, tighten your shoelaces as we embark on a journey to decode the rhythms of human activity!

Understanding the Basics

1D CNNs are particularly effective in analyzing time sequences like sensor data. Imagine a smart assistant that can analyze how you move—whether you’re walking, jogging, or sprinting. This model will help us differentiate between these activities based on data collected from mobile devices, such as an accelerometer located on your waist.

Getting Started: Prerequisites

  • Python installed on your machine
  • Keras library
  • Access to the WISDM dataset for training
  • A good Python IDE or Jupyter Notebook for coding

Step-by-Step Guide to Building Your 1D CNN

1. Download the WISDM Dataset

First things first, you need to download the WISDM dataset, which you can find here. This dataset contains accelerometer data for various activities.

2. Data Preprocessing

Similar to preparing your ingredients before cooking, data preprocessing is essential. You will need to clean the dataset, remove any noise, and segment the data into fixed-length frames. This helps the model learn better.

3. Model Building

Think of a 1D CNN as a chef, with each layer improving the dish’s complexity. The first layer identifies basic patterns (like recognizing a walking step), and with each subsequent layer, it recognizes more complex movements (like running versus jogging).


from keras.models import Sequential
from keras.layers import Conv1D, MaxPooling1D, Flatten, Dense

# Initializing the CNN
model = Sequential()

# First convolutional layer
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(timesteps, features)))

# Adding pooling layer
model.add(MaxPooling1D(pool_size=2))

# Flattening the layers
model.add(Flatten())

# Adding a fully connected layer
model.add(Dense(units=64, activation='relu'))

# Output layer
model.add(Dense(units=num_classes, activation='softmax'))

In the code above:

  • Conv1D: Represents the chef’s knife, slicing and dicing the raw ingredients (features). The filters help identify basic patterns from the sensor data.
  • MaxPooling1D: This acts as the sieve, ensuring only the most significant features move to the next stage.
  • Flatten: Think of this as prepping your ingredients to be served, converting them into a format that the final dish (output) can consume.

4. Model Compilation and Training

Now that your model is built, it’s time to compile and train it. Here, you need to select the appropriate loss function and optimizer, which act like the seasoning—adding flavor to your dish.


model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Fitting the model to the training data
model.fit(X_train, y_train, epochs=10, batch_size=32)

Troubleshooting Tips

As you venture into the coding realm, you might encounter some hiccups along the way. Here are some troubleshooting ideas:

  • Model not training: Ensure that your dataset is preprocessed correctly, and that you’re using the appropriate input shape.
  • Overfitting: If your model performs well on training but poorly on validation, consider adding dropout layers or using data augmentation techniques.
  • Errors in imports or function calls: Double-check your package installations and ensure all functions are correctly invoked.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Final Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

And there you have it! You are now ready to recognize human activities using a 1D CNN. Enjoy experimenting with different parameters and datasets, and watch as the magic of AI unfolds!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox