69 lines
1.9 KiB
Python
69 lines
1.9 KiB
Python
|
|
import torch
|
|
from datasets import Dataset
|
|
from modelscope import snapshot_download, AutoTokenizer
|
|
from qwen_vl_utils import process_vision_info
|
|
from transformers import (
|
|
TrainingArguments,
|
|
Trainer,
|
|
DataCollatorForSeq2Seq,
|
|
Qwen2VLForConditionalGeneration,
|
|
AutoProcessor,
|
|
)
|
|
|
|
import json
|
|
|
|
|
|
# 在modelscope上下载Qwen2-VL模型到本地目录下
|
|
# model_dir = snapshot_download(
|
|
# model_id="Qwen/Qwen2-VL-2B-Instruct",
|
|
# cache_dir=model_path,
|
|
# revision="master"
|
|
# )
|
|
# print(f"模型已下载到: {model_dir}")
|
|
|
|
model_dir = "/root/PMN_WS/qwen-test/model/Qwen/Qwen2-VL-2B-Instruct"
|
|
# 使用Transformers加载模型权重
|
|
tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=False, trust_remote_code=True)
|
|
processor = AutoProcessor.from_pretrained(model_dir)
|
|
|
|
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
|
model_dir, torch_dtype="auto", device_map="auto"
|
|
)
|
|
|
|
messages = [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{
|
|
"type": "image",
|
|
"image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
|
|
},
|
|
{"type": "text", "text": "Describe this image."},
|
|
],
|
|
}
|
|
]
|
|
|
|
# Preparation for inference
|
|
text = processor.apply_chat_template(
|
|
messages, tokenize=False, add_generation_prompt=True
|
|
)
|
|
image_inputs, video_inputs = process_vision_info(messages)
|
|
inputs = processor(
|
|
text=[text],
|
|
images=image_inputs,
|
|
videos=video_inputs,
|
|
padding=True,
|
|
return_tensors="pt",
|
|
)
|
|
inputs = inputs.to("cuda")
|
|
|
|
# Inference: Generation of the output
|
|
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
|
generated_ids_trimmed = [
|
|
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
|
]
|
|
output_text = processor.batch_decode(
|
|
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
|
)
|
|
print(output_text) |