Wendy-Fly commited on
Commit
6bbac51
·
verified ·
1 Parent(s): 7bf67b9

Upload infer_3.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. infer_3.py +128 -0
infer_3.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import torch
4
+ from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
5
+ from qwen_vl_utils import process_vision_info
6
+ import json
7
+ from tqdm import tqdm
8
+ import os
9
+
10
+ def read_json(file_path):
11
+ with open(file_path, 'r', encoding='utf-8') as file:
12
+ data = json.load(file)
13
+ return data
14
+
15
+ def write_json(file_path, data):
16
+ with open(file_path, 'w', encoding='utf-8') as file:
17
+ json.dump(data, file, ensure_ascii=False, indent=4)
18
+
19
+ # default: Load the model on the available device(s)
20
+ print(torch.cuda.device_count())
21
+ model_path = "/home/zbz5349/WorkSpace/aigeeks/Qwen2.5-VL/LLaMA-Factory/output/Qwen2.5-VL-3B_all"
22
+ # model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
23
+ # model_path, torch_dtype="auto", device_map="auto"
24
+ # )
25
+
26
+ # We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
27
+ model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
28
+ model_path,
29
+ torch_dtype=torch.bfloat16,
30
+ attn_implementation="flash_attention_2",
31
+ device_map="auto",
32
+ )
33
+
34
+ # default processor
35
+ processor = AutoProcessor.from_pretrained(model_path)
36
+ print(model.device)
37
+
38
+
39
+
40
+
41
+ data = read_json('/home/zbz5349/WorkSpace/aigeeks/Qwen2.5-VL/LLaMA-Factory/data/Percption.json')
42
+ save_data = []
43
+ correct_num = 0
44
+ begin = 0
45
+ end = len(data)
46
+ batch_size = 2
47
+ for batch_idx in tqdm(range(begin, end, batch_size)):
48
+ batch = data[batch_idx:batch_idx + batch_size]
49
+
50
+ image_list = []
51
+ input_text_list = []
52
+ data_list = []
53
+ save_data = []
54
+
55
+ # while True:
56
+ for idx, i in enumerate(batch):
57
+ save_ = {
58
+ "role": "user",
59
+ "content": [
60
+ {
61
+ "type": "video",
62
+ "video": "file:///path/to/video1.mp4",
63
+ "max_pixels": 360 * 420,
64
+ "fps": 1.0,
65
+ },
66
+ {"type": "image", "image": "file:///path/to/image2.jpg"},
67
+ {"type": "text", "text": "Describe this video."},
68
+ ],
69
+ "answer":""
70
+ }
71
+ messages = {
72
+ "role": "user",
73
+ "content": [
74
+ {
75
+ "type": "video",
76
+ "video": "file:///path/to/video1.mp4",
77
+ "max_pixels": 360 * 420,
78
+ "fps": 1.0,
79
+ },
80
+ {"type": "image", "image": "file:///path/to/image2.jpg"},
81
+ {"type": "text", "text": "Describe this video."},
82
+ ],
83
+ }
84
+
85
+
86
+ video_path = i['videos']
87
+ image_path = i['images']
88
+ question = i['messages'][0]['content']
89
+ answer = i['messages'][1]['content']
90
+ messages['content'][0]['video'] = video_path
91
+ messages['content'][1]['image'] = image_path
92
+ messages['content'][2]['text'] = question
93
+
94
+ save_['content'][0]['video'] = video_path
95
+ save_['content'][1]['image'] = image_path
96
+ save_['content'][2]['text'] = question
97
+ data_list.append(messages)
98
+ save_data.append(save_)
99
+
100
+ text = processor.apply_chat_template(data_list, tokenize=False, add_generation_prompt=True)
101
+ image_inputs, video_inputs, video_kwargs = process_vision_info(data_list, return_video_kwargs=True)
102
+ fps = 1
103
+ inputs = processor(
104
+ text=[text],
105
+ images=image_inputs,
106
+ videos=video_inputs,
107
+ padding=True,
108
+ return_tensors="pt",
109
+ **video_kwargs,
110
+ )
111
+ inputs = inputs.to(model.device)
112
+
113
+ # Inference
114
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
115
+ generated_ids_trimmed = [
116
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
117
+ ]
118
+ output_text = processor.batch_decode(
119
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
120
+ )
121
+ save_["answer"] = output_text
122
+ if output_text == answer:
123
+ correct_num = correct_num + 1
124
+ save_data.append(save_)
125
+ print("correct_num", correct_num)
126
+ write_json(save_data, "infer_answer.json")
127
+
128
+