mirror of
https://github.com/huggingface/transformers.git
synced 2025-07-03 12:50:06 +06:00
Merge 124afd759a
into 2d561713f8
This commit is contained in:
commit
71ece4541a
@ -14,288 +14,156 @@ rendered properly in your Markdown viewer.
|
|||||||
|
|
||||||
-->
|
-->
|
||||||
|
|
||||||
# LLaVA-NeXT
|
<div style="float: right;">
|
||||||
|
<div class="flex flex-wrap space-x-1">
|
||||||
<div class="flex flex-wrap space-x-1">
|
<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white">
|
||||||
<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white">
|
<img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat">
|
||||||
<img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat">
|
<img alt="Multimodal" src="https://img.shields.io/badge/Multimodal-vision--language-blue">
|
||||||
<img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white">
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
## Overview
|
# LLaVA-NeXT
|
||||||
|
|
||||||
The LLaVA-NeXT model was proposed in [LLaVA-NeXT: Improved reasoning, OCR, and world knowledge](https://llava-vl.github.io/blog/2024-01-30-llava-next/) by Haotian Liu, Chunyuan Li, Yuheng Li, Bo Li, Yuanhan Zhang, Sheng Shen, Yong Jae Lee. LLaVa-NeXT (also called LLaVa-1.6) improves upon [LLaVa](llava) by increasing the input image resolution and training on an improved visual instruction tuning dataset to improve OCR and common sense reasoning.
|
[LLaVA‑NeXT](https://llava-vl.github.io/blog/2024-05-10-llava-next-stronger-llms/) improves on [Llava](./llava) by increasing the input image resolution by 4x more pixels and supporting 3 aspect ratios (up to 672x672, 336x1344, 1344x336) to better grasp visual details. It is also trained on an improved visual instruction tuning dataset covering more scenarios and applications to improve OCR and common sense reasoning.
|
||||||
|
|
||||||
The introduction from the blog is the following:
|
You can find all the original LLaVA‑NeXT checkpoints under the [LLaVA-NeXT](https://huggingface.co/collections/llava-hf/llava-next-65f75c4afac77fd37dbbe6cf) collection.
|
||||||
|
|
||||||
*In October 2023, we released LLaVA-1.5 with a simple and efficient design along with great performance on a benchmark suite of 12 datasets. It has since served as the foundation of many comprehensive studies of data, model, and capabilities of large multimodal models (LMM), and has enabled various new applications.
|
> [!TIP]
|
||||||
|
> Click on the LLaVA‑NeXT models in the right sidebar for more examples of how to apply Llava-NeXT to different multimodal tasks.
|
||||||
|
|
||||||
Today, we are thrilled to present LLaVA-NeXT, with improved reasoning, OCR, and world knowledge. LLaVA-NeXT even exceeds Gemini Pro on several benchmarks.
|
The example below demonstrates how to generate text based on an image with [`Pipeline`] or the [`AutoModel`] class.
|
||||||
|
|
||||||
Compared with LLaVA-1.5, LLaVA-NeXT has several improvements:
|
<hfoptions id="usage">
|
||||||
|
|
||||||
Increasing the input image resolution to 4x more pixels. This allows it to grasp more visual details. It supports three aspect ratios, up to 672x672, 336x1344, 1344x336 resolution.
|
<hfoption id="Pipeline">
|
||||||
Better visual reasoning and OCR capability with an improved visual instruction tuning data mixture.
|
|
||||||
Better visual conversation for more scenarios, covering different applications. Better world knowledge and logical reasoning.
|
|
||||||
Efficient deployment and inference with SGLang.
|
|
||||||
Along with performance improvements, LLaVA-NeXT maintains the minimalist design and data efficiency of LLaVA-1.5. It re-uses the pretrained connector of LLaVA-1.5, and still uses less than 1M visual instruction tuning samples. The largest 34B variant finishes training in ~1 day with 32 A100s.*
|
|
||||||
|
|
||||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/llava_next_overview.png"
|
|
||||||
alt="drawing" width="600"/>
|
|
||||||
|
|
||||||
<small> LLaVa-NeXT incorporates a higher input resolution by encoding various patches of the input image. Taken from the <a href="https://huggingface.co/papers/2310.03744">original paper.</a> </small>
|
|
||||||
|
|
||||||
This model was contributed by [nielsr](https://huggingface.co/nielsr).
|
|
||||||
The original code can be found [here](https://github.com/haotian-liu/LLaVA/tree/main).
|
|
||||||
|
|
||||||
## Usage tips
|
|
||||||
|
|
||||||
- We advise users to use `padding_side="left"` when computing batched generation as it leads to more accurate results. Simply make sure to call `processor.tokenizer.padding_side = "left"` before generating.
|
|
||||||
|
|
||||||
<Tip warning={true}>
|
|
||||||
|
|
||||||
- Llava-Next uses different number of patches for images and thus has to pad the inputs inside modeling code, aside from the padding done when processing the inputs. The default setting is "left-padding" if model is in `eval()` mode, otherwise "right-padding".
|
|
||||||
|
|
||||||
</Tip>
|
|
||||||
|
|
||||||
|
|
||||||
> [!NOTE]
|
|
||||||
> LLaVA models after release v4.46 will raise warnings about adding `processor.patch_size = {{patch_size}}`, `processor.num_additional_image_tokens = {{num_additional_image_tokens}}` and processor.vision_feature_select_strategy = {{vision_feature_select_strategy}}`. It is strongly recommended to add the attributes to the processor if you own the model checkpoint, or open a PR if it is not owned by you.
|
|
||||||
Adding these attributes means that LLaVA will try to infer the number of image tokens required per image and expand the text with as many `<image>` placeholders as there will be tokens. Usually it is around 500 tokens per image, so make sure that the text is not truncated as otherwise there will be failure when merging the embeddings.
|
|
||||||
The attributes can be obtained from model config, as `model.config.vision_config.patch_size` or `model.config.vision_feature_select_strategy`. The `num_additional_image_tokens` should be `1` if the vision backbone adds a CLS token or `0` if nothing extra is added to the vision patches.
|
|
||||||
|
|
||||||
|
|
||||||
### Formatting Prompts with Chat Templates
|
|
||||||
|
|
||||||
Each **checkpoint** is trained with a specific prompt format, depending on the underlying large language model backbone. To ensure correct formatting, use the processor’s `apply_chat_template` method.
|
|
||||||
|
|
||||||
**Important:**
|
|
||||||
- You must construct a conversation history — passing a plain string won't work.
|
|
||||||
- Each message should be a dictionary with `"role"` and `"content"` keys.
|
|
||||||
- The `"content"` should be a list of dictionaries for different modalities like `"text"` and `"image"`.
|
|
||||||
|
|
||||||
|
|
||||||
Here’s an example of how to structure your input. We will use [llava-v1.6-mistral-7b-hf](https://huggingface.co/llava-hf/llava-v1.6-mistral-7b-hf) and a conversation history of text and image.
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from transformers import LlavaNextProcessor
|
from transformers import pipeline
|
||||||
|
from PIL import Image
|
||||||
|
import requests
|
||||||
|
|
||||||
processor = LlavaNextProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")
|
pipe = pipeline("image-to-text", model="llava-hf/llava-v1.6-mistral-7b-hf", device="cuda")
|
||||||
|
image = Image.open(requests.get("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/llava_next_ocr.png", stream=True).raw)
|
||||||
|
|
||||||
conversation = [
|
result = pipe(image, prompt="What does this chart show?")
|
||||||
{
|
print(result[0]["generated_text"])
|
||||||
"role": "user",
|
|
||||||
"content": [
|
|
||||||
{"type": "image"},
|
|
||||||
{"type": "text", "text": "What’s shown in this image?"},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"role": "assistant",
|
|
||||||
"content": [{"type": "text", "text": "This image shows a red stop sign."},]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
|
|
||||||
"role": "user",
|
|
||||||
"content": [
|
|
||||||
{"type": "text", "text": "Describe the image in more details."},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
|
||||||
|
|
||||||
# Note that the template simply formats your prompt, you still have to tokenize it and obtain pixel values for your images
|
|
||||||
print(text_prompt)
|
|
||||||
>>> "[INST] <image>\nWhat's shown in this image? [/INST] This image shows a red stop sign. [INST] Describe the image in more details. [/INST]"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
- If you want to construct a chat prompt yourself, below is a list of possible formats
|
</hfoption>
|
||||||
.
|
|
||||||
[llava-v1.6-mistral-7b-hf](https://huggingface.co/llava-hf/llava-v1.6-mistral-7b-hf) requires the following format:
|
|
||||||
```bash
|
|
||||||
"[INST] <image>\nWhat is shown in this image? [/INST]"
|
|
||||||
```
|
|
||||||
|
|
||||||
[llava-v1.6-vicuna-7b-hf](https://huggingface.co/llava-hf/llava-v1.6-vicuna-7b-hf) and [llava-v1.6-vicuna-13b-hf](https://huggingface.co/llava-hf/llava-v1.6-vicuna-13b-hf) require the following format:
|
<hfoption id="AutoModel">
|
||||||
```bash
|
|
||||||
"A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions. USER: <image>\nWhat is shown in this image? ASSISTANT:"
|
|
||||||
```
|
|
||||||
|
|
||||||
[llava-v1.6-34b-hf](https://huggingface.co/llava-hf/llava-v1.6-34b-hf) requires the following format:
|
|
||||||
```bash
|
|
||||||
"<|im_start|>system\nAnswer the questions.<|im_end|><|im_start|>user\n<image>\nWhat is shown in this image?<|im_end|><|im_start|>assistant\n"
|
|
||||||
```
|
|
||||||
|
|
||||||
[llama3-llava-next-8b-hf](https://huggingface.co/llava-hf/llava-next-8b-hf) requires the following format:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
"<|start_header_id|>system<|end_header_id|>\n\nYou are a helpful language and vision assistant. You are able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language.<|eot_id|><|start_header_id|><|start_header_id|>user<|end_header_id|>\n\n<image>\nWhat is shown in this image?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
|
||||||
```
|
|
||||||
|
|
||||||
[llava-next-72b-hf](https://huggingface.co/llava-hf/llava-next-72b-hf) and [llava-next-110b-hf](https://huggingface.co/llava-hf/llava-next-110b-hf) require the following format:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<image>\nWhat is shown in this image?<|im_end|>\n<|im_start|>assistant\n"
|
|
||||||
```
|
|
||||||
|
|
||||||
🚀 **Bonus:** If you're using `transformers>=4.49.0`, you can also get a vectorized output from `apply_chat_template`. See the **Usage Examples** below for more details on how to use it.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Usage example
|
|
||||||
|
|
||||||
### Single image inference
|
|
||||||
|
|
||||||
Here's how to load the model and perform inference in half-precision (`torch.float16`):
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration
|
from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration
|
||||||
import torch
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import requests
|
import requests, torch
|
||||||
|
|
||||||
processor = LlavaNextProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")
|
processor = LlavaNextProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")
|
||||||
|
model = LlavaNextForConditionalGeneration.from_pretrained(
|
||||||
|
"llava-hf/llava-v1.6-mistral-7b-hf", torch_dtype=torch.float16
|
||||||
|
).to("cuda")
|
||||||
|
|
||||||
model = LlavaNextForConditionalGeneration.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf", torch_dtype=torch.float16)
|
image = Image.open(requests.get(
|
||||||
model.to("cuda:0")
|
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/llava_next_ocr.png", stream=True).raw)
|
||||||
|
|
||||||
# prepare image and text prompt, using the appropriate prompt template
|
|
||||||
url = "https://github.com/haotian-liu/LLaVA/blob/1a91fc274d7c35a9b50b3cb29c4247ae5837ce39/images/llava_v1_5_radar.jpg?raw=true"
|
|
||||||
image = Image.open(requests.get(url, stream=True).raw)
|
|
||||||
|
|
||||||
conversation = [
|
conversation = [
|
||||||
{
|
{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "What does this chart show?"}]}
|
||||||
"role": "user",
|
|
||||||
"content": [
|
|
||||||
{"type": "image"},
|
|
||||||
{"type": "text", "text": "What is shown in this image?"},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
||||||
inputs = processor(image, prompt, return_tensors="pt").to("cuda:0")
|
inputs = processor(image, prompt, return_tensors="pt").to("cuda")
|
||||||
|
|
||||||
# autoregressively complete prompt
|
|
||||||
output = model.generate(**inputs, max_new_tokens=100)
|
output = model.generate(**inputs, max_new_tokens=100)
|
||||||
|
|
||||||
print(processor.decode(output[0], skip_special_tokens=True))
|
print(processor.decode(output[0], skip_special_tokens=True))
|
||||||
```
|
```
|
||||||
|
|
||||||
### Multi image inference
|
</hfoption>
|
||||||
|
|
||||||
LLaVa-Next can perform inference with multiple images as input, where images either belong to the same prompt or different prompts (in batched inference). Here is how you can do it:
|
<hfoption id="transformers-cli">
|
||||||
|
</hfoption>
|
||||||
|
</hfoptions>
|
||||||
|
|
||||||
|
Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the [Quantization](../quantization/overview) overview for more available quantization backends.
|
||||||
|
|
||||||
|
The example below uses [bitsandbytes](../quantization/bitsandbytes) to only quantize the weights to int4.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import requests
|
from transformers import AutoModelForImageTextToText, AutoProcessor, BitsAndBytesConfig
|
||||||
from PIL import Image
|
|
||||||
import torch
|
|
||||||
from transformers import AutoProcessor, AutoModelForImageTextToText
|
|
||||||
|
|
||||||
# Load the model in half-precision
|
quant_config = BitsAndBytesConfig(
|
||||||
model = AutoModelForImageTextToText.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf", torch_dtype=torch.float16, device_map="auto")
|
|
||||||
processor = AutoProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")
|
|
||||||
|
|
||||||
# Get three different images
|
|
||||||
url = "https://www.ilankelman.org/stopsigns/australia.jpg"
|
|
||||||
image_stop = Image.open(requests.get(url, stream=True).raw)
|
|
||||||
|
|
||||||
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
|
||||||
image_cats = Image.open(requests.get(url, stream=True).raw)
|
|
||||||
|
|
||||||
url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.jpg"
|
|
||||||
image_snowman = Image.open(requests.get(url, stream=True).raw)
|
|
||||||
|
|
||||||
# Prepare a batch of two prompts, where the first one is a multi-turn conversation and the second is not
|
|
||||||
conversation_1 = [
|
|
||||||
{
|
|
||||||
"role": "user",
|
|
||||||
"content": [
|
|
||||||
{"type": "image"},
|
|
||||||
{"type": "text", "text": "What is shown in this image?"},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"role": "assistant",
|
|
||||||
"content": [
|
|
||||||
{"type": "text", "text": "There is a red stop sign in the image."},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"role": "user",
|
|
||||||
"content": [
|
|
||||||
{"type": "image"},
|
|
||||||
{"type": "text", "text": "What about this image? How many cats do you see?"},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
conversation_2 = [
|
|
||||||
{
|
|
||||||
"role": "user",
|
|
||||||
"content": [
|
|
||||||
{"type": "image"},
|
|
||||||
{"type": "text", "text": "What is shown in this image?"},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
prompt_1 = processor.apply_chat_template(conversation_1, add_generation_prompt=True)
|
|
||||||
prompt_2 = processor.apply_chat_template(conversation_2, add_generation_prompt=True)
|
|
||||||
prompts = [prompt_1, prompt_2]
|
|
||||||
|
|
||||||
# We can simply feed images in the order they have to be used in the text prompt
|
|
||||||
# Each "<image>" token uses one image leaving the next for the subsequent "<image>" tokens
|
|
||||||
inputs = processor(images=[image_stop, image_cats, image_snowman], text=prompts, padding=True, return_tensors="pt").to(model.device)
|
|
||||||
|
|
||||||
# Generate
|
|
||||||
generate_ids = model.generate(**inputs, max_new_tokens=30)
|
|
||||||
processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Model optimization
|
|
||||||
|
|
||||||
### Quantization using Bitsandbytes
|
|
||||||
|
|
||||||
The model can be loaded in 8 or 4 bits, greatly reducing the memory requirements while maintaining the performance of the original model. First make sure to install bitsandbytes, `pip install bitsandbytes`, and to have access to a GPU/accelerator that is supported by the library.
|
|
||||||
|
|
||||||
<Tip>
|
|
||||||
|
|
||||||
bitsandbytes is being refactored to support multiple backends beyond CUDA. Currently, ROCm (AMD GPU) and Intel CPU implementations are mature, with Intel XPU in progress and Apple Silicon support expected by Q4/Q1. For installation instructions and the latest backend updates, visit [this link](https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend).
|
|
||||||
|
|
||||||
We value your feedback to help identify bugs before the full release! Check out [these docs](https://huggingface.co/docs/bitsandbytes/main/en/non_cuda_backends) for more details and feedback links.
|
|
||||||
|
|
||||||
</Tip>
|
|
||||||
|
|
||||||
Simply change the snippet above with:
|
|
||||||
|
|
||||||
```python
|
|
||||||
from transformers import AutoModelForImageTextToText, BitsAndBytesConfig
|
|
||||||
|
|
||||||
# specify how to quantize the model
|
|
||||||
quantization_config = BitsAndBytesConfig(
|
|
||||||
load_in_4bit=True,
|
load_in_4bit=True,
|
||||||
bnb_4bit_quant_type="nf4",
|
|
||||||
bnb_4bit_compute_dtype=torch.float16,
|
bnb_4bit_compute_dtype=torch.float16,
|
||||||
|
bnb_4bit_quant_type="nf4"
|
||||||
)
|
)
|
||||||
|
|
||||||
model = AutoModelForImageTextToText.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf", quantization_config=quantization_config, device_map="auto")
|
model = AutoModelForImageTextToText.from_pretrained(
|
||||||
|
"llava-hf/llava-v1.6-mistral-7b-hf",
|
||||||
|
quantization_config=quant_config,
|
||||||
|
device_map="auto"
|
||||||
|
)
|
||||||
|
processor = AutoProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")
|
||||||
```
|
```
|
||||||
|
|
||||||
### Use Flash-Attention 2 to further speed-up generation
|
Use the AttentionMaskVisualizer to explore which tokens the model attends to:
|
||||||
|
|
||||||
First make sure to install flash-attn. Refer to the [original repository of Flash Attention](https://github.com/Dao-AILab/flash-attention) regarding that package installation. Simply change the snippet above with:
|
```py
|
||||||
|
from transformers.utils.attention_visualizer import AttentionMaskVisualizer
|
||||||
|
|
||||||
|
viz = AttentionMaskVisualizer("llava-hf/llava-v1.6-mistral-7b-hf")
|
||||||
|
viz("<image> What is shown in this image?")
|
||||||
|
```
|
||||||
|
|
||||||
|
<div class="flex justify-center">
|
||||||
|
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/llava_next_ocr.png"/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
* Different checkpoints (Mistral, Vicuna, etc.) require a specific prompt format depending on the underlying LLM. Always use [`~ProcessorMixin.apply_chat_template`] to ensure correct formatting. Refer to the [Templates](../chat_templating) guide for more details.
|
||||||
|
|
||||||
|
* Set `padding_side="left"` during batched generation for more accurate results.
|
||||||
|
|
||||||
|
```py
|
||||||
|
processor.tokenizer.padding_side = "left"
|
||||||
|
```
|
||||||
|
|
||||||
|
* LLaVA-NeXT uses different numbers of patches for images and pads the inputs inside the modeling code except when padding is done during processing. The default setting is *left-padding* if the model is in `eval()` mode, otherwise it is *right-padding*.
|
||||||
|
|
||||||
|
* LLaVA models after v4.46 raises warnings about adding `processor.patch_size = {{patch_size}}`, `processor.num_additional_image_tokens = {{num_additional_image_tokens}}`, and `processor.vision_feature_select_strategy = {{vision_feature_select_strategy}}`. It is strongly recommended to add these attributes to the processor if you own the model checkpoint or open a PR if it isn't.
|
||||||
|
|
||||||
|
Adding these attributes means LLaVA will try to infer the number of image tokens required per image and expand the text with the same number of `<image>` token placeholders. There are usually ~500 tokens per image, so make sure the text is not truncated because it will cause a failure when merging the embeddings. The attributes can be found in `model.config.vision_config.patch_size` or `model.config.vision_feature_select_strategy`.
|
||||||
|
|
||||||
|
The `num_additional_image_tokens` should be `1` if the vision backbone adds a `CLS` token or `0` if nothing extra is added.
|
||||||
|
|
||||||
|
* The example below demonstrates inference with multiple input images.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from transformers import AutoModelForImageTextToText
|
from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration
|
||||||
|
from PIL import Image
|
||||||
|
import requests, torch
|
||||||
|
|
||||||
model = AutoModelForImageTextToText.from_pretrained(
|
processor = LlavaNextProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")
|
||||||
model_id,
|
model = LlavaNextForConditionalGeneration.from_pretrained(
|
||||||
torch_dtype=torch.float16,
|
"llava-hf/llava-v1.6-mistral-7b-hf", torch_dtype=torch.float16
|
||||||
use_flash_attention_2=True
|
).to("cuda")
|
||||||
).to(0)
|
|
||||||
|
# Load multiple images
|
||||||
|
url1 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/llava_next_ocr.png"
|
||||||
|
url2 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/llava_next_comparison.png"
|
||||||
|
|
||||||
|
image1 = Image.open(requests.get(url1, stream=True).raw)
|
||||||
|
image2 = Image.open(requests.get(url2, stream=True).raw)
|
||||||
|
|
||||||
|
conversation = [
|
||||||
|
{"role": "user", "content": [{"type": "image"}, {"type": "image"}, {"type": "text", "text": "Compare these two images and describe the differences."}]}
|
||||||
|
]
|
||||||
|
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
||||||
|
inputs = processor([image1, image2], prompt, return_tensors="pt").to("cuda")
|
||||||
|
|
||||||
|
output = model.generate(**inputs, max_new_tokens=100)
|
||||||
|
print(processor.decode(output[0], skip_special_tokens=True))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## LlavaNextConfig
|
## LlavaNextConfig
|
||||||
|
|
||||||
[[autodoc]] LlavaNextConfig
|
[[autodoc]] LlavaNextConfig
|
||||||
|
Loading…
Reference in New Issue
Block a user