Views
No views yet
KeyError: 'qwen2_vl' or ImportError: cannot import name 'Qwen2VLForConditionalGeneration' from 'transformers', try installing the latest version of the transformers library from source:pip install git+https://github.com/huggingface/transformers accelerate1import torch
2from PIL import Image
3from transformers import (
4 AutoModelForImageTextToText,
5 AutoProcessor
6)
7
8
9
10model_id = "Ertugrul/Qwen2.5-VL-7B-Captioner-Relaxed"
11image_path = "path/to/your/image.jpg"
12
13# the model requires more than 16GB of VRAM,
14# if you don't have you can use bitsandbytes to quantize the model to 8bit or 4bit
15
16
17model = AutoModelForImageTextToText.from_pretrained(
18 model_id,
19 device_map="auto",
20 torch_dtype=torch.bfloat16,
21 attn_implementation="flash_attention_2", # Use "flash_attention_2" when running on Ampere or newer GPU or use "eager" for older GPUs
22)
23
24
25#### For lower precision less than 12GB VRAM ####
26
27# Configure 4-bit quantization using BitsAndBytesConfig
28
29#from transformers import BitsAndBytesConfig
30
31# quantization_config = BitsAndBytesConfig(
32# load_in_4bit=True,
33# bnb_4bit_use_double_quant=True,
34# bnb_4bit_quant_type="nf4",
35# bnb_4bit_compute_dtype=torch.bfloat16,
36# bnb_4bit_quant_storage=torch.bfloat16,
37# )
38# model = AutoModelForImageTextToText.from_pretrained(
39# model_id,
40# device_map="auto",
41# torch_dtype=torch.bfloat16,
42# attn_implementation="flash_attention_2", # Use "flash_attention_2" when running on Ampere or newer GPU or use "eager" for older GPUs
43# quantization_config=quantization_config, # Use BitsAndBytesConfig instead of load_in_4bit
44# )
45
46########################################################################
47
48# you can change the min and max pixels to fit your needs to decrease compute cost to trade off quality
49min_pixels = 256*28*28
50max_pixels = 1280*28*28
51
52processor = AutoProcessor.from_pretrained(model_id, max_pixels=max_pixels, min_pixels=min_pixels)
53
54
55
56system_message = "You are an expert image describer."
57
58def generate_description(path, model, processor):
59 image_inputs = Image.open(path).convert("RGB")
60 messages = [
61 {
62 "role": "system",
63 "content": [{"type": "text", "text": system_message}],
64 },
65 {
66 "role": "user",
67 "content": [
68 {"type": "text", "text": "Describe this image."},
69 {"type": "image", "image": image_inputs},
70 ],
71 },
72 ]
73 text = processor.apply_chat_template(
74 messages, tokenize=False, add_generation_prompt=True
75 )
76
77 inputs = processor(
78 text=[text],
79 images=image_inputs,
80 padding=True,
81 return_tensors="pt",
82 )
83 inputs = inputs.to(model.device)
84
85 # min_p and temperature are experemental parameters, you can change them to fit your needs
86 generated_ids = model.generate(**inputs, max_new_tokens=512, min_p=0.1, do_sample=True, temperature=1.5)
87 generated_ids_trimmed = [out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]
88 output_text = processor.batch_decode(
89 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
90 )
91 return output_text[0]
92
93description = generate_description(image_path, model, processor)
94print(description)