shapiq_games.ImageClassifier¶
- class shapiq_games.ImageClassifier(image, model='vit_9_patches', *, n_superpixels=14, regions=None, fill=None, mask_strategy=None, class_index=None, batch_size=16, device='cpu', revision=None, normalize=True, verbose=False)[source]¶
Bases:
RegionGameThe image classification game: the probability of a class when only some regions are visible.
The players are regions of the image, given by
regionsfor every model:for the vision transformers [Dosovitskiy et al., 2021] (
"vit_9_patches","vit_16_patches","vit_36_patches","vit_144_patches"), square groups of the model’s 12 x 12 patches;for DINOv2 [Oquab et al., 2024] (
"dinov2_16_patches","dinov2_20_patches","dinov2_25_patches": a 4 x 4, 5 x 4, or 5 x 5 grid), rectangular groups of the model’s 16 x 16 patch tokens;for ResNet-18 [He et al., 2016] (
"resnet_18") or a custom classifier, SLIC superpixels [Achanta et al., 2012] or your ownregions.
How absent players are removed:
DINOv2 drops their patch tokens from the sequence (the present tokens keep their position embeddings), and only so: its checkpoint has no trained mask token.
The vision transformers remove them in token space by default: their patch tokens are masked (
mask_strategy="mask", the default: the content is replaced by a zero mask token, the position embedding is kept) or dropped ("remove"). With afillthey remove them in image space instead.In image space (
fill; always for ResNet-18 and custom classifiers): their pixels are replaced by the image’s mean color (the default), gray, black, a blurred copy, or any image of the same shape, and the model sees the filled image.
ResNet-18 and DINOv2 see a
224 x 224center crop, so the game explains that crop:imageis what the model sees and every region is visible to it. The explained class defaults to the class predicted on the full image.To look at the game,
masked_image()shows what the model sees for a coalition,attribution_map()spreads first-order values over the pixels for a heatmap, andplayer_images()returns one image per player, e.g. forshapiq.plot.si_graph_plot().- Variables:
image – The explained RGB image (for ResNet-18 and DINOv2, its
224 x 224crop).regions – The player of every pixel, of shape
(height, width), numbered from0.mask_strategy – How a transformer removes players in token space (
"mask"or"remove"; always"remove"for DINOv2), orNonein image space.class_index – The explained class.
class_name – The name of the explained class, if the model provides class names.
empty_value – The probability of the class with every region removed, before centering.
original_model_output – The probability of the class on the full image.
Examples
>>> image = np.random.default_rng(0).integers(0, 255, (64, 64, 3), dtype=np.uint8) >>> def classifier(images): # (batch, height, width, 3) -> (batch, n_classes) ... brightness = images.mean(axis=(1, 2, 3)) / 255 ... return np.stack([brightness, 1 - brightness], axis=1) >>> game = ImageClassifier(image, model=classifier, n_superpixels=8) >>> game.n_players 8 >>> from shapiq_games.vision import grid_regions >>> grid = ImageClassifier(image, classifier, regions=grid_regions(64, 64, 2, 2), fill="blur") >>> grid.n_players, grid.masked_image([1, 0, 0, 1]).shape (4, (64, 64, 3))
The vision transformers mask or drop patch tokens, or remove pixels with a
fill; DINOv2 drops tokens:>>> game = ImageClassifier(image, "vit_16_patches") >>> game = ImageClassifier(image, "vit_16_patches", mask_strategy="remove") >>> game = ImageClassifier(image, "vit_16_patches", fill="blur") >>> game = ImageClassifier(image, "dinov2_20_patches")
Initialize the image classification game.
- Parameters:
image (
ndarray|str|Path) – An RGB image array of shape(height, width, 3)or the path of an image. Float images with values in[0, 1](e.g. frommatplotlib.pyplot.imread) are scaled to0, ..., 255.model (
ImageModel|Callable[[ndarray],ndarray]) – A builtin model name or a classifier mapping a batch of images of shape(batch, height, width, 3)to class probabilities of shape(batch, n_classes). Defaults to"vit_9_patches".n_superpixels (
int) – The number of SLIC superpixels for"resnet_18"and custom classifiers when noregionsare given. Defaults to14.regions (
ndarray|None) – Your own players for"resnet_18"and custom classifiers: the player of every pixel, of shape(height, width), numbered0, ..., n - 1(seegrid_regions()). For ResNet-18 they refer to its224 x 224crop.fill (
Fill|ndarray|None) – Remove players in image space, replacing their pixels with"mean"(the image’s mean color),"gray","black","blur", or an image of the same shape. ResNet-18 and custom classifiers always do ("mean"by default); the vision transformers only when a fill is given; DINOv2 never.mask_strategy (
MaskStrategy|None) – How a vision transformer removes players in token space:"mask"(None, the default) or"remove"(see above). DINOv2 only drops tokens (Noneor"remove").class_index (
int|None) – The explained class, orNonefor the class predicted on the image.batch_size (
int) – The number of masked images per forward pass. The builtin models pad smaller batches to this size, so that a value does not depend on the batch. Defaults to16.device (
str) – The torch device of the builtin models. Defaults to"cpu".revision (
str|None) – The Hugging Face revision (branch, tag, or commit) of the vision transformer or DINOv2.Noneloads the default branch. ResNet-18 uses pinned torchvision weights and takes no revision.normalize (
bool) – Whether to center the game such that the value of the empty coalition is zero. Defaults toTrue.verbose (
bool) – Whether to show a progress bar when evaluating the game.
- Raises:
ValueError – If the model is unknown; a revision is given for a model that is not a Hugging Face model (vision transformer or DINOv2);
regionsare given for a transformer (its players are its patch grid);mask_strategyis given for a model without tokens, or together with afill; DINOv2 is asked to mask or to fill; the regions, the fill or the mask strategy are invalid; orclass_indexis out of range.
- attribution_map(values)¶
Spread one value per player over the player’s pixels, e.g. for a heatmap.
- Parameters:
values (
InteractionValues|ndarray) – Interaction values (their first-order values are used) or one value per player.- Return type:
- Returns:
The value of every pixel, of shape
(height, width).
- masked_image(coalition)¶
Return the image with the players outside
coalitionremoved.In image space this is the image the model sees (up to rounding, for models that fill normalized pixels). In token space the model removes the players inside the network, so their regions are shown in gray.
- player_images(*, fade=0.75)¶
Return one image per player: its bounding box, with other pixels faded to white.
The images can be passed to
shapiq.plot.si_graph_plot()asfeature_image_patches.
- value_function(coalitions)¶
Return the model output with only each coalition’s regions present.
- Return type:
GenericAlias[floating]
- game_id¶
A unique identifier for the game, based on its class name and hash.
- game_values¶
The mapping for coalitions to game values without normalization applied.
- image¶
- n_players¶
The number of players in the game.
- normalization_value¶
The value which is used to normalize (center) the game values such that the value for the empty coalition is zero. If this is zero, the game values are not normalized.
- player_name_lookup¶
- regions¶