bruAristimunha commited on
Commit
c7a0d0f
·
verified ·
1 Parent(s): 99de909

Add architecture-only model card

Browse files
Files changed (1) hide show
  1. README.md +293 -0
README.md ADDED
@@ -0,0 +1,293 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ - foundation-model
12
+ - convolutional
13
+ - transformer
14
+ ---
15
+
16
+ # Labram
17
+
18
+ Labram from Jiang, W B et al (2024) .
19
+
20
+ > **Architecture-only repository.** This repo documents the
21
+ > `braindecode.models.Labram` class. **No pretrained weights are
22
+ > distributed here** — instantiate the model and train it on your own
23
+ > data, or fine-tune from a published foundation-model checkpoint
24
+ > separately.
25
+
26
+ ## Quick start
27
+
28
+ ```bash
29
+ pip install braindecode
30
+ ```
31
+
32
+ ```python
33
+ from braindecode.models import Labram
34
+
35
+ model = Labram(
36
+ n_chans=22,
37
+ sfreq=200,
38
+ input_window_seconds=4.0,
39
+ n_outputs=2,
40
+ )
41
+ ```
42
+
43
+ The signal-shape arguments above are example defaults — adjust them
44
+ to match your recording.
45
+
46
+ ## Documentation
47
+
48
+ - Full API reference (parameters, references, architecture figure):
49
+ <https://braindecode.org/stable/generated/braindecode.models.Labram.html>
50
+ - Interactive browser with live instantiation:
51
+ <https://huggingface.co/spaces/braindecode/model-explorer>
52
+ - Source on GitHub: <https://github.com/braindecode/braindecode/blob/master/braindecode/models/labram.py#L196>
53
+
54
+ ## Architecture description
55
+
56
+ The block below is the rendered class docstring (parameters,
57
+ references, architecture figure where available).
58
+
59
+ <div class='bd-doc'><main>
60
+ <p>Labram from Jiang, W B et al (2024) [Jiang2024]_.</p>
61
+ <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><span style="display:inline-block;padding:2px 8px;border-radius:4px;background:#d9534f;color:white;font-size:11px;font-weight:600;margin-right:4px;">Foundation Model</span>
62
+
63
+
64
+
65
+ .. figure:: https://arxiv.org/html/2405.18765v1/x1.png
66
+ :align: center
67
+ :alt: Labram Architecture.
68
+
69
+ Large Brain Model for Learning Generic Representations with Tremendous
70
+ EEG Data in BCI from [Jiang2024]_.
71
+
72
+ This is an **adaptation** of the code [Code2024]_ from the Labram model.
73
+
74
+ The model is transformer architecture with **strong** inspiration from
75
+ BEiTv2 [BeiTv2]_.
76
+
77
+ The models can be used in two modes:
78
+
79
+ - Neural Tokenizer: Design to get an embedding layers (e.g. classification).
80
+ - Neural Decoder: To extract the ampliture and phase outputs with a VQSNP.
81
+
82
+ The braindecode's modification is to allow the model to be used in
83
+ with an input shape of (batch, n_chans, n_times), if neural tokenizer
84
+ equals True. The original implementation uses (batch, n_chans, n_patches,
85
+ patch_size) as input with static segmentation of the input data.
86
+
87
+ The models have the following sequence of steps::
88
+
89
+ if neural tokenizer:
90
+ - SegmentPatch: Segment the input data in patches;
91
+ - TemporalConv: Apply a temporal convolution to the segmented data;
92
+ - Residual adding cls, temporal and position embeddings (optional);
93
+ - WindowsAttentionBlock: Apply a windows attention block to the data;
94
+ - LayerNorm: Apply layer normalization to the data;
95
+ - Linear: An head linear layer to transformer the data into classes.
96
+
97
+ else:
98
+ - PatchEmbed: Apply a patch embedding to the input data;
99
+ - Residual adding cls, temporal and position embeddings (optional);
100
+ - WindowsAttentionBlock: Apply a windows attention block to the data;
101
+ - LayerNorm: Apply layer normalization to the data;
102
+ - Linear: An head linear layer to transformer the data into classes.
103
+
104
+ .. important::
105
+ **Pre-trained Weights Available**
106
+
107
+ This model has pre-trained weights available on the Hugging Face Hub.
108
+ You can load them using:
109
+
110
+ .. code:: python
111
+ from braindecode.models import Labram
112
+
113
+ # Load pre-trained model from Hugging Face Hub
114
+ model = Labram.from_pretrained("braindecode/labram-pretrained")
115
+
116
+ To push your own trained model to the Hub:
117
+
118
+ .. code:: python
119
+ # After training your model
120
+ model.push_to_hub(
121
+ repo_id="username/my-labram-model", commit_message="Upload trained Labram model"
122
+ )
123
+
124
+ Requires installing ``braindecode[hug]`` for Hub integration.
125
+
126
+ .. versionadded:: 0.9
127
+
128
+
129
+ Examples
130
+ --------
131
+ Load pre-trained weights::
132
+
133
+ >>> import torch
134
+ >>> from braindecode.models import Labram
135
+ >>> model = Labram(n_times=1600, n_chans=64, n_outputs=4)
136
+ >>> url = "https://huggingface.co/braindecode/Labram-Braindecode/blob/main/braindecode_labram_base.pt"
137
+ >>> state = torch.hub.load_state_dict_from_url(url, progress=True)
138
+ >>> model.load_state_dict(state)
139
+
140
+
141
+ Parameters
142
+ ----------
143
+ patch_size : int
144
+ The size of the patch to be used in the patch embedding.
145
+ learned_patcher : bool
146
+ Whether to use a learned patch embedding (via a convolutional layer) or a fixed patch embedding (via rearrangement).
147
+ embed_dim : int
148
+ The dimension of the embedding.
149
+ conv_in_channels : int
150
+ The number of convolutional input channels.
151
+ conv_out_channels : int
152
+ The number of convolutional output channels.
153
+ num_layers : int (default=12)
154
+ The number of attention layers of the model.
155
+ num_heads : int (default=10)
156
+ The number of attention heads.
157
+ mlp_ratio : float (default=4.0)
158
+ The expansion ratio of the mlp layer
159
+ qkv_bias : bool (default=False)
160
+ If True, add a learnable bias to the query, key, and value tensors.
161
+ qk_norm : Pytorch Normalize layer (default=nn.LayerNorm)
162
+ If not None, apply LayerNorm to the query and key tensors.
163
+ Default is nn.LayerNorm for better weight transfer from original LaBraM.
164
+ Set to None to disable Q,K normalization.
165
+ qk_scale : float (default=None)
166
+ If not None, use this value as the scale factor. If None,
167
+ use head_dim**-0.5, where head_dim = dim // num_heads.
168
+ drop_prob : float (default=0.0)
169
+ Dropout rate for the attention weights.
170
+ attn_drop_prob : float (default=0.0)
171
+ Dropout rate for the attention weights.
172
+ drop_path_prob : float (default=0.0)
173
+ Dropout rate for the attention weights used on DropPath.
174
+ norm_layer : Pytorch Normalize layer (default=nn.LayerNorm)
175
+ The normalization layer to be used.
176
+ init_values : float (default=0.1)
177
+ If not None, use this value to initialize the gamma_1 and gamma_2
178
+ parameters for residual scaling. Default is 0.1 for better weight
179
+ transfer from original LaBraM. Set to None to disable.
180
+ use_abs_pos_emb : bool (default=True)
181
+ If True, use absolute position embedding.
182
+ use_mean_pooling : bool (default=True)
183
+ If True, use mean pooling.
184
+ init_scale : float (default=0.001)
185
+ The initial scale to be used in the parameters of the model.
186
+ neural_tokenizer : bool (default=True)
187
+ The model can be used in two modes: Neural Tokenizer or Neural Decoder.
188
+ attn_head_dim : bool (default=None)
189
+ The head dimension to be used in the attention layer, to be used only
190
+ during pre-training.
191
+ activation: nn.Module, default=nn.GELU
192
+ Activation function class to apply. Should be a PyTorch activation
193
+ module class like ``nn.ReLU`` or ``nn.ELU``. Default is ``nn.GELU``.
194
+
195
+ References
196
+ ----------
197
+ .. [Jiang2024] Wei-Bang Jiang, Li-Ming Zhao, Bao-Liang Lu. 2024, May.
198
+ Large Brain Model for Learning Generic Representations with Tremendous
199
+ EEG Data in BCI. The Twelfth International Conference on Learning
200
+ Representations, ICLR.
201
+ .. [Code2024] Wei-Bang Jiang, Li-Ming Zhao, Bao-Liang Lu. 2024. Labram
202
+ Large Brain Model for Learning Generic Representations with Tremendous
203
+ EEG Data in BCI. GitHub https://github.com/935963004/LaBraM
204
+ (accessed 2024-03-02)
205
+ .. [BeiTv2] Zhiliang Peng, Li Dong, Hangbo Bao, Qixiang Ye, Furu Wei. 2024.
206
+ BEiT v2: Masked Image Modeling with Vector-Quantized Visual Tokenizers.
207
+ arXiv:2208.06366 [cs.CV]
208
+
209
+ .. rubric:: Hugging Face Hub integration
210
+
211
+ When the optional ``huggingface_hub`` package is installed, all models
212
+ automatically gain the ability to be pushed to and loaded from the
213
+ Hugging Face Hub. Install with::
214
+
215
+ pip install braindecode[hub]
216
+
217
+ **Pushing a model to the Hub:**
218
+
219
+ .. code::
220
+ from braindecode.models import Labram
221
+
222
+ # Train your model
223
+ model = Labram(n_chans=22, n_outputs=4, n_times=1000)
224
+ # ... training code ...
225
+
226
+ # Push to the Hub
227
+ model.push_to_hub(
228
+ repo_id="username/my-labram-model",
229
+ commit_message="Initial model upload",
230
+ )
231
+
232
+ **Loading a model from the Hub:**
233
+
234
+ .. code::
235
+ from braindecode.models import Labram
236
+
237
+ # Load pretrained model
238
+ model = Labram.from_pretrained("username/my-labram-model")
239
+
240
+ # Load with a different number of outputs (head is rebuilt automatically)
241
+ model = Labram.from_pretrained("username/my-labram-model", n_outputs=4)
242
+
243
+ **Extracting features and replacing the head:**
244
+
245
+ .. code::
246
+ import torch
247
+
248
+ x = torch.randn(1, model.n_chans, model.n_times)
249
+ # Extract encoder features (consistent dict across all models)
250
+ out = model(x, return_features=True)
251
+ features = out["features"]
252
+
253
+ # Replace the classification head
254
+ model.reset_head(n_outputs=10)
255
+
256
+ **Saving and restoring full configuration:**
257
+
258
+ .. code::
259
+ import json
260
+
261
+ config = model.get_config() # all __init__ params
262
+ with open("config.json", "w") as f:
263
+ json.dump(config, f)
264
+
265
+ model2 = Labram.from_config(config) # reconstruct (no weights)
266
+
267
+ All model parameters (both EEG-specific and model-specific such as
268
+ dropout rates, activation functions, number of filters) are automatically
269
+ saved to the Hub and restored when loading.
270
+
271
+ See :ref:`load-pretrained-models` for a complete tutorial.</main>
272
+ </div>
273
+
274
+ ## Citation
275
+
276
+ Please cite both the original paper for this architecture (see the
277
+ *References* section above) and braindecode:
278
+
279
+ ```bibtex
280
+ @article{aristimunha2025braindecode,
281
+ title = {Braindecode: a deep learning library for raw electrophysiological data},
282
+ author = {Aristimunha, Bruno and others},
283
+ journal = {Zenodo},
284
+ year = {2025},
285
+ doi = {10.5281/zenodo.17699192},
286
+ }
287
+ ```
288
+
289
+ ## License
290
+
291
+ BSD-3-Clause for the model code (matching braindecode).
292
+ Pretraining-derived weights, if you fine-tune from a checkpoint,
293
+ inherit the licence of that checkpoint and its training corpus.