|
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor |
|
from qwen_vl_utils import process_vision_info |
|
import json |
|
|
|
def read_json(file_path): |
|
with open(file_path, 'r', encoding='utf-8') as file: |
|
data = json.load(file) |
|
return data |
|
|
|
def write_json(file_path, data): |
|
with open(file_path, 'w', encoding='utf-8') as file: |
|
json.dump(data, file, ensure_ascii=False, indent=4) |
|
|
|
|
|
model_path = '/inspire/hdd/ws-ba572160-47f8-4ca1-984e-d6bcdeb95dbb/a100-maybe/albus/ICCV_2025/qvq/models/QVQ-72B-Preview' |
|
model = Qwen2VLForConditionalGeneration.from_pretrained( |
|
model_path, torch_dtype="auto", device_map="auto" |
|
) |
|
|
|
|
|
processor = AutoProcessor.from_pretrained(model_path) |
|
|
|
|
|
|
|
|
|
|
|
|
|
import glob |
|
from PIL import Image |
|
import argparse |
|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
folder = "/inspire/hdd/ws-ba572160-47f8-4ca1-984e-d6bcdeb95dbb/a100-maybe/albus/ICCV_2025/qvq/dataset" |
|
|
|
file_names = os.listdir(folder) |
|
|
|
num_image = len(file_names) |
|
|
|
begin, end, batch_size= 0, num_image, 6 |
|
print(f"beigin : {begin}, end : {end}, batch_size : {batch_size}") |
|
messages = [ |
|
{ |
|
"role": "system", |
|
"content": [ |
|
{"type": "text", "text": "You are a helpful and harmless assistant. You are Qwen developed by Alibaba. You should think step-by-step."} |
|
], |
|
}, |
|
{ |
|
"role": "user", |
|
"content": [ |
|
{ |
|
"type": "image", |
|
"image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/QVQ/demo.png", |
|
}, |
|
{"type": "text", "text": "Please describe in detail the content of the picture."}, |
|
], |
|
} |
|
] |
|
|
|
from tqdm import tqdm |
|
|
|
ans = [] |
|
counter = 0 |
|
for batch_idx in tqdm(range(begin, end, batch_size)): |
|
up = min(batch_idx + batch_size, end) |
|
batch = file_names[batch_idx: up] |
|
print(f"data index range : {batch_idx} ~ {up}") |
|
image_inputs_batch, video_inputs_batch,text_batch = [], [], [] |
|
for idx,i in enumerate(batch): |
|
|
|
|
|
img_path = '/inspire/hdd/ws-ba572160-47f8-4ca1-984e-d6bcdeb95dbb/a100-maybe/albus/ICCV_2025/qvq/dataset/' + i |
|
|
|
messages[1]["content"][0]["image"] = '/inspire/hdd/ws-ba572160-47f8-4ca1-984e-d6bcdeb95dbb/a100-maybe/albus/ICCV_2025/qvq/dataset/' + i |
|
text = processor.apply_chat_template( |
|
messages, tokenize=False, add_generation_prompt=True |
|
) |
|
text_batch.append(text) |
|
image_inputs, video_inputs = process_vision_info(messages) |
|
print(video_inputs) |
|
image_inputs_batch.append(image_inputs) |
|
video_inputs_batch.append(video_inputs) |
|
inputs = processor( |
|
text=text_batch, |
|
images=image_inputs_batch, |
|
videos=None, |
|
padding=True, |
|
return_tensors="pt", |
|
) |
|
inputs = inputs.to("cuda") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
generated_ids = model.generate(**inputs, max_new_tokens=8192) |
|
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 |
|
) |
|
ans.append(output_text) |
|
save_path = "output_final.json" |
|
counter = counter + 1 |
|
if counter % 10 == 0 or up + 10 >= end: |
|
print(f"Saving data at iteration {idx + 1}") |
|
write_json(save_path, ans) |
|
|
|
|
|
|