Skip to content

eliassrm/NEW_MLP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CASAS Smart Home Activity Learner (AL)

This project provides a powerful and modular implementation for activity recognition and anomaly detection in smart home environments using the CASAS dataset. It supports multi-class classification using traditional machine learning (Random Forest) and neural networks (MLP, Adaline, Linear Regression) with options for anomaly alerting and explainable outputs.


🧠 Core Features

  • Activity classification from raw timestamped sensor data
  • Multiple learning algorithms: random forest, MLP, Adaline, linear regression
  • Customizable training modes: TRAIN, CV, PARTITION, LOO, TEST, ANNOTATE
  • Anomaly alerts for irregular behaviors (e.g., "Leave_Home", "Enter_Home", "Evening_Meds")
  • Automatic clustering of "Other_Activity" into subclasses using k-means
  • Visualization of irregular activity alerts over time
  • Flexible feature extraction and preprocessing pipeline

🗂 Project Structure

al.py                 # Main script for training/testing/predicting
config.py             # Global settings and argparse parsing
data/                 # Sensor data file (default: "data")
model/                # Trained models and config files saved here

📦 Installation

# (Recommended) Create a virtual environment
python -m venv .venv
source .venv/bin/activate  # or .venv\Scripts\activate on Windows

# Install required packages
pip install -r requirements.txt

🚀 Example Usage

1. Basic Random Forest Training and Testing

python al.py --mode PARTITION --data data --algorithm rf

2. Use Adaline with Epoch Logging

python al.py --mode PARTITION --data data --algorithm adaline --epochs 50 --logepochs

3. Train a Neural Network (MLP) and Get Anomaly Alerts

python al.py --mode PARTITION --data data --algorithm mlp --epochs 50 --irregular_threshold 0.4

4. Leave-One-Out Cross-Validation

python al.py --mode LOO --data data/f1 data/f2 data/f3 --algorithm rf

5. Annotate New Data Using Trained Model

python al.py --mode ANNOTATE --data data/newdata.txt --model model

🆕 New Features Added

Feature Description
✅ Gradient-based learners Added Adaline, Linear Regression, and MLP support
✅ Epoch logging --logepochs prints training loss and accuracy per epoch
✅ Irregular behavior alerts Activity-specific alerts for nurse or police actions
✅ Threshold control --irregular_threshold determines confidence threshold for alerts
✅ Cluster "Other_Activity" --clusterother splits ambiguous activity into meaningful clusters
✅ PCA augmentation Optional dimensionality boost with --add_pca (manually set)
✅ Leave-one-out testing Cross-subject generalization via --mode LOO
✅ Modular torch.nn.Module training Unified gradient descent training via _train_gradient()

📊 Output Example

Epoch   1/50: loss=180.272980  acc=0.038
...
[ALERT] Irregular “Evening_Meds” in sample 20501 (conf=0.04) – calling nurse!
[INFO] No irregular activities detected.

🔍 Visualization

If irregular alerts are triggered, a scatter plot of alert timestamps is generated by plot_irregular_events():

  • X-axis: Hour of Day
  • Y-axis: Day of Week
  • Color: Day of Week

🛠 Development Notes

  • Sensor and activity mappings are defined in config.py
  • Feature vectors include 14 statistical/temporal attributes + per-sensor counts
  • Window size = 30 events; configurable in Config

📚 Citation

Diane J. Cook, Washington State University – Original CASAS codebase and datasets


📁 Sample Dataset Format

2010-01-02 00:00:05.173837 M001 M001 ON Bed_Toilet_Transition
2010-01-02 00:00:14.205359 M002 M002 ON Bed_Toilet_Transition
...

✅ Recommended Settings

Use Case Command
Debug first feature vector --featuredump
Ignore "Other_Activity" in training --ignoreother
Automatically cluster "Other_Activity" --clusterother
Anomaly = low model confidence --irregular_threshold 0.4 (default 0.80)

🧪 Testing All Activities

python al.py --mode PARTITION --algorithm adaline --data data --activities "Enter_Home,Leave_Home,Personal_Hygiene,Evening_Meds,Sleep_Out_Of_Bed,Step_Out,Morning_Meds,Bathe" --epochs 50 --logepochs

🧠 License and Acknowledgement

This code and data are adapted from the CASAS Smart Home Project by WSU and are for academic use only.


About

exam

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages