Update README.md
Browse files
README.md
CHANGED
@@ -32,7 +32,57 @@ VLM2Vec-LlaVa-Next could outperform the baselines and other version of VLM2Vec b
|
|
32 |
|
33 |
|
34 |
## How to use VLM2Vec-LlaVa-Next
|
|
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
|
38 |
## Citation
|
|
|
32 |
|
33 |
|
34 |
## How to use VLM2Vec-LlaVa-Next
|
35 |
+
(More details please refer to our Github repo, here is just a simple demo.)
|
36 |
|
37 |
+
```python
|
38 |
+
from src.model import MMEBModel
|
39 |
+
from src.arguments import ModelArguments
|
40 |
+
from src.utils import load_processor
|
41 |
+
|
42 |
+
import torch
|
43 |
+
from transformers import HfArgumentParser, AutoProcessor
|
44 |
+
from PIL import Image
|
45 |
+
import numpy as np
|
46 |
+
|
47 |
+
|
48 |
+
model_args = ModelArguments(
|
49 |
+
model_name='TIGER-Lab/VLM2Vec-LLaVa-Next',
|
50 |
+
pooling='last',
|
51 |
+
normalize=True,
|
52 |
+
model_backbone='llava_next')
|
53 |
+
|
54 |
+
processor = load_processor(model_args)
|
55 |
+
|
56 |
+
model = MMEBModel.load(model_args)
|
57 |
+
model.eval()
|
58 |
+
model = model.to('cuda', dtype=torch.bfloat16)
|
59 |
+
|
60 |
+
# Image + Text -> Text
|
61 |
+
inputs = processor(text='<image> Represent the given image with the following question: What is in the image',
|
62 |
+
images=Image.open('figures/example.jpg'),
|
63 |
+
return_tensors="pt")
|
64 |
+
inputs = {key: value.to('cuda') for key, value in inputs.items()}
|
65 |
+
qry_output = model(qry=inputs)["qry_reps"]
|
66 |
+
|
67 |
+
string = 'A cat and a dog'
|
68 |
+
inputs = processor(text=string,
|
69 |
+
images=None,
|
70 |
+
return_tensors="pt")
|
71 |
+
inputs = {key: value.to('cuda') for key, value in inputs.items()}
|
72 |
+
tgt_output = model(tgt=inputs)["tgt_reps"]
|
73 |
+
print(string, '=', model.compute_similarity(qry_output, tgt_output))
|
74 |
+
## A cat and a dog = tensor([[0.4414]], device='cuda:0', dtype=torch.bfloat16)
|
75 |
+
|
76 |
+
string = 'A cat and a tiger'
|
77 |
+
inputs = processor(text=string,
|
78 |
+
images=None,
|
79 |
+
return_tensors="pt")
|
80 |
+
inputs = {key: value.to('cuda') for key, value in inputs.items()}
|
81 |
+
tgt_output = model(tgt=inputs)["tgt_reps"]
|
82 |
+
print(string, '=', model.compute_similarity(qry_output, tgt_output))
|
83 |
+
## A cat and a tiger = tensor([[0.3555]], device='cuda:0', dtype=torch.bfloat16)
|
84 |
+
|
85 |
+
```
|
86 |
|
87 |
|
88 |
## Citation
|