peal.sparse_dictionaries.splice_decomposition.SpLICEDecomposition¶
- class peal.sparse_dictionaries.splice_decomposition.SpLICEDecomposition[source]¶
Bases:
SparseDictionarySparse dictionary using SpLICE for concept decomposition.
Wraps the SpLICE library to decompose CLIP image embeddings into sparse nonnegative weights over a vocabulary of text concepts. This serves as the disentangled dictionary for the DiDAE framework when using pretrained foundation models.
No training is required — SpLICE uses a pretrained CLIP vocabulary dictionary and an L1-regularized solver at inference time. The only data-dependent step is computing the dataset-specific image mean via fit_from_dataloaders().
Methods
Sparse concept weights of CLIP image embeddings.
Set the dataset-specific image mean from precomputed embeddings.
Compute dataset-specific image mean using the CLIP encoder.
The concept dictionary as a CPU tensor of shape
[D, n_components].Get the text vocabulary used by SpLICE.
Load saved SpLICE decomposition state.
Reconstruct image embeddings from concept weights.
Save the SpLICE decomposition state (image mean + config).
Inject an external CLIP model to ensure identity with the generator.
Attributes
config- __init__(config=pydantic.BaseModel)[source]¶
Store the config; the SpLICE model itself is loaded lazily.
- Parameters:
config (
SpLICEDecompositionConfig) – Backbone, vocabulary and solver settings.
- set_clip_model(clip_model)[source]¶
Inject an external CLIP model to ensure identity with the generator.
- Parameters:
clip_model – The loaded CLIP model instance (must be ViT-L/14)
- fit(X, y=None)[source]¶
Set the dataset-specific image mean from precomputed embeddings.
- Parameters:
X (
torch.Tensor) – CLIP image embeddings[N, D]; their mean replaces SpLICE’s internet-scaleimage_mean. Ignored whenNoneor empty.y (
torch.Tensor, optional) – Unused; accepted for interface compatibility.
- fit_from_dataloaders(dataloaders, feature_extractor)[source]¶
Compute dataset-specific image mean using the CLIP encoder. This replaces SpLICE’s generic internet-scale mean with a domain-specific mean, improving decomposition quality for the target dataset.
- Parameters:
dataloaders – List of dataloaders providing (image, label) batches
feature_extractor – The CLIP image encoder (nn.Module)
- decompose(embeddings)[source]¶
Sparse concept weights of CLIP image embeddings.
The embeddings are L2-normalised, centred by the image mean, re-normalised and passed to
splice_model.decompose.- Parameters:
embeddings (
torch.Tensor) – CLIP image embeddings[N, D]on any device.- Returns:
Non-negative weights
[N, n_components]onself.device.- Return type:
- recompose(weights)[source]¶
Reconstruct image embeddings from concept weights.
- Parameters:
weights (
torch.Tensor) – Concept weights[N, n_components]as returned bydecompose().- Returns:
Reconstructed (mean-added, normalised) embeddings
[N, D].- Return type: