-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDcode.py
More file actions
297 lines (219 loc) · 11.2 KB
/
Dcode.py
File metadata and controls
297 lines (219 loc) · 11.2 KB
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
"""
D-CoDe: Decomposed Chain-of-Thought for Video Understanding
Core utility functions for frame selection and question decomposition.
"""
import os
import sys
from pathlib import Path
sys.path.insert(0, Path(__file__).parent.as_posix())
sys.path.insert(0, os.path.join(Path(__file__).parent.as_posix(), "slowfast_llava"))
import re
import ast
import torch
import numpy as np
import openai
from transformers import CLIPProcessor, CLIPModel
# OpenAI API Key (read from environment variable)
API_KEY = os.environ.get("OPENAI_API_KEY", None)
def extract_list_from_text(text):
"""
Extract a Python list from text containing list format.
Args:
text: String containing a Python list
Returns:
Extracted list or empty list if not found
"""
match = re.search(r"\[.*\]", text, re.DOTALL)
if match:
return ast.literal_eval(match.group(0))
return []
def load_clip_model(model_name="openai/clip-vit-large-patch14-336"):
"""
Load CLIP model and processor for frame selection.
Args:
model_name: HuggingFace model name for CLIP
Returns:
clip_processor: CLIP processor
clip_model: CLIP model on CUDA
"""
clip_processor = CLIPProcessor.from_pretrained(model_name)
clip_model = CLIPModel.from_pretrained(model_name).cuda()
return clip_processor, clip_model
def generate_subquestions(question, prompt_variant="original", api_key=API_KEY):
assert api_key is not None, "You must provide a valid OpenAI API key."
prompts = {
"original": f"""
I am working on a video understanding task. Your job is to break down the given question into a series of subquestions that guide the model toward solving the problem. The subquestions should focus on temporal and dynamic aspects of the video, rather than just static information that could be answered from a single frame. I will provide a question, and you should output the corresponding subquestions in English.
Question: "{question}"
Output the subquestions as a Python list of strings.
""",
"no_background": f"""
Your job is to break down the given question into a series of subquestions that guide the model toward solving the problem. The subquestions should focus on temporal and dynamic aspects of the video, rather than just static information.
Question: "{question}"
Output the subquestions as a Python list of strings.
""",
"no_temporal_focus": f"""
I am working on a video understanding task. Your job is to break down the given question into subquestions that guide the model toward solving the problem.
Question: "{question}"
Output the subquestions as a Python list of strings.
""",
"re": f"""
You're assisting with a task that involves understanding events in videos over time. When given a question, your role is to transform it into several focused subquestions that explore how things change, move, or unfold throughout the video. Avoid questions that could be answered by looking at a single frame—prioritize those that require analyzing sequences, transitions, actions, or time-based dependencies.
I'll provide a main question about the video. Your output should be a list of specific, time-aware subquestions in English, formatted as a Python list of strings.
Main Question: “{question}”
Your Output (Python list of strings):
"""
}
if prompt_variant not in prompts:
raise ValueError(f"Invalid prompt_variant: {prompt_variant}")
prompt = prompts[prompt_variant]
client = openai.OpenAI(api_key=api_key)
response = client.chat.completions.create(
model="gpt-3.5-turbo", # or "gpt-4"
messages=[
{"role": "system", "content": "You are an expert in video understanding."},
{"role": "user", "content": prompt}
],
temperature=0.5
)
subquestions = response.choices[0].message.content
return subquestions.replace('%', '')
def supp_frame_selection(video_frames, N=15, uniform_ratio=0.85, clip_model=None, clip_processor=None):
"""
Supplementary frame selection based on semantic similarity.
Args:
video_frames: List of video frames
N: Number of frames to select
uniform_ratio: Ratio for uniform sampling (e.g., NextQA=16/0.85, IntentQA=20/0.75)
clip_model: Pre-loaded CLIP model (optional, will auto-load if None)
clip_processor: Pre-loaded CLIP processor (optional, will auto-load if None)
Returns:
selected_frames: List of selected frames
frame_idxs: Indices of selected frames
"""
# Load CLIP model if not provided
if clip_processor is None:
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14-336")
if clip_model is None:
clip_model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14-336").cuda()
# Extract frame features
with torch.no_grad():
CLS_list = []
for frame in video_frames:
clip_inputs = clip_processor(images=frame, return_tensors="pt").to('cuda')
outputs = clip_model.get_image_features(**clip_inputs)
CLS_list.append(outputs)
# Select representative frames
frame_idxs = _select_representative_frames(CLS_list, N=N, uniform_ratio=uniform_ratio)
selected_frames = [video_frames[i] for i in frame_idxs]
return selected_frames, frame_idxs
def _select_representative_frames(CLS_list, N, uniform_ratio=0.5):
"""
Internal function to select representative frames based on similarity.
Args:
CLS_list: List of CLIP features
N: Number of frames to select
uniform_ratio: Ratio for uniform sampling
Returns:
selected_indices: List of selected frame indices
"""
CLS_matrix = torch.cat(CLS_list, dim=0).cpu().numpy() # (num_frames, feature_dim)
# Normalize CLS vectors (prevent division by zero)
eps = 1e-8
norms = np.linalg.norm(CLS_matrix, axis=1, keepdims=True)
CLS_matrix = CLS_matrix / (norms + eps)
# Compute cosine similarity matrix
similarity_matrix = np.dot(CLS_matrix, CLS_matrix.T) # (num_frames, num_frames)
num_frames = len(CLS_list)
# Step 1: Uniformly select a portion of frames
num_uniform = int(N * uniform_ratio)
uniform_indices = np.linspace(0, num_frames - 1, num=num_uniform, dtype=int).tolist()
selected_indices = list(uniform_indices)
remaining_N = N - num_uniform
if remaining_N > 0:
# Step 2: Select remaining frames based on similarity
for _ in range(remaining_N):
min_sim_to_selected = np.full(num_frames, 1.0)
for idx in range(num_frames):
if idx in selected_indices:
continue
# Compute mean similarity to already selected frames
min_sim_to_selected[idx] = np.mean(similarity_matrix[idx, selected_indices])
# Select frame with minimum similarity to ensure diversity
next_index = np.argmin(min_sim_to_selected)
selected_indices.append(next_index)
selected_indices = sorted(selected_indices)
return selected_indices
# =============================================================================
# Token Selection and Merge Functions (for Vision Features)
# =============================================================================
def token_select_and_merge(image_features, top_k=288, merge_strategy="mean", similarity_threshold=0.8):
"""
Select and merge tokens from image features to reduce redundancy.
This function performs two main operations:
1. Select top-k tokens based on L2 activation values
2. Merge similar tokens to remove redundancy
Args:
image_features: Tensor of shape (T, N, D) where T=frames, N=tokens, D=dim
top_k: Maximum number of tokens to select per frame (default: 288 for 7B, 320 for 34B)
merge_strategy: Strategy for merging similar tokens ("mean", "max", "weighted_mean")
similarity_threshold: Cosine similarity threshold for considering tokens as duplicates
Returns:
merged_features: Tensor of shape (1, total_tokens, D) with selected and merged tokens
"""
T, N, D = image_features.shape
top_k = min(top_k, N)
selected_features_list = []
for frame_features in image_features:
# Compute L2 activation values
activations = torch.norm(frame_features, dim=-1)
# Select top-k tokens
_, topk_indices = torch.topk(activations, k=top_k, dim=-1)
selected_tokens = frame_features[topk_indices, :] # [top_k, D]
# Remove redundant tokens
selected_tokens, keep_indices = _merge_similar_tokens(
selected_tokens,
activations[topk_indices],
top_k,
merge_strategy,
similarity_threshold
)
selected_features_list.append(selected_tokens)
# Concatenate all selected tokens
merged_features = torch.cat(selected_features_list, dim=0).unsqueeze(0) # [1, total_tokens, D]
return merged_features
def _merge_similar_tokens(selected_tokens, activations, top_k, merge_strategy="mean", threshold=0.8):
"""
Internal function to merge similar tokens based on cosine similarity.
Args:
selected_tokens: Tensor of shape (top_k, D)
activations: Tensor of activation values for weighting
top_k: Number of tokens
merge_strategy: "mean", "max", or "weighted_mean"
threshold: Cosine similarity threshold
Returns:
merged_tokens: Tensor with merged tokens
keep_indices: Boolean tensor indicating which tokens to keep
"""
# Normalize tokens for cosine similarity
normalized_tokens = torch.nn.functional.normalize(selected_tokens, p=2, dim=-1)
cosine_sim_matrix = torch.mm(normalized_tokens, normalized_tokens.T) # [top_k, top_k]
# Find similar tokens
mask = cosine_sim_matrix >= threshold # [top_k, top_k]
keep_indices = torch.ones(top_k, dtype=torch.bool, device=selected_tokens.device)
for i in range(top_k):
if keep_indices[i]:
similar_indices = mask[i].nonzero(as_tuple=True)[0]
if len(similar_indices) > 1:
# Merge similar tokens based on strategy
if merge_strategy == "mean":
selected_tokens[i] = selected_tokens[similar_indices].mean(dim=0)
elif merge_strategy == "max":
selected_tokens[i] = selected_tokens[similar_indices].max(dim=0).values
elif merge_strategy == "weighted_mean":
weights = activations[similar_indices] / activations[similar_indices].sum()
selected_tokens[i] = torch.sum(weights[:, None] * selected_tokens[similar_indices], dim=0)
# Discard duplicate tokens (keep only the first one)
keep_indices[similar_indices[1:]] = False
merged_tokens = selected_tokens[keep_indices]
return merged_tokens, keep_indices