Views
No views yet
transformers >= 4.35.3.
The model supports multi-image and multi-prompt generation. Meaning that you can pass multiple images in your prompt. Make sure also to follow the correct prompt template (USER: xxx\nASSISTANT:) and add the token <image> to the location where you want to query images:pipeline:"llava-hf/llava-interleave-qwen-0.5b-hf" checkpoint.1from transformers import pipeline
2
3pipe = pipeline("image-text-to-text", model="llava-interleave-qwen-7b-hf")
4messages = [
5 {
6 "role": "user",
7 "content": [
8 {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"},
9 {"type": "text", "text": "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"},
10 ],
11 },
12]
13
14out = pipe(text=messages, max_new_tokens=20)
15print(out)
16>>> [{'input_text': [{'role': 'user', 'content': [{'type': 'image', 'url': 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'}, {'type': 'text', 'text': 'What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'}]}], 'generated_text': 'Lava'}]transformers:float16 precision on a GPU device:1import requests
2from PIL import Image
3
4import torch
5from transformers import AutoProcessor, LlavaForConditionalGeneration
6
7model_id = "llava-hf/llava-interleave-qwen-7b-hf"
8model = LlavaForConditionalGeneration.from_pretrained(
9 model_id,
10 torch_dtype=torch.float16,
11 low_cpu_mem_usage=True,
12).to(0)
13
14processor = AutoProcessor.from_pretrained(model_id)
15
16# Define a chat history and use `apply_chat_template` to get correctly formatted prompt
17# Each value in "content" has to be a list of dicts with types ("text", "image")
18conversation = [
19 {
20
21 "role": "user",
22 "content": [
23 {"type": "text", "text": "What are these?"},
24 {"type": "image"},
25 ],
26 },
27]
28prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
29
30image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
31raw_image = Image.open(requests.get(image_file, stream=True).raw)
32inputs = processor(images=raw_image, text=prompt, return_tensors='pt').to(0, torch.float16)
33
34output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
35print(processor.decode(output[0][2:], skip_special_tokens=True))1# if you downsampled n frames from the input
2
3image_tokens = "<image>" * n
4prompt = f"<|im_start|>user {image_tokens}\nWhat are these?|im_end|><|im_start|>assistant"
5
6# With chat template if you sampled 5 frames you have to have 5 images in one conversation turn
7conversation = [
8 {
9
10 "role": "user",
11 "content": [
12 {"type": "text", "text": "What are these?"},
13 {"type": "image"},
14 {"type": "image"},
15 {"type": "image"},
16 {"type": "image"},
17 {"type": "image"},
18 ],
19 },
20]
21prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)1# two interleaved images
2prompt = "<|im_start|>user <image><image>\nWhat is the difference between these two images?|im_end|><|im_start|>assistant"
3
4# two interleaved videos, if you downsampled n frames in total from both videos
5image_tokens = "<image>" * n
6prompt = f"<|im_start|>user {image_tokens}\nWhat are these?|im_end|><|im_start|>assistant"
7
8# chat template in interleaved format work same as in sampling videos. Just pass in as many images you want for a prompt
9conversation = [
10 {
11
12 "role": "user",
13 "content": [
14 {"type": "text", "text": "What is the difference between these two images?"},
15 {"type": "image"},
16 {"type": "image"},
17 ],
18 },
19]torch.Tensor which you can pass directly to model.generate()1messages = [
2 {
3 "role": "user",
4 "content": [
5 {"type": "image", "url": "https://www.ilankelman.org/stopsigns/australia.jpg"}
6 {"type": "text", "text": "What is shown in this image?"},
7 ],
8 },
9]
10
11inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors"pt")
12output = model.generate(**inputs, max_new_tokens=50)bitsandbytes librarybitsandbytes, pip install bitsandbytes and make sure to have access to a CUDA compatible GPU device. Simply change the snippet above with:1model = LlavaForConditionalGeneration.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 = LlavaForConditionalGeneration.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{li2024llavanextinterleavetacklingmultiimagevideo,
2 title={LLaVA-NeXT-Interleave: Tackling Multi-image, Video, and 3D in Large Multimodal Models},
3 author={Feng Li and Renrui Zhang and Hao Zhang and Yuanhan Zhang and Bo Li and Wei Li and Zejun Ma and Chunyuan Li},
4 year={2024},
5 eprint={2407.07895},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV},
8 url={https://arxiv.org/abs/2407.07895},
9}