AGENT SKILL

pytorch-grad-cam

Guides PyTorch grad-cam workflows for class activation maps, model/task adaptation, CAM metrics, method selection, and troubleshooting.

License 许可
MIT
最近更新
2026-09-03
Stars
★ 351
Forks
27

01 前言 Front Matter

name
pytorch-grad-cam
license
MIT
metadata
{ "disco-role": "operating" }
description
Guides PyTorch grad-cam workflows for class activation maps, model/task adaptation, CAM metrics, method selection, and troubleshooting.
disable-model-invocation
true

02 资源文件 6

03 SKILL.md 正文

PyTorch Grad-CAM Repo Skill

Use this repo skill for the grad-cam Python distribution, imported as pytorch_grad_cam, when a task involves class activation maps, pixel attribution, PyTorch vision explainability, CAM method selection, custom model targets, reshape transforms, ROAD/ARCC metrics, Deep Feature Factorization, or troubleshooting CAM outputs.

Package identity and installation

  • Install package: pip install grad-cam
  • Import package: import pytorch_grad_cam
  • Core dependency family: PyTorch, TorchVision, NumPy, OpenCV, ttach, matplotlib, SciPy, scikit-learn, Pillow, tqdm.
  • Optional workflow dependencies: timm for Swin examples, transformers for CLIP/HuggingFace examples, vendor packages for HPU.

Minimal public import check:

python - <<'PY'
from importlib.metadata import version
from pytorch_grad_cam import GradCAM, ScoreCAM, AblationCAM, FinerCAM
print("grad-cam", version("grad-cam"))
print(GradCAM, ScoreCAM, AblationCAM, FinerCAM)
PY

For a safe diagnostic that does not download models, run scripts/check_grad_cam_environment.py. For method names and installed signatures, run scripts/inspect_cam_methods.py.

Read references/package-overview.md for the verified package surface, dependencies, and workflow map. Read references/troubleshooting.md when install, import, optional dependency, device, or source-independence issues appear. Read references/repo-provenance.md before deciding whether this skill is stale for another checkout.

Route map

Core CAM generation

Read sub-skills/cam-generation/SKILL.md when the task asks to produce Grad-CAM/CAM heatmaps for a PyTorch classifier, choose target layers, target classes, smoothing options, guided backpropagation, batch behavior, or a safe single-image smoke test.

Common triggers: "run GradCAM on my model", "overlay a heatmap", "why is my CAM blank", "use aug_smooth/eigen_smooth", "combine CAM with guided backprop", or "test GradCAM without downloading ImageNet weights".

Model/task adaptation

Read sub-skills/model-task-adaptation/SKILL.md when activations or outputs are not ordinary CNN classification logits: ViT, Swin, CLIP, HuggingFace vision models, object detection, semantic segmentation, embeddings/similarity, custom target callables, reshape_transform, or FasterRCNNBoxScoreTarget / SemanticSegmentationTarget.

Metrics, evaluation, and factorization

Read sub-skills/metrics-and-evaluation/SKILL.md for ROAD metrics, confidence-change metrics, ARCC, RefineCAM, Deep Feature Factorization, or tasks that compare/tune explanations rather than only creating one heatmap.

Method and API selection

Read sub-skills/methods-and-api/SKILL.md for expert method tradeoffs, installed class signatures, lifecycle/context manager behavior, method-specific gotchas, device/backends, FinerCAM, SegEigenCAM, ShapleyCAM, KPCA-CAM, AblationCAM variants, or low-level API questions.

Operating rules

  1. Build workflows around a user's model, input tensor, target layer(s), and target scalar(s). CAM methods explain a scalar output with respect to a spatial activation tensor.
  2. Do not rely on original repository examples, notebooks, or images at runtime. Use the bundled references and scripts in this skill tree.
  3. Prefer safe synthetic checks before downloading pretrained weights or large datasets. The bundled smoke scripts use tiny in-memory models and tensors.
  4. Treat ScoreCAM and AblationCAM as potentially expensive because they use many forward passes; set cam.batch_size deliberately.
  5. Use a with CAMClass(...) as cam: block when possible so hooks are released.
  6. Keep backend claims precise: CPU verifies the core API; CUDA/MPS/HPU require matching framework/vendor runtimes for real device execution.
  7. If a task needs CLIP/Swin/HuggingFace or external pretrained models, surface the optional dependency and network/model-cache requirement before running.

04 同仓库更多 Skills

VectorSpaceLab
VectorSpaceLab/AREX-Skill

passnet-triton-opt

Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.

★ 351
VectorSpaceLab
VectorSpaceLab/AREX-Skill

passnet-solve

END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.

★ 351
VectorSpaceLab
VectorSpaceLab/AREX-Skill

passnet-skill

PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.

★ 351
VectorSpaceLab
VectorSpaceLab/AREX-Skill

passnet-pattern-fusion

Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.

★ 351
VectorSpaceLab
VectorSpaceLab/AREX-Skill

passnet-orchestrate

Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.

★ 351
VectorSpaceLab
VectorSpaceLab/AREX-Skill

passnet-feedback

Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).

★ 351