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
File size: 2,708 Bytes
2db6c20 | 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 71 | """``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"
@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, 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()
@property
def variant(self) -> str:
return self.encoder.variant
def _init_weights(self, module):
"""All weights come from the checkpoint."""
@property
def monkeys(self) -> list[str]:
return self.encoder.monkeys
@property
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"]
|