bruAristimunha commited on
Commit
160c0ae
·
verified ·
1 Parent(s): aa165f9

Add architecture-only model card

Browse files
Files changed (1) hide show
  1. README.md +269 -0
README.md ADDED
@@ -0,0 +1,269 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: bsd-3-clause
3
+ library_name: braindecode
4
+ pipeline_tag: feature-extraction
5
+ tags:
6
+ - eeg
7
+ - biosignal
8
+ - pytorch
9
+ - neuroscience
10
+ - braindecode
11
+ - convolutional
12
+ - transformer
13
+ ---
14
+
15
+ # SCCNet
16
+
17
+ SCCNet from Wei, C S (2019) .
18
+
19
+ > **Architecture-only repository.** This repo documents the
20
+ > `braindecode.models.SCCNet` class. **No pretrained weights are
21
+ > distributed here** — instantiate the model and train it on your own
22
+ > data, or fine-tune from a published foundation-model checkpoint
23
+ > separately.
24
+
25
+ ## Quick start
26
+
27
+ ```bash
28
+ pip install braindecode
29
+ ```
30
+
31
+ ```python
32
+ from braindecode.models import SCCNet
33
+
34
+ model = SCCNet(
35
+ n_chans=22,
36
+ sfreq=250,
37
+ input_window_seconds=4.0,
38
+ n_outputs=4,
39
+ )
40
+ ```
41
+
42
+ The signal-shape arguments above are example defaults — adjust them
43
+ to match your recording.
44
+
45
+ ## Documentation
46
+
47
+ - Full API reference (parameters, references, architecture figure):
48
+ <https://braindecode.org/stable/generated/braindecode.models.SCCNet.html>
49
+ - Interactive browser with live instantiation:
50
+ <https://huggingface.co/spaces/braindecode/model-explorer>
51
+ - Source on GitHub: <https://github.com/braindecode/braindecode/blob/master/braindecode/models/sccnet.py#L17>
52
+
53
+ ## Architecture description
54
+
55
+ The block below is the rendered class docstring (parameters,
56
+ references, architecture figure where available).
57
+
58
+ <div class='bd-doc'><main>
59
+ <p>SCCNet from Wei, C S (2019) [sccnet]_.</p>
60
+ <span style="display:inline-block;padding:2px 8px;border-radius:4px;background:#5cb85c;color:white;font-size:11px;font-weight:600;margin-right:4px;">Convolution</span>
61
+
62
+
63
+
64
+ Spatial component-wise convolutional network (SCCNet) for motor-imagery EEG
65
+ classification.
66
+
67
+ .. figure:: https://dt5vp8kor0orz.cloudfront.net/6e3ec5d729cd51fe8acc5a978db27d02a5df9e05/2-Figure1-1.png
68
+ :align: center
69
+ :alt: Spatial component-wise convolutional network
70
+ :width: 680px
71
+
72
+ .. rubric:: Architectural Overview
73
+
74
+ SCCNet is a spatial-first convolutional layer that fixes temporal kernels in seconds
75
+ to make its filters correspond to neurophysiologically aligned windows. The model
76
+ comprises four stages:
77
+
78
+ 1. **Spatial Component Analysis**: Performs convolution spatial filtering
79
+ across all EEG channels to extract spatial components, effectively
80
+ reducing the channel dimension.
81
+ 2. **Spatio-Temporal Filtering**: Applies convolution across the spatial
82
+ components and temporal domain to capture spatio-temporal patterns.
83
+ 3. **Temporal Smoothing (Pooling)**: Uses average pooling over time to smooth the
84
+ features and reduce the temporal dimension, focusing on longer-term patterns.
85
+ 4. **Classification**: Flattens the features and applies a fully connected
86
+ layer.
87
+
88
+ .. rubric:: Macro Components
89
+
90
+ - `SCCNet.spatial_conv` **(spatial component analysis)**
91
+
92
+ - *Operations.*
93
+ - :class:`~torch.nn.Conv2d` with kernel `(n_chans, N_t)` and stride `(1, 1)` on an input reshaped to `(B, 1, n_chans, T)`; typical choice `N_t=1` yields a pure across-channel projection (montage-wide linear spatial filter).
94
+ - Zero padding to preserve time, :class:`~torch.nn.BatchNorm2d`; output has `N_u` component signals shaped `(B, 1, N_u, T)` after a permute step.
95
+
96
+ *Interpretability/robustness.* Mimics CSP-like spatial filtering: each learned filter is a channel-weighted component, easing inspection and reducing channel noise.
97
+
98
+ - `SCCNet.spatial_filt_conv` **(spatio-temporal filtering)**
99
+
100
+ - *Operations.*
101
+ - :class:`~torch.nn.Conv2d` with kernel `(N_u, 12)` over components and time (12 samples ~ 0.1 s at 125 Hz),
102
+ - :class:`~torch.nn.BatchNorm2d`;
103
+ - Nonlinearity is **power-like**: the original paper uses **square** like :class:`~braindecode.models.ShallowFBCSPNet` with the class :class:`~braindecode.modules.LogActivation` as default.
104
+ - :class:`~torch.nn.Dropout` with rate `p=0.5`.
105
+
106
+ - *Role.* Learns frequency-selective energy features and inter-component interactions within a 0.1 s context (beta/alpha cycle scale).
107
+
108
+ - `SCCNet.temporal_smoothing` **(aggregation + readout)**
109
+
110
+ - *Operations.*
111
+ - :class:`~torch.nn.AvgPool2d` with size `(1, 62)` (~ 0.5 s) for temporal smoothing and downsampling
112
+ - :class:`~torch.nn.Flatten`
113
+ - :class:`~torch.nn.Linear` to `n_outputs`.
114
+
115
+
116
+ .. rubric:: Convolutional Details
117
+
118
+ * **Temporal (where time-domain patterns are learned).**
119
+ The second block's kernel length is fixed to 12 samples (≈ 100 ms) and slides with
120
+ stride 1; average pooling `(1, 62)` (≈ 500 ms) integrates power over longer spans.
121
+ These choices bake in short-cycle detection followed by half-second trend smoothing.
122
+
123
+ * **Spatial (how electrodes are processed).**
124
+ The first block's kernel spans **all electrodes** `(n_chans, N_t)`. With `N_t=1`,
125
+ it reduces to a montage-wide linear projection, mapping channels → `N_u` components.
126
+ The second block mixes **across components** via kernel height `N_u`.
127
+
128
+ * **Spectral (how frequency information is captured).**
129
+ No explicit transform is used; learned **temporal kernels** serve as bandpass-like
130
+ filters, and the **square/log power** nonlinearity plus 0.5 s averaging approximate
131
+ band-power estimation (ERD/ERS-style features).
132
+
133
+ .. rubric:: Attention / Sequential Modules
134
+
135
+ This model contains **no attention** and **no recurrent units**.
136
+
137
+ .. rubric:: Additional Mechanisms
138
+
139
+ - :class:`~torch.nn.BatchNorm2d` and zero-padding are applied to both convolutions;
140
+ L2 weight decay was used in the original paper; dropout `p=0.5` combats overfitting.
141
+ - Contrasting with other compact neural network, in EEGNet performs a temporal depthwise conv
142
+ followed by a **depthwise spatial** conv (separable), learning temporal filters first.
143
+ SCCNet inverts this order: it performs a **full spatial projection first** (CSP-like),
144
+ then a short **spatio-temporal** conv with an explicit 0.1 s kernel, followed by
145
+ **power-like** nonlinearity and longer temporal averaging. EEGNet's ELU and
146
+ separable design favor parameter efficiency; SCCNet's second-scale kernels and
147
+ square/log emphasize interpretable **band-power** features.
148
+
149
+ - Reference implementation: see [sccnetcode]_.
150
+
151
+ .. rubric:: Usage and Configuration
152
+
153
+ * **Training from the original authors.**
154
+
155
+ * Match window length so that `T` is comfortably larger than pooling length
156
+ (e.g., > 1.5-2 s for MI).
157
+ * Start with standard MI augmentations (channel dropout/shuffle, time reverse)
158
+ and tune `n_spatial_filters` before deeper changes.
159
+
160
+ Parameters
161
+ ----------
162
+ n_spatial_filters : int, optional
163
+ Number of spatial filters in the first convolutional layer, variable `N_u` from the
164
+ original paper. Default is 22.
165
+ n_spatial_filters_smooth : int, optional
166
+ Number of spatial filters used as filter in the second convolutional
167
+ layer. Default is 20.
168
+ drop_prob : float, optional
169
+ Dropout probability. Default is 0.5.
170
+ activation : nn.Module, optional
171
+ Activation function after the second convolutional layer. Default is
172
+ logarithm activation.
173
+
174
+ References
175
+ ----------
176
+ .. [sccnet] Wei, C. S., Koike-Akino, T., & Wang, Y. (2019, March). Spatial
177
+ component-wise convolutional network (SCCNet) for motor-imagery EEG
178
+ classification. In 2019 9th International IEEE/EMBS Conference on
179
+ Neural Engineering (NER) (pp. 328-331). IEEE.
180
+ .. [sccnetcode] Hsieh, C. Y., Chou, J. L., Chang, Y. H., & Wei, C. S.
181
+ XBrainLab: An Open-Source Software for Explainable Artificial
182
+ Intelligence-Based EEG Analysis. In NeurIPS 2023 AI for
183
+ Science Workshop.
184
+
185
+ .. rubric:: Hugging Face Hub integration
186
+
187
+ When the optional ``huggingface_hub`` package is installed, all models
188
+ automatically gain the ability to be pushed to and loaded from the
189
+ Hugging Face Hub. Install with::
190
+
191
+ pip install braindecode[hub]
192
+
193
+ **Pushing a model to the Hub:**
194
+
195
+ .. code::
196
+ from braindecode.models import SCCNet
197
+
198
+ # Train your model
199
+ model = SCCNet(n_chans=22, n_outputs=4, n_times=1000)
200
+ # ... training code ...
201
+
202
+ # Push to the Hub
203
+ model.push_to_hub(
204
+ repo_id="username/my-sccnet-model",
205
+ commit_message="Initial model upload",
206
+ )
207
+
208
+ **Loading a model from the Hub:**
209
+
210
+ .. code::
211
+ from braindecode.models import SCCNet
212
+
213
+ # Load pretrained model
214
+ model = SCCNet.from_pretrained("username/my-sccnet-model")
215
+
216
+ # Load with a different number of outputs (head is rebuilt automatically)
217
+ model = SCCNet.from_pretrained("username/my-sccnet-model", n_outputs=4)
218
+
219
+ **Extracting features and replacing the head:**
220
+
221
+ .. code::
222
+ import torch
223
+
224
+ x = torch.randn(1, model.n_chans, model.n_times)
225
+ # Extract encoder features (consistent dict across all models)
226
+ out = model(x, return_features=True)
227
+ features = out["features"]
228
+
229
+ # Replace the classification head
230
+ model.reset_head(n_outputs=10)
231
+
232
+ **Saving and restoring full configuration:**
233
+
234
+ .. code::
235
+ import json
236
+
237
+ config = model.get_config() # all __init__ params
238
+ with open("config.json", "w") as f:
239
+ json.dump(config, f)
240
+
241
+ model2 = SCCNet.from_config(config) # reconstruct (no weights)
242
+
243
+ All model parameters (both EEG-specific and model-specific such as
244
+ dropout rates, activation functions, number of filters) are automatically
245
+ saved to the Hub and restored when loading.
246
+
247
+ See :ref:`load-pretrained-models` for a complete tutorial.</main>
248
+ </div>
249
+
250
+ ## Citation
251
+
252
+ Please cite both the original paper for this architecture (see the
253
+ *References* section above) and braindecode:
254
+
255
+ ```bibtex
256
+ @article{aristimunha2025braindecode,
257
+ title = {Braindecode: a deep learning library for raw electrophysiological data},
258
+ author = {Aristimunha, Bruno and others},
259
+ journal = {Zenodo},
260
+ year = {2025},
261
+ doi = {10.5281/zenodo.17699192},
262
+ }
263
+ ```
264
+
265
+ ## License
266
+
267
+ BSD-3-Clause for the model code (matching braindecode).
268
+ Pretraining-derived weights, if you fine-tune from a checkpoint,
269
+ inherit the licence of that checkpoint and its training corpus.