Skip to content

Repository files navigation

๐Ÿšถ Gait Detection System Using CNN-BiLSTM

A comprehensive deep learning system for detecting human gait patterns from multi-sensor wearable data (accelerometer, gyroscope, and EMG signals). Built with PyTorch, featuring explainable AI with SHAP integration.


๐Ÿ“‹ Table of Contents


โœจ Features

  • ๐Ÿ”„ Complete Preprocessing Pipeline

    • Data cleaning (missing values, outliers)
    • Signal filtering (Butterworth, Savitzky-Golay, Median)
    • Sensor-specific filtering optimization
    • Z-score normalization with persistence
    • Window segmentation with configurable overlap
  • ๐Ÿง  Advanced Deep Learning Architecture

    • 1D-CNN for local temporal feature extraction
    • Bidirectional LSTM for sequential dependencies
    • Residual connections and batch normalization
    • Attention mechanism for feature importance
    • Binary classification (Gait vs Non-Gait)
  • ๐Ÿ“Š Comprehensive Evaluation

    • Multiple metrics (Accuracy, Precision, Recall, F1, AUC-ROC)
    • Confusion matrices and ROC/PR curves
    • Threshold optimization
    • Per-class performance analysis
  • ๐Ÿ” Explainability with SHAP

    • Feature importance ranking
    • Force plots for individual predictions
    • Summary plots for global interpretability
    • Ready for federated learning integration
  • โš™๏ธ Production-Ready

    • Modular and extensible design
    • Configuration management (YAML/JSON)
    • Comprehensive unit and integration tests
    • CLI interface for inference
    • Model checkpointing and early stopping

๐Ÿ“ Project Structure

gait-detection-system/
โ”‚
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ raw/                          # Original CSV datasets (200+ files)
โ”‚   โ”œโ”€โ”€ processed/                    # Preprocessed numpy arrays
โ”‚   โ””โ”€โ”€ splits/                       # Train/val/test splits
โ”‚
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ preprocessing/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ data_loader.py           # CSV loading & parsing
โ”‚   โ”‚   โ”œโ”€โ”€ cleaner.py               # Missing values & outliers
โ”‚   โ”‚   โ”œโ”€โ”€ filter.py                # Signal filtering
โ”‚   โ”‚   โ”œโ”€โ”€ normalizer.py            # Z-score normalization
โ”‚   โ”‚   โ”œโ”€โ”€ segmentation.py          # Window segmentation
โ”‚   โ”‚   โ””โ”€โ”€ pipeline.py              # Complete pipeline
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ features/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ””โ”€โ”€ time_domain.py           # Time-domain features
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ cnn_bilstm.py            # Model architecture
โ”‚   โ”‚   โ””โ”€โ”€ model_utils.py           # Checkpointing utilities
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ training/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ trainer.py               # Training loop
โ”‚   โ”‚   โ”œโ”€โ”€ validator.py             # Evaluation
โ”‚   โ”‚   โ””โ”€โ”€ metrics.py               # Performance metrics
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ explainability/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ””โ”€โ”€ shap_explainer.py        # SHAP integration
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ utils/
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ”œโ”€โ”€ config.py                # Configuration management
โ”‚       โ””โ”€โ”€ visualization.py         # Plotting utilities
โ”‚
โ”œโ”€โ”€ configs/
โ”‚   โ””โ”€โ”€ config.yaml                  # Hyperparameters
โ”‚
โ”œโ”€โ”€ checkpoints/                     # Saved models
โ”‚
โ”œโ”€โ”€ results/
โ”‚   โ”œโ”€โ”€ logs/                        # Training logs
โ”‚   โ”œโ”€โ”€ plots/                       # Visualizations
โ”‚   โ””โ”€โ”€ shap_outputs/                # SHAP explanations
โ”‚
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ test_preprocessing.py
โ”‚   โ”œโ”€โ”€ test_model.py
โ”‚   โ””โ”€โ”€ test_pipeline.py
โ”‚
โ”œโ”€โ”€ notebooks/
โ”‚   โ”œโ”€โ”€ 01_data_exploration.ipynb
โ”‚   โ”œโ”€โ”€ 02_preprocessing_demo.ipynb
โ”‚   โ””โ”€โ”€ 03_model_evaluation.ipynb
โ”‚
โ”œโ”€โ”€ main.py                          # Main training script
โ”œโ”€โ”€ inference.py                     # Inference script
โ”œโ”€โ”€ requirements.txt                 # Dependencies
โ””โ”€โ”€ README.md                        # This file

๐Ÿ”ง Installation

Prerequisites

  • Python 3.8+
  • CUDA 11.0+ (optional, for GPU acceleration)

Setup

  1. Clone the repository
git clone https://github.com/yourusername/gait-detection-system.git
cd gait-detection-system
  1. Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
  1. Verify installation
python -c "import torch; print(f'PyTorch: {torch.__version__}')"
python -c "import shap; print('SHAP installed successfully')"

๐Ÿš€ Quick Start

1. Prepare Your Data

Place your CSV files in the data/raw/ directory. Expected format:

data/raw/
โ”œโ”€โ”€ HuGaDB_v2_various_01_00.csv
โ”œโ”€โ”€ HuGaDB_v2_various_01_01.csv
โ””โ”€โ”€ ... (200+ files)

2. Configure Settings

Edit configs/config.yaml or use defaults:

preprocessing:
  sampling_rate: 100.0
  window_size: 128
  overlap: 0.5
  normalization_method: 'zscore'
  
model:
  input_features: 38
  seq_length: 128
  conv_filters: [64, 128, 256]
  lstm_hidden_size: 128
  
training:
  num_epochs: 50
  batch_size: 32
  learning_rate: 0.001

3. Train Model

python main.py

This will:

  • โœ… Preprocess all data
  • โœ… Train CNN-BiLSTM model
  • โœ… Evaluate on test set
  • โœ… Generate SHAP explanations
  • โœ… Save results and checkpoints

4. Run Inference

Single file:

python inference.py --input data/raw/test_file.csv --output predictions.npy

Batch inference:

python inference.py --input data/raw/ --output results/predictions/

With probabilities:

python inference.py --input data/raw/test_file.csv --probabilities

๐Ÿ“– Usage Guide

Training from Scratch

from preprocessing.pipeline import PreprocessingPipeline
from models import CNN_BiLSTM_GaitDetector, get_device
from training import GaitDetectorTrainer, create_data_loaders
import torch.nn as nn
import torch.optim as optim

# 1. Preprocess data
pipeline = PreprocessingPipeline(
    window_size=128,
    overlap=0.5,
    normalization_method='zscore'
)

windowed_data, labels = pipeline.preprocess_multiple_files(
    data_dir='data/raw/',
    max_files=10
)

# 2. Split data
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(
    windowed_data, labels, test_size=0.2, random_state=42
)

# 3. Create data loaders
train_loader, val_loader = create_data_loaders(
    X_train, y_train, X_val, y_val, batch_size=32
)

# 4. Initialize model
device = get_device()
model = CNN_BiLSTM_GaitDetector(input_features=38, seq_length=128)

# 5. Setup training
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

trainer = GaitDetectorTrainer(
    model=model,
    train_loader=train_loader,
    val_loader=val_loader,
    criterion=criterion,
    optimizer=optimizer,
    device=device
)

# 6. Train
trainer.train(num_epochs=50)

Making Predictions

from inference import GaitDetectionInference

# Initialize inference system
inference = GaitDetectionInference(
    model_path='checkpoints/best_model.pt',
    normalizer_path='data/processed/normalizer.pkl',
    config_path='checkpoints/training_config.json'
)

# Predict on new file
results = inference.predict_file(
    filepath='data/raw/new_data.csv',
    return_probabilities=False
)

print(f"Prediction: {results['overall_prediction']}")
print(f"Gait %: {results['gait_percentage']:.1f}%")

SHAP Explainability

from explainability import GaitSHAPExplainer
from models import CNN_BiLSTM_GaitDetector, load_checkpoint, get_device

# Load model
device = get_device()
model = CNN_BiLSTM_GaitDetector(input_features=38, seq_length=128)
load_checkpoint('checkpoints/best_model.pt', model, device=device)

# Create explainer
explainer = GaitSHAPExplainer(model=model, device=device)

# Generate explanations
explainer.create_explainer(background_data=X_train[:100])
explainer.explain_samples(test_data=X_test[:20])

# Generate comprehensive report
explainer.generate_explanation_report(
    test_data=X_test[:20],
    output_dir='results/shap_outputs'
)

# Get feature importance
importance = explainer.get_feature_importance()
for feature, score in list(importance.items())[:10]:
    print(f"{feature}: {score:.6f}")

๐Ÿ“Š Dataset Format

Your CSV files should follow this structure (HuGaDB format):

Column Description Type
accelerometer_{location}_{axis} Accelerometer readings (18 columns) Integer
gyroscope_{location}_{axis} Gyroscope readings (18 columns) Integer
EMG_right Right leg EMG Integer
EMG_left Left leg EMG Integer
activity Activity label String

Locations: right_foot, right_shin, right_thigh, left_foot, left_shin, left_thigh
Axes: x, y, z

Total: 38 features + 1 label column

Activity Labels

The system automatically converts activity labels to binary:

  • Gait (1): walk, walking, jog, jogging, run, stairs_up, stairs_down
  • Non-Gait (0): sit, stand, lay, etc.

๐Ÿ—๏ธ Model Architecture

CNN-BiLSTM Hybrid

Input (batch, 128, 38)
    โ†“
Conv1D Block 1: 38 โ†’ 64 filters
    โ†“ (+ Residual Connection)
Conv1D Block 2: 64 โ†’ 128 filters
    โ†“ (+ Residual Connection)
Conv1D Block 3: 128 โ†’ 256 filters
    โ†“
BiLSTM Layer 1: 256 โ†’ 128ร—2
    โ†“
BiLSTM Layer 2: 256 โ†’ 128ร—2
    โ†“
Attention Mechanism
    โ†“
FC Layer 1: 256 โ†’ 256
    โ†“
FC Layer 2: 256 โ†’ 128
    โ†“
Output Layer: 128 โ†’ 1 (Sigmoid)

Key Features

  • 1D Convolutions: Extract local temporal patterns
  • Bidirectional LSTM: Capture forward and backward dependencies
  • Residual Connections: Improve gradient flow
  • Batch Normalization: Stabilize training
  • Attention: Weight important time steps
  • Dropout (30%): Prevent overfitting

Model Parameters

  • Total Parameters: ~2-3M (depending on configuration)
  • Training Time: ~30-60 minutes (GPU) for 50 epochs
  • Inference Speed: ~100-200 windows/second (GPU)

๐Ÿ“ˆ Results

Expected Performance

Metric Score
Accuracy 92-95%
Precision 91-94%
Recall 90-93%
F1-Score 91-94%
AUC-ROC 0.95-0.98

Outputs

After training, you'll find:

  1. Checkpoints (checkpoints/)

    • best_model.pt - Best model weights
    • training_config.json - Training configuration
  2. Logs (results/logs/)

    • training_history.json - Loss/accuracy per epoch
  3. Plots (results/plots/)

    • training_history.png - Training curves
    • confusion_matrix.png - Confusion matrix
    • roc_curve.png - ROC curve
    • pr_curve.png - Precision-Recall curve
  4. SHAP Outputs (results/shap_outputs/)

    • feature_importance.png - Top features
    • shap_summary.png - SHAP summary plot
    • force_plot_sample_*.png - Individual explanations
    • feature_importance.txt - Feature rankings

๐Ÿงช Testing

Run All Tests

# Run all tests
python -m unittest discover tests/

# Run with verbose output
python -m unittest discover tests/ -v

# Run specific test module
python -m unittest tests.test_preprocessing
python -m unittest tests.test_model
python -m unittest tests.test_pipeline

Run with Coverage

# Install coverage
pip install coverage

# Run tests with coverage
coverage run -m unittest discover tests/

# Generate report
coverage report

# Generate HTML report
coverage html
# Open htmlcov/index.html in browser

Test Categories

  1. Unit Tests

    • test_preprocessing.py - All preprocessing components
    • test_model.py - Model architecture and utilities
  2. Integration Tests

    • test_pipeline.py - End-to-end workflow

๐Ÿค Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow PEP 8 style guide
  • Add unit tests for new features
  • Update documentation as needed
  • Use type hints where appropriate

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ™ Acknowledgments

  • HuGaDB Dataset for gait data format
  • PyTorch team for the deep learning framework
  • SHAP library for explainability
  • Open-source community

๐Ÿ“ง Contact

For questions or issues, please:

  • Open an issue on GitHub

๐Ÿ”ฎ Future Enhancements

  • Real-time gait detection
  • Multi-class activity recognition
  • Federated learning implementation
  • Mobile deployment (ONNX/TensorFlow Lite)
  • Web interface for visualization
  • Support for additional sensor types
  • Transfer learning from pre-trained models

Made with โค๏ธ for Human Gait Analysis

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages