visheratin
commited on
Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- config.json +8 -0
- mexma_siglip.py +86 -0
- model.safetensors +3 -0
- preprocessor_config.json +24 -0
- special_tokens_map.json +15 -0
- tokenizer.json +3 -0
- tokenizer_config.json +54 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
config.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"MexmaSigLIP"
|
4 |
+
],
|
5 |
+
"optimized": false,
|
6 |
+
"torch_dtype": "float32",
|
7 |
+
"transformers_version": "4.46.3"
|
8 |
+
}
|
mexma_siglip.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from transformers import (
|
5 |
+
PretrainedConfig,
|
6 |
+
PreTrainedModel,
|
7 |
+
SiglipVisionConfig,
|
8 |
+
SiglipVisionModel,
|
9 |
+
XLMRobertaConfig,
|
10 |
+
XLMRobertaModel,
|
11 |
+
)
|
12 |
+
|
13 |
+
|
14 |
+
class MexmaSigLIPConfig(PretrainedConfig):
|
15 |
+
def __init__(
|
16 |
+
self,
|
17 |
+
optimized: bool = False,
|
18 |
+
**kwargs,
|
19 |
+
):
|
20 |
+
super().__init__(**kwargs)
|
21 |
+
self.optimized = optimized
|
22 |
+
|
23 |
+
|
24 |
+
class MexmaSigLIP(PreTrainedModel):
|
25 |
+
config_class = MexmaSigLIPConfig
|
26 |
+
|
27 |
+
def __init__(self, config: MexmaSigLIPConfig):
|
28 |
+
super().__init__(config)
|
29 |
+
self.config = config
|
30 |
+
text_config = XLMRobertaConfig.from_pretrained("facebook/MEXMA")
|
31 |
+
if self.config.optimized:
|
32 |
+
text_config._attn_implementation = "sdpa"
|
33 |
+
self.text_model = XLMRobertaModel(text_config, add_pooling_layer=False)
|
34 |
+
self.text_projector = torch.nn.Linear(1024, 1152, bias=False)
|
35 |
+
vision_congig = SiglipVisionConfig.from_pretrained(
|
36 |
+
"google/siglip-so400m-patch14-384"
|
37 |
+
)
|
38 |
+
if self.config.optimized:
|
39 |
+
vision_congig._attn_implementation = "flash_attention_2"
|
40 |
+
self.vision_model = SiglipVisionModel(vision_congig).vision_model
|
41 |
+
self.logit_scale = torch.nn.Parameter(torch.ones([]) * np.log(1 / 0.07))
|
42 |
+
self.logit_bias = torch.nn.Parameter(torch.ones([]) * -10)
|
43 |
+
|
44 |
+
def forward(self, image_inputs, input_ids, attention_mask, normalize=False):
|
45 |
+
text_features = self.encode_texts(input_ids, attention_mask, normalize)
|
46 |
+
image_features = self.encode_images(image_inputs, normalize)
|
47 |
+
return {
|
48 |
+
"image_features": image_features,
|
49 |
+
"text_features": text_features,
|
50 |
+
"logit_scale": self.logit_scale,
|
51 |
+
"logit_bias": self.logit_bias,
|
52 |
+
}
|
53 |
+
|
54 |
+
def encode_images(
|
55 |
+
self,
|
56 |
+
pixel_values,
|
57 |
+
normalize=False,
|
58 |
+
):
|
59 |
+
features = self.vision_model(pixel_values).pooler_output
|
60 |
+
return F.normalize(features, dim=-1) if normalize else features
|
61 |
+
|
62 |
+
def encode_texts(
|
63 |
+
self,
|
64 |
+
input_ids,
|
65 |
+
attention_mask,
|
66 |
+
normalize=False,
|
67 |
+
):
|
68 |
+
features = self.text_model(
|
69 |
+
input_ids=input_ids, attention_mask=attention_mask
|
70 |
+
).last_hidden_state[:, 0]
|
71 |
+
features = self.text_projector(features)
|
72 |
+
return F.normalize(features, dim=-1) if normalize else features
|
73 |
+
|
74 |
+
def get_logits(
|
75 |
+
self,
|
76 |
+
input_ids,
|
77 |
+
attention_mask,
|
78 |
+
pixel_values,
|
79 |
+
):
|
80 |
+
image_features = self.encode_images(pixel_values, normalize=True)
|
81 |
+
text_features = self.encode_texts(input_ids, attention_mask, normalize=True)
|
82 |
+
image_logits = (
|
83 |
+
self.logit_scale.exp() * image_features @ text_features.T + self.logit_bias
|
84 |
+
)
|
85 |
+
text_logits = image_logits.T
|
86 |
+
return image_logits, text_logits
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:419bd95e6ccccc504a98cdf5c8887d4feb4afe1dcdc785a3ebfab44bac7c644e
|
3 |
+
size 3953089552
|
preprocessor_config.json
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"do_convert_rgb": null,
|
3 |
+
"do_normalize": true,
|
4 |
+
"do_rescale": true,
|
5 |
+
"do_resize": true,
|
6 |
+
"image_mean": [
|
7 |
+
0.5,
|
8 |
+
0.5,
|
9 |
+
0.5
|
10 |
+
],
|
11 |
+
"image_processor_type": "SiglipImageProcessor",
|
12 |
+
"image_std": [
|
13 |
+
0.5,
|
14 |
+
0.5,
|
15 |
+
0.5
|
16 |
+
],
|
17 |
+
"processor_class": "SiglipProcessor",
|
18 |
+
"resample": 3,
|
19 |
+
"rescale_factor": 0.00392156862745098,
|
20 |
+
"size": {
|
21 |
+
"height": 384,
|
22 |
+
"width": 384
|
23 |
+
}
|
24 |
+
}
|
special_tokens_map.json
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": "<s>",
|
3 |
+
"cls_token": "<s>",
|
4 |
+
"eos_token": "</s>",
|
5 |
+
"mask_token": {
|
6 |
+
"content": "<mask>",
|
7 |
+
"lstrip": true,
|
8 |
+
"normalized": false,
|
9 |
+
"rstrip": false,
|
10 |
+
"single_word": false
|
11 |
+
},
|
12 |
+
"pad_token": "<pad>",
|
13 |
+
"sep_token": "</s>",
|
14 |
+
"unk_token": "<unk>"
|
15 |
+
}
|
tokenizer.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3a56def25aa40facc030ea8b0b87f3688e4b3c39eb8b45d5702b3a1300fe2a20
|
3 |
+
size 17082734
|
tokenizer_config.json
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"added_tokens_decoder": {
|
3 |
+
"0": {
|
4 |
+
"content": "<s>",
|
5 |
+
"lstrip": false,
|
6 |
+
"normalized": false,
|
7 |
+
"rstrip": false,
|
8 |
+
"single_word": false,
|
9 |
+
"special": true
|
10 |
+
},
|
11 |
+
"1": {
|
12 |
+
"content": "<pad>",
|
13 |
+
"lstrip": false,
|
14 |
+
"normalized": false,
|
15 |
+
"rstrip": false,
|
16 |
+
"single_word": false,
|
17 |
+
"special": true
|
18 |
+
},
|
19 |
+
"2": {
|
20 |
+
"content": "</s>",
|
21 |
+
"lstrip": false,
|
22 |
+
"normalized": false,
|
23 |
+
"rstrip": false,
|
24 |
+
"single_word": false,
|
25 |
+
"special": true
|
26 |
+
},
|
27 |
+
"3": {
|
28 |
+
"content": "<unk>",
|
29 |
+
"lstrip": false,
|
30 |
+
"normalized": false,
|
31 |
+
"rstrip": false,
|
32 |
+
"single_word": false,
|
33 |
+
"special": true
|
34 |
+
},
|
35 |
+
"250001": {
|
36 |
+
"content": "<mask>",
|
37 |
+
"lstrip": true,
|
38 |
+
"normalized": false,
|
39 |
+
"rstrip": false,
|
40 |
+
"single_word": false,
|
41 |
+
"special": true
|
42 |
+
}
|
43 |
+
},
|
44 |
+
"bos_token": "<s>",
|
45 |
+
"clean_up_tokenization_spaces": false,
|
46 |
+
"cls_token": "<s>",
|
47 |
+
"eos_token": "</s>",
|
48 |
+
"mask_token": "<mask>",
|
49 |
+
"model_max_length": 512,
|
50 |
+
"pad_token": "<pad>",
|
51 |
+
"sep_token": "</s>",
|
52 |
+
"tokenizer_class": "XLMRobertaTokenizer",
|
53 |
+
"unk_token": "<unk>"
|
54 |
+
}
|