Instructions to use flavour/clipmean-dinov2-small-bge-small-en-v1-5 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use flavour/clipmean-dinov2-small-bge-small-en-v1-5 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="flavour/clipmean-dinov2-small-bge-small-en-v1-5", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("flavour/clipmean-dinov2-small-bge-small-en-v1-5", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Commit ·
2de3147
1
Parent(s): f5cf448
Upload model
Browse files- config.json +179 -0
- model.safetensors +3 -0
- modelling.py +180 -0
config.json
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_commit_hash": "0f211b9582116394dea1bb98fae7450dfa3d672b",
|
| 3 |
+
"_name_or_path": "flavour/vtde-dinov2-small-bge-small-en-v1.5",
|
| 4 |
+
"architectures": [
|
| 5 |
+
"VTDEModel"
|
| 6 |
+
],
|
| 7 |
+
"auto_map": {
|
| 8 |
+
"AutoConfig": "modelling.VTDEConfig",
|
| 9 |
+
"AutoModel": "modelling.VTDEModel"
|
| 10 |
+
},
|
| 11 |
+
"logit_scale_init_value": 2.6592,
|
| 12 |
+
"model_type": "vtde",
|
| 13 |
+
"projection_dim": 384,
|
| 14 |
+
"text_config": {
|
| 15 |
+
"_name_or_path": "BAAI/bge-small-en-v1.5",
|
| 16 |
+
"add_cross_attention": false,
|
| 17 |
+
"architectures": [
|
| 18 |
+
"BertModel"
|
| 19 |
+
],
|
| 20 |
+
"attention_probs_dropout_prob": 0.1,
|
| 21 |
+
"bad_words_ids": null,
|
| 22 |
+
"begin_suppress_tokens": null,
|
| 23 |
+
"bos_token_id": null,
|
| 24 |
+
"chunk_size_feed_forward": 0,
|
| 25 |
+
"classifier_dropout": null,
|
| 26 |
+
"cross_attention_hidden_size": null,
|
| 27 |
+
"decoder_start_token_id": null,
|
| 28 |
+
"diversity_penalty": 0.0,
|
| 29 |
+
"do_sample": false,
|
| 30 |
+
"early_stopping": false,
|
| 31 |
+
"encoder_no_repeat_ngram_size": 0,
|
| 32 |
+
"eos_token_id": null,
|
| 33 |
+
"exponential_decay_length_penalty": null,
|
| 34 |
+
"finetuning_task": null,
|
| 35 |
+
"forced_bos_token_id": null,
|
| 36 |
+
"forced_eos_token_id": null,
|
| 37 |
+
"hidden_act": "gelu",
|
| 38 |
+
"hidden_dropout_prob": 0.1,
|
| 39 |
+
"hidden_size": 384,
|
| 40 |
+
"id2label": {
|
| 41 |
+
"0": "LABEL_0"
|
| 42 |
+
},
|
| 43 |
+
"initializer_range": 0.02,
|
| 44 |
+
"intermediate_size": 1536,
|
| 45 |
+
"is_decoder": false,
|
| 46 |
+
"is_encoder_decoder": false,
|
| 47 |
+
"label2id": {
|
| 48 |
+
"LABEL_0": 0
|
| 49 |
+
},
|
| 50 |
+
"layer_norm_eps": 1e-12,
|
| 51 |
+
"length_penalty": 1.0,
|
| 52 |
+
"max_length": 20,
|
| 53 |
+
"max_position_embeddings": 512,
|
| 54 |
+
"min_length": 0,
|
| 55 |
+
"model_type": "bert",
|
| 56 |
+
"no_repeat_ngram_size": 0,
|
| 57 |
+
"num_attention_heads": 12,
|
| 58 |
+
"num_beam_groups": 1,
|
| 59 |
+
"num_beams": 1,
|
| 60 |
+
"num_hidden_layers": 12,
|
| 61 |
+
"num_return_sequences": 1,
|
| 62 |
+
"output_attentions": false,
|
| 63 |
+
"output_hidden_states": false,
|
| 64 |
+
"output_scores": false,
|
| 65 |
+
"pad_token_id": 0,
|
| 66 |
+
"position_embedding_type": "absolute",
|
| 67 |
+
"prefix": null,
|
| 68 |
+
"problem_type": null,
|
| 69 |
+
"pruned_heads": {},
|
| 70 |
+
"remove_invalid_values": false,
|
| 71 |
+
"repetition_penalty": 1.0,
|
| 72 |
+
"return_dict": true,
|
| 73 |
+
"return_dict_in_generate": false,
|
| 74 |
+
"sep_token_id": null,
|
| 75 |
+
"suppress_tokens": null,
|
| 76 |
+
"task_specific_params": null,
|
| 77 |
+
"temperature": 1.0,
|
| 78 |
+
"tf_legacy_loss": false,
|
| 79 |
+
"tie_encoder_decoder": false,
|
| 80 |
+
"tie_word_embeddings": true,
|
| 81 |
+
"tokenizer_class": null,
|
| 82 |
+
"top_k": 50,
|
| 83 |
+
"top_p": 1.0,
|
| 84 |
+
"torch_dtype": "float32",
|
| 85 |
+
"torchscript": false,
|
| 86 |
+
"transformers_version": "4.32.0.dev0",
|
| 87 |
+
"type_vocab_size": 2,
|
| 88 |
+
"typical_p": 1.0,
|
| 89 |
+
"use_bfloat16": false,
|
| 90 |
+
"use_cache": true,
|
| 91 |
+
"vocab_size": 30522
|
| 92 |
+
},
|
| 93 |
+
"text_pooling_mode": "mean",
|
| 94 |
+
"torch_dtype": "float32",
|
| 95 |
+
"transformers_version": null,
|
| 96 |
+
"vision_config": {
|
| 97 |
+
"_name_or_path": "facebook/dinov2-small",
|
| 98 |
+
"add_cross_attention": false,
|
| 99 |
+
"architectures": [
|
| 100 |
+
"Dinov2Model"
|
| 101 |
+
],
|
| 102 |
+
"attention_probs_dropout_prob": 0.0,
|
| 103 |
+
"bad_words_ids": null,
|
| 104 |
+
"begin_suppress_tokens": null,
|
| 105 |
+
"bos_token_id": null,
|
| 106 |
+
"chunk_size_feed_forward": 0,
|
| 107 |
+
"cross_attention_hidden_size": null,
|
| 108 |
+
"decoder_start_token_id": null,
|
| 109 |
+
"diversity_penalty": 0.0,
|
| 110 |
+
"do_sample": false,
|
| 111 |
+
"drop_path_rate": 0.0,
|
| 112 |
+
"early_stopping": false,
|
| 113 |
+
"encoder_no_repeat_ngram_size": 0,
|
| 114 |
+
"eos_token_id": null,
|
| 115 |
+
"exponential_decay_length_penalty": null,
|
| 116 |
+
"finetuning_task": null,
|
| 117 |
+
"forced_bos_token_id": null,
|
| 118 |
+
"forced_eos_token_id": null,
|
| 119 |
+
"hidden_act": "gelu",
|
| 120 |
+
"hidden_dropout_prob": 0.0,
|
| 121 |
+
"hidden_size": 384,
|
| 122 |
+
"id2label": {
|
| 123 |
+
"0": "LABEL_0",
|
| 124 |
+
"1": "LABEL_1"
|
| 125 |
+
},
|
| 126 |
+
"image_size": 518,
|
| 127 |
+
"initializer_range": 0.02,
|
| 128 |
+
"is_decoder": false,
|
| 129 |
+
"is_encoder_decoder": false,
|
| 130 |
+
"label2id": {
|
| 131 |
+
"LABEL_0": 0,
|
| 132 |
+
"LABEL_1": 1
|
| 133 |
+
},
|
| 134 |
+
"layer_norm_eps": 1e-06,
|
| 135 |
+
"layerscale_value": 1.0,
|
| 136 |
+
"length_penalty": 1.0,
|
| 137 |
+
"max_length": 20,
|
| 138 |
+
"min_length": 0,
|
| 139 |
+
"mlp_ratio": 4,
|
| 140 |
+
"model_type": "dinov2",
|
| 141 |
+
"no_repeat_ngram_size": 0,
|
| 142 |
+
"num_attention_heads": 6,
|
| 143 |
+
"num_beam_groups": 1,
|
| 144 |
+
"num_beams": 1,
|
| 145 |
+
"num_channels": 3,
|
| 146 |
+
"num_hidden_layers": 12,
|
| 147 |
+
"num_return_sequences": 1,
|
| 148 |
+
"output_attentions": false,
|
| 149 |
+
"output_hidden_states": false,
|
| 150 |
+
"output_scores": false,
|
| 151 |
+
"pad_token_id": null,
|
| 152 |
+
"patch_size": 14,
|
| 153 |
+
"prefix": null,
|
| 154 |
+
"problem_type": null,
|
| 155 |
+
"pruned_heads": {},
|
| 156 |
+
"qkv_bias": true,
|
| 157 |
+
"remove_invalid_values": false,
|
| 158 |
+
"repetition_penalty": 1.0,
|
| 159 |
+
"return_dict": true,
|
| 160 |
+
"return_dict_in_generate": false,
|
| 161 |
+
"sep_token_id": null,
|
| 162 |
+
"suppress_tokens": null,
|
| 163 |
+
"task_specific_params": null,
|
| 164 |
+
"temperature": 1.0,
|
| 165 |
+
"tf_legacy_loss": false,
|
| 166 |
+
"tie_encoder_decoder": false,
|
| 167 |
+
"tie_word_embeddings": true,
|
| 168 |
+
"tokenizer_class": null,
|
| 169 |
+
"top_k": 50,
|
| 170 |
+
"top_p": 1.0,
|
| 171 |
+
"torch_dtype": "float32",
|
| 172 |
+
"torchscript": false,
|
| 173 |
+
"transformers_version": "4.32.0.dev0",
|
| 174 |
+
"typical_p": 1.0,
|
| 175 |
+
"use_bfloat16": false,
|
| 176 |
+
"use_swiglu_ffn": false
|
| 177 |
+
},
|
| 178 |
+
"vision_pooling_mode": "mean"
|
| 179 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:28e0a3684bb2930013f5c5f142a38ce1936aab3ba4180774d6f240a0ccbd04e0
|
| 3 |
+
size 223489116
|
modelling.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../notebooks/12_modelling.ipynb.
|
| 2 |
+
|
| 3 |
+
# %% auto 0
|
| 4 |
+
__all__ = ['VTDEConfig', 'VTDEModel']
|
| 5 |
+
|
| 6 |
+
# %% ../notebooks/12_modelling.ipynb 1
|
| 7 |
+
from transformers.models.clip.modeling_clip import CLIPOutput, clip_loss
|
| 8 |
+
from typing import Optional, Tuple, Union
|
| 9 |
+
from transformers import PreTrainedModel, VisionTextDualEncoderModel
|
| 10 |
+
import torch
|
| 11 |
+
from transformers import VisionTextDualEncoderConfig
|
| 12 |
+
|
| 13 |
+
class VTDEConfig(VisionTextDualEncoderConfig):
|
| 14 |
+
model_type = "vtde"
|
| 15 |
+
|
| 16 |
+
def __init__(self, projection_dim=512, logit_scale_init_value=2.6592,
|
| 17 |
+
text_pooling_mode='mean',
|
| 18 |
+
vision_pooling_mode='max',
|
| 19 |
+
**kwargs):
|
| 20 |
+
"""
|
| 21 |
+
pooling_mode in ['mean', 'max', 'cls']
|
| 22 |
+
https://arxiv.org/pdf/2210.09996.pdf
|
| 23 |
+
https://github.com/kahnchana/clippy/blob/3c102c29c32f7c66c6e52e09b795fe9c061bbb03/src/open_clip/hf_model.py#L56
|
| 24 |
+
also
|
| 25 |
+
https://arxiv.org/pdf/2301.07836.pdf
|
| 26 |
+
"""
|
| 27 |
+
self.text_pooling_mode = text_pooling_mode
|
| 28 |
+
self.vision_pooling_mode = vision_pooling_mode
|
| 29 |
+
super().__init__(projection_dim, logit_scale_init_value, **kwargs)
|
| 30 |
+
|
| 31 |
+
VTDEConfig.register_for_auto_class()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class VTDEModel(VisionTextDualEncoderModel):
|
| 35 |
+
config_class = VTDEConfig
|
| 36 |
+
base_model_prefix = "vtde"
|
| 37 |
+
|
| 38 |
+
def __init__(
|
| 39 |
+
self,
|
| 40 |
+
config: Optional[VTDEConfig] = None,
|
| 41 |
+
vision_model: Optional[PreTrainedModel] = None,
|
| 42 |
+
text_model: Optional[PreTrainedModel] = None,
|
| 43 |
+
):
|
| 44 |
+
# You can customize the constructor if needed
|
| 45 |
+
super().__init__(config, vision_model, text_model)
|
| 46 |
+
self.text_pooling_mode = config.text_pooling_mode
|
| 47 |
+
self.vision_pooling_mode = config.vision_pooling_mode
|
| 48 |
+
|
| 49 |
+
def get_text_features(
|
| 50 |
+
self,
|
| 51 |
+
input_ids=None,
|
| 52 |
+
attention_mask=None,
|
| 53 |
+
position_ids=None,
|
| 54 |
+
token_type_ids=None,
|
| 55 |
+
output_attentions=None,
|
| 56 |
+
output_hidden_states=None,
|
| 57 |
+
return_dict=None,
|
| 58 |
+
):
|
| 59 |
+
text_outputs = self.text_model(
|
| 60 |
+
input_ids=input_ids,
|
| 61 |
+
attention_mask=attention_mask,
|
| 62 |
+
token_type_ids=token_type_ids,
|
| 63 |
+
position_ids=position_ids,
|
| 64 |
+
output_attentions=output_attentions,
|
| 65 |
+
output_hidden_states=output_hidden_states,
|
| 66 |
+
return_dict=return_dict,
|
| 67 |
+
)
|
| 68 |
+
if self.text_pooling_mode == 'cls':
|
| 69 |
+
pooled_output = text_outputs[1]
|
| 70 |
+
elif self.text_pooling_mode == 'mean':
|
| 71 |
+
pooled_output = torch.mean(text_outputs[0], dim=1)
|
| 72 |
+
elif self.text_pooling_mode == 'max':
|
| 73 |
+
pooled_output = torch.max(text_outputs[0], dim=1)[0]
|
| 74 |
+
elif self.text_pooling_mode == 'norm':
|
| 75 |
+
"""we select the patch with the largest norm"""
|
| 76 |
+
last_hidden_states = text_outputs[0]
|
| 77 |
+
patch_norms = torch.norm(last_hidden_states[:, 1:, :], dim=-1)
|
| 78 |
+
max_norm_idx = torch.argmax(patch_norms, dim=1)
|
| 79 |
+
pooled_output = last_hidden_states[:, max_norm_idx, :][:, 0, :]
|
| 80 |
+
else:
|
| 81 |
+
"We want to raise the name of the pooling mode"
|
| 82 |
+
raise NotImplementedError
|
| 83 |
+
|
| 84 |
+
text_features = self.text_projection(pooled_output)
|
| 85 |
+
|
| 86 |
+
return text_features
|
| 87 |
+
|
| 88 |
+
def get_image_features(
|
| 89 |
+
self,
|
| 90 |
+
pixel_values=None,
|
| 91 |
+
output_attentions=None,
|
| 92 |
+
output_hidden_states=None,
|
| 93 |
+
return_dict=None,
|
| 94 |
+
):
|
| 95 |
+
vision_outputs = self.vision_model(
|
| 96 |
+
pixel_values=pixel_values,
|
| 97 |
+
output_attentions=output_attentions,
|
| 98 |
+
output_hidden_states=output_hidden_states,
|
| 99 |
+
return_dict=return_dict,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
if self.vision_pooling_mode == 'cls':
|
| 103 |
+
pooled_output = vision_outputs[1]
|
| 104 |
+
elif self.vision_pooling_mode == 'mean':
|
| 105 |
+
pooled_output = torch.mean(vision_outputs[0], dim=1)
|
| 106 |
+
elif self.vision_pooling_mode == 'max':
|
| 107 |
+
pooled_output = torch.max(vision_outputs[0], dim=1)[0]
|
| 108 |
+
elif self.vision_pooling_mode == 'norm':
|
| 109 |
+
"""we select the patch with the largest norm"""
|
| 110 |
+
last_hidden_states = vision_outputs[0]
|
| 111 |
+
patch_norms = torch.norm(last_hidden_states[:, 1:, :], dim=-1)
|
| 112 |
+
max_norm_idx = torch.argmax(patch_norms, dim=1)
|
| 113 |
+
pooled_output = last_hidden_states[:, max_norm_idx, :][:, 0, :]
|
| 114 |
+
else:
|
| 115 |
+
raise NotImplementedError
|
| 116 |
+
|
| 117 |
+
image_features = self.visual_projection(pooled_output)
|
| 118 |
+
|
| 119 |
+
return image_features
|
| 120 |
+
|
| 121 |
+
def forward(
|
| 122 |
+
self,
|
| 123 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 124 |
+
pixel_values: Optional[torch.FloatTensor] = None,
|
| 125 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 126 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 127 |
+
return_loss: Optional[bool] = None,
|
| 128 |
+
token_type_ids: Optional[torch.LongTensor] = None,
|
| 129 |
+
output_attentions: Optional[bool] = None,
|
| 130 |
+
output_hidden_states: Optional[bool] = None,
|
| 131 |
+
return_dict: Optional[bool] = None,
|
| 132 |
+
) -> Union[Tuple[torch.Tensor], CLIPOutput]:
|
| 133 |
+
|
| 134 |
+
return_dict = return_dict if return_dict is not None else self.config.return_dict
|
| 135 |
+
|
| 136 |
+
image_embeds = self.get_image_features(
|
| 137 |
+
pixel_values=pixel_values,
|
| 138 |
+
output_attentions=output_attentions,
|
| 139 |
+
output_hidden_states=output_hidden_states,
|
| 140 |
+
return_dict=return_dict,
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
text_embeds = self.get_text_features(
|
| 144 |
+
input_ids=input_ids,
|
| 145 |
+
attention_mask=attention_mask,
|
| 146 |
+
position_ids=position_ids,
|
| 147 |
+
output_attentions=output_attentions,
|
| 148 |
+
output_hidden_states=output_hidden_states,
|
| 149 |
+
return_dict=return_dict,
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
# normalized features
|
| 153 |
+
image_embeds = image_embeds / image_embeds.norm(dim=-1, keepdim=True)
|
| 154 |
+
text_embeds = text_embeds / text_embeds.norm(dim=-1, keepdim=True)
|
| 155 |
+
|
| 156 |
+
# cosine similarity as logits
|
| 157 |
+
logit_scale = self.logit_scale.exp()
|
| 158 |
+
logits_per_text = torch.matmul(text_embeds, image_embeds.t()) * logit_scale
|
| 159 |
+
logits_per_image = logits_per_text.T
|
| 160 |
+
|
| 161 |
+
loss = None
|
| 162 |
+
if return_loss:
|
| 163 |
+
loss = clip_loss(logits_per_text)
|
| 164 |
+
|
| 165 |
+
if not return_dict:
|
| 166 |
+
output = (logits_per_image, logits_per_text, text_embeds, image_embeds, text_embeds, image_embeds)
|
| 167 |
+
return ((loss,) + output) if loss is not None else output
|
| 168 |
+
|
| 169 |
+
return CLIPOutput(
|
| 170 |
+
loss=loss,
|
| 171 |
+
logits_per_image=logits_per_image,
|
| 172 |
+
logits_per_text=logits_per_text,
|
| 173 |
+
text_embeds=text_embeds,
|
| 174 |
+
image_embeds=image_embeds,
|
| 175 |
+
text_model_output=text_embeds,
|
| 176 |
+
vision_model_output=image_embeds,
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
VTDEModel.register_for_auto_class("AutoModel")
|