-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference_qa.py
executable file
·1100 lines (955 loc) · 48.5 KB
/
inference_qa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import torch
from llamavid.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
from llamavid.conversation import conv_templates, SeparatorStyle
from llamavid.model.builder import load_pretrained_model
from llava.utils import disable_torch_init
from llava.mm_utils import tokenizer_image_token, get_model_name_from_path, KeywordsStoppingCriteria
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, \
LlamaConfig, LlamaModel, LlamaForCausalLM
import json
import os
import re
import ast
import math
from tqdm import tqdm
import pandas as pd
from decord import VideoReader, cpu
def split_list(lst, n):
"""Split a list into n (roughly) equal-sized chunks"""
chunk_size = math.ceil(len(lst) / n) # integer division
return [lst[i:i+chunk_size] for i in range(0, len(lst), chunk_size)]
def get_chunk(lst, n, k):
chunks = split_list(lst, n)
return chunks[k]
def parse_args():
"""
Parse command-line arguments.
"""
parser = argparse.ArgumentParser()
# Define the command-line arguments
parser.add_argument('--video_dir', help='Directory containing video files.', required=True)
parser.add_argument('--llama3', help='Path to the llama3 model file.', required=True)
parser.add_argument('--gt_file', help='Path to the ground truth file containing question.', required=True)
parser.add_argument('--output_dir', help='Directory to save the model results JSON.', required=True)
parser.add_argument('--output_name', help='Name of the file for storing results JSON.', required=True)
parser.add_argument("--model-path", type=str, default="facebook/opt-350m")
parser.add_argument("--model-base", type=str, default=None)
parser.add_argument("--data_set", type=str, default="msvd")
parser.add_argument("--conv-mode", type=str, default=None)
parser.add_argument("--num-chunks", type=int, default=1)
parser.add_argument("--chunk-idx", type=int, default=0)
parser.add_argument("--model-max-length", type=int, default=None)
parser.add_argument("--debug", type=bool, default=False)
return parser.parse_args()
def load_video(video_path):
vr = VideoReader(video_path, ctx=cpu(0))
total_frame_num = len(vr)
fps = round(vr.get_avg_fps())
frame_idx = [i for i in range(0, len(vr), fps)]
spare_frames = vr.get_batch(frame_idx).asnumpy()
return spare_frames
def prepare_caption(caption_list):
captions = []
for index, single_caption in enumerate(caption_list):
single_caption = str(index+1) + ":" + single_caption
captions.append(single_caption)
caption = " ".join(captions)
return caption
def prepare_prompt(tokenizer:AutoTokenizer, messages:list):
# tokens = []
complete_message = []
complete_message.append("<|begin_of_text|>")
for messgae in messages:
# tokens.append(tokenizer("<|start_header_id|>"))
# tokens.append()
complete_message.append("<|start_header_id|>")
complete_message.append(messgae["role"])
complete_message.append("<|end_header_id|>")
complete_message.append("\n\n")
complete_message.append(messgae["content"])
complete_message.append("<|eot_id|>")
# complete_message.append("<|eot_id|>")
# complete_message.append("<|start_header_id|>")
# complete_message.append("assistant")
# complete_message.append("<|end_header_id|>")
# complete_message.append("\n\n")
complete_message = " ".join(complete_message)
message_ids = tokenizer(complete_message)
# print(message_ids)
# message_token
# print(complete_message)
# assert 1==2
# message_ids = torch.tensor(message_ids, dtype=torch.long)
return complete_message, message_ids
def run_inference_video_chat(args):
"""
Run inference on VideoChat QA DataSet using the Video-ChatGPT model.
Args:
args: Command-line arguments.
"""
# Initialize the model
debug = args.debug
print("Loading LLaMA-VID model !!!")
model_name = get_model_name_from_path(args.model_path)
tokenizer, model, image_processor, context_len = load_pretrained_model(args.model_path, args.model_base, model_name, args.model_max_length)
print("LLaMA-VID load finish !!")
print("loading LLaMa3-8B for captioning !!")
llama_path = args.llama3
kwargs = {"device_map": "auto"}
kwargs['torch_dtype'] = torch.float16
llama_config = LlamaConfig.from_pretrained(llama_path)
llama_tokenizer = AutoTokenizer.from_pretrained(llama_path, use_fast=True)
llama_model = LlamaForCausalLM.from_pretrained(llama_path, low_cpu_mem_usage=True, config=llama_config, **kwargs)
llama_tokenizer.pad_token = llama_tokenizer.eos_token
print(llama_model.device)
print("LLaMa3-8B laod finish !!")
# llama_vid_prompt = [
# "What is the background of this movie?",
# "What is this movie talking about?",
# # "What is the main character of this movie?"
# ]
# Load both ground truth file containing questions and answers
with open(args.gt_file) as file:
gt_questions = json.load(file)
gt_questions = get_chunk(gt_questions, args.num_chunks, args.chunk_idx)
# Create the output directory if it doesn't exist
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
video_formats = ['.mp4', '.avi', '.mov', '.mkv']
if args.num_chunks > 1:
output_name = f"{args.num_chunks}_{args.chunk_idx}"
else:
output_name = args.output_name
answers_file = os.path.join(args.output_dir, f"{output_name}.json")
ans_file = open(answers_file, "w")
for sample in tqdm(gt_questions, desc="LLaMA-VID with LLaMA-3 in {}:".format(args.data_set)):
# video_name = sample['video_id']
# question = sample['question']
# id = sample['id']
# answer = sample['answer']
video_name = sample['video']
question = sample['question']
answer = sample['answer']
id = sample['question_id']
sample_set = {'id': id, 'question': question, 'answer': answer}
# Load the video file
for fmt in video_formats: # Added this line
temp_path = os.path.join(args.video_dir, f"{video_name}")
if os.path.exists(temp_path):
video_path = temp_path
break
# Check if the video exists
if os.path.exists(video_path):
video = load_video(video_path)
video = image_processor.preprocess(video, return_tensors='pt')['pixel_values'].half().cuda()
video = [video]
# try:
# Run inference on the video and add the output to the list
captions = []
# llama_vid_prompt.append(question)
llama_vid_prompt = [
# "What is the background of this movie?",
"What can you see from this video? What's the relationship between them?",
"Describe this video as much detail as possible."
# "What is this video talking about?",
# "What is the main character of this movie?"
]
llama_vid_prompt.append(question)
assert len(llama_vid_prompt) == 3
for pe_prompt in llama_vid_prompt:
qs = pe_prompt
if model.config.mm_use_im_start_end:
qs = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + '\n' + qs
else:
qs = DEFAULT_IMAGE_TOKEN + '\n' + qs
conv = conv_templates[args.conv_mode].copy()
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda()
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
keywords = [stop_str]
stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
cur_prompt = pe_prompt
with torch.inference_mode():
model.update_prompt([[cur_prompt]])
output_ids = model.generate(
input_ids,
images=video,
do_sample=True,
temperature=0.1,
max_new_tokens=1024,
use_cache=True,
stopping_criteria=[stopping_criteria])
input_token_len = input_ids.shape[1]
n_diff_input_output = (input_ids != output_ids[:, :input_token_len]).sum().item()
if n_diff_input_output > 0:
print(f'[Warning] {n_diff_input_output} output_ids are not the same as the input_ids')
outputs = tokenizer.batch_decode(output_ids[:, input_token_len:], skip_special_tokens=True)[0]
outputs = outputs.strip()
if outputs.endswith(stop_str):
outputs = outputs[:-len(stop_str)]
outputs = outputs.strip()
captions.append(outputs)
# print("llama-vid:{}".format(outputs))
caption = prepare_caption(captions)
# print("llama-vid caption:{}".format(caption))
llama3_prompt =[
{
"role": "system",
"content":
"You are an intelligent chatbot designed for generating safe, complete answers based on instructions provided by user."
# "These information come from another multi-modal chatbot, which needs to watch a video and get corresponding answers based on prompts with different attributes."
},
{
"role": "user",
"content":
"The video-based question and captin pair:\n"
f"Question: {question}\n"
f"Captions: {caption}\n"
"Please generate the answer of the question based on the information provided."
"Please generate the response in the form of a Python dictionary string with no keys, where the generated string is between '{' and '}'."
"DO NOT PROVIDE ANY OTHER OUTPUT TEXT OR EXPLANATION. Please make supplementary summaries in order to provide a richer and more comprehensive answer. Only provide the right answer string. "
"For example, your response should look like this: { YOUR ANSWER }."
# "Please generate the response in the form of a Python dictionary string with keys 'llama_pred', where value of 'llama_pred' is the strings generated."
# "DO NOT PROVIDE ANY OTHER OUTPUT TEXT OR EXPLANATION. Only provide the right answer string. "
# "For example, your response should look like this: {'llama_pred': 'YOUR ANSWER' }."
}
]
complete_message, message_ids = prepare_prompt(llama_tokenizer, llama3_prompt)
# print("llama3_messgae:{}".format(complete_message))
ids = torch.tensor(message_ids['input_ids'], dtype=torch.long).to(llama_model.device)
attention_mask = torch.tensor(message_ids['attention_mask']).to(llama_model.device).unsqueeze(0)
embeddings = llama_model.model.embed_tokens(ids).unsqueeze(0).to(dtype=torch.float16)
with torch.inference_mode():
output_ids = llama_model.generate(
inputs_embeds = embeddings,
attention_mask = attention_mask,
pad_token_id=llama_tokenizer.eos_token_id,
do_sample=True,
temperature=0.1,
# top_p=args.top_p,
max_new_tokens=512,
# use_cache=True
)
# print(answer)
out_text = llama_tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
# print("original out:{}".format(out_text))
match = re.search(r'\{(.*?)\}', out_text)
# 检查是否有匹配结果
if match:
extracted_text = match.group(1)
# matches = re.findall(r'\{.*?\}', out_text)
# for match in matches:
# result_dict = ast.literal_eval(match)
# print("llama3 answer:{}".format(result_dict['llama_pred']))
# if debug:
# print("llama3 prediction:{}".format(extracted_text))
sample_set['pred'] = extracted_text
sample_set['caption'] = caption
ans_file.write(json.dumps(sample_set) + "\n")
ans_file.flush()
ans_file.close()
def run_inference_intent(args):
"""
Run inference on Intent QA DataSet using our model.
Args:
args: Command-line arguments.
"""
# Initialize the model
debug = args.debug
print("Loading LLaMA-VID model !!!")
model_name = get_model_name_from_path(args.model_path)
tokenizer, model, image_processor, context_len = load_pretrained_model(args.model_path, args.model_base, model_name, args.model_max_length)
print("LLaMA-VID load finish !!")
print("loading LLaMa3-8B for captioning !!")
llama_path = args.llama3
kwargs = {"device_map": "auto"}
kwargs['torch_dtype'] = torch.float16
llama_config = LlamaConfig.from_pretrained(llama_path)
llama_tokenizer = AutoTokenizer.from_pretrained(llama_path, use_fast=True)
llama_model = LlamaForCausalLM.from_pretrained(llama_path, low_cpu_mem_usage=True, config=llama_config, **kwargs)
llama_tokenizer.pad_token = llama_tokenizer.eos_token
print(llama_model.device)
print("LLaMa3-8B laod finish !!")
# llama_vid_prompt = [
# "What is the background of this movie?",
# "What is this movie talking about?",
# # "What is the main character of this movie?"
# ]
# Load both ground truth file containing questions and answers
# with open("/13390024681/All_Data/nextqa/map_vid_vidorID.json") as file:
# map_file = json.load(file)
gt_questions = pd.read_csv(args.gt_file)
gt_questions = get_chunk(gt_questions, args.num_chunks, args.chunk_idx)
# Create the output directory if it doesn't exist
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
video_formats = ['.mp4', '.avi', '.mov', '.mkv']
if args.num_chunks > 1:
output_name = f"{args.num_chunks}_{args.chunk_idx}"
else:
output_name = args.output_name
answers_file = os.path.join(args.output_dir, f"{output_name}.json")
ans_file = open(answers_file, "w")
for sample in tqdm(gt_questions.iterrows(), desc="LLaMA-VID with LLaMA-3 in Intent-QA"):
# video_name = sample['video_id']
# question = sample['question']
# id = sample['id']
# answer = sample['answer']
# print(sample)
if isinstance(sample, tuple):
sample = sample[-1]
# print(sample)
video_name = str(sample['video'])
question, truth = sample['question'], sample['answer']
qid, q_type = sample['qid'], sample['type']
choices = [sample['a0'], sample['a1'], sample['a2'], sample['a3'], sample['a4']]
quid = f'{video_name}_{qid}'
# video_name = map_file[uid]
sample_set = {'id': quid, 'question': question, "truth":truth, "q_type":q_type}
# Load the video file
for fmt in video_formats: # Added this line
temp_path = os.path.join(args.video_dir, f"{video_name}.mp4")
if os.path.exists(temp_path):
video_path = temp_path
break
# Check if the video exists
if os.path.exists(video_path):
video = load_video(video_path)
video = image_processor.preprocess(video, return_tensors='pt')['pixel_values'].half().cuda()
video = [video]
# try:
# Run inference on the video and add the output to the list
captions = []
# llama_vid_prompt.append(question)
llama_vid_prompt = [
# "What is the background of this movie?",
"What can you see from this video? What's the relationship between them?",
"This video you see is mainly about a person or object doing something .Describe this video as much detail as possible."
# "What is this video talking about?",
# "What is the main character of this movie?"
]
llama_vid_prompt.append(question)
assert len(llama_vid_prompt) == 3
for pe_prompt in llama_vid_prompt:
qs = pe_prompt
if model.config.mm_use_im_start_end:
qs = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + '\n' + qs
else:
qs = DEFAULT_IMAGE_TOKEN + '\n' + qs
conv = conv_templates[args.conv_mode].copy()
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda()
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
keywords = [stop_str]
stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
cur_prompt = pe_prompt
with torch.inference_mode():
model.update_prompt([[cur_prompt]])
output_ids = model.generate(
input_ids,
images=video,
do_sample=True,
temperature=0.1,
max_new_tokens=1024,
use_cache=True,
stopping_criteria=[stopping_criteria])
input_token_len = input_ids.shape[1]
n_diff_input_output = (input_ids != output_ids[:, :input_token_len]).sum().item()
if n_diff_input_output > 0:
print(f'[Warning] {n_diff_input_output} output_ids are not the same as the input_ids')
outputs = tokenizer.batch_decode(output_ids[:, input_token_len:], skip_special_tokens=True)[0]
outputs = outputs.strip()
if outputs.endswith(stop_str):
outputs = outputs[:-len(stop_str)]
outputs = outputs.strip()
captions.append(outputs)
# print("llama-vid:{}".format(outputs))
caption = prepare_caption(captions)
# print("llama-vid caption:{}".format(caption))
llama3_prompt =[
{
"role": "system",
"content":
"You are an intelligent chatbot designed for generating safe, complete answers based on instructions provided by user."
# "These information come from another multi-modal chatbot, which needs to watch a video and get corresponding answers based on prompts with different attributes."
},
{
"role": "user",
"content":
"The video-based question and captin pair:\n"
f"Question: {question}\n"
f"Captions: {caption}\n"
"Here are the five answer choices for the question:"
f"0:{choices[0]};1:{choices[1]};2.{choices[2]};3.{choices[3]};4.{choices[4]}."
"Please choose the answer you think is correct among the above options in the form of a Python dictionary string with no keys, where the generated string is between '{' and '}'."
"DO NOT PROVIDE ANY OTHER OUTPUT TEXT OR EXPLANATION. You just need to generate the number corresponding to the correct option. "
"For example, if you think the right answer is option 3 and your response should look like this: { 3 }."
}
]
complete_message, message_ids = prepare_prompt(llama_tokenizer, llama3_prompt)
# print("llama3_messgae:{}".format(complete_message))
ids = torch.tensor(message_ids['input_ids'], dtype=torch.long).to(llama_model.device)
attention_mask = torch.tensor(message_ids['attention_mask']).to(llama_model.device).unsqueeze(0)
embeddings = llama_model.model.embed_tokens(ids).unsqueeze(0).to(dtype=torch.float16)
with torch.inference_mode():
output_ids = llama_model.generate(
inputs_embeds = embeddings,
attention_mask = attention_mask,
pad_token_id=llama_tokenizer.eos_token_id,
do_sample=True,
temperature=0.1,
# top_p=args.top_p,
max_new_tokens=100,
# use_cache=True
)
# print(answer)
out_text = llama_tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
# print("original out:{}".format(out_text))
# match = re.search(r'\{(.*?)\}', out_text)
match = re.search(r'{\s*(\d+)\s*}', out_text)
# 检查是否有匹配结果
if match:
extracted_text = int(match.group(1))
# print(extracted_text)
# matches = re.findall(r'\{.*?\}', out_text)
# for match in matches:
# result_dict = ast.literal_eval(match)
# print("llama3 answer:{}".format(result_dict['llama_pred']))
# if debug:
# print("llama3 prediction:{}".format(extracted_text))
sample_set['correct_answer'] = extracted_text
sample_set['caption'] = caption
ans_file.write(json.dumps(sample_set) + "\n")
ans_file.flush()
ans_file.close()
pass
def run_inference_next(args):
"""
Run inference on NExT QA DataSet using our model.
Args:
args: Command-line arguments.
"""
# Initialize the model
debug = args.debug
print("Loading LLaMA-VID model !!!")
model_name = get_model_name_from_path(args.model_path)
tokenizer, model, image_processor, context_len = load_pretrained_model(args.model_path, args.model_base, model_name, args.model_max_length)
print("LLaMA-VID load finish !!")
print("loading LLaMa3-8B for captioning !!")
llama_path = args.llama3
kwargs = {"device_map": "auto"}
kwargs['torch_dtype'] = torch.float16
llama_config = LlamaConfig.from_pretrained(llama_path)
llama_tokenizer = AutoTokenizer.from_pretrained(llama_path, use_fast=True)
llama_model = LlamaForCausalLM.from_pretrained(llama_path, low_cpu_mem_usage=True, config=llama_config, **kwargs)
llama_tokenizer.pad_token = llama_tokenizer.eos_token
print(llama_model.device)
print("LLaMa3-8B laod finish !!")
# llama_vid_prompt = [
# "What is the background of this movie?",
# "What is this movie talking about?",
# # "What is the main character of this movie?"
# ]
# Load both ground truth file containing questions and answers
with open("/13390024681/All_Data/nextqa/map_vid_vidorID.json") as file:
map_file = json.load(file)
gt_questions = pd.read_csv(args.gt_file)
gt_questions = get_chunk(gt_questions, args.num_chunks, args.chunk_idx)
# Create the output directory if it doesn't exist
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
video_formats = ['.mp4', '.avi', '.mov', '.mkv']
if args.num_chunks > 1:
output_name = f"{args.num_chunks}_{args.chunk_idx}"
else:
output_name = args.output_name
answers_file = os.path.join(args.output_dir, f"{output_name}.json")
ans_file = open(answers_file, "w")
for sample in tqdm(gt_questions.iterrows(), desc="LLaMA-VID with LLaMA-3 in NExT-QA"):
# video_name = sample['video_id']
# question = sample['question']
# id = sample['id']
# answer = sample['answer']
# print(sample)
if isinstance(sample, tuple):
sample = sample[-1]
# print(sample)
uid = str(sample['video'])
question, truth = sample['question'], sample['answer']
qid, q_type = sample['qid'], sample['type']
choices = [sample['a0'], sample['a1'], sample['a2'], sample['a3'], sample['a4']]
quid = f'{uid}_{qid}'
video_name = map_file[uid]
sample_set = {'id': quid, 'question': question, "truth":truth, "q_type":q_type}
# Load the video file
for fmt in video_formats: # Added this line
temp_path = os.path.join(args.video_dir, f"{video_name}.mp4")
if os.path.exists(temp_path):
video_path = temp_path
break
# Check if the video exists
if os.path.exists(video_path):
video = load_video(video_path)
video = image_processor.preprocess(video, return_tensors='pt')['pixel_values'].half().cuda()
video = [video]
# try:
# Run inference on the video and add the output to the list
captions = []
# llama_vid_prompt.append(question)
llama_vid_prompt = [
# "What is the background of this movie?",
"What can you see from this video? What's the relationship between them?",
"Describe this video as much detail as possible."
# "What is this video talking about?",
# "What is the main character of this movie?"
]
llama_vid_prompt.append(question)
assert len(llama_vid_prompt) == 3
for pe_prompt in llama_vid_prompt:
qs = pe_prompt
if model.config.mm_use_im_start_end:
qs = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + '\n' + qs
else:
qs = DEFAULT_IMAGE_TOKEN + '\n' + qs
conv = conv_templates[args.conv_mode].copy()
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda()
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
keywords = [stop_str]
stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
cur_prompt = pe_prompt
with torch.inference_mode():
model.update_prompt([[cur_prompt]])
output_ids = model.generate(
input_ids,
images=video,
do_sample=True,
temperature=0.1,
max_new_tokens=1024,
use_cache=True,
stopping_criteria=[stopping_criteria])
input_token_len = input_ids.shape[1]
n_diff_input_output = (input_ids != output_ids[:, :input_token_len]).sum().item()
if n_diff_input_output > 0:
print(f'[Warning] {n_diff_input_output} output_ids are not the same as the input_ids')
outputs = tokenizer.batch_decode(output_ids[:, input_token_len:], skip_special_tokens=True)[0]
outputs = outputs.strip()
if outputs.endswith(stop_str):
outputs = outputs[:-len(stop_str)]
outputs = outputs.strip()
captions.append(outputs)
# print("llama-vid:{}".format(outputs))
caption = prepare_caption(captions)
# print("llama-vid caption:{}".format(caption))
llama3_prompt =[
{
"role": "system",
"content":
"You are an intelligent chatbot designed for generating safe, complete answers based on instructions provided by user."
# "These information come from another multi-modal chatbot, which needs to watch a video and get corresponding answers based on prompts with different attributes."
},
{
"role": "user",
"content":
"The video-based question and captin pair:\n"
f"Question: {question}\n"
f"Captions: {caption}\n"
"Here are the five answer choices for the question:"
f"0:{choices[0]};1:{choices[1]};2.{choices[2]};3.{choices[3]};4.{choices[4]}."
"Please choose the answer you think is correct among the above options in the form of a Python dictionary string with no keys, where the generated string is between '{' and '}'."
"DO NOT PROVIDE ANY OTHER OUTPUT TEXT OR EXPLANATION. You just need to generate the number corresponding to the correct option. "
"For example, if you think the right answer is option 3 and your response should look like this: { 3 }."
}
]
complete_message, message_ids = prepare_prompt(llama_tokenizer, llama3_prompt)
# print("llama3_messgae:{}".format(complete_message))
ids = torch.tensor(message_ids['input_ids'], dtype=torch.long).to(llama_model.device)
attention_mask = torch.tensor(message_ids['attention_mask']).to(llama_model.device).unsqueeze(0)
embeddings = llama_model.model.embed_tokens(ids).unsqueeze(0).to(dtype=torch.float16)
with torch.inference_mode():
output_ids = llama_model.generate(
inputs_embeds = embeddings,
attention_mask = attention_mask,
pad_token_id=llama_tokenizer.eos_token_id,
do_sample=True,
temperature=0.1,
# top_p=args.top_p,
max_new_tokens=100,
# use_cache=True
)
# print(answer)
out_text = llama_tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
# print("original out:{}".format(out_text))
# match = re.search(r'\{(.*?)\}', out_text)
match = re.search(r'{\s*(\d+)\s*}', out_text)
# 检查是否有匹配结果
if match:
extracted_text = int(match.group(1))
# print(extracted_text)
# matches = re.findall(r'\{.*?\}', out_text)
# for match in matches:
# result_dict = ast.literal_eval(match)
# print("llama3 answer:{}".format(result_dict['llama_pred']))
# if debug:
# print("llama3 prediction:{}".format(extracted_text))
sample_set['correct_answer'] = extracted_text
sample_set['caption'] = caption
ans_file.write(json.dumps(sample_set) + "\n")
ans_file.flush()
ans_file.close()
def run_inference_ego(args):
"""
Run inference on EgoSchema QA DataSet using our model.
Args:
args: Command-line arguments.
"""
# Initialize the model
debug = args.debug
print("Loading LLaMA-VID model !!!")
model_name = get_model_name_from_path(args.model_path)
tokenizer, model, image_processor, context_len = load_pretrained_model(args.model_path, args.model_base, model_name, args.model_max_length)
print("LLaMA-VID load finish !!")
print("loading LLaMa3-8B for captioning !!")
llama_path = args.llama3
kwargs = {"device_map": "auto"}
kwargs['torch_dtype'] = torch.float16
llama_config = LlamaConfig.from_pretrained(llama_path)
llama_tokenizer = AutoTokenizer.from_pretrained(llama_path, use_fast=True)
llama_model = LlamaForCausalLM.from_pretrained(llama_path, low_cpu_mem_usage=True, config=llama_config, **kwargs)
llama_tokenizer.pad_token = llama_tokenizer.eos_token
print(llama_model.device)
print("LLaMa3-8B laod finish !!")
# llama_vid_prompt = [
# "What is the background of this movie?",
# "What is this movie talking about?",
# # "What is the main character of this movie?"
# ]
# Load both ground truth file containing questions and answers
with open(args.gt_file) as file:
gt_questions = json.load(file)
gt_questions = get_chunk(gt_questions, args.num_chunks, args.chunk_idx)
# Create the output directory if it doesn't exist
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
video_formats = ['.mp4', '.avi', '.mov', '.mkv']
if args.num_chunks > 1:
output_name = f"{args.num_chunks}_{args.chunk_idx}"
else:
output_name = args.output_name
answers_file = os.path.join(args.output_dir, f"{output_name}.json")
ans_file = open(answers_file, "w")
for sample in tqdm(gt_questions, desc="LLaMA-VID with LLaMA-3 in EgoSchema"):
# video_name = sample['video_id']
# question = sample['question']
# id = sample['id']
# answer = sample['answer']
video_name = sample['q_uid']
question = sample['question']
options = [sample['option 0'], sample['option 1'], sample['option 2'], sample['option 3'], sample['option 4']]
# answer = sample['answer']
# id = sample['question_id']
sample_set = {'id': video_name, 'question': question, 'options': options}
# Load the video file
for fmt in video_formats: # Added this line
temp_path = os.path.join(args.video_dir, f"{video_name}.mp4")
if os.path.exists(temp_path):
video_path = temp_path
break
# Check if the video exists
if os.path.exists(video_path):
video = load_video(video_path)
video = image_processor.preprocess(video, return_tensors='pt')['pixel_values'].half().cuda()
video = [video]
# try:
# Run inference on the video and add the output to the list
captions = []
# llama_vid_prompt.append(question)
llama_vid_prompt = [
# "What is the background of this movie?",
"This is a first-person perspective video. Can you describe what the person in the video is doing?",
"Based on this first-person perspective video, what do you think is the purpose of this person’s behavior in the video?"
# "What is this video talking about?",
# "What is the main character of this movie?"
]
llama_vid_prompt.append(question)
assert len(llama_vid_prompt) == 3
for pe_prompt in llama_vid_prompt:
qs = pe_prompt
if model.config.mm_use_im_start_end:
qs = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + '\n' + qs
else:
qs = DEFAULT_IMAGE_TOKEN + '\n' + qs
conv = conv_templates[args.conv_mode].copy()
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda()
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
keywords = [stop_str]
stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
cur_prompt = pe_prompt
with torch.inference_mode():
model.update_prompt([[cur_prompt]])
output_ids = model.generate(
input_ids,
images=video,
do_sample=True,
temperature=0.1,
max_new_tokens=1024,
use_cache=True,
stopping_criteria=[stopping_criteria])
input_token_len = input_ids.shape[1]
n_diff_input_output = (input_ids != output_ids[:, :input_token_len]).sum().item()
if n_diff_input_output > 0:
print(f'[Warning] {n_diff_input_output} output_ids are not the same as the input_ids')
outputs = tokenizer.batch_decode(output_ids[:, input_token_len:], skip_special_tokens=True)[0]
outputs = outputs.strip()
if outputs.endswith(stop_str):
outputs = outputs[:-len(stop_str)]
outputs = outputs.strip()
captions.append(outputs)
# print("llama-vid:{}".format(outputs))
caption = prepare_caption(captions)
# print("llama-vid caption:{}".format(caption))
llama3_prompt =[
{
"role": "system",
"content":
"You are an intelligent chatbot designed for generating safe, complete answers based on instructions provided by user."
# "These information come from another multi-modal chatbot, which needs to watch a video and get corresponding answers based on prompts with different attributes."
},
{
"role": "user",
"content":
"The video-based question and captin pair:\n"
f"Question: {question}\n"
f"Captions: {caption}\n"
"Here are the five answer options for the question:"
f"option 0:{options[0]}; option 1:{options[1]}; option 2:{options[2]}; option 3:{options[3]}; option 4:{options[4]}."
"Please choose the answer you think is correct among the above options in the form of a Python dictionary string with no keys, where the generated string is between '{' and '}'."
"DO NOT PROVIDE ANY OTHER OUTPUT TEXT OR EXPLANATION. You just need to generate the number corresponding to the correct option. "
"For example, if you think the right answer is option 0 and your response should look like this: { 0 }."
}
]
complete_message, message_ids = prepare_prompt(llama_tokenizer, llama3_prompt)
# print("llama3_messgae:{}".format(complete_message))
ids = torch.tensor(message_ids['input_ids'], dtype=torch.long).to(llama_model.device)
attention_mask = torch.tensor(message_ids['attention_mask']).to(llama_model.device).unsqueeze(0)
embeddings = llama_model.model.embed_tokens(ids).unsqueeze(0).to(dtype=torch.float16)
with torch.inference_mode():
output_ids = llama_model.generate(
inputs_embeds = embeddings,
attention_mask = attention_mask,
pad_token_id=llama_tokenizer.eos_token_id,
do_sample=True,
temperature=0.1,
# top_p=args.top_p,
max_new_tokens=100,
# use_cache=True
)
# print(answer)
out_text = llama_tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
print("original out:{}".format(out_text))
# match = re.search(r'\{(.*?)\}', out_text)
match = re.search(r'{\s*(\d+)\s*}', out_text) # etract number from {}
# 检查是否有匹配结果
if match:
extracted_text = int(match.group(1))
# print(extracted_text)
# matches = re.findall(r'\{.*?\}', out_text)
# for match in matches:
# result_dict = ast.literal_eval(match)
# print("llama3 answer:{}".format(result_dict['llama_pred']))
# if debug:
# print("llama3 prediction:{}".format(extracted_text))
sample_set['correct_answer'] = extracted_text
sample_set['caption'] = caption
ans_file.write(json.dumps(sample_set) + "\n")
ans_file.flush()
ans_file.close()
def run_inference(args):
"""
Run inference on ActivityNet QA DataSet using the Video-ChatGPT model.
Args:
args: Command-line arguments.
"""
# Initialize the model
debug = args.debug
print("Loading LLaMA-VID model !!!")
model_name = get_model_name_from_path(args.model_path)
tokenizer, model, image_processor, context_len = load_pretrained_model(args.model_path, args.model_base, model_name, args.model_max_length)
print("LLaMA-VID load finish !!")
print("loading LLaMa3-8B for captioning !!")
llama_path = args.llama3
kwargs = {"device_map": "auto"}
kwargs['torch_dtype'] = torch.float16
llama_config = LlamaConfig.from_pretrained(llama_path)
llama_tokenizer = AutoTokenizer.from_pretrained(llama_path, use_fast=True)
llama_model = LlamaForCausalLM.from_pretrained(llama_path, low_cpu_mem_usage=True, config=llama_config, **kwargs)
llama_tokenizer.pad_token = llama_tokenizer.eos_token
print(llama_model.device)
print("LLaMa3-8B laod finish !!")
# llama_vid_prompt = [
# "What is the background of this movie?",
# "What is this movie talking about?",
# # "What is the main character of this movie?"
# ]
# Load both ground truth file containing questions and answers
with open(args.gt_file) as file:
gt_questions = json.load(file)
gt_questions = get_chunk(gt_questions, args.num_chunks, args.chunk_idx)
# Create the output directory if it doesn't exist
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
video_formats = ['.mp4', '.avi', '.mov', '.mkv']
if args.num_chunks > 1:
output_name = f"{args.num_chunks}_{args.chunk_idx}"
else:
output_name = args.output_name
answers_file = os.path.join(args.output_dir, f"{output_name}.json")
ans_file = open(answers_file, "w")
for sample in tqdm(gt_questions, desc="LLaMA-VID with LLaMA-3 in {}:".format(args.data_set)):
# video_name = sample['video_id']
# question = sample['question']
# id = sample['id']
# answer = sample['answer']
video_name = sample['video']
question = sample['question']
answer = sample['answer']
id = sample['question_id']
sample_set = {'id': id, 'question': question, 'answer': answer}
# Load the video file
for fmt in video_formats: # Added this line
temp_path = os.path.join(args.video_dir, f"{video_name}")
if os.path.exists(temp_path):
video_path = temp_path
break
# Check if the video exists
if os.path.exists(video_path):
video = load_video(video_path)
video = image_processor.preprocess(video, return_tensors='pt')['pixel_values'].half().cuda()
video = [video]
# try:
# Run inference on the video and add the output to the list
captions = []
# llama_vid_prompt.append(question)
llama_vid_prompt = [
# "What is the background of this movie?",
"What can you see from this video? What's the relationship between them?",
"Describe this video as much detail as possible."
# "What is this video talking about?",
# "What is the main character of this movie?"
]
llama_vid_prompt.append(question)
assert len(llama_vid_prompt) == 3
for pe_prompt in llama_vid_prompt:
qs = pe_prompt
if model.config.mm_use_im_start_end:
qs = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + '\n' + qs
else:
qs = DEFAULT_IMAGE_TOKEN + '\n' + qs
conv = conv_templates[args.conv_mode].copy()
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda()
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
keywords = [stop_str]
stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
cur_prompt = pe_prompt
with torch.inference_mode():