{ "cells": [ { "cell_type": "markdown", "id": "vision_models-01", "metadata": {}, "source": "# Configure vision models and backends\n\n`VisionModel` provides one interface over `transformers`, `timm`, and `thingsvision` for layer-level activation extraction.\n\n| Backend | Install extra | Example model ID |\n|---|---|---|\n| `transformers` | `vneurotk[vision]` | `facebook/dinov2-base` |\n| `timm` | `vneurotk[timm]` | `resnet50.a1_in1k` |\n| `thingsvision` | `vneurotk[thingsvision]` | backend-specific |\n\nModel construction can access the network unless weights and processor/config files are already cached. The cells below require the selected optional extra and model assets; the documentation build does not execute them." }, { "cell_type": "markdown", "id": "vision_models-02", "metadata": {}, "source": [ "## Load a model" ] }, { "cell_type": "code", "execution_count": null, "id": "vision_models-03", "metadata": {}, "outputs": [], "source": [ "import torch\n", "\n", "import vneurotk as vtk\n", "\n", "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", "model = vtk.VisionModel(\n", " \"facebook/dinov2-base\",\n", " backend=\"transformers\",\n", " device=device,\n", ")" ] }, { "cell_type": "markdown", "id": "vision_models-04", "metadata": {}, "source": [ "VneuroTK passes model identifiers to the selected backend unchanged and does not silently fall back to another backend or model." ] }, { "cell_type": "markdown", "id": "vision_models-05", "metadata": {}, "source": [ "## Inspect and select layers" ] }, { "cell_type": "code", "execution_count": null, "id": "vision_models-06", "metadata": {}, "outputs": [], "source": [ "model.print_modules(max_depth=3)\n", "model.set_selector(module_type=\"Dinov2Layer\")\n", "model.module_names" ] }, { "cell_type": "code", "execution_count": null, "id": "vision_models-07", "metadata": {}, "outputs": [], "source": [ "# Select by name, or take a union of type and name matches.\n", "model.set_selector(module_type=\"Dinov2Layer\", module_name=\"layernorm\")\n", "\n", "# A custom list can be assembled from model.module_list.\n", "last_norm = [item for item in model.module_list if item.module_type == \"LayerNorm\"][-1]\n", "model.set_selector([last_norm])" ] }, { "cell_type": "markdown", "id": "vision_models-08", "metadata": {}, "source": "Use `vneurotk.print_cached_models()` when you need to inspect locally discoverable assets. Its result depends on the machine cache and is intentionally not stored in this notebook.\n\n## Next steps\n\n- [Extract from an image mapping](vision_alone)\n- [Extract with VneuroTK neural data](vision_union)\n- [Vision API](../api/vision.md)" } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.11" } }, "nbformat": 4, "nbformat_minor": 5 }