Views
No views yet
Qwen/Qwen3-VL-8B-Instruct 进行Lora微调,旨在提供卓越的图像理解和描述生成能力。1from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
2# default: Load the model on the available device(s)
3model = Qwen3VLForConditionalGeneration.from_pretrained(
4 "thesby/Qwen3-VL-8B-NSFW-Caption-V4.5", dtype="auto", device_map="auto"
5)
6# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
7# model = Qwen3VLForConditionalGeneration.from_pretrained(
8# "thesby/Qwen3-VL-8B-NSFW-Caption-V4.5",
9# dtype=torch.bfloat16,
10# attn_implementation="flash_attention_2",
11# device_map="auto",
12# )
13processor = AutoProcessor.from_pretrained("thesby/Qwen3-VL-8B-NSFW-Caption-V4.5")
14messages = [
15 {
16 "role": "user",
17 "content": [
18 {
19 "type": "image",
20 "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
21 },
22 {"type": "text", "text": "请用自然流畅的中文对以下图片进行全面而详细的描述。包括所有可见元素及其属性(如颜色、大小、形状、质地),它们的空间关系,以及任何显著特征或上下文。确保用自然流畅的中文描述清晰、生动,能够捕捉图片的每一个方面,不遗漏任何重要细节。"},
23 ],
24 }
25]
26# Preparation for inference
27inputs = processor.apply_chat_template(
28 messages,
29 tokenize=True,
30 add_generation_prompt=True,
31 return_dict=True,
32 return_tensors="pt"
33)
34inputs = inputs.to(model.device)
35# Inference: Generation of the output
36generated_ids = model.generate(**inputs, max_new_tokens=2048)
37generated_ids_trimmed = [
38 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
39]
40output_text = processor.batch_decode(
41 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
42)
43print(output_text)Qwen/Qwen3-VL-8B-Instruct