Repository navigation
Add a ConceptLocalizer via Black-box Attributions - #186
Merged
Merged
Conversation
Agustin-Picard
marked this pull request as ready for review
September 3, 2026 09:20
AntoninPoche
requested changes
Sep 7, 2026
Collaborator
There was a problem hiding this comment.
There are few artifacts due to AI usage:
- Different names for the same thing
- Overcomplex code
- Lack of comments
- Many tests with weird structure, this will surely force future change to follow an arbitraly defined architecture. We called that technical debt the last time. (I did not review in detail the regression one).
Nonetheless, it seems to do the job, depends how much effort you want to put into it.
Member
Author
|
I've updated the notebook following your suggestions @AntoninPoche : https://drive.google.com/file/d/19eg2uU95A_LW1hQ1ENrhGI41lLK35UHu/view?usp=sharing |
Agustin-Picard
force-pushed
the
feat/concept-localizer
branch
from
September 23, 2026 14:37
8fc5ca3 to
c50aebd
Compare
Member
Author
|
Thanks again @AntoninPoche ! Since the last review, I pushed a broader cleanup/simplification pass addressing most of the points you raised:
I've also adjusted the PR's description to reflect the current API! |
AntoninPoche
approved these changes
Sep 24, 2026
AntoninPoche
left a comment
Collaborator
There was a problem hiding this comment.
LGTM!
Much clearer on the documentation and efficient in the code!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds input-to-concept localization to
HolisticCraft.The new API answers:
It does so by exposing the fitted latent extractor and factorizer as a model that returns one concept activation score per concept, then applying a standard Xplique black-box attribution method to the selected score.
This complements the existing visualization of latent concept activation maps; it does not replace it.
The implementation is currently validated with
RiseandSobolAttributionMethodand is designed to work with compatible black-box attribution methods.Motivation
HolisticCraftalready produces concept coefficientscoeffs_u.When the latent representation is spatial, these coefficients form concept activation maps:
which can be resized and overlaid on the input.
With the default spatial mean reducer, the concept score is
s_k(x) = mean_{i,j} U_k(x)_{i,j}. ResizingU_k(x)is therefore a CAM-stylemap for the concept score: it displays the spatial map underlying that score
directly, without gradients or class-prediction weights. This works well for CNN
architectures, but may not render well for very specific deep architectures,
large receptive fields, attention-based models, multi-scale representations,
or other cases where a latent position does not correspond
directly to a local input region.
This PR adds the complementary input-to-concept path. It computes a concept
score, then a black-box explainer perturbs input regions to attribute that score
to the input:
Terminology
The implementation and documentation use four distinct quantities:
U_k(x), stored incoeffs_u; CAM-style map for the spatial mean concept scorekactivated in latent space?s_k(x) = R(U_k(x))kactivated overall?For perturbation-based methods, the two attribution paths vary different quantities:
Concept importance also supports gradient-based methods; the first path
describes Sobol-based importance.
For CNN activations with shape
(N, H', W', C), factorization producescoeffs_uwith shape(N, H', W', K), so eachU_k(x)is spatial.Spatial ViT tokens can similarly form a concept activation map when mapped back to their patch grid.
For global representations with shape
(N, K), there is no spatial concept activation map; the coefficient for each concept is already a global activation score.Public API
make_concept_localizer()The returned callable maps a batch of input images to:
concept activation scores.
Built-in reducers are:
A custom callable can also be provided as long as it returns one scalar per input and concept.
The localizer preserves signed concept coefficients. Users who intentionally want magnitude-based scores can provide an appropriate custom reducer.
The concrete TensorFlow/PyTorch localizer implementations are private implementation details; the public entry point is
HolisticCraft.make_concept_localizer().attribute_concepts_to_inputs()This method:
For image inputs, the result has shape:
Channel
kalways corresponds to conceptk.If only a subset of concepts is requested, uncomputed channels are filled entirely with
NaN, so they cannot be confused with valid zero attribution.TensorFlow callers use native channel-last inputs. PyTorch callers pass native channel-first tensors to the high-level method; the framework wrapper handles the layout conversion required by the attribution stack.
Why localization is black-box only
Input-to-concept localization evaluates the fitted factorizer on perturbed inputs through:
This path is not guaranteed to be differentiable.
For this reason,
attribute_concepts_to_inputs()currently supports black-box attribution methods only.Gradient-based methods remain supported for the separate concept importance path, where concept activations are decoded toward the task prediction.
No task
operatoris required for concept localization because one-hot targets directly select a concept activation score.If a non-
Noneoperatoris supplied in thePartialExplainer, it is ignored with a warning.Factorizers used for localization must support out-of-sample
encode(). A non-inductive factorizer raises an explicit runtime error when localization is requested.Example
Sobol
RISE
RISE uses the same public localization API; only the
PartialExplainerconfiguration changes:Visualization integration
The existing display API now supports both interpretation modes.
Concept activation map visualization
Existing behavior remains available through
coeffs_u:For spatial coefficients and the default mean score, this is a CAM-style
visualization of the concept score in latent space. No input perturbations are
used to produce this map.
Concept localization maps
Input attribution maps can instead be provided with:
display_top_images_per_concept()also acceptsconcept_maps, with an important semantic distinction:coeffs_udetermines which images rank highest for a concept;concept_mapsdetermines the localization overlay.Concept localization magnitude therefore does not replace concept activation as the ranking signal.
If a localization map already matches the input resolution,
display_concept_heatmap()avoids an unnecessary resize.Signed localization maps are currently displayed by absolute magnitude. Neither concept activation maps nor concept localization maps should be interpreted as segmentation masks.
Backward compatibility
The feature is opt-in.
Existing usage of:
retains its previous behavior.
No existing call triggers black-box input attribution unless
attribute_concepts_to_inputs()is explicitly requested.Tests
The PR includes coverage for:
mean,sum,max, and custom callables;NaNchannels for uncomputed concepts;operatorbehavior;Cross-framework parity tests and framework-agnostic localization regressions are kept separate from the PyTorch-specific classification tests to keep the test responsibilities explicit.
Documentation
The HolisticCraft documentation now:
encode()requirement;coeffs_u,concept_maps, and top-image ranking.Updated notebook:
https://drive.google.com/file/d/19eg2uU95A_LW1hQ1ENrhGI41lLK35UHu/view?usp=sharing