Update README.md
Browse files
README.md
CHANGED
|
@@ -13,6 +13,76 @@ base_model:
|
|
| 13 |
|
| 14 |

|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
## NOTE
|
| 17 |
|
| 18 |
当前版本为一个实验版本,后续版本迭代进行中
|
|
|
|
| 13 |
|
| 14 |

|
| 15 |
|
| 16 |
+
## HOW TO USE
|
| 17 |
+
|
| 18 |
+
模型的使用方案与Qwen2-VL-2B-Instruct一致:https://huggingface.co/Qwen/Qwen2-VL-2B-Instruct#quickstart
|
| 19 |
+
|
| 20 |
+
```
|
| 21 |
+
# pip install qwen-vl-utils
|
| 22 |
+
|
| 23 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
| 24 |
+
from qwen_vl_utils import process_vision_info
|
| 25 |
+
|
| 26 |
+
# default: Load the model on the available device(s)
|
| 27 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 28 |
+
"Qwen/Qwen2-VL-2B-Instruct", torch_dtype="auto", device_map="auto"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
|
| 32 |
+
# model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 33 |
+
# "Qwen/Qwen2-VL-2B-Instruct",
|
| 34 |
+
# torch_dtype=torch.bfloat16,
|
| 35 |
+
# attn_implementation="flash_attention_2",
|
| 36 |
+
# device_map="auto",
|
| 37 |
+
# )
|
| 38 |
+
|
| 39 |
+
# default processer
|
| 40 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
|
| 41 |
+
|
| 42 |
+
# The default range for the number of visual tokens per image in the model is 4-16384. You can set min_pixels and max_pixels according to your needs, such as a token count range of 256-1280, to balance speed and memory usage.
|
| 43 |
+
# min_pixels = 256*28*28
|
| 44 |
+
# max_pixels = 1280*28*28
|
| 45 |
+
# processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
|
| 46 |
+
|
| 47 |
+
messages = [
|
| 48 |
+
{
|
| 49 |
+
"role": "user",
|
| 50 |
+
"content": [
|
| 51 |
+
{
|
| 52 |
+
"type": "image",
|
| 53 |
+
"image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
|
| 54 |
+
},
|
| 55 |
+
{"type": "text", "text": "Describe this image."},
|
| 56 |
+
],
|
| 57 |
+
}
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
# Preparation for inference
|
| 61 |
+
text = processor.apply_chat_template(
|
| 62 |
+
messages, tokenize=False, add_generation_prompt=True
|
| 63 |
+
)
|
| 64 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
| 65 |
+
inputs = processor(
|
| 66 |
+
text=[text],
|
| 67 |
+
images=image_inputs,
|
| 68 |
+
videos=video_inputs,
|
| 69 |
+
padding=True,
|
| 70 |
+
return_tensors="pt",
|
| 71 |
+
)
|
| 72 |
+
inputs = inputs.to("cuda")
|
| 73 |
+
|
| 74 |
+
# Inference: Generation of the output
|
| 75 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
| 76 |
+
generated_ids_trimmed = [
|
| 77 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 78 |
+
]
|
| 79 |
+
output_text = processor.batch_decode(
|
| 80 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 81 |
+
)
|
| 82 |
+
print(output_text)
|
| 83 |
+
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
## NOTE
|
| 87 |
|
| 88 |
当前版本为一个实验版本,后续版本迭代进行中
|