Skip to content
프로젝트

소개

A utility for generating heatmaps of YOLO object detection models using Layerwise Relevance Propagation (LRP/CRP).

AI 및 머신러닝

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

yolo-heatmaps

Explanatory heatmaps for YOLO26 object detection results, using Layer-wise Relevance Propagation (LRP) and its contrastive variant (CRP) — Bach et al.. YOLOv8/YOLOv9/YOLOv10/YOLO11 are also fully supported — see "Supported YOLO versions" below.

Given a detector and an image, YOLOLRP explains why the model predicted a given class at a given location: LRP produces a per-pixel relevance map for one class on its own, and CRP contrasts that class against every other class, so the resulting map shows what counts for the class (positive) as well as against it (negative).

LRP heatmaps

LRP explanation of a dog, bicycle, and truck

Installation

Install straight from a checkout:

py -m venv .venv
.venv\Scripts\activate
pip install .

That gives you the yolo_lrp package (import yolo_lrp...) plus a yolo-lrp console script. If you're developing this repo instead — running the test suite, editing the code, using the notebook — install it editable with the dev extras:

pip install -e ".[dev]"

requirements.txt/requirements-dev.txt still exist alongside pyproject.toml for anyone who wants a plain, non-editable dependency install rather than the packaged one; requirements-dev.txt in particular is an exact-pinned snapshot of this repo's own working dev venv (see its own header comment) rather than [project.optional-dependencies].dev's loose version ranges.

Weights aren't bundled — the first YOLO(...) call downloads the requested checkpoint (e.g. yolo26n.pt) automatically via ultralytics.

Quick start

Command line

yolo-lrp dog_bicycle_truck.jpg --classes dog bicycle truck --contrastive

(Working from a checkout without installing? python -m yolo_lrp.cli ... works identically.)

Writes one heatmap PNG per requested class (plus the raw relevance as .npy) to an auto-named output directory. Run yolo-lrp --help for the full list of knobs — model/weights, propagation-rule parameters (power/eps/positive), contrastive weighting, colormap, etc.

Python API

from ultralytics import YOLO
from yolo_lrp.yolo.explainer import YOLOLRP

# `image` is a (3, H, W) float tensor, H and W divisible by 32
yolo = YOLO('yolo26n.pt')
lrp = YOLOLRP(yolo, power=2, eps=1e-05)

explanation_lrp = lrp.explain(image, cls='person', contrastive=False)
explanation_crp = lrp.explain(image, cls='person', contrastive=True)

Don't call yolo(...)/yolo.predict(...) on the wrapped model directly — that fuses it for standalone inference and breaks the internal structure YOLOLRP reads from. Construct YOLOLRP from a freshly-loaded model and only ever call it through YOLOLRP/explain() afterward.

Supported YOLO versions

Version Checkpoint tested
YOLOv8 yolov8n.pt
YOLOv9 yolov9t.pt
YOLO26 yolo26n.pt
YOLOv10 yolov10n.pt
YOLO11 yolo11n.pt

Project layout

yolo_lrp/
  lrp/            Architecture-independent LRP machinery
    relevance.py    RelevanceMessage / LayerRelevance - the scatter/gather/cache data structures
    rules.py         PropRule (Conv/Linear/MaxPool/Upsample) - Strategy-pattern relevance rules
    inverter.py      Inverter - per-layer-type backward dispatch
    fwd_hooks.py     Default forward hooks (cache in/out tensors per primitive layer type)
    utils.py
  yolo/           YOLO26-specific wiring
    explainer.py     YOLOLRP - the main entry point
    block_rules.py   Relevance rules for YOLO26's composite blocks (C3k2, C2PSA, SPPF, Attention, ...)
    fwd_hooks.py     Forward hooks for YOLO-specific blocks (SPPF, Concat)
  cli.py          yolo-lrp console script's real implementation
tests/            pytest suite (see below)

lrp/ has no dependency on ultralytics or YOLO at all; yolo/ is where every YOLO26-architecture assumption lives. A different YOLO version whose block types are a straightforward registry swap doesn't need a new YOLOLRP subclass — prop_registry/fwd_hook_registry are constructor parameters, defaulting to YOLO26's own (block_rules.PROP_REGISTRY/fwd_hooks.FWD_HOOK_REGISTRY).

Testing

pip install -r requirements-dev.txt
pytest

Everything except tests/test_integration.py runs offline against stubbed/synthetic modules. test_integration.py (marked integration) downloads a real checkpoint per supported YOLO version on first run (see "Supported YOLO versions" above) and exercises the full explain() pipeline against each — deselect it with pytest -m "not integration" if you want the fast, network-free subset. pytest reports coverage automatically (pyproject.toml's addopts).

mypy . runs in strict mode across yolo_lrp/; black, isort, and flake8 are also part of CI (.github/workflows/ci.yml), which also does a real pip install -e . as an installability smoke test. Run the lint/type checks together with:

black --check yolo_lrp tests
isort --check yolo_lrp tests
flake8 yolo_lrp
mypy .

Notes on the LRP rules

  • Attention (C2PSA, and the attn=True branch of C3k2) has a genuine relevance rule, not an identity placeholder — a from-scratch implementation in the spirit of AttnLRP (Achtibat et al., 2024): a bilinear split for the two matrix products in self-attention (Q·Kᵀ, attention·V), and a dedicated softmax rule derived from its own Jacobian, rather than treating attention as opaque. It's a principled derivation done for this project, not a verified reproduction of the paper's exact published rules.
  • BatchNorm is treated as transparent to relevance throughout (a standard LRP-for-CNNs simplification), not explicitly decomposed.
  • Contrastive (CRP) relevance intentionally isn't globally conserved the way plain LRP roughly is — the dual (primal + "everything else") path expands the relevance total as it flows backward, which is expected, not a bug.
  • Relevance seeding (YOLOLRP.explain/_initialize_relevance) follows this repo's own paper (cited below) with two exceptions: object-confidence weighting (eq. 8) isn't implemented, since modern YOLO heads (v8+) have no separate objectness score to weight by; and seed_strategy/seed_dilate/a confidence floor under max-class selection are extra levers the paper doesn't have, added to handle cases outside its original scope.

If you are planning to utilize this repo in your research kindly cite the following work:

@INPROCEEDINGS{9827744,
  author={Karasmanoglou, Apostolos and Antonakakis, Marios and Zervakis, Michalis},
  booktitle={2022 IEEE International Conference on Imaging Systems and Techniques (IST)}, 
  title={Heatmap-based Explanation of YOLOv5 Object Detection with Layer-wise Relevance Propagation}, 
  year={2022},
  volume={},
  number={},
  pages={1-6},
  doi={10.1109/IST55454.2022.9827744}
}

License

MIT

관련 프로젝트 살펴보기

불러오는 중…