Views
No views yet

Idefics3-8B can be used to perform inference on multimodal (image + text) tasks in which the input is composed of a text query along with one (or multiple) image(s). Text and images can be arbitrarily interleaved. That includes image captioning, visual question answering, etc. These model does not support image generation.Idefics3-8B on a specific task, we provide a fine-tuning tutorial.
Other resources for the fine-tuning of Idefics2 (can easily be adapted to Idefics3):| Model | MMMU (val) | MathVista (test) | MMStar (val) | DocVQA (test) | TextVQA (val) |
|---|---|---|---|---|---|
| Idefics2-8B | 45.2 | 52.2 | 49.5 | 74.0 | 73.0 |
| Idefics3-8B | 46.6 | 58.4 | 55.9 | 87.7 | 74.9 |
Idefics3-8B.1import requests
2import torch
3from PIL import Image
4from io import BytesIO
5
6from transformers import AutoProcessor, AutoModelForVision2Seq
7from transformers.image_utils import load_image
8
9DEVICE = "cuda:0"
10
11# Note that passing the image urls (instead of the actual pil images) to the processor is also possible
12image1 = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
13image2 = load_image("https://cdn.britannica.com/59/94459-050-DBA42467/Skyline-Chicago.jpg")
14image3 = load_image("https://cdn.britannica.com/68/170868-050-8DDE8263/Golden-Gate-Bridge-San-Francisco.jpg")
15
16processor = AutoProcessor.from_pretrained("HuggingFaceM4/Idefics3-8B-Llama3")
17model = AutoModelForVision2Seq.from_pretrained(
18 "HuggingFaceM4/Idefics3-8B-Llama3", torch_dtype=torch.bfloat16
19).to(DEVICE)
20
21# Create inputs
22messages = [
23 {
24 "role": "user",
25 "content": [
26 {"type": "image"},
27 {"type": "text", "text": "What do we see in this image?"},
28 ]
29 },
30 {
31 "role": "assistant",
32 "content": [
33 {"type": "text", "text": "In this image, we can see the city of New York, and more specifically the Statue of Liberty."},
34 ]
35 },
36 {
37 "role": "user",
38 "content": [
39 {"type": "image"},
40 {"type": "text", "text": "And how about this image?"},
41 ]
42 },
43]
44prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
45inputs = processor(text=prompt, images=[image1, image2], return_tensors="pt")
46inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
47
48
49# Generate
50generated_ids = model.generate(**inputs, max_new_tokens=500)
51generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)
52
53print(generated_texts)torch.float16 or torch.bfloat16).1model = AutoModelForVision2Seq.from_pretrained(
2 "HuggingFaceM4/Idefics3-8B-Llama3",
3+ torch_dtype=torch.bfloat16,
4).to(DEVICE)size= {"longest_edge": N*364} when initializing the processor (AutoProcessor.from_pretrained), with N your desired value.
N=4 works best in practice (this is the default value), but for very large images, it could be interesting to pass N=5.
This will have an impact on the number of visual tokens passed to the language model.
If you are GPU-memory-constrained, you can decrease N, and choose for example N=3 or N=2, especially for low resolution images.flash-attn. Refer to the original repository of Flash Attention for the package installation. Simply change the snippet above with:1model = AutoModelForVision2Seq.from_pretrained(
2 "HuggingFaceM4/Idefics3-8B-Llama3",
3+ torch_dtype=torch.bfloat16,
4+ _attn_implementation="flash_attention_2",
5).to(DEVICE)1@misc{laurençon2024building,
2 title={Building and better understanding vision-language models: insights and future directions.},
3 author={Hugo Laurençon and Andrés Marafioti and Victor Sanh and Léo Tronchon},
4 year={2024},
5 eprint={2408.12637},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV}
8}