Views
No views yet

pip install torch==2.2.0 torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 transformers==4.38.1 accelerate==0.27.2 sentencepiece==0.1.99 einops==0.7.0 xformers==0.0.24 protobuf==3.20.3 triton==2.1.0 bitsandbytes==0.43.0.dev0 1pip install bitsandbytes-0.43.0.dev0-cp310-cp310-win_amd64.whl
2
3pip install triton-2.1.0-cp310-cp310-win_amd64.whl1import torch
2from PIL import Image
3from transformers import AutoModelForCausalLM, LlamaTokenizer
4
5model_path = "'local/model/folder/path/here' or 'Rodeszones/CogVLM-grounding-generalist-hf-quant4'"
6
7
8tokenizer = LlamaTokenizer.from_pretrained('lmsys/vicuna-7b-v1.5')
9model = AutoModelForCausalLM.from_pretrained(
10 model_path,
11 torch_dtype=torch.bfloat16,
12 low_cpu_mem_usage=True,
13 trust_remote_code=True
14).eval()
15
16
17# chat example
18query = 'Can you provide a description of the image and include the coordinates [[x0,y0,x1,y1]] for each mentioned object?'
19image = Image.open("your/image/path/here").convert('RGB')
20inputs = model.build_conversation_input_ids(tokenizer, query=query, history=[], images=[image]) # chat mode
21inputs = {
22 'input_ids': inputs['input_ids'].unsqueeze(0).to('cuda'),
23 'token_type_ids': inputs['token_type_ids'].unsqueeze(0).to('cuda'),
24 'attention_mask': inputs['attention_mask'].unsqueeze(0).to('cuda'),
25 'images': [[inputs['images'][0].to('cuda').to(torch.bfloat16)]],
26}
27gen_kwargs = {"max_length": 2048, "do_sample": False}
28
29with torch.no_grad():
30 outputs = model.generate(**inputs, **gen_kwargs)
31 outputs = outputs[:, inputs['input_ids'].shape[1]:]
32 print(tokenizer.decode(outputs[0]))
33
34# example output
35# a room with a ladder [[378,107,636,998]] and a blue and white towel [[073,000,346,905]].</s>
36# NOTE: The model's squares have dimensions of 1000 by 1000, which is important to consider.
37 @article{wang2023cogvlm,
title={CogVLM: Visual Expert for Pretrained Language Models},
author={Weihan Wang and Qingsong Lv and Wenmeng Yu and Wenyi Hong and Ji Qi and Yan Wang and Junhui Ji and Zhuoyi Yang and Lei Zhao and Xixuan Song and Jiazheng Xu and Bin Xu and Juanzi Li and Yuxiao Dong and Ming Ding and Jie Tang},
year={2023},
eprint={2311.03079},
archivePrefix={arXiv},
primaryClass={cs.CV}
}