Skip to content

Commit 0c60559

Browse files
committed
fix vjepa code
1 parent 4581410 commit 0c60559

8 files changed

Lines changed: 19 additions & 14 deletions

File tree

src/sailsprep/action_model_testing/vjepa/clips_fixed_length/vjepa_clip_level_ablation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ def _load_clip(self, s):
235235
total_vid = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
236236

237237
bbox_map = load_bbox_map(s["h5_path"])
238+
if not bbox_map:
239+
cap.release()
240+
raise ValueError("empty bbox map")
238241
bbox_keys = np.array(sorted(bbox_map.keys()))
239242

240243
ann_frames = np.arange(s["start_frame"], s["end_frame"] + 1)
@@ -423,8 +426,7 @@ def extract_features(encoder, samples, label_map, device, batch_size=4):
423426
all_labels.append(labels)
424427
except Exception as e:
425428
print(f" batch {i} error: {e}")
426-
all_feats.append(torch.zeros(clips.shape[0], 1, EMBED_DIM))
427-
all_labels.append(labels)
429+
raise
428430

429431
return torch.cat(all_feats, 0), torch.cat(all_labels, 0)
430432

src/sailsprep/action_model_testing/vjepa/clips_without_coi_crop/extract_features.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def extract_all_features(model, processor, video_paths, labels, device):
121121
all_labels.append(batch_labels)
122122
except Exception as e:
123123
print(f" Error at batch {batch_idx}: {e}")
124-
errors.append(batch_idx)
124+
raise
125125

126126
all_features = torch.cat(all_features, dim=0)
127127
all_labels = torch.cat(
@@ -152,9 +152,10 @@ def main():
152152

153153
# Load encoder
154154
processor = AutoVideoProcessor.from_pretrained(HF_MODEL_NAME)
155+
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
155156
encoder = AutoModel.from_pretrained(
156157
HF_MODEL_NAME,
157-
torch_dtype=torch.float16,
158+
torch_dtype=dtype,
158159
attn_implementation="sdpa",
159160
)
160161
encoder.eval()

src/sailsprep/action_model_testing/vjepa/clips_without_coi_crop/rmm/extract_features.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def extract_all_features(model, processor, video_paths, labels, device):
124124
all_labels.append(batch_labels)
125125
except Exception as e:
126126
print(f" Error at batch {batch_idx}: {e}", flush=True)
127-
errors.append(batch_idx)
127+
raise
128128

129129
all_features = torch.cat(all_features, dim=0)
130130
all_labels = torch.cat(
@@ -155,9 +155,10 @@ def main():
155155

156156
# Load encoder
157157
processor = AutoVideoProcessor.from_pretrained(HF_MODEL_NAME)
158+
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
158159
encoder = AutoModel.from_pretrained(
159160
HF_MODEL_NAME,
160-
torch_dtype=torch.float16,
161+
torch_dtype=dtype,
161162
attn_implementation="sdpa",
162163
)
163164
encoder.eval()

src/sailsprep/action_model_testing/vjepa/coi_crop/finetune_vjepa2_h5bbox.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,9 @@ def __init__(self, num_classes=NUM_CLASSES, full_finetune=False, class_weights=N
202202
self.save_hyperparameters(ignore=["class_weights"])
203203
self.full_finetune = full_finetune
204204

205+
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
205206
self.encoder = AutoModel.from_pretrained(
206-
MODEL_NAME, torch_dtype=torch.float16, attn_implementation="sdpa",
207+
MODEL_NAME, torch_dtype=dtype, attn_implementation="sdpa",
207208
)
208209
if not full_finetune:
209210
for p in self.encoder.parameters(): p.requires_grad = False
@@ -230,8 +231,8 @@ def forward(self, inputs):
230231

231232
def _step(self, batch, stage):
232233
inputs, labels = batch
233-
# Cast float inputs to half to match encoder
234-
inputs = {k: (v.half() if v.is_floating_point() else v) for k, v in inputs.items()}
234+
# Cast float inputs to match encoder dtype
235+
inputs = {k: (v.to(self.encoder.dtype) if v.is_floating_point() else v) for k, v in inputs.items()}
235236
logits = self(inputs)
236237
loss = F.cross_entropy(logits, labels,
237238
weight=self.class_weights if stage == "train" else None)
@@ -324,7 +325,7 @@ def run_inference(model, test_samples, label_map, processor, device):
324325
try:
325326
frames, label = ds[i]
326327
inputs = processor([frames], return_tensors="pt")
327-
inputs = {k: (v.half().to(device) if v.is_floating_point() else v.to(device))
328+
inputs = {k: (v.to(device=device, dtype=model.encoder.dtype) if v.is_floating_point() else v.to(device))
328329
for k, v in inputs.items()}
329330
with torch.no_grad():
330331
logits = model(inputs)

src/sailsprep/action_model_testing/vjepa/full_video/rerun_window_inference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
SPLITS_CSV = "/home/aparnabg/orcd/scratch/latest_split_csv_new.csv"
3030
BASE_DIR = "/orcd/data/satra/002/projects/SAILS/vjepa_features/models_output_seeds/vjepa/"
3131

32-
EMBED_DIM = 1408
32+
EMBED_DIM = 1024
3333
WINDOW_FRAMES = 30
3434
STRIDE_FRAMES = 15
3535
BATCH_SIZE = 256

src/sailsprep/action_model_testing/vjepa/full_video/train_probe_framelevel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
SPLITS_CSV = "/home/aparnabg/orcd/scratch/latest_split_csv_new.csv"
2929
OUTPUT_BASE = "/orcd/data/satra/002/projects/SAILS/vjepa_features/models_output_seeds/vjepa/framelevel_onest/"
3030

31-
EMBED_DIM = 1408
31+
EMBED_DIM = 1024
3232
CONTEXT = 5 # ±5 frames → window of 11
3333
WINDOW_SIZE = 2 * CONTEXT + 1 # 11
3434
BATCH_SIZE = 256 # larger batch fine — frames are cheap

src/sailsprep/action_model_testing/vjepa/full_video/train_probe_framelevel_hierarchical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
SPLITS_CSV = "/home/aparnabg/orcd/scratch/latest_split_csv_new.csv"
3131
OUTPUT_BASE = "/orcd/data/satra/002/projects/SAILS/vjepa_features/models_output_seeds/vjepa/framelevel_hierarchical/"
3232

33-
EMBED_DIM = 1408
33+
EMBED_DIM = 1024
3434
CONTEXT = 5
3535
WINDOW_SIZE = 2 * CONTEXT + 1 # 11
3636
BATCH_SIZE = 256

src/sailsprep/action_model_testing/vjepa/full_video/two_stage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
SPLITS_CSV = "/home/aparnabg/orcd/scratch/latest_split_csv_new.csv"
3030
OUTPUT_BASE = "/orcd/data/satra/002/projects/SAILS/vjepa_features/models_output_seeds/vjepa/window_two_st/"
3131

32-
EMBED_DIM = 1408
32+
EMBED_DIM = 1024
3333
WINDOW_FRAMES = 30 # 2s at 15fps
3434
STRIDE_FRAMES = 15 # 1s stride
3535
BATCH_SIZE = 64

0 commit comments

Comments
 (0)