forked from Vision-CAIR/MiniGPT4-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_video.py
214 lines (200 loc) · 11.2 KB
/
eval_video.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
import os
import json
from tqdm import tqdm
from torch.utils.data import DataLoader
from minigpt4.common.eval_utils import prepare_texts, init_model, eval_parser
from minigpt4.conversation.conversation import CONV_VISION
from minigpt4.processors.blip_processors import Blip2ImageTrainProcessor,BlipCaptionProcessor
from minigpt4.datasets.datasets.video_datasets import VideoChatGPTEvalDataset,VideoChatGPTEval_consistancy,Video_validation_Dataset,TVQAEVAL
parser = eval_parser()
parser.add_argument("--dataset", type=str, default='msvd', help="dataset to evaluate")
parser.add_argument("--add_subtitles",action='store_true',help="whether to add subtitles to the video")
parser.add_argument("--name", type=str, default='3_datasets', help="evaluation name")
parser.add_argument("--batch_size", type=int, default=1, help="batch size")
parser.add_argument("--start", type=int, default=0, help="start from video number")
parser.add_argument("--end", type=int, default=10000000, help="end at video number")
args = parser.parse_args()
print(args.ckpt)
print(args.name)
print(args.cfg_path)
if "test_configs/mistral_test_config.yaml" == args.cfg_path:
llm_name="mistral"
else:
llm_name="llama2"
print("using captions",args.add_subtitles)
model, vis_processor = init_model(args)
conv_temp = CONV_VISION.copy()
conv_temp.system = ""
if args.dataset == 'video_chatgpt_generic':
ann_path="datasets/evaluation_datasets/videochatgpt_benchmark/generic_qa.json"
videos_path= "videos path"
subtitles_path="whisper_generated_subtitles"
annotations_keys=['Q','A','video_name']
data = VideoChatGPTEvalDataset(vis_processor, videos_path, ann_path,subtitles_path,annotations_keys, add_subtitles=args.add_subtitles,llm_name=llm_name)
elif args.dataset == 'video_chatgpt_temporal':
ann_path="datasets/evaluation_datasets/videochatgpt_benchmark/temporal_qa.json"
videos_path= "videos path"
subtitles_path="whisper_generated_subtitles"
annotations_keys=['Q','A','video_name']
data = VideoChatGPTEvalDataset(vis_processor, videos_path, ann_path,subtitles_path,annotations_keys, add_subtitles=args.add_subtitles,llm_name=llm_name)
elif args.dataset == 'video_chatgpt_consistency':
ann_path="datasets/evaluation_datasets/videochatgpt_benchmark/consistency_qa.json"
videos_path= "videos path"
subtitles_path="whisper_generated_subtitles"
annotations_keys=[['Q1','Q2'],'A','video_name']
data = VideoChatGPTEval_consistancy(vis_processor, videos_path, ann_path,subtitles_path,annotations_keys, add_subtitles=args.add_subtitles,llm_name=llm_name)
elif args.dataset == 'msrvtt':
ann_path="datasets/evaluation_datasets/msrvtt/val_qa_edited.json"
videos_path= "videos path"
subtitles_path="whisper_generated_subtitles"
annotations_keys=['question','answer','video_id']
data = VideoChatGPTEvalDataset(vis_processor, videos_path, ann_path,subtitles_path,annotations_keys, add_subtitles=args.add_subtitles,llm_name=llm_name)
elif args.dataset == 'msvd':
ann_path="datasets/evaluation_datasets/msvd/val_qa_edited.json"
videos_path= "videos path"
subtitles_path="" # no subtitles for msvd as these videos don't have audio
annotations_keys=['question','answer','video_id']
data = VideoChatGPTEvalDataset(vis_processor, videos_path, ann_path,subtitles_path,annotations_keys, add_subtitles=args.add_subtitles,llm_name=llm_name)
elif args.dataset == 'activitynet':
ann_path="datasets/evaluation_datasets/activityNet/test_qa.json"
videos_path= "videos path"
subtitles_path="whisper_generated_subtitles"
annotations_keys=['question','answer','video_id']
data = VideoChatGPTEvalDataset(vis_processor, videos_path, ann_path,subtitles_path,annotations_keys, add_subtitles=args.add_subtitles,llm_name=llm_name)
elif args.dataset == 'tgif':
ann_path="datasets/evaluation_datasets/tgif/Test_frameqa_question.json"
videos_path= "videos path"
subtitles_path="" # no subtitles for TGIF as these videos don't have audio
annotations_keys=['question','answer','gif_name']
# annotations_keys=['question','description','gif_name']
data = VideoChatGPTEvalDataset(vis_processor, videos_path, ann_path,subtitles_path,annotations_keys, add_subtitles=False,llm_name=llm_name)
elif args.dataset == 'tvqa':
# TVQA dataset
ann_path="datasets/evaluation_datasets/tvqa_short/tvqa_val.json"
videos_path= "videos path"
subtitles_path="tvqa subtitles"
data = TVQAEVAL(vis_processor, videos_path, ann_path,subtitles_path,add_subtitles=args.add_subtitles,llm_name=llm_name)
eval_dataloader = DataLoader(data, batch_size=args.batch_size, shuffle=False)
minigpt4_predict = []
sub="subtitles" if args.add_subtitles else "no_subtitles"
if args.start == 0 and args.end == 10000000:
save_path = f'results/{args.name}_{args.dataset}_{sub}.json'
else:
print("start from video number",args.start)
print("end at video number",args.end)
save_path = f'results/{args.name}_{args.dataset}_{sub}_{args.start}_{args.end}.json'
os.makedirs("results", exist_ok=True)
c=0
pred_result = {}
gt_result = {}
if args.dataset == 'video_chatgpt_consistency':
for images, texts_1,texts_2, gt_answers, lengths,videos_ids in tqdm(eval_dataloader,desc=f"Eval {args.dataset}"):
if args.start<= c <args.end :
texts_q1 = prepare_texts(texts_1, conv_temp, template='', lengths=lengths) # warp the texts with conversation template
texts_q2 = prepare_texts(texts_2, conv_temp, template='', lengths=lengths) # warp the texts with conversation template
models_answers_q1 = model.generate(images, texts_q1, max_new_tokens=args.max_new_tokens, do_sample=False, lengths=lengths,num_beams=1)
models_answers_q2 = model.generate(images, texts_q2, max_new_tokens=args.max_new_tokens, do_sample=False, lengths=lengths,num_beams=1)
for video_id,model_answer_q1,model_answer_q2, gt_answer,text_q1,text_q2 in zip(videos_ids,models_answers_q1,models_answers_q2, gt_answers,texts_q1,texts_q2):
result = dict()
result['video_name'] = video_id
result['Q1'] = text_q1.split('\n')[-1].replace('[/INST]','')
result['Q2'] = text_q2.split('\n')[-1].replace('[/INST]','')
result['A'] = gt_answer
result['pred1'] = model_answer_q1
result['pred2'] = model_answer_q2
pred_result[video_id] = [model_answer_q1,model_answer_q2]
gt_result[video_id] = [gt_answer]
minigpt4_predict.append(result)
# save results every 100 videos to avoid losing results
if c%100==0:
with open(save_path, 'w') as f:
json.dump(minigpt4_predict, f)
if c >= args.end :
break
c+=1
elif args.dataset == 'tvr':
for images, texts, gt_answers, lengths,videos_ids in tqdm(eval_dataloader,desc=f"Eval {args.dataset}"):
if args.start<= c <args.end :
texts = prepare_texts(texts, conv_temp, template='', lengths=lengths) # warp the texts with conversation template
models_answers = model.generate(images, texts, max_new_tokens=args.max_new_tokens, do_sample=False, lengths=lengths,num_beams=1)
for video_id,model_answer, gt_answer,text in zip(videos_ids,models_answers, gt_answers,texts):
result = dict()
result['video_name'] = video_id
result['Q'] = text.split('\n')[-1].replace('[/INST]','')
result['A'] = gt_answer
result['pred'] = model_answer
pred_result[video_id] = [model_answer]
gt_result[video_id] = [gt_answer]
minigpt4_predict.append(result)
# save results every 100 videos to avoid losing results
if c%100==0:
with open(save_path, 'w') as f:
json.dump(minigpt4_predict, f)
if c >= args.end :
break
c+=1
elif args.dataset == 'ego_schema' or args.dataset == 'tvqa' or args.dataset == 'tvqa_long_videos':
for images, texts, gt_answers, lengths,videos_ids in tqdm(eval_dataloader,desc=f"Eval {args.dataset}"):
if args.start<= c <args.end :
texts = prepare_texts(texts, conv_temp, template='', lengths=lengths) # warp the texts with conversation template
models_answers = model.generate(images, texts, max_new_tokens=args.max_new_tokens, do_sample=False, lengths=lengths,num_beams=1)
for video_id,model_answer, gt_answer,text in zip(videos_ids,models_answers, gt_answers,texts):
result = dict()
result['video_name'] = video_id
if args.dataset == 'tvqa_long_videos':
result['Q'] = text.split('\n\n')[1:]
else:
result['Q'] = text.split('\n')[1:]
result['A'] = gt_answer
result['pred'] = model_answer
pred_result[video_id] = [model_answer]
gt_result[video_id] = [gt_answer]
minigpt4_predict.append(result)
# save results every 100 videos to avoid losing results
if c%100==0:
with open(save_path, 'w') as f:
json.dump(minigpt4_predict, f)
if c >= args.end :
break
c+=1
else:
for images, texts, gt_answers, lengths,videos_ids in tqdm(eval_dataloader,desc=f"Eval {args.dataset}"):
if args.start<= c <args.end :
texts = prepare_texts(texts, conv_temp, template='', lengths=lengths) # warp the texts with conversation template
models_answers = model.generate(images, texts, max_new_tokens=args.max_new_tokens, do_sample=False, lengths=lengths,num_beams=1)
for video_id,model_answer, gt_answer,text in zip(videos_ids,models_answers, gt_answers,texts):
result = dict()
result['video_name'] = video_id
result['Q'] = text.split('\n')[-1].replace('[/INST]','')
result['A'] = gt_answer
result['pred'] = model_answer
pred_result[video_id] = [model_answer]
gt_result[video_id] = [gt_answer]
minigpt4_predict.append(result)
# save results every 100 videos to avoid losing results
if c%100==0:
with open(save_path, 'w') as f:
json.dump(minigpt4_predict, f)
if c >= args.end :
break
c+=1
with open(save_path, 'w') as f:
json.dump(minigpt4_predict, f)
print("saved results to",save_path)
# save results
# bleu_save_path = f'results/{args.name}_{args.dataset}_bleu.json'
# cider_save_path = f'results/{args.name}_{args.dataset}_cider.json'
# chatgpt_eval_save_path = f'results/{args.name}_{args.dataset}_chatgpt_eval.json'
# bleu_results=eval_bleu(minigpt4_predict)
# with open(bleu_save_path, 'w') as f:
# json.dump(bleu_results, f)
# print("bleu_results",bleu_results)
# cider_results=eval_cider(pred_result,gt_result)
# with open(cider_save_path, 'w') as f:
# json.dump(cider_results, f)
# print("mean_cider_scores:",cider_results['mean_cider_scores'])
# chatgpt_results=chat_gpt_eval(pred_result,gt_result)
# with open(chatgpt_eval_save_path, 'w') as f:
# json.dump(chatgpt_results, f)
# print("avg_chatgpt_score",chatgpt_results['avg_chatgpt_score'])
# print(chatgpt_results)