Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lighthouse/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def __init__(
slowfast_path: Optional[str] = None,
pann_path: Optional[str] = None) -> None:

ckpt = torch.load(ckpt_path, map_location='cpu')
# Checkpoints store their config as an EasyDict under 'opt', which the
# weights-only unpickler (default since torch 2.6) rejects. Only load
# checkpoints from trusted sources such as the released weights.
ckpt = torch.load(ckpt_path, map_location='cpu', weights_only=False)
args = ckpt['opt']
self._clip_len: float = args.clip_length
self._device: str = device
Expand Down
37 changes: 37 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import pytest
import subprocess
import random
import torch
import yaml
from easydict import EasyDict
from lighthouse.common.moment_detr import build_model as build_model_moment_detr
from lighthouse.models import (MomentDETRPredictor, QDDETRPredictor, EaTRPredictor,
CGDETRPredictor, UVCOMPredictor)

Expand Down Expand Up @@ -141,3 +145,36 @@ def test_audio_model_prediction():
f'The number of moments from {feature}_{model_name}_{dataset} is expected {MOMENT_NUM}, but got {len(prediction["pred_relevant_windows"])}.'
assert len(prediction['pred_saliency_scores']) == math.ceil(second / model._clip_len), \
f'The number of saliency scores from {feature}_{model_name}_{dataset} is expected {math.ceil(second / model._clip_len)}, but got {len(prediction["pred_saliency_scores"])}.'


def test_load_checkpoint_with_easydict_opt(tmp_path, monkeypatch):
"""
Checkpoints store their config as an EasyDict under 'opt'. torch>=2.6 defaults to
torch.load(weights_only=True), which cannot unpickle it, so the predictor must pass
weights_only=False. The torch.load spy keeps this test meaningful on torch<2.6.
"""
opt = {}
for cfg in ['configs/base.yml', 'configs/feature/clip.yml',
'configs/model/moment_detr.yml', 'configs/dataset/qvhighlight.yml']:
with open(cfg, 'r') as f:
opt.update(yaml.load(f, Loader=yaml.FullLoader))
opt = EasyDict(opt)
opt.device = 'cpu'
opt.enc_layers, opt.dec_layers, opt.hidden_dim, opt.dim_feedforward = 1, 1, 32, 64

model, _ = build_model_moment_detr(opt)
ckpt_path = tmp_path / 'best.ckpt'
torch.save({'model': model.state_dict(), 'opt': opt}, ckpt_path)

load_kwargs = []
original_load = torch.load

def spy_load(*args, **kwargs):
load_kwargs.append(kwargs)
return original_load(*args, **kwargs)

monkeypatch.setattr(torch, 'load', spy_load)
predictor = MomentDETRPredictor(str(ckpt_path), device='cpu', feature_name='clip')

assert load_kwargs[0].get('weights_only') is False
assert predictor._clip_len == opt.clip_length