Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Quickstart
|
| 2 |
+
|
| 3 |
+
```python
|
| 4 |
+
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from transformers import AutoModelForCausalLM, LlamaTokenizer
|
| 7 |
+
|
| 8 |
+
tokenizer = LlamaTokenizer.from_pretrained('lmsys/vicuna-7b-v1.5')
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
'THUDM/cogvlm-grounding-generalist-hf',
|
| 11 |
+
torch_dtype=torch.bfloat16,
|
| 12 |
+
low_cpu_mem_usage=True,
|
| 13 |
+
trust_remote_code=True
|
| 14 |
+
).to('cuda').eval()
|
| 15 |
+
|
| 16 |
+
query = 'Can you provide a description of the image and include the coordinates [[x0,y0,x1,y1]] for each mentioned object?'
|
| 17 |
+
image = Image.open(requests.get('https://github.com/THUDM/CogVLM/blob/main/examples/4.jpg?raw=true', stream=True).raw).convert('RGB')
|
| 18 |
+
inputs = model.build_conversation_input_ids(tokenizer, query=query, images=[image])
|
| 19 |
+
inputs = {
|
| 20 |
+
'input_ids': inputs['input_ids'].unsqueeze(0).to('cuda'),
|
| 21 |
+
'token_type_ids': inputs['token_type_ids'].unsqueeze(0).to('cuda'),
|
| 22 |
+
'attention_mask': inputs['attention_mask'].unsqueeze(0).to('cuda'),
|
| 23 |
+
'images': [[inputs['images'][0].to('cuda').to(torch.bfloat16)]],
|
| 24 |
+
}
|
| 25 |
+
gen_kwargs = {"max_length": 2048, "do_sample": False}
|
| 26 |
+
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
outputs = model.generate(**inputs, **gen_kwargs)
|
| 29 |
+
outputs = outputs[:, inputs['input_ids'].shape[1]:]
|
| 30 |
+
print(tokenizer.decode(outputs[0]))
|
| 31 |
+
```
|