Views
No views yet
KeyError: 'qwen2_vl' or ImportError: cannot import name 'Qwen2VLForConditionalGeneration' from 'transformers', you can try building transformers from source with command pip install git+https://github.com/huggingface/transformers1from PIL import Image
2import requests
3import torch
4from torchvision import io
5from typing import Dict
6from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
7
8path = "natong19/Qwen2-VL-7B-Instruct-abliterated"
9
10# Load the model in half-precision on the available device(s)
11model = Qwen2VLForConditionalGeneration.from_pretrained(
12 path, torch_dtype="auto", device_map="auto"
13)
14
15min_pixels = 256*28*28
16max_pixels = 1280*28*28
17processor = AutoProcessor.from_pretrained(path, min_pixels=min_pixels, max_pixels=max_pixels)
18
19# Image
20url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
21image = Image.open(requests.get(url, stream=True).raw)
22
23conversation = [
24 {
25 "role": "user",
26 "content": [
27 {
28 "type": "image",
29 },
30 {"type": "text", "text": "Describe this image."},
31 ],
32 }
33]
34
35# Preprocess the inputs
36text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
37# Excepted output: '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Describe this image.<|im_end|>\n<|im_start|>assistant\n'
38
39inputs = processor(
40 text=[text_prompt], images=[image], padding=True, return_tensors="pt"
41)
42inputs = inputs.to("cuda")
43
44# Inference: Generation of the output
45output_ids = model.generate(**inputs, max_new_tokens=128)
46generated_ids = [
47 output_ids[len(input_ids) :]
48 for input_ids, output_ids in zip(inputs.input_ids, output_ids)
49]
50output_text = processor.batch_decode(
51 generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
52)
53print(output_text)| Datasets | Qwen2-VL-7B-Instruct | Qwen2-VL-7B-Instruct-abliterated |
|---|---|---|
| Text benchmarks | ||
| ARC (25-shot) | 57.8 | 57.8 |
| MMLU (5-shot) | 69.7 | 68.4 |
| TruthfulQA (0-shot) | 49.5 | 45.4 |
| Winogrande (5-shot) | 72.6 | 72.8 |
| Multimodal benchmarks | ||
| AI2D (lite) | 78.8 | 79.8 |
| GQA (lite) | 73.2 | 73.6 |
| MMBench (EN dev, lite) | 84.1 | 82.6 |
| MMMU (val) | 50.8 | 51.6 |
| OCRBench | 77.7 | 78.1 |
| VQAv2 (val, lite) | 79.9 | 79.8 |