Skip to content

Add dgcnn model - #951

Merged
bruAristimunha merged 28 commits into
braindecode:masterfrom
ShahVandit:add-dgcnn-model
Mar 21, 2026
Merged

bruAristimunha merged 28 commits into
braindecode:masterfrom
ShahVandit:add-dgcnn-model

Conversation

@ShahVandit

Copy link
Copy Markdown
Contributor

Summary

  • Adds DGCNN (Dynamic Graph Convolutional Neural Network) adapted for EEG classification
  • Treats EEG electrodes as graph nodes, dynamically builds k-NN graph in feature space at each layer
  • Four EdgeConv blocks with multi-scale feature concatenation, global pooling, and MLP head
  • Adapted from the original point cloud DGCNN (Wang et al.) for EEG following Song et al. (2018)

Reference

Song, T., Zheng, W., Song, P., & Cui, Z. (2018). EEG emotion recognition using dynamical
graph convolutional neural networks. IEEE Transactions on Affective Computing, 11(3), 532–541.
https://doi.org/10.1109/TAFFC.2018.2817622

@PierreGtch PierreGtch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ShahVandit for your work already! The PR looks good overall. Here are some comments to implement before we merge

Also I launched the tests, let's see if they pass

Comment thread braindecode/models/dgcnn.py Resolved
Comment thread braindecode/models/dgcnn.py Resolved
Comment thread braindecode/models/dgcnn.py Resolved
Comment thread braindecode/models/dgcnn.py Resolved
Comment thread braindecode/models/dgcnn.py Resolved
Comment thread braindecode/models/dgcnn.py Resolved
Comment thread braindecode/models/dgcnn.py Resolved

@bruAristimunha bruAristimunha left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

many thanks for the contributions, small details to adjust :)

Comment thread braindecode/models/dgcnn.py Resolved
Comment thread braindecode/models/dgcnn.py Resolved
Comment thread braindecode/models/dgcnn.py Resolved
Comment thread braindecode/models/summary.csv Resolved
@bruAristimunha

Copy link
Copy Markdown
Collaborator

some small details, can you please fill the whats new file, apply pre-commit and then, install your branch here and run the benchmark:

https://colab.research.google.com/drive/1pWVGiwJLT53jA2m7nQ_XwvlF3v0heQ_o?usp=sharing

Here is some code that is usually used to test if the model is okay. EEGNex is an good baseline method

@ShahVandit

Copy link
Copy Markdown
Contributor Author

I ran a quick benchmark on the BNCI2014_001 motor imagery dataset (MOABB) comparing DGCNN against EEGNeX and BrainModule: https://colab.research.google.com/drive/10PSyIqr0PRNJUWDjzkrin9k93vUxhzTl

DGCNN underperforms on this benchmark, the original paper used precomputed differential entropy (DE) features on the SEED emotion dataset, not raw EEG time series.

Comment thread braindecode/models/dgcnn.py Resolved
@bruAristimunha

Copy link
Copy Markdown
Collaborator

nice revision!

I understand, thinking about it, I think we can generate the features and feed them into the model.

We can generate this in many ways, such as preprocessing (in braindecode or moabb) or as a dataloader transformation.

Do you have an implementation reference for the feature? I can help you :)

@PierreGtch

Copy link
Copy Markdown
Collaborator

All comments on my side have been addressed, thanks @ShahVandit :)

@ShahVandit

Copy link
Copy Markdown
Contributor Author

Hi @bruAristimunha !

Here is my approach:

  1. Bandpass filter the raw EEG into 5 frequency bands (delta, theta, alpha, beta, gamma)
  2. Compute log-variance per band per channel -> output shape (n_chans, 5)
  3. Feed this into DGCNN with n_times=5
    We could implement this as a braindecode preprocessing function.

Would love your guidance on where this fits best - preprocessing or dataloader transform.

References:
Duan et al. (2013) — "Differential Entropy Feature for EEG-based Emotion Classification" (introduced DE)- https://bcmi.sjtu.edu.cn/home/blu/papers/2013/2013-7.pdf

@ShahVandit

Copy link
Copy Markdown
Contributor Author

Hi @bruAristimunha @PierreGtch
I think all the requested changes have been addressed. If so, can we move forward with merging?

@ShahVandit

Copy link
Copy Markdown
Contributor Author

Sorry, I had pushed the fix to the wrong branch! I've now applied it to the correct branch and verified all DGCNN tests pass locally. Could you please re-run CI?

@bruAristimunha

Copy link
Copy Markdown
Collaborator

hey @ShahVandit,

I am looking at the model, and the main problem is that its performance isn't super good. I am testing some things based on your adjusted tutorial.

@bruAristimunha

Copy link
Copy Markdown
Collaborator

I tested many, many things today. Looks like it is very sensitive to normalization, and if we adjust the learning parameters, we can improve a little (56% was my max), but still very, very far from the 80% for eegnex.

@ShahVandit

Copy link
Copy Markdown
Contributor Author

Thanks for testing! I'm currently running on PhysionetMI (64 channels, closer to SEED's channel count) - the KNN graph should work better with more channels imo. Will update once I have results. Do you think it's worth merging as-is and iterating later, or should we wait for better benchmark results?

@ShahVandit

Copy link
Copy Markdown
Contributor Author

@bruAristimunha @PierreGtch
Apart from this, are there any models, preprocessing methods you'd like to see added to the repo? I'd love to research and discuss potential contributions when I get the opportunity :)

@ShahVandit

Copy link
Copy Markdown
Contributor Author

Hi @bruAristimunha
I'm running benchmarks on my end but keep running into memory/GPU issues, so it's taking longer than expected. Would it be okay to merge based on the results you've already collected? We can always revisit the benchmarks and make improvements.

@bruAristimunha

Copy link
Copy Markdown
Collaborator

hey @ShahVandit,

Many thanks for this PR I am testing, testing, and benchmarking, but it will eventually be merged. If you want to start another PR, I'm happy to review.

Replace the EdgeConv+kNN point-cloud architecture (Wang et al. 2019)
with the correct Chebyshev spectral graph convolution and learnable
adjacency matrix from the original DGCNN paper for EEG emotion
recognition.

Key changes:
- Learnable adjacency matrix initialized from electrode spatial
  positions (chs_info required), with ReLU non-negativity
- Chebyshev polynomial graph filtering (order K) on the normalized
  Laplacian L = I - D^{-1/2} A D^{-1/2}
- Single graph conv layer + FC head (matching official TF code)
- chs_info with 3-D electrode positions is now mandatory; the model
  raises ValueError if positions are missing

Performance improvement:
- SEED-V within-session: 0.47-0.55 -> 0.97
- BNCI2014_001 cross-session: 0.53 -> 0.63

Official TF code reference: http://aip.seu.edu.cn/songtengfei
(archived in EEG-TAC-official/)
- Fix loc[3:6] → loc[0:3] in extract_channel_locations_from_chs_info
  (was reading MNE reference vector instead of electrode position)
- Add degenerate-position check (return None if all zeros)
- Remove DGCNN _extract_positions workaround, use shared utility
- Rename graph_dim → n_filters (default=64) for clarity
- Use sklearn pairwise_distances and kneighbors_graph
- Enrich docstrings with paper math (Eqs. 1, 2, 11-13, Algorithm 1)
- Add local architecture figure (DGCNN.gif)
- Clean up header comment with archive URL
@bruAristimunha

Copy link
Copy Markdown
Collaborator

hey @ShahVandit,

After a long, long time of revision, I discovered the problem. The initial model implemented was the DGCNN for EEG; it was the implementation for point cloud.

Even though they share the same name, they don't actually have the same architecture.

Comment thread docs/models/categorization/gnn.rst Resolved
Comment thread braindecode/models/dgcnn.py Resolved
… GNN docs and TorchScript

- Add MNE montage inference fallback in _build_initial_adjacency when
  locs are missing but channel names are available
- Add chs_info to required params in model registry (raises ValueError
  if not provided)
- Add activation parameter (type[nn.Module], default=nn.ReLU)
- Separate final_layer from classifier Sequential
- Add type annotations to all __init__ parameters
- Enable GNN category card in docs (remove figclass: unavailable)
- Remove DGCNN from TorchScript skip list
- Add Channel badge and category to DGCNN
- Move import mne to top-level in test_models.py
- Fix parameter count in summary.csv (1037385)
- Update DGCNN description to match actual architecture (Chebyshev
  spectral graph convolution, not EdgeConv/k-NN in feature space)
- Change GNN badge from bdg-success to bdg-light to match docs
@bruAristimunha

Copy link
Copy Markdown
Collaborator

Now it is working, after many hours of studying the paper and finding the original source code. If we want to continue on the graph neural network, it could be interesting to implement the top 5-7 more famous methods.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new graph-based EEG classification model (DGCNN) to the braindecode.models package, along with docs, model categorization metadata, and a unit test to validate a forward pass.

Changes:

  • Added braindecode.models.DGCNN implementation and exposed it via braindecode.models.__init__.
  • Updated model registry/summary/categorization (including docs + badges) to include a new “Graph Neural Network” category and the DGCNN entry.
  • Added a unit test that builds chs_info from an MNE standard montage and checks the model forward pass.

Reviewed changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
braindecode/models/dgcnn.py New DGCNN model implementation (learnable adjacency + Chebyshev graph conv + MLP head) and model docstring.
braindecode/models/__init__.py Exports DGCNN from the models package.
braindecode/models/util.py Registers DGCNN mandatory params; adjusts channel-location extraction behavior.
braindecode/models/summary.csv Adds DGCNN to the model summary table with categories/metadata.
test/unit_tests/models/test_models.py Adds a dummy forward-pass test for DGCNN using MNE montage-based chs_info.
test/unit_tests/models/test_model_categorization.py Adds “Graph Neural Network” as a known categorization badge.
docs/models/categorization/gnn.rst Updates GNN category page to include DGCNN.
docs/models/models_categorization.rst Minor doc tweak for GNN models diagram markup.
docs/whats_new.rst Adds a release note entry and author link for the DGCNN addition.
docs/_static/model/DGCNN.gif Adds model architecture image asset.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread braindecode/models/dgcnn.py Resolved
Comment thread braindecode/models/dgcnn.py Resolved
Comment thread braindecode/models/dgcnn.py Resolved
Fix the DGCNN docstring figure path from ../docs/_static/ to ../_static/
so it resolves correctly from the autosummary-generated pages in
docs/generated/. Set DOCSTRING_INHERITANCE_ENABLE=1 in docs/conf.py
before any imports so NumpyDocstringInheritanceInitMeta actually merges
parent parameters (n_outputs, n_chans, etc.) into child model docstrings.
Replace short math-style names (A, d, W, I, k) with descriptive
names following braindecode conventions: electrode_positions,
adjacency, degree, gaussian_weights, cheb_components, etc.
Also expand "FC" to "fully connected" in docstrings.
Comment thread docs/conf.py Resolved
bruAristimunha and others added 5 commits March 21, 2026 13:49
Add Architectural Overview, Macro Components, Graph Convolution Details,
and Additional Comments sections following the same structure used in
EEGNet and ATCNet. Include direct quotes from the original paper and
describe how spatial, spectral, and temporal information is encoded.
@bruAristimunha
bruAristimunha merged commit ba567c2 into braindecode:master Mar 21, 2026
1 check was pending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants