Views
No views yet

"[INST] <image>\nWhat is shown in this image? [/INST]"1from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration
2import torch
3from PIL import Image
4import requests
5
6processor = LlavaNextProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")
7
8model = LlavaNextForConditionalGeneration.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf", torch_dtype=torch.float16, low_cpu_mem_usage=True)
9model.to("cuda:0")
10
11# prepare image and text prompt, using the appropriate prompt template
12url = "https://github.com/haotian-liu/LLaVA/blob/1a91fc274d7c35a9b50b3cb29c4247ae5837ce39/images/llava_v1_5_radar.jpg?raw=true"
13image = Image.open(requests.get(url, stream=True).raw)
14
15# Define a chat history and use `apply_chat_template` to get correctly formatted prompt
16# Each value in "content" has to be a list of dicts with types ("text", "image")
17conversation = [
18 {
19
20 "role": "user",
21 "content": [
22 {"type": "text", "text": "What is shown in this image?"},
23 {"type": "image"},
24 ],
25 },
26]
27prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
28
29inputs = processor(images=image, text=prompt, return_tensors="pt").to("cuda:0")
30
31# autoregressively complete prompt
32output = model.generate(**inputs, max_new_tokens=100)
33
34print(processor.decode(output[0], skip_special_tokens=True))bitsandbytes librarybitsandbytes, pip install bitsandbytes and make sure to have access to a CUDA compatible GPU device. Simply change the snippet above with:1model = LlavaNextForConditionalGeneration.from_pretrained(
2 model_id,
3 torch_dtype=torch.float16,
4 low_cpu_mem_usage=True,
5+ load_in_4bit=True
6)flash-attn. Refer to the original repository of Flash Attention regarding that package installation. Simply change the snippet above with:1model = LlavaNextForConditionalGeneration.from_pretrained(
2 model_id,
3 torch_dtype=torch.float16,
4 low_cpu_mem_usage=True,
5+ use_flash_attention_2=True
6).to(0)1@misc{liu2023improved,
2 title={Improved Baselines with Visual Instruction Tuning},
3 author={Haotian Liu and Chunyuan Li and Yuheng Li and Yong Jae Lee},
4 year={2023},
5 eprint={2310.03744},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV}
8}