Wendy-Fly commited on
Commit
e04cc25
·
verified ·
1 Parent(s): d1a5569

Upload infer_1.py with huggingface_hub

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