Feature Extraction
Transformers
Safetensors
encoder_things_eeg2
brain-encoding
neuroscience
eeg
vision
things-eeg2
image-to-eeg
custom_code
Instructions to use epfl-neuroai/encoder-things-eeg2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use epfl-neuroai/encoder-things-eeg2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="epfl-neuroai/encoder-things-eeg2", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("epfl-neuroai/encoder-things-eeg2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 2,645 Bytes
0cdf91f ce8cf99 0cdf91f ce8cf99 0cdf91f ce8cf99 0cdf91f ce8cf99 0cdf91f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | """``transformers`` wrapper of encoder-things-eeg2: ``AutoModel.from_pretrained(...)`` returns this
class, which runs the plain-PyTorch model in ``modeling.py``."""
from transformers import PreTrainedModel
from .configuration_encoder_things_eeg2 import EncoderThingsEEG2Config
from .modeling import EncoderThingsEEG2, variant_config
class EncoderThingsEEG2Model(PreTrainedModel):
config_class = EncoderThingsEEG2Config
base_model_prefix = "encoder"
main_input_name = "images"
@classmethod
def from_pretrained(cls, pretrained_model_name_or_path, *args, variant=None, **kwargs):
"""``variant`` selects a variant (default: the repository's ``default_variant``).
``transformers`` then loads only that variant's weights, ``model.<variant>.safetensors``
(the default variant's are ``model.safetensors``)."""
config = kwargs.get("config")
if not isinstance(config, EncoderThingsEEG2Config):
hub = ("revision", "cache_dir", "token", "local_files_only", "force_download")
config = cls.config_class.from_pretrained(
pretrained_model_name_or_path, **{k: kwargs[k] for k in hub if k in kwargs}
)
kwargs["config"] = config
name = variant or config.default_variant
if name not in config.variants:
raise ValueError(f"unknown variant {name!r}; available: {', '.join(config.variants)}")
config.selected_variant = name
weights = None if name == config.default_variant else name
return super().from_pretrained(
pretrained_model_name_or_path, *args, variant=weights, **kwargs
)
def __init__(self, config: EncoderThingsEEG2Config):
super().__init__(config)
selected = getattr(config, "selected_variant", None)
self.encoder = EncoderThingsEEG2(variant_config(config.to_dict(), selected))
self.post_init()
self.eval()
@property
def variant(self) -> str:
return self.encoder.variant
def _init_weights(self, module):
"""All weights come from the checkpoint."""
@property
def subjects(self) -> list[str]:
return self.encoder.subjects
@property
def channels(self) -> list[str]:
return self.encoder.channels
@property
def times(self) -> list[float]:
return self.encoder.times
def forward(self, images, subjects=None, batch_size: int = 64):
"""See ``EncoderThingsEEG2.forward``: ``(images, 63, 80)`` by default."""
return self.encoder(images, subjects=subjects, batch_size=batch_size)
__all__ = ["EncoderThingsEEG2Model"]
|