Views
No views yet

qkv (attention projections in vision transformer)q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj1pip install transformers accelerate peft pillow torch
2# For 4-bit quantization (optional but recommended):
3pip install bitsandbytes1from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
2from peft import PeftModel
3from PIL import Image
4import torch
5
6# Load base model with 4-bit quantization
7model = Qwen2VLForConditionalGeneration.from_pretrained(
8 "Qwen/Qwen2-VL-2B-Instruct",
9 device_map="auto",
10 trust_remote_code=True,
11 torch_dtype=torch.bfloat16
12)
13
14# Load LoRA adapter
15model = PeftModel.from_pretrained(model, "ahczhg/qwen3-vl-2b-pagoda-lora")
16model.eval()
17
18# Load processor
19processor = AutoProcessor.from_pretrained(
20 "ahczhg/qwen3-vl-2b-pagoda-lora",
21 trust_remote_code=True
22)
23
24# Load and process image
25image = Image.open("your_image.jpg")
26conversation = [
27 {
28 "role": "user",
29 "content": [
30 {"type": "image", "image": image},
31 {"type": "text", "text": "Describe this image in detail."}
32 ]
33 }
34]
35
36# Generate response
37text = processor.apply_chat_template(
38 conversation,
39 tokenize=False,
40 add_generation_prompt=True
41)
42inputs = processor(
43 text=[text],
44 images=[[image]],
45 return_tensors="pt"
46).to(model.device)
47
48with torch.no_grad():
49 output_ids = model.generate(
50 **inputs,
51 max_new_tokens=256,
52 do_sample=True,
53 temperature=0.7,
54 top_p=0.9
55 )
56
57response = processor.batch_decode(
58 output_ids,
59 skip_special_tokens=True,
60 clean_up_tokenization_spaces=False
61)[0]
62print(response)1from peft import PeftModel
2
3# Load and merge
4model = PeftModel.from_pretrained(base_model, "ahczhg/qwen3-vl-2b-pagoda-lora")
5merged_model = model.merge_and_unload()
6
7# Save merged model
8merged_model.save_pretrained("./merged_model")
9processor.save_pretrained("./merged_model")Input: [Image of a pagoda]
Prompt: "Describe this image in detail."
Output: [Model-generated description]1@misc{qwen2vl-pagoda-lora,
2 author = {Your Name},
3 title = {Qwen2-VL-2B Fine-tuned on Pagoda Dataset},
4 year = {2025},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/ahczhg/qwen3-vl-2b-pagoda-lora}}
7}