π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
feat: add initial skeleton for paligemma mode (#3477)
Mandeep Singha38cf3d02c5a
diff --git a/kornia/models/paligemma/__init__.py b/kornia/models/paligemma/__init__.pynew file mode 100644index 000000000..18b4700d3--- /dev/null+++ b/kornia/models/paligemma/__init__.py@@ -0,0 +1,21 @@+# LICENSE HEADER MANAGED BY add-license-header+#+# Copyright 2018 Kornia Team+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#++from .configuration_paligemma import PaliGemmaConfig+from .modeling_paligemma import PaliGemma++__all__ = ["PaliGemma", "PaliGemmaConfig"]diff --git a/kornia/models/paligemma/configuration_paligemma.py b/kornia/models/paligemma/configuration_paligemma.pynew file mode 100644index 000000000..9580209f2--- /dev/null+++ b/kornia/models/paligemma/configuration_paligemma.py@@ -0,0 +1,58 @@+# LICENSE HEADER MANAGED BY add-license-header+#+# Copyright 2018 Kornia Team+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#++from __future__ import annotations++from dataclasses import dataclass+from typing import Optional++from kornia.models.siglip2.config import SigLip2VisionConfig+++@dataclass+class PaliGemmaConfig:+ """Configuration class for PaliGemma.++ Args:+ vision_config: Configuration for the SigLip2 vision encoder.+ vocab_size: Size of the vocabulary.+ hidden_size: Dimension of the embeddings.+ intermediate_size: Dimension of the intermediate (Feed Forward) layer.+ num_hidden_layers: Number of Transformer layers in the text model.+ num_attention_heads: Number of attention heads per transformer layer.+ num_key_value_heads: Number of key/value heads (for multi-query attention).+ head_dim: Dimension of each attention head.+ max_position_embeddings: Maximum number of position embeddings.+ ignore_index: Index to ignore in the loss function.+ image_token_index: Token index used for image placeholders.+ """++ vision_config: Optional[SigLip2VisionConfig] = None+ vocab_size: int = 257152+ hidden_size: int = 2048+ intermediate_size: int = 16384+ num_hidden_layers: int = 18+ num_attention_heads: int = 8+ num_key_value_heads: int = 1+ head_dim: int = 256+ max_position_embeddings: int = 8192+ ignore_index: int = -100+ image_token_index: int = 256000++ def __post_init__(self):+ if self.vision_config is None:+ self.vision_config = SigLip2VisionConfig()diff --git a/kornia/models/paligemma/modeling_paligemma.py b/kornia/models/paligemma/modeling_paligemma.pynew file mode 100644index 000000000..9ae69c36d--- /dev/null+++ b/kornia/models/paligemma/modeling_paligemma.py@@ -0,0 +1,68 @@+# LICENSE HEADER MANAGED BY add-license-header+#+# Copyright 2018 Kornia Team+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#++from __future__ import annotations++from typing import Optional++import torch+from torch import nn++from kornia.models.siglip2.vision_encoder import SigLip2VisionModel++from .configuration_paligemma import PaliGemmaConfig+++class PaliGemma(nn.Module):+ """PaliGemma Model for Vision-Language tasks.++ This model combines a SigLip2 Vision Encoder with a Gemma Language Decoder.+ """++ def __init__(self, config: PaliGemmaConfig) -> None:+ super().__init__()+ self.config = config++ self.vision_tower = SigLip2VisionModel(config.vision_config)++ self.multi_modal_projector = nn.Linear(config.vision_config.hidden_size, config.hidden_size)++ def forward(+ self,+ input_ids: torch.Tensor,+ pixel_values: torch.Tensor,+ attention_mask: Optional[torch.Tensor] = None,+ ) -> torch.Tensor:+ """Forward pass of the model.++ Args:+ input_ids: Text tokens (batch, seq_len)+ pixel_values: Images (batch, channels, height, width)+ attention_mask: Optional attention mask.++ Returns:+ logits: Prediction scores (batch, seq_len, vocab_size)+ """+ vision_outputs = self.vision_tower(pixel_values)+ image_features = vision_outputs[0]++ image_features = self.multi_modal_projector(image_features)++ batch_size, seq_len = input_ids.shape+ vocab_size = self.config.vocab_size++ return torch.randn(batch_size, seq_len, vocab_size, device=pixel_values.device)