This repository presents a Real-Time Facial Expression Recognition System built upon a Spiking Neural Network (SNN). Inspired by the brain's computational principles, this system processes visual information from a live camera feed to identify human facial expressions (e.g., smile, neutral, surprised) in an unsupervised, adaptive manner. Utilizing Leaky Integrate-and-Fire (LIF) neurons and Spike-Timing-Dependent Plasticity (STDP), the network learns and adapts its synaptic weights based on the temporal correlation of neuronal firing, enhanced by homeostatic plasticity and lateral inhibition mechanisms to promote robust and distinct expression representations.
The project demonstrates a practical application of neuromorphic computing principles for real-world tasks, focusing on energy efficiency and biologically plausible learning.
- Neuromorphic Computing Paradigm: Implements a biologically-inspired SNN architecture for real-time processing.
- Real-Time Performance: Processes live camera input with concurrent visualization of network dynamics (neuronal potentials, spike events, and synaptic weights).
- Adaptive Learning: Employs Spike-Timing-Dependent Plasticity (STDP) for unsupervised, online learning of facial features and their association with expressions.
- Robust Biological Mechanisms: Integrates Homeostatic Plasticity (L1-norm weight normalization) to maintain stable neuronal activity and Lateral Inhibition in the output layer for competitive and distinct expression classification.
- Gabor Filter Feature Extraction: Utilizes a bank of Gabor filters for initial feature extraction, mimicking early visual cortex processing.
- Modular Design: Separates the SNN simulation, real-time camera processing (OpenCV), and dynamic plotting (Matplotlib with
multiprocessing) for enhanced performance and modularity.
The system comprises three main components working in parallel:
-
Input Preprocessing and Feature Extraction:
- Grayscale conversion and resizing of facial ROI.
- Application of a diverse Gabor filter bank to extract orientation and scale-specific features.
- Spatial pooling (downsampling) to create a compact feature map.
-
Spiking Neural Network (SNN):
- Feature Neuron Layer: A grid of LIF neurons receives input from the pooled Gabor feature map.
- Expression Neuron Layer: Dedicated LIF neurons for each expression (e.g., 'smile', 'neutral', 'surprise') receive input from the feature layer.
- Synaptic Connections: All-to-all excitatory connections from feature neurons to expression neurons, with weights adapted by STDP.
- Learning Rules:
- STDP: Updates synaptic weights based on the precise timing of pre- and post-synaptic spikes.
- Weight Normalization (Homeostatic Plasticity): Ensures the sum of incoming weights to each expression neuron remains constant, promoting healthy competition and preventing runaway potentiation/depression.
- Lateral Inhibition: Implemented in the expression layer, where the most active expression neuron suppresses the activity of competing neurons, leading to a "winner-take-all" like behavior.
-
Real-Time Visualization and User Interface:
- OpenCV: Handles camera feed acquisition, face detection, ROI extraction, and display of the processed frame with detected expression.
- Matplotlib (Separate Process): Provides real-time plots of neuronal membrane potentials, spike raster plots of expression neurons, and dynamic heatmaps of synaptic weights, offering deep insights into the network's internal dynamics. This component runs in a separate process to avoid blocking the main OpenCV thread.
The subthreshold dynamics of the membrane potential
Where:
-
$V_m$ : Membrane potential -
$\tau_m$ : Membrane time constant -
$V_{rest}$ : Resting potential -
$R_m$ : Membrane resistance -
$I(t)$ : Input current at time$t$
Upon reaching a threshold
The change in synaptic weight
Where:
-
$A_{pre}$ : Potentiation amplitude -
$A_{post}$ : Depression amplitude (typically negative) -
$\tau_{pre}$ : Potentiation time constant -
$\tau_{post}$ : Depression time constant
Synaptic weights
To maintain stable network activity, the sum of absolute incoming weights to each post-synaptic neuron is normalized to a target sum
Where:
-
$w_{ij}$ : Weight from pre-synaptic neuron$i$ to post-synaptic neuron$j$ . -
$S_{target}$ : Desired sum of absolute weights (e.g.,WEIGHT_NORMALIZATION_SUM).
For a set of competing post-synaptic neurons (e.g., expression neurons), the input to a neuron
Where:
-
$I_{j}^{final}$ : Final input current to neuron$j$ . -
$I_{j}^{raw}$ : Raw input current to neuron$j$ . -
$I_{m}^{raw}$ : Raw input current to the most active neuron$m$ . -
$\alpha$ : Lateral inhibition strength (e.g.,LATERAL_INHIBITION_STRENGTH).
- Python 3.x
numpyopencv-pythonmatplotlib
- Clone this repository:
git clone [https://github.com/galihru/NeuromorphicRecognitionComputing.git](https://github.com/galihru/NeuromorphicRecognitionComputing.git) cd Spiking-Neural-Network-Facial-Expression - Install the required Python packages:
pip install numpy opencv-python matplotlib
Execute the main Python script:
python your_main_script_name.py