Skip to main content

DAAM for timm Vision Transformers

DAAM attention flow

A compact, timm-native implementation of Dynamic Accumulated Attention Map (Liao, Gao, Zhang — Pattern Recognition 2025, arXiv:2503.14640). It reveals the attention flow: one map per transformer block, accumulated from the first block to the last, showing how the decision-making token forms its attention.

pip install daam-timm-vit

Quick start

import timm
from PIL import Image
from daam import DAAM, heatmap, overlay

model = timm.create_model("deit_small_patch16_224", pretrained=True)
transform = timm.data.create_transform(**timm.data.resolve_model_data_config(model))

image = Image.open("InputImage/ILSVRC2012_val_00046384.JPEG").convert("RGB")
with DAAM(model) as daam:
    result = daam(transform(image).unsqueeze(0))          # explains the top-1 class
    # result = daam(x, target=74)                          # or any class index

maps = heatmap(result.maps[0], size=image.size[::-1])     # (num_blocks, H, W) uint8
overlay(image, maps[-1]).save("daam.jpg")                 # final block
for b, hm in enumerate(maps, 1):                          # the whole flow
    overlay(image, hm).save(f"daam_block{b}.jpg")

python example.py [model_name] runs this on InputImage/ and writes to results/.

Two variants

The paper defines the channel importance coefficients C_b = ∂Y/∂T_b for two settings; the variant is picked from the model automatically:

model Y target
has a classifier (num_classes > 0) class score (Eq. 8) class index, default top-1
no head (num_classes == 0, e.g. DINO/DINOv2/DINOv3, or model.reset_classifier(0)) feature · dimension-wise weight w (Eq. 13) optional (K, D) reference features (k-NN memory bank neighbours, class prototypes, …); default: the image's own feature, w_d = f̂_d²
model = timm.create_model("vit_small_patch14_dinov2", pretrained=True)   # no head
with DAAM(model) as daam:
    result = daam(x)                      # label-free
    result = daam(x, target=bank[idx])    # weight from k-NN neighbours, as in the paper

What you get

  • result.block_maps — (B, L, h, w) per-block maps L_b on the patch grid (Eq. 9).
  • result.maps — (B, L, h, w) accumulated maps L_{b,DAAM} (Eq. 11–12).
  • result.output, result.target — logits/features and the explained class.
  • heatmap(maps, size) — official rendering: scale by the final block's max, sigmoid(5x) − 0.5, min-max, bilinear upsample → uint8.
  • overlay(image, hm, alpha=0.4) — jet-style blend as a PIL image.

Supported models

Any timm model with model.blocks[*].attn exposing attn_drop/proj (ViT, DeiT, DINOv2/v3, EVA/EVA02, BEiT, FlexiViT, …) and global_pool of 'token' (the paper's [CLS] decomposition) or 'avg' (the mean patch token is decomposed the same way). Register tokens are handled. The attention matrix is read from timm's own non-fused path, so RoPE, q/k-norm and relative position bias need no special casing; fused_attn is restored on close().

Custom weights: use timm — timm.create_model(name, checkpoint_path="my.pt", num_classes=...).

Citation

@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}
}

MIT license. Official code: https://github.com/ly9802/DynamicAccumulatedAttentionMap

Metadata

Release files for daam-timm-vit 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for daam-timm-vit 0.2.0
File Size Uploaded Provenance Actions
daam_timm_vit-0.2.0.tar.gz 6.8 kB Without provenance Details for daam_timm_vit-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for daam-timm-vit 0.2.0
File Interpreter ABI Platform Actions
daam_timm_vit-0.2.0-py3-none-any.whl Python 3 none any Details for daam_timm_vit-0.2.0-py3-none-any.whl

Total release size: 14.0 kB

Release files / daam_timm_vit-0.2.0.tar.gz

Download URL daam_timm_vit-0.2.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
42f574f9ef81b4e8a9743e9074b3a716e9bae51f0b523461bd556a2b8055e957
BLAKE2b-256 checksum
How to use checksums
b0bac0a9ed1c605737074398e7d75360a3058c6fd815b03357cd8c720cbadfb8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / daam_timm_vit-0.2.0-py3-none-any.whl

Download URL daam_timm_vit-0.2.0-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4af21baeb9d2f49606f4c62c9110f0b016190150beb02299722e7e3915b5783d
BLAKE2b-256 checksum
How to use checksums
f8eb346993292f1d83569a1db33c778e4220aff4f5e1a094283b9b5df1094d0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for daam-timm-vit 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page