embed.py
import goldener.embed
Extraction of embeddings from models.
The embedding tools are used by the goldener.describe.GoldDescriptor to compute the embeddings of the samples:
GoldEmbeddingToolis the abstract interface to implement to plug any model.GoldTorchEmbeddingToolextracts the outputs of some layers of a PyTorch model through forward hooks.GoldMultiModalTorchEmbeddingToolcombines oneGoldTorchEmbeddingToolper modality (multimodal data) and fuses their embeddings.GoldEmbeddingFusionToolandEmbeddingFusionStrategydefine how the outputs of multiple layers are fused (concatenation, addition, average, max) into a single embedding.
Abstract base class for embedding extraction from models.
This class defines the interface for embedding tools that can embed and optionally fuse embeddings from models. Implementations should provide specific mechanisms for extracting embeddings from different types of models (e.g., PyTorch, multimodal).
Strategies to fuse embeddings from multiple layers.
CONCAT: Concatenate embeddings along the channel dimension. ADD: Element-wise addition of embeddings. AVERAGE: Element-wise average of embeddings. MAX: Element-wise maximum of embeddings.
Configuration for the GoldTorchEmbeddingTool.
Attributes:
- model: The PyTorch model from which to extract embeddings.
- layers: List of layer names or a dictionary mapping group names to lists of layer names. If None, the last layer of the model is used.
- channel_pos: Localization of the channel dimension in the embedding tensors (default is 1). It is used for concatenation and to determine the spatial dimensions for interpolation.
- layer_fusion: Strategy to fuse embeddings from multiple layers within the same group.
- group_fusion: Strategy to fuse embeddings from different groups.
Embedding fusion from multiple layers and groups.
Attributes:
- layer_fusion: Strategy to fuse embeddings from multiple layers within the same group.
- group_fusion: Strategy to fuse embeddings from different groups.
Initialize the GoldEmbeddingFusionTool.
Arguments:
- layer_fusion: Strategy to fuse embeddings from multiple layers within the same group. Defaults to CONCAT.
- group_fusion: Strategy to fuse embeddings from different groups. Defaults to CONCAT.
Fuse a list of tensors.
The tensors are at least expected to have the shape (B, C), until (B, C, D, H, W) if sizes after the channel dimension differ. In this case, all tensors are interpolated to the largest size.
Arguments:
- tensors: List of tensors to be fused.
- strategy: Strategy to fuse the tensors.
- channel_pos: Localization of the channel dimension in the embedding tensors (default is 1). It is used for concatenation and to determine the spatial dimensions for interpolation.
Returns: Fused tensors.
Raises:
- ValueError: If the tensors have a different number of dimensions.
Fuse embeddings from multiple layers and groups.
Arguments:
- x: Dictionary mapping layer names to embedding tensors.
- layers: List of layer names or a dictionary mapping group names to lists of layer names.
- channel_pos: Localization of the channel dimension in the embedding tensors (default is 1). It is used for concatenation and to determine the spatial dimensions for interpolation.
Returns: Fused embedding tensor.
Embedding tool for PyTorch models.
Once initialized, the tool registers forward hooks on the specified layers of the model. When the model processes input data, the hooks capture the outputs of these layers. The extracted embeddings can then be fused according to the specified strategies.
The model and layers cannot be changed after initialization. The embedding fusion can be changed.
Attributes:
- _model: The PyTorch model from which to extract embeddings.
- fusion: GoldEmbeddingFusionTool instance to handle embedding fusion.
- _channel_pos: Localization of the channel dimension in the embedding tensors (default is 1). It is used for concatenation and to determine the spatial dimensions for interpolation.
- _layers: List of layer names or a dictionary mapping group names to lists of layer names.
- _hooks: Dictionary mapping layer names to their corresponding forward hook handles.
- _embeddings: Dictionary to store extracted embeddings.
Initialize the GoldTorchEmbeddingTool.
Arguments:
- config: Configuration object containing the model, layers, and fusion strategies.
The layers from which to extract embeddings.
It also indicates the grouping of layers for fusion.
Embedding tool for multimodal data using PyTorch.
Each modality has its own GoldTorchEmbeddingTool defined by its own configuration. This allows for processing different types of input data (e.g., images, text, audio) with different models and then fusing their embeddings.
Attributes:
- embedders: Dictionary mapping modality names to their GoldTorchEmbeddingTool instances.
- strategy: Strategy for fusing embeddings from different modalities.
Initialize the multimodal embedding tool.
Arguments:
- configs: Dictionary mapping modality names to their GoldTorchEmbeddingToolConfig.
- strategy: Strategy to use for fusing embeddings from different modalities. Defaults to CONCAT.