Wendy-Fly commited on
Commit
0fd5fe3
·
verified ·
1 Parent(s): efb81b8

Upload infer_f_.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. infer_f_.py +126 -0
infer_f_.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = 1
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_list = []
54
+ sd_ans = []
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": "text", "text": "Describe this video."},
67
+ ],
68
+ "answer":"None",
69
+ "result":"None",
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": "text", "text": "Describe this video."},
81
+ ],
82
+ }
83
+
84
+
85
+ video_path = i['videos']
86
+ question = i['messages'][0]['content']
87
+ answer = i['messages'][1]['content']
88
+ messages['content'][0]['video'] = video_path
89
+ messages['content'][1]['text'] = question
90
+
91
+ save_['content'][0]['video'] = video_path
92
+ save_['content'][1]['text'] = question
93
+ save_['answer'] = answer
94
+ sd_ans.append(answer)
95
+ data_list.append(messages)
96
+ save_list.append(save_)
97
+
98
+ text = processor.apply_chat_template(data_list, tokenize=False, add_generation_prompt=True)
99
+ image_inputs, video_inputs, video_kwargs = process_vision_info(data_list, return_video_kwargs=True)
100
+ fps = 1
101
+ inputs = processor(
102
+ text=[text],
103
+ images=image_inputs,
104
+ videos=video_inputs,
105
+ padding=True,
106
+ return_tensors="pt",
107
+ **video_kwargs,
108
+ )
109
+ inputs = inputs.to(model.device)
110
+
111
+ # Inference
112
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
113
+ generated_ids_trimmed = [
114
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
115
+ ]
116
+ output_text = processor.batch_decode(
117
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
118
+ )
119
+ for idx,x in enumerate(output_text):
120
+ save_list[idx]['result'] = x
121
+ save_data.append(save_list[idx])
122
+
123
+ print("correct_num", correct_num)
124
+ write_json("infer_finetune_percption.json",save_data)
125
+
126
+