Feature Extraction
Transformers
Safetensors
encoder_tvsd
brain-encoding
neuroscience
macaque
electrophysiology
vision
tvsd
things
custom_code
Instructions to use epfl-neuroai/encoder-tvsd with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use epfl-neuroai/encoder-tvsd with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="epfl-neuroai/encoder-tvsd", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("epfl-neuroai/encoder-tvsd", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download modeling_encoder_tvsd.py from epfl-neuroai/encoder-tvsd: direct link, hf CLI and curl.
- Browser
- Download file 2.71 kB
-
https://huggingface.co/epfl-neuroai/encoder-tvsd/resolve/main/modeling_encoder_tvsd.py
- Command line
-
hf download hf://epfl-neuroai/encoder-tvsd/modeling_encoder_tvsd.py
-
curl -L -o modeling_encoder_tvsd.py https://huggingface.co/epfl-neuroai/encoder-tvsd/resolve/main/modeling_encoder_tvsd.py
2.71 kB
| """``transformers`` wrapper of encoder-tvsd: ``AutoModel.from_pretrained(...)`` returns this class, | |
| which runs the plain-PyTorch model in ``modeling.py``.""" | |
| from transformers import PreTrainedModel | |
| from .configuration_encoder_tvsd import EncoderTVSDConfig | |
| from .modeling import EncoderTVSD, variant_config | |
| class EncoderTVSDModel(PreTrainedModel): | |
| config_class = EncoderTVSDConfig | |
| base_model_prefix = "encoder" | |
| main_input_name = "images" | |
| 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, EncoderTVSDConfig): | |
| 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: EncoderTVSDConfig): | |
| super().__init__(config) | |
| selected = getattr(config, "selected_variant", None) | |
| self.encoder = EncoderTVSD(variant_config(config.to_dict(), selected)) | |
| self.post_init() | |
| self.eval() | |
| def variant(self) -> str: | |
| return self.encoder.variant | |
| def _init_weights(self, module): | |
| """All weights come from the checkpoint.""" | |
| def monkeys(self) -> list[str]: | |
| return self.encoder.monkeys | |
| def areas(self) -> list[str]: | |
| return list(self.encoder.areas) | |
| def features(self, images, area: str, batch_size: int = 32): | |
| """See ``EncoderTVSD.features``: the readout input of ``area``.""" | |
| return self.encoder.features(images, area, batch_size=batch_size) | |
| def forward(self, images, areas=None, batch_size: int = 32): | |
| """See ``EncoderTVSD.forward``: ``{monkey: {area: (images, electrodes)}}``.""" | |
| return self.encoder(images, areas=areas, batch_size=batch_size) | |
| __all__ = ["EncoderTVSDModel"] | |