intfloat commited on
Commit
97fe6cd
·
1 Parent(s): 5a522de

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +54 -39
README.md CHANGED
@@ -44,57 +44,72 @@ pip install -r requirements.txt
44
 
45
  Then you can enter the directory to run the following command.
46
  ```python
47
- from src.model import MMEBModel
48
- from src.arguments import ModelArguments
49
- from src.utils import load_processor
50
  import torch
51
- from transformers import HfArgumentParser, AutoProcessor
52
  from PIL import Image
53
- import numpy as np
54
- model_args = ModelArguments(
55
- model_name='intfloat/mmE5-mllama-11b-instruct',
56
- pooling='last',
57
- normalize=True,
58
- model_backbone='mllama')
59
- processor = load_processor(model_args)
60
- model = MMEBModel.load(model_args)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  model.eval()
62
- model = model.to('cuda', dtype=torch.bfloat16)
63
  # Image + Text -> Text
64
  inputs = processor(text='<|image|><|begin_of_text|> Represent the given image with the following question: What is in the image', images=[Image.open(
65
- 'figures/example.jpg')], return_tensors="pt")
66
- inputs = {key: value.to('cuda') for key, value in inputs.items()}
67
- qry_output = model(qry=inputs)["qry_reps"]
68
  string = 'A cat and a dog'
69
- inputs = processor(text=string, return_tensors="pt")
70
- inputs = {key: value.to('cuda') for key, value in inputs.items()}
71
- tgt_output = model(tgt=inputs)["tgt_reps"]
72
- print(string, '=', model.compute_similarity(qry_output, tgt_output))
73
  ## A cat and a dog = tensor([[0.3965]], device='cuda:0', dtype=torch.bfloat16)
 
74
  string = 'A cat and a tiger'
75
- inputs = processor(text=string, return_tensors="pt")
76
- inputs = {key: value.to('cuda') for key, value in inputs.items()}
77
- tgt_output = model(tgt=inputs)["tgt_reps"]
78
- print(string, '=', model.compute_similarity(qry_output, tgt_output))
79
  ## A cat and a tiger = tensor([[0.3105]], device='cuda:0', dtype=torch.bfloat16)
 
80
  # Text -> Image
81
- inputs = processor(text='Find me an everyday image that matches the given caption: A cat and a dog.', return_tensors="pt")
82
- inputs = {key: value.to('cuda') for key, value in inputs.items()}
83
- qry_output = model(qry=inputs)["qry_reps"]
84
  string = '<|image|><|begin_of_text|> Represent the given image.'
85
- inputs = processor(text=string, images=[Image.open('figures/example.jpg')], return_tensors="pt")
86
- inputs = {key: value.to('cuda') for key, value in inputs.items()}
87
- tgt_output = model(tgt=inputs)["tgt_reps"]
88
- print(string, '=', model.compute_similarity(qry_output, tgt_output))
89
  ## <|image|><|begin_of_text|> Represent the given image. = tensor([[0.4219]], device='cuda:0', dtype=torch.bfloat16)
90
- inputs = processor(text='Find me an everyday image that matches the given caption: A cat and a tiger.', return_tensors="pt")
91
- inputs = {key: value.to('cuda') for key, value in inputs.items()}
92
- qry_output = model(qry=inputs)["qry_reps"]
93
  string = '<|image|><|begin_of_text|> Represent the given image.'
94
- inputs = processor(text=string, images=[Image.open('figures/example.jpg')], return_tensors="pt")
95
- inputs = {key: value.to('cuda') for key, value in inputs.items()}
96
- tgt_output = model(tgt=inputs)["tgt_reps"]
97
- print(string, '=', model.compute_similarity(qry_output, tgt_output))
98
  ## <|image|><|begin_of_text|> Represent the given image. = tensor([[0.3887]], device='cuda:0', dtype=torch.bfloat16)
99
  ```
100
 
@@ -106,4 +121,4 @@ print(string, '=', model.compute_similarity(qry_output, tgt_output))
106
  journal={arXiv preprint arXiv:2502.08468},
107
  year={2025}
108
  }
109
- ```
 
44
 
45
  Then you can enter the directory to run the following command.
46
  ```python
47
+ from transformers import MllamaForConditionalGeneration, AutoProcessor, AutoConfig
 
 
48
  import torch
 
49
  from PIL import Image
50
+
51
+ # Pooling and Normalization
52
+ def last_pooling(last_hidden_state, attention_mask, normalize=True):
53
+ sequence_lengths = attention_mask.sum(dim=1) - 1
54
+ batch_size = last_hidden_state.shape[0]
55
+ reps = last_hidden_state[torch.arange(batch_size, device=last_hidden_state.device), sequence_lengths]
56
+ if normalize:
57
+ reps = torch.nn.functional.normalize(reps, p=2, dim=-1)
58
+ return reps
59
+
60
+ def compute_similarity(q_reps, p_reps):
61
+ return torch.matmul(q_reps, p_reps.transpose(0, 1))
62
+
63
+ model_name = "intfloat/mmE5-mllama-11b-instruct"
64
+
65
+ # Load Processor and Model
66
+ processor = AutoProcessor.from_pretrained(model_name)
67
+ processor.tokenizer.padding_side = "right"
68
+
69
+ config = AutoConfig.from_pretrained(model_name)
70
+ if hasattr(config, 'use_cache'):
71
+ config.use_cache = False
72
+ config.padding_side = "right"
73
+ model = MllamaForConditionalGeneration.from_pretrained(
74
+ model_name, config=config,
75
+ torch_dtype=torch.bfloat16
76
+ ).to("cuda")
77
+ model.padding_side = "right"
78
  model.eval()
79
+
80
  # Image + Text -> Text
81
  inputs = processor(text='<|image|><|begin_of_text|> Represent the given image with the following question: What is in the image', images=[Image.open(
82
+ 'figures/example.jpg')], return_tensors="pt").to("cuda")
83
+ qry_output = last_pooling(model(**inputs, return_dict=True, output_hidden_states=True).hidden_states[-1], inputs['attention_mask'])
84
+
85
  string = 'A cat and a dog'
86
+ text_inputs = processor(text=string, return_tensors="pt").to("cuda")
87
+ tgt_output = last_pooling(model(**text_inputs, return_dict=True, output_hidden_states=True).hidden_states[-1], text_inputs['attention_mask'])
88
+ print(string, '=', compute_similarity(qry_output, tgt_output))
 
89
  ## A cat and a dog = tensor([[0.3965]], device='cuda:0', dtype=torch.bfloat16)
90
+
91
  string = 'A cat and a tiger'
92
+ text_inputs = processor(text=string, return_tensors="pt").to("cuda")
93
+ tgt_output = last_pooling(model(**text_inputs, return_dict=True, output_hidden_states=True).hidden_states[-1], text_inputs['attention_mask'])
94
+ print(string, '=', compute_similarity(qry_output, tgt_output))
 
95
  ## A cat and a tiger = tensor([[0.3105]], device='cuda:0', dtype=torch.bfloat16)
96
+
97
  # Text -> Image
98
+ inputs = processor(text='Find me an everyday image that matches the given caption: A cat and a dog.', return_tensors="pt").to("cuda")
99
+ qry_output = last_pooling(model(**inputs, return_dict=True, output_hidden_states=True).hidden_states[-1], inputs['attention_mask'])
100
+
101
  string = '<|image|><|begin_of_text|> Represent the given image.'
102
+ tgt_inputs = processor(text=string, images=[Image.open('figures/example.jpg')], return_tensors="pt").to("cuda")
103
+ tgt_output = last_pooling(model(**tgt_inputs, return_dict=True, output_hidden_states=True).hidden_states[-1], tgt_inputs['attention_mask'])
104
+ print(string, '=', compute_similarity(qry_output, tgt_output))
 
105
  ## <|image|><|begin_of_text|> Represent the given image. = tensor([[0.4219]], device='cuda:0', dtype=torch.bfloat16)
106
+
107
+ inputs = processor(text='Find me an everyday image that matches the given caption: A cat and a tiger.', return_tensors="pt").to("cuda")
108
+ qry_output = last_pooling(model(**inputs, return_dict=True, output_hidden_states=True).hidden_states[-1], inputs['attention_mask'])
109
  string = '<|image|><|begin_of_text|> Represent the given image.'
110
+ tgt_inputs = processor(text=string, images=[Image.open('figures/example.jpg')], return_tensors="pt").to("cuda")
111
+ tgt_output = last_pooling(model(**tgt_inputs, return_dict=True, output_hidden_states=True).hidden_states[-1], tgt_inputs['attention_mask'])
112
+ print(string, '=', compute_similarity(qry_output, tgt_output))
 
113
  ## <|image|><|begin_of_text|> Represent the given image. = tensor([[0.3887]], device='cuda:0', dtype=torch.bfloat16)
114
  ```
115
 
 
121
  journal={arXiv preprint arXiv:2502.08468},
122
  year={2025}
123
  }
124
+ ```