-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.py
357 lines (312 loc) · 13.9 KB
/
main.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
import json
import logging
import random
import re
from concurrent.futures import ThreadPoolExecutor
import numpy as np
from openai import OpenAI
from utils_clip import frame_retrieval_seg_ego
from utils_general import get_from_cache, save_to_cache
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler("egoschema_subset.log")
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s (line %(lineno)d)"
)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
client = OpenAI()
def parse_json(text):
try:
# First, try to directly parse the text as JSON
return json.loads(text)
except json.JSONDecodeError:
# If direct parsing fails, use regex to extract JSON
json_pattern = r"\{.*?\}|\[.*?\]" # Pattern for JSON objects and arrays
matches = re.findall(json_pattern, text, re.DOTALL)
for match in matches:
try:
match = match.replace("'", '"')
return json.loads(match)
except json.JSONDecodeError:
continue
# If no JSON structure is found
print("No valid JSON found in the text.")
return None
def parse_text_find_number(text):
item = parse_json(text)
try:
match = int(item["final_answer"])
if match in range(-1, 5):
return match
else:
return random.randint(0, 4)
except Exception as e:
logger.error(f"Answer Parsing Error: {e}")
return -1
def parse_text_find_confidence(text):
item = parse_json(text)
try:
match = int(item["confidence"])
if match in range(1, 4):
return match
else:
return random.randint(1, 3)
except Exception as e:
logger.error(f"Confidence Parsing Error: {e}")
return 1
def get_llm_response(
system_prompt, prompt, json_format=True, model="gpt-4-1106-preview"
):
messages = [
{
"role": "system",
"content": system_prompt,
},
{"role": "user", "content": prompt},
]
key = json.dumps([model, messages])
logger.info(messages)
cached_value = get_from_cache(key)
if cached_value is not None:
logger.info("Cache Hit")
logger.info(cached_value)
return cached_value
print("Not hit cache", key)
input()
for _ in range(3):
try:
if json_format:
completion = client.chat.completions.create(
model=model,
response_format={"type": "json_object"},
messages=messages,
)
else:
completion = client.chat.completions.create(
model=model, messages=messages
)
response = completion.choices[0].message.content
logger.info(response)
save_to_cache(key, response)
return response
except Exception as e:
logger.error(f"GPT Error: {e}")
continue
return "GPT Error"
def generate_final_answer(question, caption, num_frames):
answer_format = {"final_answer": "xxx"}
prompt = f"""
Given a video that has {num_frames} frames, the frames are decoded at 1 fps. Given the following descriptions of the sampled frames in the video:
{caption}
#C to denote the sentence is an action done by the camera wearer (the person who recorded the video while wearing a camera on their head).
#O to denote that the sentence is an action done by someone other than the camera wearer.
Please answer the following question:
```
{question}
```
Please think carefully and write the best answer index in Json format {answer_format}. Note that only one answer is returned for the question, and you must select one answer index from the candidates.
"""
system_prompt = "You are a helpful assistant designed to output JSON."
response = get_llm_response(system_prompt, prompt, json_format=True)
return response
def generate_description_step(question, caption, num_frames, segment_des):
formatted_description = {
"frame_descriptions": [
{"segment_id": "1", "duration": "xxx - xxx", "description": "frame of xxx"},
{"segment_id": "2", "duration": "xxx - xxx", "description": "frame of xxx"},
{"segment_id": "3", "duration": "xxx - xxx", "description": "frame of xxx"},
]
}
prompt = f"""
Given a video that has {num_frames} frames, the frames are decoded at 1 fps. Given the following descriptions of sampled frames in the video:
{caption}
#C to denote the sentence is an action done by the camera wearer (the person who recorded the video while wearing a camera on their head).
#O to denote that the sentence is an action done by someone other than the camera wearer.
To answer the following question:
```
{question}
```
However, the information in the initial frames is not suffient.
Objective:
Our goal is to identify additional frames that contain crucial information necessary for answering the question. These frames should not only address the query directly but should also complement the insights gleaned from the descriptions of the initial frames.
To achieve this, we will:
1. Divide the video into segments based on the intervals between the initial frames as, candiate segments: {segment_des}
2. Determine which segments are likely to contain frames that are most relevant to the question. These frames should capture key visual elements, such as objects, humans, interactions, actions, and scenes, that are supportive to answer the question.
For each frame identified as potentially relevant, provide a concise description focusing on essential visual elements. Use a single sentence per frame. If the specifics of a segment's visual content are uncertain based on the current information, use placeholders for specific actions or objects, but ensure the description still conveys the segment's relevance to the query.
Select multiple frames from one segment if necessary to gather comprehensive insights.
Return the descriptions and the segment id in JSON format, note "segment_id" must be smaller than {len(segment_des) + 1}, "duration" should be the same as candiate segments:
```
{formatted_description}
```
"""
system_prompt = "You are a helpful assistant designed to output JSON."
response = get_llm_response(system_prompt, prompt, json_format=True)
return response
def self_eval(previous_prompt, answer):
confidence_format = {"confidence": "xxx"}
prompt = f"""Please assess the confidence level in the decision-making process.
The provided information is as as follows,
{previous_prompt}
The decision making process is as follows,
{answer}
Criteria for Evaluation:
Insufficient Information (Confidence Level: 1): If information is too lacking for a reasonable conclusion.
Partial Information (Confidence Level: 2): If information partially supports an informed guess.
Sufficient Information (Confidence Level: 3): If information fully supports a well-informed decision.
Assessment Focus:
Evaluate based on the relevance, completeness, and clarity of the provided information in relation to the decision-making context.
Please generate the confidence with JSON format {confidence_format}
"""
system_prompt = "You are a helpful assistant designed to output JSON."
response = get_llm_response(system_prompt, prompt, json_format=True)
return response
def ask_gpt_caption(question, caption, num_frames):
answer_format = {"final_answer": "xxx"}
prompt = f"""
Given a video that has {num_frames} frames, the frames are decoded at 1 fps. Given the following descriptions of five uniformly sampled frames in the video:
{caption}
#C to denote the sentence is an action done by the camera wearer (the person who recorded the video while wearing a camera on their head).
#O to denote that the sentence is an action done by someone other than the camera wearer.
Please answer the following question:
```
{question}
```
Please think step-by-step and write the best answer index in Json format {answer_format}. Note that only one answer is returned for the question.
"""
system_prompt = "You are a helpful assistant."
response = get_llm_response(system_prompt, prompt, json_format=False)
return prompt, response
def ask_gpt_caption_step(question, caption, num_frames):
answer_format = {"final_answer": "xxx"}
prompt = f"""
Given a video that has {num_frames} frames, the frames are decoded at 1 fps. Given the following descriptions of the sampled frames in the video:
{caption}
#C to denote the sentence is an action done by the camera wearer (the person who recorded the video while wearing a camera on their head).
#O to denote that the sentence is an action done by someone other than the camera wearer.
Please answer the following question:
```
{question}
```
Please think step-by-step and write the best answer index in Json format {answer_format}. Note that only one answer is returned for the question.
"""
system_prompt = "You are a helpful assistant."
response = get_llm_response(system_prompt, prompt, json_format=False)
return prompt, response
def read_caption(captions, sample_idx):
video_caption = {}
for idx in sample_idx:
video_caption[f"frame {idx}"] = captions[idx - 1]
return video_caption
def run_one_question(video_id, ann, caps, logs):
question = ann["question"]
answers = [ann[f"option {i}"] for i in range(5)]
formatted_question = (
f"Here is the question: {question}\n"
+ "Here are the choices: "
+ " ".join([f"{i}. {ans}" for i, ans in enumerate(answers)])
)
num_frames = len(caps)
### Step 1 ###
sample_idx = np.linspace(1, num_frames, num=5, dtype=int).tolist()
sampled_caps = read_caption(caps, sample_idx)
previous_prompt, answer_str = ask_gpt_caption(
formatted_question, sampled_caps, num_frames
)
answer = parse_text_find_number(answer_str)
confidence_str = self_eval(previous_prompt, answer_str)
confidence = parse_text_find_confidence(confidence_str)
### Step 2 ###
if confidence < 3:
try:
segment_des = {
i + 1: f"{sample_idx[i]}-{sample_idx[i + 1]}"
for i in range(len(sample_idx) - 1)
}
candiate_descriptions = generate_description_step(
formatted_question,
sampled_caps,
num_frames,
segment_des,
)
parsed_candiate_descriptions = parse_json(candiate_descriptions)
frame_idx = frame_retrieval_seg_ego(
parsed_candiate_descriptions["frame_descriptions"], video_id, sample_idx
)
logger.info(f"Step 2: {frame_idx}")
sample_idx += frame_idx
sample_idx = sorted(list(set(sample_idx)))
sampled_caps = read_caption(caps, sample_idx)
previous_prompt, answer_str = ask_gpt_caption_step(
formatted_question, sampled_caps, num_frames
)
answer = parse_text_find_number(answer_str)
confidence_str = self_eval(previous_prompt, answer_str)
confidence = parse_text_find_confidence(confidence_str)
except Exception as e:
logger.error(f"Step 2 Error: {e}")
answer_str = generate_final_answer(
formatted_question, sampled_caps, num_frames
)
answer = parse_text_find_number(answer_str)
### Step 3 ###
if confidence < 3:
try:
segment_des = {
i + 1: f"{sample_idx[i]}-{sample_idx[i + 1]}"
for i in range(len(sample_idx) - 1)
}
candiate_descriptions = generate_description_step(
formatted_question,
sampled_caps,
num_frames,
segment_des,
)
parsed_candiate_descriptions = parse_json(candiate_descriptions)
frame_idx = frame_retrieval_seg_ego(
parsed_candiate_descriptions["frame_descriptions"], video_id, sample_idx
)
logger.info(f"Step 3: {frame_idx}")
sample_idx += frame_idx
sample_idx = sorted(list(set(sample_idx)))
sampled_caps = read_caption(caps, sample_idx)
answer_str = generate_final_answer(
formatted_question, sampled_caps, num_frames
)
answer = parse_text_find_number(answer_str)
except Exception as e:
logger.error(f"Step 3 Error: {e}")
answer_str = generate_final_answer(
formatted_question, sampled_caps, num_frames
)
answer = parse_text_find_number(answer_str)
if answer == -1:
logger.info("Answer Index Not Found!")
answer = random.randint(0, 4)
logger.info(f"Finished video: {video_id}/{answer}/{ann['truth']}")
label = int(ann["truth"])
corr = int(label == answer)
count_frame = len(sample_idx)
logs[video_id] = {
"answer": answer,
"label": label,
"corr": corr,
"count_frame": count_frame,
}
def main():
# if running full set, change subset to fullset
input_ann_file = "subset_anno.json"
all_cap_file = "lavila_subset.json"
json_file_name = "egoschema_subset.json"
anns = json.load(open(input_ann_file, "r"))
all_caps = json.load(open(all_cap_file, "r"))
logs = {}
tasks = [
(video_id, anns[video_id], all_caps[video_id], logs)
for video_id in list(anns.keys())
]
with ThreadPoolExecutor(max_workers=1) as executor:
executor.map(lambda p: run_one_question(*p), tasks)
json.dump(logs, open(json_file_name, "w"))
if __name__ == "__main__":
main()