-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lmeribal
committed
Sep 10, 2024
1 parent
184e19c
commit d9058a3
Showing
2 changed files
with
54 additions
and
5 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
turbo_alignment/modeling/multimodal/encoders/image/siglip.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
import torch | ||
from transformers import CLIPModel | ||
|
||
from turbo_alignment.modeling.multimodal.encoders.image.base import BaseImageEncoder | ||
from turbo_alignment.modeling.multimodal.encoders.registry import ( | ||
ModalityEncoderRegistry, | ||
) | ||
from turbo_alignment.settings.modality import ModalityEncoderType | ||
|
||
|
||
@ModalityEncoderRegistry.register(ModalityEncoderType.CLIP) | ||
class CLIPImageModeling(BaseImageEncoder): | ||
def __init__(self, encoder_path: Path, model_clip: Optional[CLIPModel] = None, is_pickle: bool = False): | ||
super().__init__() | ||
if model_clip is not None: | ||
self.model_clip = model_clip | ||
else: | ||
self.model_clip = CLIPModel.from_pretrained(encoder_path) | ||
self.is_pickle = is_pickle | ||
|
||
@staticmethod | ||
def _get_clip_hidden_states(model_clip: CLIPModel, inputs: torch.Tensor, is_pickle: bool = False) -> torch.Tensor: | ||
if is_pickle: | ||
return inputs | ||
# https://github.com/huggingface/transformers/blob/main/src/transformers/models/llava/modeling_llava.py#L213 | ||
# -2 is default value of vision_feature_layer in llava config | ||
# [1:] is everything after vit [cls] token | ||
return model_clip.vision_model(inputs.squeeze(1), output_hidden_states=True).hidden_states[-2][ | ||
:, 1: | ||
] # FIXME: squeeze dimension? | ||
|
||
def encode(self, inputs: torch.Tensor) -> torch.Tensor: | ||
return self._get_clip_hidden_states(self.model_clip, inputs, self.is_pickle) | ||
|
||
@property | ||
def emb_dim(self): | ||
return self.model_clip.config.vision_config.hidden_size | ||
|
||
@property | ||
def device(self): | ||
return self.model_clip.device | ||
|
||
@property | ||
def n_modality_embs(self) -> int: | ||
image_size = self.model_clip.config.vision_config.image_size | ||
dummy_pixel_values = torch.empty(1, 3, image_size, image_size) | ||
hidden_states = self._get_clip_hidden_states(self.model_clip, dummy_pixel_values, is_pickle=False) | ||
return hidden_states.shape[1] |
This file contains 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