Skip to main content

Reusable DAAM visualization helpers for timm ViT classifiers.

Project description

DAAM for timm Vision Transformers

DAAM attribution overlays for timm ViT models

Lightweight DAAM attribution toolkit for timm Vision Transformer classifiers. It turns a single image prediction into cumulative and per-layer attention attribution maps that can be inspected, saved, or overlaid on the source image. This project is an independent, compact reimplementation of Dynamic Accumulated Attention Map for modern timm ViT workflows.

Highlights

  • Native timm integration for ViT-style image classifiers.
  • Model-aware preprocessing through timm.data.resolve_model_data_config.
  • Gradient-weighted attribution for the predicted class or a custom target.
  • Cumulative maps, per-block maps, and PIL heatmap overlay utilities.
  • Explicit hook lifecycle with CPU/CUDA device selection.

Installation

After the package is published to PyPI:

pip install daam-timm-vit

For local development:

pip install -e .

For the pinned local environment:

pip install -r requirements.txt

See the PyPI release guide for the release checklist used to publish this package to PyPI.

Quick Start

from daam import TimmViTDAAM, load_image, overlay_heatmap, prepare_image

image = load_image("InputImage/ILSVRC2012_val_00000269.JPEG")

with TimmViTDAAM.from_name("vit_base_patch16_224", pretrained=True) as daam:
    tensor = prepare_image(image, daam.model)
    result = daam(tensor)

overlay = overlay_heatmap(image, result.final_map)
overlay.save("daam_overlay.png")

print(result.predicted_index, result.probabilities.max().item())

Explain a non-top class by passing a target index:

result = daam(tensor, target_index=243)

Custom Weights

DAAM does not require the official timm pretrained weights. If your model is still a compatible timm ViT architecture, build the same architecture and load your own checkpoint:

from daam import TimmViTDAAM, load_image, overlay_heatmap, prepare_image

image = load_image("InputImage/ILSVRC2012_val_00000269.JPEG")

with TimmViTDAAM.from_name(
    "vit_base_patch16_224",
    pretrained=False,
    checkpoint_path="checkpoints/my_vit.pt",
    num_classes=100,
) as daam:
    tensor = prepare_image(image, daam.model)
    result = daam(tensor)

overlay_heatmap(image, result.final_map).save("custom_daam_overlay.png")

Common checkpoint formats are detected automatically, including dictionaries with state_dict, model, model_state_dict, model_ema, net, network, or module keys. For other formats, pass checkpoint_key="..." or load the weights yourself and pass state_dict=.... Pass the same timm.create_model arguments used during training, such as num_classes or img_size, so checkpoint tensor shapes match the model. See the custom checkpoint guide for a compact custom checkpoint guide.

You can also pass an already-created model directly:

import timm
import torch
from daam import TimmViTDAAM

model = timm.create_model("vit_base_patch16_224", pretrained=False, num_classes=100)
checkpoint = torch.load("checkpoints/my_vit.pt", map_location="cpu")
model.load_state_dict(checkpoint["state_dict"])

with TimmViTDAAM(model) as daam:
    ...

API

  • TimmViTDAAM.from_name(model_name, pretrained=True, device="auto", normalize_blocks=False, checkpoint_path=None, state_dict=None, checkpoint_key=None, strict=True) builds a supported timm classifier and registers attribution hooks.
  • prepare_image(image, model) applies the inference transform expected by the selected model.
  • DAAMResult.final_map returns the accumulated attribution map across blocks.
  • DAAMResult.last_layer_map returns the attribution map from the final block.
  • overlay_heatmap(image, heatmap, alpha=0.45) returns a blended PIL image.

Supported Models

DAAM targets ViT-style timm classifiers with model.blocks[*].attn attention blocks. Validated families include BEiT, DeiT, EVA, FlexiViT, NaFlexViT, ViT, and ViTamin variants.

See the smoke-tested model list. Support means hook compatibility for DAAM forward/backward passes; large models may still require CUDA memory tuning.

Constraints

  • One image per call: tensors must be shaped [1, C, H, W].
  • Model output must be a tensor of class logits.
  • Non-ViT architectures are intentionally out of scope.

Citation

DAAM was introduced in the Pattern Recognition paper below. If this implementation supports your research or engineering work, please cite the original method:

@article{yiliaoPR2025dynamic,
  title={Dynamic Accumulated Attention Map for Interpreting Evolution of Decision-making in Vision Transformer},
  author={Liao, Yi and Gao, Yongsheng and Zhang, Weichuan},
  journal={Pattern Recognition},
  volume={165},
  pages={111607},
  year={2025},
  publisher={Elsevier}
}

License

MIT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

daam_timm_vit-0.1.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

daam_timm_vit-0.1.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file daam_timm_vit-0.1.0.tar.gz.

File metadata

  • Download URL: daam_timm_vit-0.1.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for daam_timm_vit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7ad4982b1f65615cdb752925e1fbb185b8ab5faaba599810b655f477fca4a55c
MD5 cadc37861f10ab3342d4a78d0bd166e2
BLAKE2b-256 609e80bded43115e163e511d0a3e4ed44bac3afbe2137a63d7d10f7568e66c77

See more details on using hashes here.

File details

Details for the file daam_timm_vit-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: daam_timm_vit-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for daam_timm_vit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eb202b10a6496838f771577b20525edd7089698a95329fd0cba63bf8a4aca417
MD5 3b1a12949e7e8bd6e491574dfa01d645
BLAKE2b-256 141081ca1e81702fb84242f311029b335e2267aa52dc8c016ad69f206ef8716d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page